SlideShare une entreprise Scribd logo
1  sur  52
Geo                 Ruby
  (           Rails)
      @tetsutan (    )
about:me

• @tetsutan
•
• 83          82
http://gihyo.jp/news/report/01/rubykaigi2011/0001
Today’s Recipe

• RGeo
• Rails/ActiveRecordSpatialAdapter
• Rails/GeoHash
• Rails/Mongoid
RGeo
• http://virtuoso.rubyforge.org/rgeo/
• gem install rgeo
• RGeo is a geospatial data library for Ruby.
• “At its core is an implementation of the
  industry standard OGC Simple Features
  Specification,”
OpenGIS

• http://www.opengeospatial.org/
• The Open Geospatial Consortium, Inc.
• GIS
 •   GIS   Geographic Information System
GeoRuby

• http://georuby.rubyforge.org/
• “The data model roughly follows the OGC
  Simple Features for SQL specification”
• “The library inputs and outputs EWKB,
  HexEWKB and EWKT.”
EWKT, EWKB,
         HexEWKB
• EWKT/EWKB
 • Extended Well-Known Text/Binary
   •   Point(35.739406 139.653755)

   •   x000000000101000000AC3B16DBA4DE414016139B8FEB746140


• HexEWKB
 • EWKB with Hex
   •   “000000000101000000AC3B16DBA4DE414016139B8FEB746140”
Create WKB w MySQL
MySQL/PostgreSQL
•
• PostgreSQL                  PostGIS
  MySQL                            GIS



• MySQL           Doc
 •   http://dev.mysql.com/doc/refman/5.1/ja/spatial-extensions.html
• RGeo
 ‣ Ruby   GIS


• MySQL
 ‣ WKT/WKB
Today’s Recipe

• RGeo
• Rails/ActiveRecordSpatialAdapter
• Rails/GeoHash
• Rails/Mongoid
Spatial Adapter
• gem search --remote spatial
 • spatial_adapter (1.2.0)
 • activerecord-spatialite-adapter (0.3.3)
 • activerecord-mysqlspatial-adapter (0.3.2)
 • activerecord-mysql2spatial-adapter (0.3.2)
• postgres
 • activerecord-postgis-adapter (0.3.5)
Spatial Adapter
• gem search --remote spatial
 • spatial_adapter (1.2.0)
 • activerecord-spatialite-adapter (0.3.3)
 • activerecord-mysqlspatial-adapter (0.3.2)
 • activerecord-mysql2spatial-adapter (0.3.2)
• postgres
 • activerecord-postgis-adapter (0.3.5)
spatial_adapter

•
• GeoRuby
•
Spatial Adapter
• gem search --remote spatial
 • spatial_adapter (1.2.0)
 • activerecord-spatialite-adapter (0.3.3)
 • activerecord-mysqlspatial-adapter (0.3.2)
 • activerecord-mysql2spatial-adapter (0.3.2)
• postgres
 • activerecord-postgis-adapter (0.3.5)
MySQL
Gemfile


gem "activerecord-mysql2spatial-adapter"
Migrate

create_table :locations do |t|
 t.string :name, :null => false, :limit => 255
 t.point :geom, :null => false
 t.timestamps
end
Controller

factory = RGeo::Cartesian.factory
@location.geom = factory.point(3,4)
“130.0, 30.0”


                   Point(130.0 30.0)
                  RGeo




                              SpatialAdapter
INSERT INTO LOC (geom) values( Point(130.0 30.0) );



                                               MySQL
SpatialAdapter
SpatialAdapter
•   ActiveRecord


•   Mysql


•   OGC


    •
    •              Point


    •   z
Today’s Recipe

• RGeo
• Rails/ActiveRecordSpatialAdapter
• Rails/GeoHash
• Rails/Mongoid
GeoHash


• http://geohash.org/
• “It is a hierarchical spatial data structure which
  subdivides space into buckets of grid shape”
(@masuidrive)
  http://blog.masuidrive.jp/index.php/2010/01/13/geohash/
GeoHash
• AABBCCDDEE ∈ AABBCCDD
• also AABBCCDDFF ∈ AABBCCDD
               AABBCCDDEE
                        AABBCCDDFF


                            AABBCCDD
• ”GAE   DataStore
                                Geohash


             ”

                 http://blog.masuidrive.jp/index.php/2010/01/13/geohash/
GeoHash


•   String   OK
Today’s Recipe

• RGeo
• Rails/ActiveRecordSpatialAdapter
• Rails/GeoHash
• Rails/Mongoid
Mongodb
•
•       indexing

•
•
• etc
Geospatial Indexing!!
Geospatial Indexing
• mongoDB 1.3.3 or later
•
         { loc : [ 50 , 30 ] }
         { loc : { x : 50 , y : 30 } }
         { loc : { foo : 50 , y : 30 } }
         { loc : { long : 40.739037, lat: 73.992964 } }



•
           db.places.ensureIndex( { loc : "2d" } )
OR


• MongoMapper
• Mongoid
Mongoid
• http://mongoid.org/
• MongoMapper
•          id:babie

        “        Rails3
             Mongoid
             ”            http://d.hatena.ne.jp/babie/20100809/1281316971
• Document
• Spatial Index
• devise
• Spatial Index
  -   Mongoid

  -   MM


• devise
  -   Mongoid

  -   MM
Model
Controller
Conclusion
• 4sq
              GeoHash MongoDB

•
    MongoDB

• Mongoid/MongoMapper
        Mongoid
activerecord-
mysql2spatial-adapter
      (0.3.2)
activerecord-mysql2spatial-
          adapter (0.3.2)


fixture geometry
17




     https://gist.github.com/1093641
•   RGeo
    •  http://virtuoso.rubyforge.org/rgeo/
•   OpenGIS
    •  http://www.opengeospatial.org
•   GeoHash
    •  http://geohash.org/
    •  masuidrive       http://blog.masuidrive.jp/index.php/2010/01/13/geohash/

•   Mongoid
    •  http://mongoid.org
    •  babie     http://d.hatena.ne.jp/babie/20100809/1281316971

•   my gist
    •   https://gist.github.com/1093641

Contenu connexe

Similaire à rubykaigi2011_spatial.key

超カジュアルに使うMySQL @ MySQL Casual Talks #2
超カジュアルに使うMySQL @ MySQL Casual Talks #2超カジュアルに使うMySQL @ MySQL Casual Talks #2
超カジュアルに使うMySQL @ MySQL Casual Talks #2
Tasuku Suenaga
 
Polyglot Persistence & Big Data in the Cloud
Polyglot Persistence & Big Data in the CloudPolyglot Persistence & Big Data in the Cloud
Polyglot Persistence & Big Data in the Cloud
Andrei Savu
 
Node js quick tour v2
Node js quick tour v2Node js quick tour v2
Node js quick tour v2
Wyatt Fang
 

Similaire à rubykaigi2011_spatial.key (20)

Impression of Rails 3
Impression of Rails 3Impression of Rails 3
Impression of Rails 3
 
Geospatial Rails applications
Geospatial Rails applicationsGeospatial Rails applications
Geospatial Rails applications
 
PostgreSQL 9.4: NoSQL on ACID
PostgreSQL 9.4: NoSQL on ACIDPostgreSQL 9.4: NoSQL on ACID
PostgreSQL 9.4: NoSQL on ACID
 
Application devevelopment with open source libraries
Application devevelopment with open source librariesApplication devevelopment with open source libraries
Application devevelopment with open source libraries
 
GeoPackage, Context and POI (and a sprinkle of GeoJSON)
GeoPackage, Context and POI (and a sprinkle of GeoJSON)GeoPackage, Context and POI (and a sprinkle of GeoJSON)
GeoPackage, Context and POI (and a sprinkle of GeoJSON)
 
超カジュアルに使うMySQL @ MySQL Casual Talks #2
超カジュアルに使うMySQL @ MySQL Casual Talks #2超カジュアルに使うMySQL @ MySQL Casual Talks #2
超カジュアルに使うMySQL @ MySQL Casual Talks #2
 
Getting Started with Geospatial Data in MongoDB
Getting Started with Geospatial Data in MongoDBGetting Started with Geospatial Data in MongoDB
Getting Started with Geospatial Data in MongoDB
 
Saving Money with Open Source GIS
Saving Money with Open Source GISSaving Money with Open Source GIS
Saving Money with Open Source GIS
 
Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)
 
Integrating PostGIS in Web Applications
Integrating PostGIS in Web ApplicationsIntegrating PostGIS in Web Applications
Integrating PostGIS in Web Applications
 
Polyglot Persistence & Big Data in the Cloud
Polyglot Persistence & Big Data in the CloudPolyglot Persistence & Big Data in the Cloud
Polyglot Persistence & Big Data in the Cloud
 
Sinatraで触れる生SQL
Sinatraで触れる生SQLSinatraで触れる生SQL
Sinatraで触れる生SQL
 
视觉中国的MongoDB应用实践(QConBeijing2011)
视觉中国的MongoDB应用实践(QConBeijing2011)视觉中国的MongoDB应用实践(QConBeijing2011)
视觉中国的MongoDB应用实践(QConBeijing2011)
 
LocationTech Projects
LocationTech ProjectsLocationTech Projects
LocationTech Projects
 
High Performance Weibo QCon Beijing 2011
High Performance Weibo QCon Beijing 2011High Performance Weibo QCon Beijing 2011
High Performance Weibo QCon Beijing 2011
 
Geoindexing with MongoDB
Geoindexing with MongoDBGeoindexing with MongoDB
Geoindexing with MongoDB
 
2011/10/08_Playframework_GAE_to_Heroku
2011/10/08_Playframework_GAE_to_Heroku2011/10/08_Playframework_GAE_to_Heroku
2011/10/08_Playframework_GAE_to_Heroku
 
Node js quick-tour_v2
Node js quick-tour_v2Node js quick-tour_v2
Node js quick-tour_v2
 
Node js quick-tour_v2
Node js quick-tour_v2Node js quick-tour_v2
Node js quick-tour_v2
 
Node js quick tour v2
Node js quick tour v2Node js quick tour v2
Node js quick tour v2
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

rubykaigi2011_spatial.key

Notes de l'éditeur

  1. Rubyで位置情報のデータを扱う場合に着いて話をします\n
  2. 前職はKoozytという会社でARのプラットフォーム開発やってましたが、もともと位置情報好きでKoozytへ入社しました。\n4月末に会社をやめてから3ヶ月ほどニートしています。\n自分でも位置情報系のサービスを作っているんですが、\n位置情報系のサービスを位置情報関連の美味しいお仕事あればご連絡ください\nプレゼンソフトもってなかったので昨日keynote買いました\nなので文字ばっかのプレゼンなので眠いと思います\n\n\n
  3. とか闇の方から聞こえてきた気がきがしますが気ににしません\n
  4. 今日のプレゼンではこのあたりの話をします\n今作っているサービスのバックエンドの変遷\nでもあります。\n先に結論を話すと、この順番に試して結局Mongoにたどり着きました。\n練習したら20分とかかかったので、頭3つは少し飛ばし気味に話していきます\n
  5. coreはOGCで標準化されたsimple features の仕様に基づいて実装されている\n
  6. KMLとかもここで決めてます\n
  7. RGeoの一世代前のライブラリはこれです。\nこいつはまたあとで顔を出してきます。使わないですが\n
  8. ちなみにこのlat/lonは練馬文化センターの位置情報です\nWKTはPoint(x y)の間がスペースなので気をつけてください。\n
  9. ポスグレは知りませんが似たようなことはできると思います。\nMySQLの話がでたのでつぎはMysqlについて\n
  10. さっき見てもらったとおり\n\nたぶんsqliteでもデータ突っ込むだけなら単純に配列なのでOK(indexがはれないとかあるかも。重要なのはspatial index)\n
  11. となるとORマッパーが欲しいですね\n
  12. \n
  13. Spatial ってのは空間っていう意味です\n
  14. \n
  15. \n
  16. 新しいのはこいつら。\n名前でわかると思いますが、\nsqlite, mysql, mysql2, postgresql\nのアダプタです。\n
  17. \n
  18. \n
  19. このへんはspatialadapterのおかげです\n
  20. GeorubyのときはPoint.newとかできたんですが、RGeoになってからファクトリーをつくらなくてはならなくなりました\n
  21. \n
  22. \n
  23. 一応出来るんですが、複雑な計算式をSQLでかかないといけない\n
  24. OGC準拠ってのは利点があってDB側でいろんな操作が可能\n
  25. \n
  26. 階層構造で、グリッドに分割される\n
  27. \n
  28. \n
  29. ということなのでGAEを使うか、自前で前方一致が早い検索エンジンを使うとかしないとSQLでLike文使うとかになっちゃいます。Solrとかならできると思います\n\nGeoHashのruby用ライブラリも増井さんが作成してくれています\n
  30. なので、頭から2文字づつとか保存しれやればインデックスも効くし、ひとつの方法としてはありかなと思っています。\n
  31. やっと本題です\n
  32. \n
  33. まずはMongodbについて、このあたりはさらっと流します\n詳しくはdoryokujinさんのブログで\n\nほんといいことずくめのDBです。KVSとは違うって行ってるんですが、まあ考え方は一緒です。\n
  34. \n
  35. \n
  36. \n
  37. 最近のmongodbだったら問題ないです\n\n1.3.3 Releasedってのが2010/3/2にメールが流れているのでここ1年くらいに導入したなら対応しています\n\nこのあたりはMysqlがspatial indexのサポートをしているのと同じ\n\nこの例の場合、placesテーブルにlocカラムがあって、それが2D Index => Geospatial Indexとなる\n
  38. 僕はMongoidを使いました\n
  39. \n
  40. \n
  41. deviseはメールアドレスなどでログインするフォームなどを簡単に作成してくれるライブラリ。\n位置情報とは関係ないですが、このプラグインがあるのを前提に他を作ってしまってました。\nこれも詳しくはbabieさんのブログ参照\n
  42. \n
  43. \n
  44. 最終的に\n\nこのindexのつけ方もドキュメントにあります。\n
  45. こんな感じでfinderとか使えます。\narelと同じ感じでメソッドチェーンできます。\n特にnearの部分が位置情報には大事\ngeomには0にlat、1にlonが入ってます\n
  46. \n
  47. \n
  48. 残念ながら使わなくなったんですが、こいつのパッチを書きました\n何が問題かというと\n
  49. これ、多分、だれもコレ使ってテストしてないってことだと思うんですが、どうなんでしょうかね?\n
  50. しかも完全じゃない\nwktだけの対応です。\nでもコレ見つけるのかなり苦労しました。\nActiveRecordの中とか読みました\n
  51. \n
  52. \n