Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GIS(地理情報システム) - MongoDBによるジオメディア開発

Kengo Imamura
October 05, 2016
2k

GIS(地理情報システム) - MongoDBによるジオメディア開発

Kengo Imamura

October 05, 2016
Tweet

Transcript

  1. GIS とは GIS(Geographic Information System: 地理情報シス テム) とは、 位置や空間に関する様々 な情報を、

    コン ピュー タを用いて重ね合わせ、 情報の分析・ 解析をお こなったり、 情報を視覚的に表示させるシステム。 [ 参照] GIS ポー タルサイト http://www.gis.go.jp/contents/whatisgis.html
  2. 位置参照デー タベー ス PostGIS PostgreSQL デー タベー スで地理空間情報を扱う ための拡張 MySQL

    5.7 からInnoDB での空間インデックス可 MongoDB 地理空間インデックス
  3. geocoder class Place include Mongoid::Document include Geocoder::Model::Mongoid field :address, type:

    String field :coordinates, type: Array geocoded_by :address after_validation :geocode end address をもとにジオコー ド取得し coordinates に入れる
  4. index geocoder で最初から設定されているインデックスは 平面空間(2d) で作成されるのでスキップする geocoded_by :address, skip_index: true 球面空間(2dsphere)

    インデックス作成されるように設 定する index(coordinates: '2dsphere') index 作成 $ rake db:mongoid:create_indexes
  5. MongoDB で確認 > db.places.getIndexes() [ ~~~ 省略 ~~~ { "v"

    : 1, "key" : { "coordinates" : "2dsphere" }, "name" : "coordinates_2dsphere", "ns" : "meeful_development.places", "2dsphereIndexVersion" : 3 } ] >
  6. 登録 > Place.new(address: ' 墨田区亀沢一丁目27 番5 号').save => true coordinates

    にジオコー ドが入ってることを確認 > Place.where(address: ' 墨田区亀沢一丁目27 番5 号').first .coordinates => [139.7994141, 35.698556]
  7. 指定場所から近い順に取得 Place.geo_near(Geocoder.coordinates(' 場所').reverse) .spherical Speee から近い駅順にソー ト > Place .geo_near(Geocoder.coordinates('

    東京都港区六本木4-1-4') .reverse) .spherical.each { |a| puts a.address } 六本木駅 麻布十番駅 青山一丁目駅 国立競技場駅 代々 木駅 新宿駅
  8. 池袋駅から近い駅順ソー ト > Place.geo_near(Geocoder.coordinates(' 池袋駅').reverse) .spherical.each { |a| puts a.address

    } 新宿駅 代々 木駅 国立競技場駅 青山一丁目駅 六本木駅 麻布十番駅
  9. 最大距離指定し近い順に取得 max_distance で最大距離指定 会社から2km 以内の駅を取得 km からラジアンに変換 > Place .geo_near(Geocoder.coordinates('

    東京都港区六本木4-1-4') .reverse) .spherical .max_distance(Geocoder::Calculations .distance_to_radians(2)) .each { |a| puts a.addres } 六本木駅 麻布十番駅 青山一丁目駅