[PYTHON] Give latitude and longitude point sequence data and try to identify the road from OpenStreetMap data

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.

printgeojson.jpg

#!/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()

Recommended Posts

Give latitude and longitude point sequence data and try to identify the road from OpenStreetMap data
Get the address from latitude and longitude
Learn accounting data and try to predict accounts from the content of the description when entering journals
Find the distance from latitude and longitude (considering the roundness of the earth).
Find the waypoint from latitude and longitude (considering the roundness of the earth).
Try converting latitude / longitude and world coordinates to each other with python
[GPS] Use pyproj to calculate distance, azimuth, and elevation from GPS latitude and longitude
[Rust] Read the latitude / longitude csv data to find the distance between two points
[Python] How to read data from CIFAR-10 and CIFAR-100
Change the decimal point of logging from, to.
Data retrieval from MacNote3 and migration to Write
Download Geographical Survey tiles from latitude and longitude
Try to create a battle record table with matplotlib from the data of "Schedule-kun"
Obtain location information (latitude and longitude) from the address. Geocode in Python ~ Geocoder and pydams ~
Try setting SSH (Exscript) from the software to the router
Try setting NETCONF (ncclient) from software to the router
Send log data from the server to Splunk Cloud
Try to divide twitter data into SPAM and HAM
Approximately 200 latitude and longitude data for hospitals in Tokyo
Try to decipher the login data stored in Firefox
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors