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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Sergey Nartimov
June 01, 2013
Programming
440
8
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
160
Sequel - BRUG 21 Feb 2015
lest
0
98
Elixir – Belarus Ruby User Group 25 Jan 2014
lest
3
670
Authentication Security – RUBYSPB
lest
2
200
Design patterns – Belarus Ruby on Rails User Group 23 Feb 2013
lest
8
660
Ruby Stdlib – Minsk.rb October 2012
lest
10
420
Background jobs with realtime results – RailsClub'Moscow 2012
lest
5
230
Other Decks in Programming
See All in Programming
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
100
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
280
Agentic UI
manfredsteyer
PRO
0
160
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
110
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
160
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
140
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
110
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
240
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
170
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
AIで効率化できた業務・日常
ochtum
0
140
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
YesSQL, Process and Tooling at Scale
rocio
174
15k
A Modern Web Designer's Workflow
chriscoyier
698
190k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
530
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
430
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
250
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
Paper Plane
katiecoart
PRO
1
51k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
620
Optimizing for Happiness
mojombo
378
71k
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