Skip to content

Instantly share code, notes, and snippets.

@amiad
Last active December 7, 2022 12:08
Show Gist options
  • Save amiad/d0548d0d07f9009a020c8539352c8194 to your computer and use it in GitHub Desktop.
Save amiad/d0548d0d07f9009a020c8539352c8194 to your computer and use it in GitHub Desktop.
Check relations of paths in OSM
#!/usr/bin/python
from shapely import geometry, ops
import overpy, smtplib, sqlite3, os
from datetime import date
trails = {
'שביל הגולן': 568661,
# 'גב חולית': 4084669,
'שביל ישראל': 282071,
# 'סובב מכתש רמון': 6148296,
'ים אל ים': 2860967,
# 'שמורת מדבר יהודה': 8813797,
'שביל השרון': 11518999,
3418441: 'שביל רמות מנשה',
}
# email settings
from settings import email_to, email_from
today = date.today().strftime('%Y-%m-%d')
con = sqlite3.connect(os.path.dirname(os.path.abspath(__file__)) + '/rels.db')
cur = con.cursor()
cur.execute('create table if not exists history (rel, version, date)')
def checkRel(rel):
query = """[out:json][timeout:25];
rel({});
out meta;
out body;
>;
out skel qt; """.format(rel)
api = overpy.Overpass()
result = api.query(query)
global version
version = result.relations[0].attributes.get('version')
if relInHistory(rel, version):
return True
lss = [] #convert ways to linstrings
for ii_w,way in enumerate(result.ways):
ls_coords = []
for node in way.nodes:
ls_coords.append((float(node.lon),float(node.lat))) # create a list of node coordinates
lss.append(ls_coords)
line = geometry.MultiLineString(lss)
merged_line = ops.linemerge(line)
is_linestring = True
try:
ls = geometry.LineString(merged_line)
except:
is_linestring = False
saveRel(rel, version)
# print(rel)
# print('LineString: ' + str(is_linestring))
# print('Simple: ' + str(line.is_simple))
# print('Valid: ' + str(line.is_valid))
# print(' ')
return is_linestring
def relInHistory(rel, version):
cur.execute('select * from history where rel = ? and version = ? and date = ?', (rel, version, today))
data = cur.fetchone()
return data is not None
def saveRel(rel, version):
cur.execute('insert into history(rel, version, date) values(?, ?, ?)', (rel, version, today))
con.commit()
def sendEmail(trail):
global version
server = smtplib.SMTP('localhost')
subject = 'היחס {} שבור'.format(trail)
message = """Subject: {0}
{1} שבור. כדאי לבדוק אותו:
גרסה: {2}
https://israelhiking.osm.org.il/poi/OSM/relation_{3}
https://www.openstreetmap.org/relation/{3}
http://ra.osmsurround.org/analyzeRelation?relationId={3}&noCache=true&_noCache=on""".format(subject, trail, version, trails[trail]).encode('utf-8')
server.sendmail(email_from, email_to, message)
# print('alert sent')
for trail in trails:
if not checkRel(trails[trail]):
print(trail)
sendEmail(trail)
#https://stackoverflow.com/questions/60670206/how-do-i-get-the-geometry-of-a-relation-from-openstreetmap
#https://www.facebook.com/groups/lost.geographers/posts/4574980922621550/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment