Hello. Imported OpenStreetMap data into mongodb (using goosm), so as an application, on the road Given the "latitude point sequence data" (blue below) that moved along, I considered the problem of identifying the road (green). Exported in geojson format.
However, the OpenStreetMap way data (highway) must be a simple edge with vertices at both ends.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import simplejson
import textwrap
from pymongo import MongoClient
client = MongoClient('localhost')
def queryNear(coord, radius):
return ({"geometry": {"$near": {"$maxDistance": radius, "$geometry": {"type": "Point", "coordinates": coord}}}}, {"_id": 1, "geometry.coordinates": 1, "nodes": 1})
def printgeojson(type, coordinates):
fmtstr = """\
{
"type": "Feature",
"geometry": {"type": %s, "coordinates": %s},
"properties": {}
}"""
fmtstr = textwrap.dedent(fmtstr)
print(fmtstr % (type, coordinates), end='')
return 0
def printNodes(tracks):
nodes = []
colnodes = client['osm_nodes']['data']
prepend = ''
for coord in tracks:
results = colnodes.find(*queryNear(coord, radius))
for r in results:
if r['_id'] not in nodes:
nodes.append(r['_id'])
print(prepend, end='')
printgeojson("Point", r['geometry']['coordinates'])
prepend = ',\n'
return nodes
def printEdges(tracks, nodes):
edges = []
coledges = client['osm_edges']['data']
prepend = ''
for coord in tracks:
results = coledges.find(*queryNear(coord, radius))
for r in results:
if r['_id'] not in edges and set(r['nodes']).issubset(nodes):
edges.append(r['_id'])
print(prepend, end='')
printgeojson("LineString", r['geometry']['coordinates'])
prepend = ',\n'
return edges
def main():
radius = 30.0
tracks = [[lon0, lat0], [lon1, lat1], ...] # <==Give latitude and longitude point sequence data
nodes = printNodes(tracks, radius)
edges = printEdges(tracks, radius, nodes)
exit(0)
if __name__ == '__main__':
main()