7
8

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.

mongodb に OpenStreetMap データを取り込み(goosm利用)+近傍検索

Last updated at Posted at 2015-03-27

こんにちは。
mongodb に OpenStreetMap データを取り込んでみました1。Go言語の [goosm] (https://github.com/bocajim/goosm) (その内部では mgo 利用)を利用しました2

インデックス {"loc.coordinates": "2d"} を付ける前後で検索速度の大幅な効果を体感してみました。また指定円内の ways, nodes を検索し地図上に重ねてみました。
test.jpg

なお OpenStreetMap データ内の異常(?)に気がついたのですが、線分の両端の vertex が同一経緯度値を持っている場合(従って点へ縮退)がわずかに含まれていて、何か対策が必要かと思います。

$ wget http://download.geofabrik.de/asia/japan-latest.osm.bz2
$ go run goosm.go -f japan-latest.osm.bz2 -db osm
   [6時間以上かかった]

$ mongo osm_ways
MongoDB shell version: 3.0.1
connecting to: osm_ways
> show dbs
local        0.000GB
osm_nodes    5.479GB
osm_ways     1.816GB
> show collections
data
> db.data.ensureIndex({"loc.coordinates": "2d"})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 2,
	"numIndexesAfter" : 3,
	"ok" : 1
}
> db.data.getIndexes()
[
	{
		"v" : 1,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "osm_ways.data"
	},
	{
		"v" : 1,
		"key" : {
			"loc" : "2dsphere"
		},
		"name" : "loc_2dsphere",
		"ns" : "osm_ways.data",
		"2dsphereIndexVersion" : 2
	},
	{
		"v" : 1,
		"key" : {
			"loc.coordinates" : "2d"
		},
		"name" : "loc.coordinates_2d",
		"ns" : "osm_ways.data"
	}
]

> db.data.find({"loc": { "$near": { "$maxDistance": 25.0, "$geometry": { "type": "Point", "coordinates": [139.710001, 35.690283] } } } , "tags.name": {"$regex": "^花"}}).count()
2
> db.data.find({"loc.coordinates" : { "$geoWithin" : { "$centerSphere" : [ [139.710001, 35.690283] , 0.00000392 ] } } , "tags.name": {"$regex": "^花"}}).count()
2

注:0.00000392 radians = 25.0/6378137.0
なお、goosm.go は *.osm.bz2 を読み込めるように少し改造。

mongod は前もって手動起動しました。

$ ulimit -n 2048 && mongod --config /usr/local/etc/mongod.conf &
$ cat /usr/local/etc/mongod.conf
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
  engine: wiredTiger
net:
  bindIp: 127.0.0.1
  http:
    enabled: true
    RESTInterfaceEnabled: true

なお、mongodb への取り込みについて、OSMImport (https://github.com/ederoyd46/OSMImport) というものもあるようですね。ほかにも情報があるようです(http://wiki.openstreetmap.org/wiki/Databases_and_data_access_APIs )。

  1. ちなみに ArangoDB を用いた参考例は: "Using The New ArangoDB Geo Index Cursor via AQL"

  2. ちなみにMacOSXではhomebrewを使って導入可能です。

7
8
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
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?