Skip to content

Instantly share code, notes, and snippets.

@FrankRuns
Last active December 25, 2019 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankRuns/b063a71966a65cc33882e6ef2483abcf to your computer and use it in GitHub Desktop.
Save FrankRuns/b063a71966a65cc33882e6ef2483abcf to your computer and use it in GitHub Desktop.
this gist helps parse an osm file to identify good running nodes in an area of interest
# python3
# this gist helps parse an osm file to identify good
# running nodes in an area of interest
import xml.etree.cElementTree as ET # to parse OSM file
import folium # to create map
from folium.plugins import MarkerCluster # to create map
import os # to view map
import webbrowser # to view map
'''
Two pre-processing steps...
1. Go to https://www.openstreetmap.org and identify
Node ID for starting location (use Query features)
2. Find 4x4 mile box surrounding start location and
export OSM using Overpass API (see below for this)
'''
# create static variables
start_location = '213774020'
start_lat = '40.8739800'
start_lon = '-73.3164560'
# define number of lat/lon degress roughly equivalent to 1 mile
one_degree = 69 #miles
one_mile = 1/one_degree #degrees
# find boundary of lat/lons from start location
upper_lat = float(start_lat) + (one_mile * 4)
lower_lat = float(start_lat) - (one_mile * 4)
upper_lon = float(start_lon) + (one_mile * 4)
lower_lon = float(start_lon) - (one_mile * 4)
# create map file variable
filename = 'osm-maps/northport-defined-small.osm'
# parse osm file
tree = ET.parse(filename)
root = tree.getroot()
# identify all geocoords that have meaningful tags
keywords = ['natural', 'waterway', 'bicycle',
'foot', 'checkpoint', 'mountain_pass',
'historic', 'path', 'hiking',
'unpaved', 'footway']
# create list for ways and nodes of interest
meaningful_ways = []
meaningful_nodes = []
# parse osm file to find nodes of interest
for way in root.findall('way'):
taglist = way.findall('tag')
for tag in taglist:
if tag.get('k') in keywords or tag.get('v') in keywords:
meaningful_ways.append(way.get('id'))
for node in way.findall('nd'):
meaningful_nodes.append(node.get('ref'))
# create list for geocoords of nodes of interest
node_coords = []
# create function to get geocoords from osm file
def get_node_geocoords():
''' parses osm file and returns lat lon coords
example input = '66566291'
example output = '[42.4436791,-71.0727793]' '''
for m_node in meaningful_nodes:
for node in root.findall(".//node[@id='"+m_node+"']"):
temp_node_coords = [node.get('lat'), node.get('lon')]
node_coords.append(temp_node_coords)
return node_coords
# get geocoords from osm file
node_coords = get_node_geocoords()
# create map and view
filepath = '/Users/thisisprobablyyou/hotspot_map.html'
m = folium.Map(location=[start_lat, start_lon], zoom_start=13) # create base map
marker_cluster = MarkerCluster().add_to(m) # add clustering feature
for point in range(0, len(node_coords)):
folium.Marker(node_coords[point]).add_to(marker_cluster) # add nodes to plot
folium.Marker([start_lat, start_lon], icon=folium.Icon(color='red')).add_to(marker_cluster) # to add marker of start location
m.save(filepath)
webbrowser.open('file://' + filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment