mirror of
https://github.com/DerTyp7/local-analyzer-python.git
synced 2025-10-29 04:22:10 +01:00
comments and shop types
This commit is contained in:
BIN
__pycache__/managers.cpython-310.pyc
Normal file
BIN
__pycache__/managers.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
58
main.py
58
main.py
@@ -1,8 +1,17 @@
|
|||||||
|
OFFLINE = True
|
||||||
|
from managers import ShopManager
|
||||||
from osm import OSM
|
from osm import OSM
|
||||||
from places import Shops
|
|
||||||
from publicTransport import PublicTransportAccessibility
|
from publicTransport import PublicTransportAccessibility
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
# ADVANCED:
|
||||||
|
# Get external data of shops, like website
|
||||||
|
# Get General City/Region data: average age, peace index, democrazy index, average income
|
||||||
|
|
||||||
|
# IDEAS:
|
||||||
|
# Parks
|
||||||
|
# ATMs
|
||||||
|
|
||||||
# 52.9152386,,15.96z
|
# 52.9152386,,15.96z
|
||||||
# bhv 53.51500036203292, 8.603602165157444
|
# bhv 53.51500036203292, 8.603602165157444
|
||||||
|
|
||||||
@@ -23,28 +32,35 @@ maxLat = round(float(lat) + areaHeightRadius, 5)
|
|||||||
requestUrl = "https://overpass-api.de/api/map"
|
requestUrl = "https://overpass-api.de/api/map"
|
||||||
requestsUrlParams = f"?bbox={minLon},{minLat},{maxLon},{maxLat}"
|
requestsUrlParams = f"?bbox={minLon},{minLat},{maxLon},{maxLat}"
|
||||||
|
|
||||||
print(requestUrl + requestsUrlParams)
|
|
||||||
""" # ONLINE
|
|
||||||
print("Downloading OSM-File...")
|
|
||||||
# TODO Check if banned from overpass-api MAYBE get alternative API then
|
|
||||||
r = requests.get(requestUrl + requestsUrlParams, headers={'Content-Type': 'application/xml'})
|
|
||||||
print("Done: Downloading OSM-File")
|
|
||||||
osm = OSM(r.text)
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Offline
|
if OFFLINE:
|
||||||
print("Using offline file!")
|
print("Using offline file!")
|
||||||
file = open("mapHH", encoding="utf8")
|
file = open("mapHH", encoding="utf8")
|
||||||
osm = OSM(file.read())
|
osm = OSM(file.read())
|
||||||
|
else:
|
||||||
|
print(requestUrl + requestsUrlParams)
|
||||||
|
print("Downloading OSM-File...")
|
||||||
|
# TODO Check if banned from overpass-api MAYBE get alternative API then
|
||||||
|
r = requests.get(requestUrl + requestsUrlParams, headers={'Content-Type': 'application/xml'})
|
||||||
|
print("Done: Downloading OSM-File")
|
||||||
|
osm = OSM(r.text)
|
||||||
|
|
||||||
pta = PublicTransportAccessibility(osm)
|
pta = PublicTransportAccessibility(osm)
|
||||||
shops = Shops(osm)
|
shopManager = ShopManager(osm)
|
||||||
|
|
||||||
|
print("\n--- Public Transport Accessibility ---")
|
||||||
|
print("Bus: " + str(pta.isBusAccessible()))
|
||||||
|
print("Tram: " + str(pta.isTramAccessible()))
|
||||||
|
print("Light Rail: " + str(pta.isLightRailAccessible()))
|
||||||
|
print("Subway: " + str(pta.isSubwayAccessible()))
|
||||||
|
print("Train: " + str(pta.isTrainAccessible()))
|
||||||
|
print("Monorail: " + str(pta.isMonorailAccessible()))
|
||||||
|
|
||||||
|
print("\n--- 10 Most present shop types ---")
|
||||||
|
counter = 0
|
||||||
|
for s in shopManager.getCountsOfTypes():
|
||||||
|
if counter <= 10:
|
||||||
|
print(s['type'] + " : " + str(s['count']) + " times")
|
||||||
|
counter = counter + 1
|
||||||
|
|
||||||
print("Bus:" + str(pta.isBusAccessible()))
|
|
||||||
print("Tram:" + str(pta.isTramAccessible()))
|
|
||||||
print("Light Rail:" + str(pta.isLightRailAccessible()))
|
|
||||||
print("Subway:" + str(pta.isSubwayAccessible()))
|
|
||||||
print("Train:" + str(pta.isTrainAccessible()))
|
|
||||||
print("Monorail:" + str(pta.isMonorailAccessible()))
|
|
||||||
|
|
||||||
print(shops.countOfShopTypes())
|
|
||||||
|
|||||||
21
managers.py
Normal file
21
managers.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from operator import itemgetter
|
||||||
|
|
||||||
|
class ShopManager:
|
||||||
|
def __init__(self, _osm):
|
||||||
|
self.osm = _osm
|
||||||
|
|
||||||
|
def getCountsOfTypes(self):
|
||||||
|
countDict = []
|
||||||
|
|
||||||
|
def isInDict(type):
|
||||||
|
for a in countDict:
|
||||||
|
if a['type'] == type:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
if self.osm.shopList:
|
||||||
|
for shop in self.osm.shopList:
|
||||||
|
if not isInDict(shop.type):
|
||||||
|
countDict.append({'type': shop.type, 'count': sum(a.type == shop.type for a in self.osm.shopList)})
|
||||||
|
|
||||||
|
return sorted(countDict, key=itemgetter('count'), reverse=True)
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
|
class Shop:
|
||||||
|
# TODO More optional details/attributes. Example: "website", "brand", "Telefone", "Owner"
|
||||||
|
def __init__(self, _nodeIds, _name, _type):
|
||||||
|
self.nodeIds = _nodeIds
|
||||||
|
self.name = _name
|
||||||
|
self.type = _type
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, _id, _lon, _lat, _version, _timestamp, _changeset, _uid, _user):
|
def __init__(self, _id, _lon, _lat, _version, _timestamp, _changeset, _uid, _user):
|
||||||
self.id = int(_id)
|
self.id = int(_id)
|
||||||
|
|||||||
33
osm.py
33
osm.py
@@ -1,12 +1,14 @@
|
|||||||
from objects import Node, NodeTag, Way, WayTag
|
from objects import Node, NodeTag, Way, WayTag, Shop
|
||||||
|
|
||||||
class OSM:
|
class OSM:
|
||||||
|
# TODO Average value of "last updated" to see how up to date the data is.
|
||||||
def __init__(self, _osmContent):
|
def __init__(self, _osmContent):
|
||||||
self.osmContent = str(_osmContent)
|
self.osmContent = str(_osmContent)
|
||||||
self.nodeList = []
|
self.nodeList = []
|
||||||
self.nodeTagList = []
|
self.nodeTagList = []
|
||||||
self.wayList = []
|
self.wayList = []
|
||||||
self.wayTagList = []
|
self.wayTagList = []
|
||||||
|
self.shopList = []
|
||||||
self.parseOsmContent()
|
self.parseOsmContent()
|
||||||
|
|
||||||
def parseOsmContent(self):
|
def parseOsmContent(self):
|
||||||
@@ -43,6 +45,29 @@ class OSM:
|
|||||||
elif len(wayLines) > 0:
|
elif len(wayLines) > 0:
|
||||||
wayLines.append(line)
|
wayLines.append(line)
|
||||||
|
|
||||||
|
def createShop(self, lines):
|
||||||
|
# TODO Make sure shops are not duplicates.
|
||||||
|
# TODO e.g. REWE(shop) is big enough to have some "Ways" of it's own and multiple nodes, which can cause duplicates.
|
||||||
|
|
||||||
|
nodeIds = []
|
||||||
|
type = ""
|
||||||
|
name = ""
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
if '<tag ' in line:
|
||||||
|
key = str(line.split('k="')[1].split('"')[0])
|
||||||
|
value = str(line.split('v="')[1].split('"')[0])
|
||||||
|
|
||||||
|
if key == "shop":
|
||||||
|
type = value
|
||||||
|
elif key == "name":
|
||||||
|
name = value
|
||||||
|
elif '<nd ' in line:
|
||||||
|
nodeIds.append(line.split('ref="')[1].split('"')[0])
|
||||||
|
|
||||||
|
s = Shop(nodeIds, name, type)
|
||||||
|
self.shopList.append(s)
|
||||||
|
|
||||||
|
|
||||||
def createWay(self, lines):
|
def createWay(self, lines):
|
||||||
wayId = ""
|
wayId = ""
|
||||||
@@ -65,6 +90,9 @@ class OSM:
|
|||||||
wt = WayTag(len(self.wayTagList), wayId, key, value)
|
wt = WayTag(len(self.wayTagList), wayId, key, value)
|
||||||
self.wayTagList.append(wt)
|
self.wayTagList.append(wt)
|
||||||
|
|
||||||
|
if key == "shop":
|
||||||
|
self.createShop(lines)
|
||||||
|
|
||||||
def createNode(self, lines):
|
def createNode(self, lines):
|
||||||
nodeId = ""
|
nodeId = ""
|
||||||
|
|
||||||
@@ -87,3 +115,6 @@ class OSM:
|
|||||||
value = str(line.split('v="')[1].split('"')[0])
|
value = str(line.split('v="')[1].split('"')[0])
|
||||||
nt = NodeTag(len(self.nodeTagList), nodeId, key, value)
|
nt = NodeTag(len(self.nodeTagList), nodeId, key, value)
|
||||||
self.nodeTagList.append(nt)
|
self.nodeTagList.append(nt)
|
||||||
|
|
||||||
|
if key == "shop":
|
||||||
|
self.createShop(lines)
|
||||||
35
places.py
35
places.py
@@ -1,35 +0,0 @@
|
|||||||
from operator import itemgetter
|
|
||||||
|
|
||||||
class Shops:
|
|
||||||
def __init__(self, _osm):
|
|
||||||
self.osm = _osm
|
|
||||||
|
|
||||||
def getAllShopTypes(self):
|
|
||||||
shopList = []
|
|
||||||
|
|
||||||
for tag in self.osm.nodeTagList:
|
|
||||||
if tag.key == "shop":
|
|
||||||
shopList.append(tag.value)
|
|
||||||
|
|
||||||
for tag in self.osm.wayTagList:
|
|
||||||
if tag.key == "shop":
|
|
||||||
shopList.append(tag.value)
|
|
||||||
return shopList
|
|
||||||
|
|
||||||
def countOfShopTypes(self):
|
|
||||||
shopList = self.getAllShopTypes()
|
|
||||||
countDict = []
|
|
||||||
|
|
||||||
if shopList:
|
|
||||||
def isInDict(name):
|
|
||||||
for a in countDict:
|
|
||||||
if a['type'] == name:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
for shop in shopList:
|
|
||||||
print(shop)
|
|
||||||
if not isInDict(shop):
|
|
||||||
print("add" + str(shopList.count(shop)))
|
|
||||||
countDict.append({'type': shop, 'count': shopList.count(shop)})
|
|
||||||
return sorted(countDict, key=itemgetter('count'), reverse=True)
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
from osm import OSM
|
|
||||||
from objects import Node, NodeTag
|
|
||||||
|
|
||||||
class PublicTransportAccessibility:
|
class PublicTransportAccessibility:
|
||||||
|
# TODO count of each Ttransport type
|
||||||
def __init__(self, _osm):
|
def __init__(self, _osm):
|
||||||
self.osm = _osm
|
self.osm = _osm
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user