SlideShare a Scribd company logo
1 of 26
Introducing PostGIS
What is Geospatial
• Most common use
– Narrow a search within a distance from a point of
origin
– Sort by that distance
Distance Formulas
• Flat-surface formulae
– Spherical Earth projected to a plane
– Ellipsoidal Earth projected to a plane
– Polar coordinate flat-Earth formula
• Spherical-surface formulae
– Haversine
– Tunnel distance
• Ellipsoidal-surface formulae
– Lambert’s formula for long lines
– Bowring’s method for short lines
Here’s one example
What you usually care about
• Given 2 latitude and longitude points
– Calculate the distance so we can find what’s close
• This is the common capability of databases with
“geospatial” support
A few options…
• Do the math in a query
• Here’s what it looks like in SQL
– (ACOS(least(1,COS(0.607198022186895)*COS(-
1.4410239410591847)*COS(RADIANS(latitude))*COS(RAD
IANS(longitude)) + COS(0.607198022186895)*SIN(-
1.4410239410591847)*COS(RADIANS(latitude))*SIN(RAD
IANS(longitude)) +
SIN(0.607198022186895)*SIN(RADIANS(latitude))))*39
63.19)) <= 25)
– Calculating within 25 miles of a point of origin based on an origin point
– You can do that in any database (it’s just math)
– Very intense query that slows as your dataset grows
– This…is not using an index
Speed that up
• Distance query a subset instead
• Use the lat/lng to create query a box
around the boundary
• Queried with a numerical index
• Then check the distance from center
for the subset
• NOTE: Doing this as part of another
query is a lot easier with multi-index
queries
And here’s what THAT looks like
WHERE (
latitude > 34.42845936786603
AND latitude < 35.15130863213399
AND longitude > -83.00467808012291
AND longitude < -82.12450191987708
)
AND
( (ACOS(least(1,COS(0.607198022186895)*COS(-
1.4410239410591847)*COS(RADIANS(latitude))*COS(RADIANS(longitude))+
COS(0.607198022186895)*SIN(-
1.4410239410591847)*COS(RADIANS(latitude))*SIN(RADIANS(longitude))+
SIN(0.607198022186895)*SIN(RADIANS(latitude))))*3963.19)
) <= 25)
Simple, am I right?
Also, you need to drop that distance formula into the ORDER BY clause too.
There’s a Gem for that!
Geokit
https://github.com/geokit/geokit
• Distance calculations between 2 points
– Multiple formulas and units of measure
• Multiple providers for Geocoding
different data
– Addresses
• Yahoo
• Geocoder.us/.ca
• Geonames
• Bing
• Yandex
• MapQuest
• Geocode.io
• Mapbox
• Google
• FCC
• Open Street Map
– IP Address
• hostip.info
• Geoplugin.net
• RIPE
• MaxMind (HIGHLY RECOMMEND)
• freegeoip.net
Geokit Rails (any database)
https://github.com/geokit/geokit-rails
• Premium Rails Integration
• ActiveRecord distance finders
• IP based location lookup
• Cookie based user location tracking
• Scopes: within, beyond, in_range, in_bounds,
closest, farthest, by_distance
• Generate the SQL
• Auto-Geocoding
• Mixin
class Location < ActiveRecord::Base
acts_as_mappable :default_units => :miles,
:default_formula => :sphere,
:distance_field_name => :distance,
:lat_column_name => :lat,
:lng_column_name => :lng
end
Actually, there’s several…
RGeo
• Adapters for
– MySQL
– SQLite
– PostGIS
• Process GeoJSON
• Read shapefiles
• Uses C++ extensions for
processing
Geocoder
• Object Geocoding
– IP
– Address
• Reverse Geocoding
– Address from lat/lng or IP
• Center of Multiple Locations
• Geographic Queries
– Near / nearby
– Distance
– Direction
• Easier to use
• Seems to be some dispute about
accuracy of data
What about PostgreSQL?
• Provide database functions
to handle this calculations
– Distance in straight line or
great circle
– Convert lat/lng to point
– Get lat/lng from a point
– Calculate containment
within a cube
• Point datatype
– Operator to get distance
between
Earth Distance extension
http://www.postgresql.org/docs/9.2/s
tatic/earthdistance.html
Cube based
Point based
SO WHAT DOES POSTGIS DO THEN?
Funny you should ask…
EVERYTHING
Seriously…it does pretty much
everything
Import and Export Data
• Import CSV
• Import/export with GDAL (Geospatial Abstraction
Library)
– Shapefiles
– OGR
– Geospatial vector data files
– OpenStreetMap data (openstreetmap.org)
– Raster datasets
• Import OGR files
• ETL migration tools
– GeoKettle
GIS Desktop Software
Open Source
• OpenJump
• QuantumGIS
• uDig
• GvSig
• OrbisGIS
• PostGIS Viewer
• pgAdmin plugin
Commercial
• CadCorp SIS
• Manifold.net
• MapInfo Professional
• AutoCAD
• ESRI ArcGIS
Mapping Servers Integration
• Mapserver
• GeoServer
• Deegree
• QGIS Server
• MapGuide
Data: OpenStreetMap
Data: US Census
Data: WorldClim
US Geological Survey
Natural Earth
MaxMind GeoIP
Web Services / API
Constantly Updated Databases
Free and Paid Versions
Check your IP to try it out
https://www.maxmind.com/en/locate
_my_ip
Working with Data
• Datatypes
– Geography
• Ellipsoidal spatial data
– Geometry
• Planar spatial data
• Indexes
• Topology
• Spatial Joins
• 3D
– Mapping (building, etc)
– Image generation
• pgRoute
– Generate driving route
• Generate raster images from SQL
queries
• Spatial Relationship Functions
– ST_Contains
– ST_Covers
– ST_Crosses
– ST_DWithin
– ST_Intersects
– ST_Distance
• Clustered Queries for Huge
Datasets
Fun data tricks
• Use a TRIGGER to populate a geographic data
column for transparent indexing
• Create a Geospatial View
• Use table inheritance to centralize
commonality among different data types
– Geographic Data
– Search Data
AR PostGIS Adapter
Migrations
create_table :locations do |t|
t.column :shape1, :geometry
t.geometry :shape2
t.line_string :path, :srid => 3785
t.point :lonlat, :geographic => true
t.point :lonlatheight, :geographic => true, :has_z => true
t.index :lonlat, :spatial => true
end
Datatypes
:geometry -- Any geometric type
:point -- Point data
:line_string -- LineString data
:polygon -- Polygon data
:geometry_collection -- Any collection type
:multi_point -- A collection of Points
:multi_line_string -- A collection of LineStrings
:multi_polygon -- A collection of Polygons
ActiveRecord
Location.where(:lonlat => 'POINT(-122 47)').first
Location.where("ST_Distance(latlon,
'POINT(-122.330779 47.604828)') < 10000")
scope :distance_from, ->(lat, lon, dist) do
where(“ST_Distance(latlon, ‘POINT(? ?)’) < ?, lat, lon, dist)
end
Location.where("ST_Intersects(latlon,
'POLYGON((
-122.19 47.68,
-122.2 47.675,
-122.19 47.67,
-122.19 47.68))')")
https://github.com/rgeo/active
record-postgis-adapter
Features
• Uses Rgeo gem
• Spatial Migrations
• Spatial Datatype
• Spatial Queries
• Create / Update PostGIS DB
• Support central schema
Assignment 2
• Refine and improve your application
• Add authentication
• Add authors (or some type of user) to created data
• Display author info on your data
• Implement simple_form somewhere
• Add a new validation rule to a model
• Add a page to show related data for an author
• Use slim (or haml) to create one of your views
• Add a BASIC geographic capability (optional)
– Use your own discretion for how deep you go
– Suggestions / Ideas
• Geocode user IP addresses to get city/state/zip info
• Add address fields to your data type to make it geographically relevant
• Use a distance filter in search

More Related Content

What's hot

Scaling PostreSQL with Stado
Scaling PostreSQL with StadoScaling PostreSQL with Stado
Scaling PostreSQL with StadoJim Mlodgenski
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time Geostreamsguest35660bc
 
MySQL and GIS Programming
MySQL and GIS ProgrammingMySQL and GIS Programming
MySQL and GIS ProgrammingMike Benshoof
 
Spatiotemporal Raster Improvements in GeoServer
Spatiotemporal Raster Improvements in GeoServerSpatiotemporal Raster Improvements in GeoServer
Spatiotemporal Raster Improvements in GeoServerGeoSolutions
 
LocationTech Projects
LocationTech ProjectsLocationTech Projects
LocationTech ProjectsJody Garnett
 
OSM data in MariaDB / MySQL - All the world in a few large tables
OSM data in MariaDB / MySQL - All the world in a few large tablesOSM data in MariaDB / MySQL - All the world in a few large tables
OSM data in MariaDB / MySQL - All the world in a few large tableshholzgra
 
Don't optimize my queries, organize my data!
Don't optimize my queries, organize my data!Don't optimize my queries, organize my data!
Don't optimize my queries, organize my data!Julian Hyde
 
Overview of MassGIS Web Mapping Services
Overview of MassGIS Web Mapping ServicesOverview of MassGIS Web Mapping Services
Overview of MassGIS Web Mapping Servicesaleda_freeman
 
Wms Performance Tests Map Server Vs Geo Server
Wms Performance Tests Map Server Vs Geo ServerWms Performance Tests Map Server Vs Geo Server
Wms Performance Tests Map Server Vs Geo ServerDonnyV
 
Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSigmoid
 
SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)wqchen
 
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...CloudxLab
 
Askayworkshop
AskayworkshopAskayworkshop
Askayworkshopsconnin
 
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOAltinity Ltd
 
All you need to know about CREATE STATISTICS
All you need to know about CREATE STATISTICSAll you need to know about CREATE STATISTICS
All you need to know about CREATE STATISTICSEDB
 
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEXWhere the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEXJim Czuprynski
 
scalable machine learning
scalable machine learningscalable machine learning
scalable machine learningSamir Bessalah
 
WMS Performance Shootout 2009
WMS Performance Shootout 2009WMS Performance Shootout 2009
WMS Performance Shootout 2009Jeff McKenna
 
Scaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLScaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLJim Mlodgenski
 

What's hot (20)

Scaling PostreSQL with Stado
Scaling PostreSQL with StadoScaling PostreSQL with Stado
Scaling PostreSQL with Stado
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time Geostreams
 
MySQL and GIS Programming
MySQL and GIS ProgrammingMySQL and GIS Programming
MySQL and GIS Programming
 
Spatiotemporal Raster Improvements in GeoServer
Spatiotemporal Raster Improvements in GeoServerSpatiotemporal Raster Improvements in GeoServer
Spatiotemporal Raster Improvements in GeoServer
 
LocationTech Projects
LocationTech ProjectsLocationTech Projects
LocationTech Projects
 
OSM data in MariaDB / MySQL - All the world in a few large tables
OSM data in MariaDB / MySQL - All the world in a few large tablesOSM data in MariaDB / MySQL - All the world in a few large tables
OSM data in MariaDB / MySQL - All the world in a few large tables
 
Don't optimize my queries, organize my data!
Don't optimize my queries, organize my data!Don't optimize my queries, organize my data!
Don't optimize my queries, organize my data!
 
Overview of MassGIS Web Mapping Services
Overview of MassGIS Web Mapping ServicesOverview of MassGIS Web Mapping Services
Overview of MassGIS Web Mapping Services
 
Wms Performance Tests Map Server Vs Geo Server
Wms Performance Tests Map Server Vs Geo ServerWms Performance Tests Map Server Vs Geo Server
Wms Performance Tests Map Server Vs Geo Server
 
Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. Jyotiska
 
SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)
 
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
 
Askayworkshop
AskayworkshopAskayworkshop
Askayworkshop
 
Prashant de-ny-project-s1
Prashant de-ny-project-s1Prashant de-ny-project-s1
Prashant de-ny-project-s1
 
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
 
All you need to know about CREATE STATISTICS
All you need to know about CREATE STATISTICSAll you need to know about CREATE STATISTICS
All you need to know about CREATE STATISTICS
 
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEXWhere the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
 
scalable machine learning
scalable machine learningscalable machine learning
scalable machine learning
 
WMS Performance Shootout 2009
WMS Performance Shootout 2009WMS Performance Shootout 2009
WMS Performance Shootout 2009
 
Scaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQLScaling PostgreSQL With GridSQL
Scaling PostgreSQL With GridSQL
 

Viewers also liked

Day 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application ArchitectureDay 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application ArchitectureBarry Jones
 
Day 1 - Intro to Ruby
Day 1 - Intro to RubyDay 1 - Intro to Ruby
Day 1 - Intro to RubyBarry Jones
 
Day 2 - Intro to Rails
Day 2 - Intro to RailsDay 2 - Intro to Rails
Day 2 - Intro to RailsBarry Jones
 
PostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databasePostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databaseBarry Jones
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it FastBarry Jones
 

Viewers also liked (7)

Day 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application ArchitectureDay 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application Architecture
 
Day 1 - Intro to Ruby
Day 1 - Intro to RubyDay 1 - Intro to Ruby
Day 1 - Intro to Ruby
 
Day 2 - Intro to Rails
Day 2 - Intro to RailsDay 2 - Intro to Rails
Day 2 - Intro to Rails
 
PostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databasePostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty database
 
Day 4 - Models
Day 4 - ModelsDay 4 - Models
Day 4 - Models
 
Day 8 - jRuby
Day 8 - jRubyDay 8 - jRuby
Day 8 - jRuby
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it Fast
 

Similar to Day 6 - PostGIS

NAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing BasicsNAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing Basicspdituri
 
Efficient Query Processing in Geographic Web Search Engines
Efficient Query Processing in Geographic Web Search EnginesEfficient Query Processing in Geographic Web Search Engines
Efficient Query Processing in Geographic Web Search EnginesYen-Yu Chen
 
Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...
Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...
Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...Mihail Mateev
 
Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Managing GeoData with PostGIS @ KhmelnytskyiPy #1Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Managing GeoData with PostGIS @ KhmelnytskyiPy #1Volodymyr Gamula
 
Geek Sync | Having Fun with Spatial Data
Geek Sync | Having Fun with Spatial DataGeek Sync | Having Fun with Spatial Data
Geek Sync | Having Fun with Spatial DataIDERA Software
 
Geographical Data Management for Web Applications
Geographical Data Management for Web ApplicationsGeographical Data Management for Web Applications
Geographical Data Management for Web ApplicationsSymeon Papadopoulos
 
NGSI: Geoqueries & Carto integration
NGSI: Geoqueries & Carto integrationNGSI: Geoqueries & Carto integration
NGSI: Geoqueries & Carto integrationFIWARE
 
GIS Introduction.ppt
GIS Introduction.pptGIS Introduction.ppt
GIS Introduction.pptmisterjis
 
A Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph AnalyticsA Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph AnalyticsDonald Nguyen
 
R spatial presentation
R spatial presentationR spatial presentation
R spatial presentationTodd Barr
 
Building Location Aware Apps - Get Started with PostGIS, PART II
Building Location Aware Apps - Get Started with PostGIS, PART IIBuilding Location Aware Apps - Get Started with PostGIS, PART II
Building Location Aware Apps - Get Started with PostGIS, PART IIlasmasi
 
Demolitions and Dali : Web Dev and Data in a Graph Database
Demolitions and Dali : Web Dev and Data in a Graph DatabaseDemolitions and Dali : Web Dev and Data in a Graph Database
Demolitions and Dali : Web Dev and Data in a Graph DatabaseNicholas Doiron
 
Pgrouting_foss4guk_ross_mcdonald
Pgrouting_foss4guk_ross_mcdonaldPgrouting_foss4guk_ross_mcdonald
Pgrouting_foss4guk_ross_mcdonaldRoss McDonald
 
The state of geo in ElasticSearch
The state of geo in ElasticSearchThe state of geo in ElasticSearch
The state of geo in ElasticSearchFan Robbin
 
[2015/2016] Geolocation and mapping
[2015/2016] Geolocation and mapping[2015/2016] Geolocation and mapping
[2015/2016] Geolocation and mappingIvano Malavolta
 

Similar to Day 6 - PostGIS (20)

NAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing BasicsNAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing Basics
 
Mobile LBS
Mobile LBSMobile LBS
Mobile LBS
 
Efficient Query Processing in Geographic Web Search Engines
Efficient Query Processing in Geographic Web Search EnginesEfficient Query Processing in Geographic Web Search Engines
Efficient Query Processing in Geographic Web Search Engines
 
Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...
Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...
Cloud conf-varna-2014-mihail mateev-spatial-data-and-microsoft-azure-sql-data...
 
Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Managing GeoData with PostGIS @ KhmelnytskyiPy #1Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Managing GeoData with PostGIS @ KhmelnytskyiPy #1
 
Geek Sync | Having Fun with Spatial Data
Geek Sync | Having Fun with Spatial DataGeek Sync | Having Fun with Spatial Data
Geek Sync | Having Fun with Spatial Data
 
Geographical Data Management for Web Applications
Geographical Data Management for Web ApplicationsGeographical Data Management for Web Applications
Geographical Data Management for Web Applications
 
NGSI: Geoqueries & Carto integration
NGSI: Geoqueries & Carto integrationNGSI: Geoqueries & Carto integration
NGSI: Geoqueries & Carto integration
 
GIS Introduction.ppt
GIS Introduction.pptGIS Introduction.ppt
GIS Introduction.ppt
 
A Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph AnalyticsA Lightweight Infrastructure for Graph Analytics
A Lightweight Infrastructure for Graph Analytics
 
R spatial presentation
R spatial presentationR spatial presentation
R spatial presentation
 
Building Location Aware Apps - Get Started with PostGIS, PART II
Building Location Aware Apps - Get Started with PostGIS, PART IIBuilding Location Aware Apps - Get Started with PostGIS, PART II
Building Location Aware Apps - Get Started with PostGIS, PART II
 
Demolitions and Dali : Web Dev and Data in a Graph Database
Demolitions and Dali : Web Dev and Data in a Graph DatabaseDemolitions and Dali : Web Dev and Data in a Graph Database
Demolitions and Dali : Web Dev and Data in a Graph Database
 
Get mapping with leaflet js
Get mapping with leaflet jsGet mapping with leaflet js
Get mapping with leaflet js
 
Pycon2011
Pycon2011Pycon2011
Pycon2011
 
Pgrouting_foss4guk_ross_mcdonald
Pgrouting_foss4guk_ross_mcdonaldPgrouting_foss4guk_ross_mcdonald
Pgrouting_foss4guk_ross_mcdonald
 
The state of geo in ElasticSearch
The state of geo in ElasticSearchThe state of geo in ElasticSearch
The state of geo in ElasticSearch
 
[2015/2016] Geolocation and mapping
[2015/2016] Geolocation and mapping[2015/2016] Geolocation and mapping
[2015/2016] Geolocation and mapping
 
Post gispguk
Post gispgukPost gispguk
Post gispguk
 
Geolocation and Mapping
Geolocation and MappingGeolocation and Mapping
Geolocation and Mapping
 

More from Barry Jones

Repeating History...On Purpose...with Elixir
Repeating History...On Purpose...with ElixirRepeating History...On Purpose...with Elixir
Repeating History...On Purpose...with ElixirBarry Jones
 
Go from a PHP Perspective
Go from a PHP PerspectiveGo from a PHP Perspective
Go from a PHP PerspectiveBarry Jones
 
Protecting Users from Fraud
Protecting Users from FraudProtecting Users from Fraud
Protecting Users from FraudBarry Jones
 
AWS re:Invent 2013 Recap
AWS re:Invent 2013 RecapAWS re:Invent 2013 Recap
AWS re:Invent 2013 RecapBarry Jones
 
Pair Programming - the lightning talk
Pair Programming - the lightning talkPair Programming - the lightning talk
Pair Programming - the lightning talkBarry Jones
 
What's the "right" PHP Framework?
What's the "right" PHP Framework?What's the "right" PHP Framework?
What's the "right" PHP Framework?Barry Jones
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLBarry Jones
 

More from Barry Jones (7)

Repeating History...On Purpose...with Elixir
Repeating History...On Purpose...with ElixirRepeating History...On Purpose...with Elixir
Repeating History...On Purpose...with Elixir
 
Go from a PHP Perspective
Go from a PHP PerspectiveGo from a PHP Perspective
Go from a PHP Perspective
 
Protecting Users from Fraud
Protecting Users from FraudProtecting Users from Fraud
Protecting Users from Fraud
 
AWS re:Invent 2013 Recap
AWS re:Invent 2013 RecapAWS re:Invent 2013 Recap
AWS re:Invent 2013 Recap
 
Pair Programming - the lightning talk
Pair Programming - the lightning talkPair Programming - the lightning talk
Pair Programming - the lightning talk
 
What's the "right" PHP Framework?
What's the "right" PHP Framework?What's the "right" PHP Framework?
What's the "right" PHP Framework?
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
 

Recently uploaded

AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxellan12
 
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...Escorts Call Girls
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...tanu pandey
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...APNIC
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)Delhi Call girls
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...Neha Pandey
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...Diya Sharma
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableSeo
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebJames Anderson
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Servicesexy call girls service in goa
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Call Girls in Nagpur High Profile
 
INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.
INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.
INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.CarlotaBedoya1
 
✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663
✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663
✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663Call Girls Mumbai
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Sheetaleventcompany
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
 
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.
INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.
INDIVIDUAL ASSIGNMENT #3 CBG, PRESENTATION.
 
✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663
✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663
✂️ 👅 Independent Andheri Escorts With Room Vashi Call Girls 💃 9004004663
 
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
 

Day 6 - PostGIS

  • 2. What is Geospatial • Most common use – Narrow a search within a distance from a point of origin – Sort by that distance
  • 3. Distance Formulas • Flat-surface formulae – Spherical Earth projected to a plane – Ellipsoidal Earth projected to a plane – Polar coordinate flat-Earth formula • Spherical-surface formulae – Haversine – Tunnel distance • Ellipsoidal-surface formulae – Lambert’s formula for long lines – Bowring’s method for short lines
  • 5. What you usually care about • Given 2 latitude and longitude points – Calculate the distance so we can find what’s close • This is the common capability of databases with “geospatial” support
  • 6. A few options… • Do the math in a query • Here’s what it looks like in SQL – (ACOS(least(1,COS(0.607198022186895)*COS(- 1.4410239410591847)*COS(RADIANS(latitude))*COS(RAD IANS(longitude)) + COS(0.607198022186895)*SIN(- 1.4410239410591847)*COS(RADIANS(latitude))*SIN(RAD IANS(longitude)) + SIN(0.607198022186895)*SIN(RADIANS(latitude))))*39 63.19)) <= 25) – Calculating within 25 miles of a point of origin based on an origin point – You can do that in any database (it’s just math) – Very intense query that slows as your dataset grows – This…is not using an index
  • 7. Speed that up • Distance query a subset instead • Use the lat/lng to create query a box around the boundary • Queried with a numerical index • Then check the distance from center for the subset • NOTE: Doing this as part of another query is a lot easier with multi-index queries
  • 8. And here’s what THAT looks like WHERE ( latitude > 34.42845936786603 AND latitude < 35.15130863213399 AND longitude > -83.00467808012291 AND longitude < -82.12450191987708 ) AND ( (ACOS(least(1,COS(0.607198022186895)*COS(- 1.4410239410591847)*COS(RADIANS(latitude))*COS(RADIANS(longitude))+ COS(0.607198022186895)*SIN(- 1.4410239410591847)*COS(RADIANS(latitude))*SIN(RADIANS(longitude))+ SIN(0.607198022186895)*SIN(RADIANS(latitude))))*3963.19) ) <= 25) Simple, am I right? Also, you need to drop that distance formula into the ORDER BY clause too.
  • 9. There’s a Gem for that! Geokit https://github.com/geokit/geokit • Distance calculations between 2 points – Multiple formulas and units of measure • Multiple providers for Geocoding different data – Addresses • Yahoo • Geocoder.us/.ca • Geonames • Bing • Yandex • MapQuest • Geocode.io • Mapbox • Google • FCC • Open Street Map – IP Address • hostip.info • Geoplugin.net • RIPE • MaxMind (HIGHLY RECOMMEND) • freegeoip.net Geokit Rails (any database) https://github.com/geokit/geokit-rails • Premium Rails Integration • ActiveRecord distance finders • IP based location lookup • Cookie based user location tracking • Scopes: within, beyond, in_range, in_bounds, closest, farthest, by_distance • Generate the SQL • Auto-Geocoding • Mixin class Location < ActiveRecord::Base acts_as_mappable :default_units => :miles, :default_formula => :sphere, :distance_field_name => :distance, :lat_column_name => :lat, :lng_column_name => :lng end
  • 10. Actually, there’s several… RGeo • Adapters for – MySQL – SQLite – PostGIS • Process GeoJSON • Read shapefiles • Uses C++ extensions for processing Geocoder • Object Geocoding – IP – Address • Reverse Geocoding – Address from lat/lng or IP • Center of Multiple Locations • Geographic Queries – Near / nearby – Distance – Direction • Easier to use • Seems to be some dispute about accuracy of data
  • 11. What about PostgreSQL? • Provide database functions to handle this calculations – Distance in straight line or great circle – Convert lat/lng to point – Get lat/lng from a point – Calculate containment within a cube • Point datatype – Operator to get distance between Earth Distance extension http://www.postgresql.org/docs/9.2/s tatic/earthdistance.html Cube based Point based
  • 12. SO WHAT DOES POSTGIS DO THEN? Funny you should ask…
  • 14. Import and Export Data • Import CSV • Import/export with GDAL (Geospatial Abstraction Library) – Shapefiles – OGR – Geospatial vector data files – OpenStreetMap data (openstreetmap.org) – Raster datasets • Import OGR files • ETL migration tools – GeoKettle
  • 15. GIS Desktop Software Open Source • OpenJump • QuantumGIS • uDig • GvSig • OrbisGIS • PostGIS Viewer • pgAdmin plugin Commercial • CadCorp SIS • Manifold.net • MapInfo Professional • AutoCAD • ESRI ArcGIS
  • 16. Mapping Servers Integration • Mapserver • GeoServer • Deegree • QGIS Server • MapGuide
  • 22. MaxMind GeoIP Web Services / API Constantly Updated Databases Free and Paid Versions Check your IP to try it out https://www.maxmind.com/en/locate _my_ip
  • 23. Working with Data • Datatypes – Geography • Ellipsoidal spatial data – Geometry • Planar spatial data • Indexes • Topology • Spatial Joins • 3D – Mapping (building, etc) – Image generation • pgRoute – Generate driving route • Generate raster images from SQL queries • Spatial Relationship Functions – ST_Contains – ST_Covers – ST_Crosses – ST_DWithin – ST_Intersects – ST_Distance • Clustered Queries for Huge Datasets
  • 24. Fun data tricks • Use a TRIGGER to populate a geographic data column for transparent indexing • Create a Geospatial View • Use table inheritance to centralize commonality among different data types – Geographic Data – Search Data
  • 25. AR PostGIS Adapter Migrations create_table :locations do |t| t.column :shape1, :geometry t.geometry :shape2 t.line_string :path, :srid => 3785 t.point :lonlat, :geographic => true t.point :lonlatheight, :geographic => true, :has_z => true t.index :lonlat, :spatial => true end Datatypes :geometry -- Any geometric type :point -- Point data :line_string -- LineString data :polygon -- Polygon data :geometry_collection -- Any collection type :multi_point -- A collection of Points :multi_line_string -- A collection of LineStrings :multi_polygon -- A collection of Polygons ActiveRecord Location.where(:lonlat => 'POINT(-122 47)').first Location.where("ST_Distance(latlon, 'POINT(-122.330779 47.604828)') < 10000") scope :distance_from, ->(lat, lon, dist) do where(“ST_Distance(latlon, ‘POINT(? ?)’) < ?, lat, lon, dist) end Location.where("ST_Intersects(latlon, 'POLYGON(( -122.19 47.68, -122.2 47.675, -122.19 47.67, -122.19 47.68))')") https://github.com/rgeo/active record-postgis-adapter Features • Uses Rgeo gem • Spatial Migrations • Spatial Datatype • Spatial Queries • Create / Update PostGIS DB • Support central schema
  • 26. Assignment 2 • Refine and improve your application • Add authentication • Add authors (or some type of user) to created data • Display author info on your data • Implement simple_form somewhere • Add a new validation rule to a model • Add a page to show related data for an author • Use slim (or haml) to create one of your views • Add a BASIC geographic capability (optional) – Use your own discretion for how deep you go – Suggestions / Ideas • Geocode user IP addresses to get city/state/zip info • Add address fields to your data type to make it geographically relevant • Use a distance filter in search