6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

経緯度点列データを与えOpenStreetMapデータから道路を特定してみる

Last updated at Posted at 2015-05-15

こんにちは。
mongodb に OpenStreetMap データを取り込んだので(goosm利用)、応用として、道路に沿って移動した「経緯度点列データ」(下記で青色)を与えた時、その道路(緑色)を特定する問題を考えてみました。geojson形式で書き出しています。

ただし、OpenStreetMap の way データ (highway) は、両端に vertex を持つ単純な edge (線分)である必要があります。

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], ...] # <== 経緯度点列データを与える
    nodes = printNodes(tracks, radius)
    edges = printEdges(tracks, radius, nodes)
    exit(0)

if __name__ == '__main__':
    main()
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?