2
2

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 5 years have passed since last update.

mongodb に geojson データを読み込み(mongoimport、jq 利用)+近傍検索

Last updated at Posted at 2015-04-01

こんにちは。
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
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?