Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Geospatial applications on Rails
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Sergey Nartimov
June 01, 2013
Programming
430
8
Share
Geospatial applications on Rails
Sergey Nartimov
June 01, 2013
More Decks by Sergey Nartimov
See All by Sergey Nartimov
PubSub at Rails
lest
0
140
Rails in production - RubyConfBY 22 Mar 2015
lest
1
150
Sequel - BRUG 21 Feb 2015
lest
0
92
Elixir – Belarus Ruby User Group 25 Jan 2014
lest
3
670
Authentication Security – RUBYSPB
lest
2
190
Design patterns – Belarus Ruby on Rails User Group 23 Feb 2013
lest
8
660
Ruby Stdlib – Minsk.rb October 2012
lest
10
400
Background jobs with realtime results – RailsClub'Moscow 2012
lest
5
220
Other Decks in Programming
See All in Programming
瑠璃の宝石に学ぶ技術の声の聴き方 / 【劇場版】アニメから得た学びを発表会2026 #エンジニアニメ
mazrean
0
240
PHPで TLSのプロトコルを実装してみるをもう一度しゃべりたい
higaki_program
0
200
PHPのバージョンアップ時にも役立ったAST(2026年版)
matsuo_atsushi
0
310
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
120
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
210
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
180
「効かない!」依存性注入(DI)を活用したAPI Platformのエラーハンドリング奮闘記
mkmk884
1
330
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
370
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
0
130
RSAが破られる前に知っておきたい 耐量子計算機暗号(PQC)入門 / Intro to PQC: Preparing for the Post-RSA Era
mackey0225
3
130
Coding as Prompting Since 2025
ragingwind
0
830
L’IA au service des devs : Anatomie d'un assistant de Code Review
toham
0
230
Featured
See All Featured
Fireside Chat
paigeccino
42
3.9k
Site-Speed That Sticks
csswizardry
13
1.2k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
The Curse of the Amulet
leimatthew05
1
11k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
160
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
190
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.1k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
310
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
130
Transcript
Geospatial applications on Rails Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest
About • Rails, Rubinius, Elixir contributor • Software engineer at
Brainspec Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest
Geospatial data
Geospatial data types • Point • Linestring • Polygon •
Multi... • Geometry collection
Geospatial analysis • Coverage • Intersection • Distance • etc.
Geographic coordinate system
Geographic distance
Map projection
Mercator projection
None
Mercator projection
Mercator projection
Geospatial support
Geospatial support • MySQL • http://dev.mysql.com/doc/refman/5.5/en/ spatial-extensions.html
Geospatial support • MongoDB • http://www.mongodb.org/display/DOCS/ Geospatial+Indexing
Geospatial support • SpatiaLite • http://www.gaia-gis.it/gaia-sins/splite- doxy-4.0.0/index.html
Geospatial support • PostGIS • http://postgis.org/docs/
Rails & PostGIS
• https://github.com/dazuma/activerecord- postgis-adapter • https://github.com/dazuma/rgeo
# config/database.yml adapter: postgis # When postgresql >= 9.1 and
postgis >= 2.0 postgis_extension: true # When postgresql < 9.1 or postgis < 2.0 # e.g. for postgresql 9.1 and postgis 1.5 on ubuntu script_dir: "/usr/share/postgresql/9.1/contrib/postgis-1.5" schema_search_path: public,postgis
create_table :locations do |t| t.point :coords, geographic: true end create_table
:locations do |t| t.string :text t.point :coords, srid: 3785 end add_index :locations, :coords, spatial: true
class Location < ActiveRecord::Base GEOFACTORY = RGeo::Geographic.spherical_factory(srid: 4326) set_rgeo_factory_for_column :coords,
GEOFACTORY end
Points in radius
point = Location::GEOFACTORY.point(27, 53) radius = 50_000 # 50 km
Location.where('st_dwithin(coords, ?, ?)', point, radius) .order('st_distance(coords, ?)', point)
Squeel • https://github.com/ernie/squeel
point = Location::GEOFACTORY.point(27, 53) radius = 50_000 # 50 km
Location.where { st_dwithin(coords, point, radius) } .order { st_distance(coords, point) }
class Location < ActiveRecord::Base GEOFACTORY = RGeo::Geographic.simple_mercator_factory set_rgeo_factory_for_column :coords, GEOFACTORY.projection_factory
end point = Location::GEOFACTORY.point(27, 53) radius = 50_000 / Math.cos(lat * Math::PI / 180.0) Location.where { st_dwithin(coords, point, radius) } .order { st_distance(coords, point) }
Points in bounding box
None
None
sw = Location::GEOFACTORY.point(48.389537, 54.306922) ne = Location::GEOFACTORY.point(48.399643, 54.311216) bbox =
RGeo::Cartesian::BoundingBox.create_from_points(sw, ne) Location.where('coords && ?', bbox)
Clustering • http://pgxn.org/dist/kmeans/ • http://postgis.refractions.net/docs/ ST_SnapToGrid.html
None
Time zones example
• http://efele.net/maps/tz/world/
None
• https://github.com/dazuma/rgeo-shapefile
class CreateTimeZonePolygons < ActiveRecord::Migration def change create_table :time_zone_polygons do |t|
t.string :tzid t.geometry :geometry, geographic: true t.timestamps end add_index :time_zone_polygons, :geometry, spatial: true end end
class TimeZonePolygon < ActiveRecord::Base set_rgeo_factory_for_column :geometry, RGeo::Geographic.spherical_factory(srid: 4326) end
RGeo::Shapefile::Reader.open('tz_world.shp') do |file| file.each do |record| polygon = TimeZonePolygon.new polygon.tzid
= record.attributes['TZID'] polygon.geometry = record.geometry polygon.save! end end
point = TimeZonePolygon::GEOFACTORY.point(27, 53) time_zone = TimeZonePolygon .where { st_covers(geometry,
point) }.first time_zone.tzid # => "Europe/Minsk"
• Further improvements
Thanks <3 Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest