こんにちは。
mongodb に geojson データ(Pointの集合)を import し、指定円内に含まれるものを検索してみました。
$ cat points.json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [139.710001, 35.690283]
},
"properties": {
},
},
:
:
]
}
$ cat points.json | jq -c '.features[]' | jq -c 'del(.type)' | mongoimport --db points --collection data --type json
$ mongo points
MongoDB shell version: 3.0.1
connecting to: points
> db.data.ensureIndex({"geometry.coordinates": "2dsphere"})
> db.data.getIndexes()
> db.data.find({"geometry.coordinates": {"$geoWithin": {"$centerSphere": [[139.710001, 35.690283] , 0.00000392]}}})
> db.data.find({"geometry.coordinates": {"$near": {"$maxDistance": 25.0, "$geometry": {"type": "Point", "coordinates": [139.710001, 35.690283]}}}})
{ "_id" : ObjectId("52fcf4611d41c875feaff3e3"), "geometry" : { "type" : "Point", "coordinates" : [ 139.710001, 35.690283 ] }, "properties" : {} }
>
注:0.00000392 radians = 25.0/6378137.0
また趣向を変えて jsonarray 形式のファイルで読み込みの方法も試しました。
$ tmpd=$(mktmp -d /tmp/tmpd.XXXXXX)
$ cat points.geojson | jq '.features' | jq 'del(.[].type)' > $tmpd/tmp.json
$ ls -ld $tmpd/tmp.json
[16 MB 以下を確認]
$ jsonlint -v $tmpd/tmp.json
$ mongoimport -d points -c data --type json --jsonarray -f $tmpd/tmp.json
$ rm -r $tmpd