merged shops and amenities to places

This commit is contained in:
Janis M
2022-03-23 15:07:19 +01:00
parent 7e8596759d
commit 6bee1fb352
12 changed files with 199 additions and 34 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

12
main.py
View File

@@ -1,5 +1,5 @@
OFFLINE = True
from managers import ShopManager
from managers import PlaceManager
from osm import OSM
from publicTransport import PublicTransportAccessibility
import requests
@@ -46,7 +46,7 @@ else:
osm = OSM(r.text)
pta = PublicTransportAccessibility(osm)
shopManager = ShopManager(osm)
placeManager = PlaceManager(osm)
print("\n--- Public Transport Accessibility ---")
print("Bus: " + str(pta.isBusAccessible()))
@@ -56,11 +56,9 @@ print("Subway: " + str(pta.isSubwayAccessible()))
print("Train: " + str(pta.isTrainAccessible()))
print("Monorail: " + str(pta.isMonorailAccessible()))
print("\n--- 10 Most present shop types ---")
print("\n--- 20 Most present places types ---")
counter = 0
for s in shopManager.getCountsOfTypes():
if counter <= 10:
for s in placeManager.getCountsOfTypes():
if counter <= 20:
print(s['type'] + " : " + str(s['count']) + " times")
counter = counter + 1

View File

@@ -1,6 +1,6 @@
from operator import itemgetter
class ShopManager:
class PlaceManager:
def __init__(self, _osm):
self.osm = _osm
@@ -13,9 +13,25 @@ class ShopManager:
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)})
if self.osm.placeList:
for place in self.osm.placeList:
if not isInDict(place.type):
countDict.append({'type': place.type, 'count': sum(a.type == place.type for a in self.osm.placeList)})
return sorted(countDict, key=itemgetter('count'), reverse=True)
def getPlacesByType(self, type):
result = []
for a in self.osm.placeList:
if a.type == type:
result.append(a)
return result
def getPlacesByName(self, name):
result = []
for a in self.osm.placeList:
if a.name == name:
result.append(a)
return result

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,18 +1,19 @@
# ! "amenity" are used very often in osm it's referreing to healtcare, police, cafe, pub, ice cream, post etc.
# ! There is defently a need to sort them more on not just take them all into a "amenity" class
# TODO new objects: healthcare, police
# TODO Address object "addr:"
# ? offices
# ? How to handle districts
class Shop:
# TODO More optional details/attributes. Example: "website", "brand", "Telefone", "Owner", "opening times"
def __init__(self, _nodeIds, _name, _type):
class Place:
def __init__(self, _nodeIds, _type, _name, _address = None, _contact = None, _openingHours = None):
self.nodeIds = _nodeIds
self.name = _name
self.type = _type
self.name = _name
self.address = _address
self.contact = _contact
self.openingHours = _openingHours
class Node:
def __init__(self, _id, _lon, _lat, _version, _timestamp, _changeset, _uid, _user):

44
objects/metaObjects.py Normal file
View File

@@ -0,0 +1,44 @@
class Contact:
def __init__(self, _phone = None, _fax = None, _email = None, _website = None, _mobile = None):
self.phone = _phone
self.fax = _fax
self.email = _email
self.website = _website
self.mobile = _mobile
#Social
self.facebook = None
self.vk = None
self.instagram = None
self.twitter = None
self.youtube = None
self.ok = None
self.webcam = None
self.telegram = None
self.whatsapp = None
self.linkedin = None
self.pinterest = None
self.viper = None
self.foursquare = None
self.skype = None
self.xing = None
self.vhf = None
self.flickr = None
self.mastodon = None
self.sip = None
self.diaspora = None
self.gnusocial = None
# TODO support custom social tags MAYBE: there should ne no need
class Address:
def __init__(self, _housenumber = None, _street = None, _place = None,
_city = None, _postcode = None, _country = None, _suburb = None, _state = None, _province = None):
self.housenumber = _housenumber
self.street = _street
self.place = _place
self.city = _city
self.postcode = _postcode
self.country = _country
self.suburb = _suburb
self.state = _state
self.province = _province

136
osm.py
View File

@@ -1,4 +1,5 @@
from objects import Node, NodeTag, Way, WayTag, Shop
from objects.baseObjects import Node, NodeTag, Way, WayTag, Place
from objects.metaObjects import Address, Contact
class OSM:
# TODO Average value of "last updated" to see how up to date the data is.
@@ -8,7 +9,8 @@ class OSM:
self.nodeTagList = []
self.wayList = []
self.wayTagList = []
self.shopList = []
self.placeList = []
self.parseOsmContent()
def parseOsmContent(self):
@@ -45,29 +47,129 @@ class OSM:
elif len(wayLines) > 0:
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 = ""
def createContact(self, lines):
c = Contact()
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":
if key == "contact:website" or key == "website":
c.website = value
elif key == "contact:phone" or key == "phone":
c.phone = value
elif key == "contact:mobile" or key == "mobile":
c.mobile = value
elif key == "contact:fax" or key == "fax":
c.fax = value
elif key == "contact:email" or key == "email":
c.email = value
elif key == "contact:facebook":
c.facebook = value
elif key == "contact:vk":
c.vk = value
elif key == "contact:instagram":
c.instagram = value
elif key == "contact:twitter":
c.twitter = value
elif key == "contact:youtube":
c.youtube = value
elif key == "contact:ok":
c.ok = value
elif key == "contact:webcam":
c.webcam = value
elif key == "contact:telegram":
c.telegram = value
elif key == "contact:whatsapp":
c.whatsapp = value
elif key == "contact:linkedin":
c.linkedin = value
elif key == "contact:pinterest":
c.pinterest = value
elif key == "contact:viper":
c.viper = value
elif key == "contact:foursquare":
c.foursquare = value
elif key == "contact:skype":
c.skype = value
elif key == "contact:xing":
c.xing = value
elif key == "contact:vhf":
c.vhf = value
elif key == "contact:flickr":
c.flickr = value
elif key == "contact:mastodon":
c.mastodon = value
elif key == "contact:sip":
c.sip = value
elif key == "contact:diaspora":
c.diaspora = value
elif key == "contact:gnusocial":
c.gnusocial = value
return c
def createAddress(self, lines):
a = Address()
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 == "addr:housenumber":
a.housenumber = value
elif key == "addr:street":
a.street = value
elif key == "addr:place":
a.place = value
elif key == "addr:city":
a.city = value
elif key == "addr:postcode":
a.postcode = value
elif key == "addr:country":
a.country = value
elif key == "addr:suburb":
a.suburb = value
elif key == "addr:state":
a.state = value
elif key == "addr:province":
a.province = value
return a
def createPlace(self, lines, isShop):
# 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 = ""
openingHours = ""
address = self.createAddress(lines)
contact = self.createContact(lines)
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 == "amenity" and not isShop:
type = value
elif key == "shop" and isShop:
type = value
elif key == "name":
name = value
elif key == "opening_hours":
openingHours = value
elif '<nd ' in line:
nodeIds.append(line.split('ref="')[1].split('"')[0])
s = Shop(nodeIds, name, type)
self.shopList.append(s)
elif "<node" in line:
nodeIds.append(str(line.split('id="')[1].split('"')[0]))
p = Place(nodeIds, type, name, address, contact, openingHours)
self.placeList.append(p)
def createWay(self, lines):
wayId = ""
@@ -91,7 +193,9 @@ class OSM:
self.wayTagList.append(wt)
if key == "shop":
self.createShop(lines)
self.createPlace(lines, True)
elif key == "amenity":
self.createPlace(lines, False)
def createNode(self, lines):
nodeId = ""
@@ -117,4 +221,6 @@ class OSM:
self.nodeTagList.append(nt)
if key == "shop":
self.createShop(lines)
self.createPlace(lines, True)
elif key == "amenity":
self.createPlace(lines, False)