SlideShare a Scribd company logo
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

ODP
Introduction To PostGIS
mleslie
 
ODP
Intro To PostGIS
mleslie
 
PPT
Using PostGIS To Add Some Spatial Flavor To Your Application
Steven Pousty
 
PPTX
Why is postgis awesome?
Kasper Van Lombeek
 
PPTX
GeoMesa: Scalable Geospatial Analytics
VisionGEOMATIQUE2014
 
PPTX
PostGIS and Spatial SQL
Todd Barr
 
PPTX
MySQL 5.7 GIS
Pavan Naik
 
PDF
Spatial functions in MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and others
Henrik Ingo
 
Introduction To PostGIS
mleslie
 
Intro To PostGIS
mleslie
 
Using PostGIS To Add Some Spatial Flavor To Your Application
Steven Pousty
 
Why is postgis awesome?
Kasper Van Lombeek
 
GeoMesa: Scalable Geospatial Analytics
VisionGEOMATIQUE2014
 
PostGIS and Spatial SQL
Todd Barr
 
MySQL 5.7 GIS
Pavan Naik
 
Spatial functions in MySQL 5.6, MariaDB 5.5, PostGIS 2.0 and others
Henrik Ingo
 

What's hot (20)

PDF
Scaling PostreSQL with Stado
Jim Mlodgenski
 
KEY
Handling Real-time Geostreams
guest35660bc
 
ODP
MySQL and GIS Programming
Mike Benshoof
 
PPTX
Spatiotemporal Raster Improvements in GeoServer
GeoSolutions
 
PPTX
LocationTech Projects
Jody Garnett
 
PDF
OSM data in MariaDB / MySQL - All the world in a few large tables
hholzgra
 
PDF
Don't optimize my queries, organize my data!
Julian Hyde
 
PPT
Overview of MassGIS Web Mapping Services
aleda_freeman
 
PPT
Wms Performance Tests Map Server Vs Geo Server
DonnyV
 
PDF
Spark Dataframe - Mr. Jyotiska
Sigmoid
 
PDF
SparkR - Play Spark Using R (20160909 HadoopCon)
wqchen
 
PDF
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
CloudxLab
 
PPT
Askayworkshop
sconnin
 
PDF
Prashant de-ny-project-s1
Prashant Ratnaparkhi
 
PDF
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
Altinity Ltd
 
PDF
All you need to know about CREATE STATISTICS
EDB
 
PPSX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Jim Czuprynski
 
PPTX
scalable machine learning
Samir Bessalah
 
ODP
WMS Performance Shootout 2009
Jeff McKenna
 
ODP
Scaling PostgreSQL With GridSQL
Jim Mlodgenski
 
Scaling PostreSQL with Stado
Jim Mlodgenski
 
Handling Real-time Geostreams
guest35660bc
 
MySQL and GIS Programming
Mike Benshoof
 
Spatiotemporal Raster Improvements in GeoServer
GeoSolutions
 
LocationTech Projects
Jody Garnett
 
OSM data in MariaDB / MySQL - All the world in a few large tables
hholzgra
 
Don't optimize my queries, organize my data!
Julian Hyde
 
Overview of MassGIS Web Mapping Services
aleda_freeman
 
Wms Performance Tests Map Server Vs Geo Server
DonnyV
 
Spark Dataframe - Mr. Jyotiska
Sigmoid
 
SparkR - Play Spark Using R (20160909 HadoopCon)
wqchen
 
Writing MapReduce Programs using Java | Big Data Hadoop Spark Tutorial | Clou...
CloudxLab
 
Askayworkshop
sconnin
 
Prashant de-ny-project-s1
Prashant Ratnaparkhi
 
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
Altinity Ltd
 
All you need to know about CREATE STATISTICS
EDB
 
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Jim Czuprynski
 
scalable machine learning
Samir Bessalah
 
WMS Performance Shootout 2009
Jeff McKenna
 
Scaling PostgreSQL With GridSQL
Jim Mlodgenski
 
Ad

Viewers also liked (7)

PPTX
Day 9 - PostgreSQL Application Architecture
Barry Jones
 
PPTX
Day 1 - Intro to Ruby
Barry Jones
 
PPTX
Day 2 - Intro to Rails
Barry Jones
 
PPTX
PostgreSQL - It's kind've a nifty database
Barry Jones
 
PPTX
Day 4 - Models
Barry Jones
 
PPTX
Day 8 - jRuby
Barry Jones
 
PPTX
Day 7 - Make it Fast
Barry Jones
 
Day 9 - PostgreSQL Application Architecture
Barry Jones
 
Day 1 - Intro to Ruby
Barry Jones
 
Day 2 - Intro to Rails
Barry Jones
 
PostgreSQL - It's kind've a nifty database
Barry Jones
 
Day 4 - Models
Barry Jones
 
Day 8 - jRuby
Barry Jones
 
Day 7 - Make it Fast
Barry Jones
 
Ad

Similar to Day 6 - PostGIS (20)

PPTX
Gis and Ruby 101 at Ruby Conf Kenya 2017 by Kamal Ogudah
Michael Kimathi
 
KEY
OSCON july 2011
chelm
 
PDF
Comparing Geospatial Implementation in MongoDB, Postgres, and Elastic
Antonios Giannopoulos
 
PDF
Geo search introduction
kenshin03
 
PDF
Building Location Aware Apps - Get Started with PostGIS, PART I
lasmasi
 
PDF
Pg intro part1-theory_slides
lasmasi
 
PDF
Postgres Vision 2018: PostGIS and Spatial Extensions
EDB
 
PDF
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial World
GIS in the Rockies
 
PDF
Covering the earth and the cloud the next generation of spatial in sql server...
Texas Natural Resources Information System
 
PDF
Geolocation on Rails
nebirhos
 
KEY
rubykaigi2011_spatial.key
Tomonori Yano
 
PDF
Building Location Aware Apps - Get Started with PostGIS, PART II
lasmasi
 
PDF
Adelaide Ruby Meetup PostGIS Notes
chris-teague
 
PDF
Geographical Data Management for Web Applications
Symeon Papadopoulos
 
PDF
Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Volodymyr Gamula
 
PDF
A travellers guide to mapping technologies in django
Anthony Joseph
 
PDF
Geospatial capabilities on Ruby
Javier de la Torre
 
PPT
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your App
Steven Pousty
 
PPTX
FOSS4G 2017 Spatial Sql for Rookies
Todd Barr
 
Gis and Ruby 101 at Ruby Conf Kenya 2017 by Kamal Ogudah
Michael Kimathi
 
OSCON july 2011
chelm
 
Comparing Geospatial Implementation in MongoDB, Postgres, and Elastic
Antonios Giannopoulos
 
Geo search introduction
kenshin03
 
Building Location Aware Apps - Get Started with PostGIS, PART I
lasmasi
 
Pg intro part1-theory_slides
lasmasi
 
Postgres Vision 2018: PostGIS and Spatial Extensions
EDB
 
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial World
GIS in the Rockies
 
Covering the earth and the cloud the next generation of spatial in sql server...
Texas Natural Resources Information System
 
Geolocation on Rails
nebirhos
 
rubykaigi2011_spatial.key
Tomonori Yano
 
Building Location Aware Apps - Get Started with PostGIS, PART II
lasmasi
 
Adelaide Ruby Meetup PostGIS Notes
chris-teague
 
Geographical Data Management for Web Applications
Symeon Papadopoulos
 
Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Volodymyr Gamula
 
A travellers guide to mapping technologies in django
Anthony Joseph
 
Geospatial capabilities on Ruby
Javier de la Torre
 
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your App
Steven Pousty
 
FOSS4G 2017 Spatial Sql for Rookies
Todd Barr
 

More from Barry Jones (7)

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

Recently uploaded (20)

PPTX
Black Yellow Modern Minimalist Elegant Presentation.pptx
nothisispatrickduhh
 
PPTX
The Latest Scam Shocking the USA in 2025.pptx
onlinescamreport4
 
PDF
LB# 820-1889_051-7370_C000.schematic.pdf
matheusalbuquerqueco3
 
PDF
LOGENVIDAD DANNYFGRETRRTTRRRTRRRRRRRRR.pdf
juan456ytpro
 
PPTX
The Monk and the Sadhurr and the story of how
BeshoyGirgis2
 
PPTX
Google SGE SEO: 5 Critical Changes That Could Wreck Your Rankings in 2025
Reversed Out Creative
 
PPTX
原版北不列颠哥伦比亚大学毕业证文凭UNBC成绩单2025年新版在线制作学位证书
e7nw4o4
 
PPT
Transformaciones de las funciones elementales.ppt
rirosel211
 
PDF
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
PPTX
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
PDF
Cybersecurity Awareness Presentation ppt.
banodhaharshita
 
PPTX
LESSON-2-Roles-of-ICT-in-Teaching-for-learning_123922 (1).pptx
renavieramopiquero
 
PDF
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
PPTX
办理方法西班牙假毕业证蒙德拉贡大学成绩单MULetter文凭样本
xxxihn4u
 
PPTX
Artificial-Intelligence-in-Daily-Life (2).pptx
nidhigoswami335
 
PPTX
Slides Powerpoint: Eco Economic Epochs.pptx
Steven McGee
 
PPTX
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
PDF
UI/UX Developer Guide: Tools, Trends, and Tips for 2025
Penguin peak
 
PPTX
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
PPT
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 
Black Yellow Modern Minimalist Elegant Presentation.pptx
nothisispatrickduhh
 
The Latest Scam Shocking the USA in 2025.pptx
onlinescamreport4
 
LB# 820-1889_051-7370_C000.schematic.pdf
matheusalbuquerqueco3
 
LOGENVIDAD DANNYFGRETRRTTRRRTRRRRRRRRR.pdf
juan456ytpro
 
The Monk and the Sadhurr and the story of how
BeshoyGirgis2
 
Google SGE SEO: 5 Critical Changes That Could Wreck Your Rankings in 2025
Reversed Out Creative
 
原版北不列颠哥伦比亚大学毕业证文凭UNBC成绩单2025年新版在线制作学位证书
e7nw4o4
 
Transformaciones de las funciones elementales.ppt
rirosel211
 
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
Cybersecurity Awareness Presentation ppt.
banodhaharshita
 
LESSON-2-Roles-of-ICT-in-Teaching-for-learning_123922 (1).pptx
renavieramopiquero
 
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
办理方法西班牙假毕业证蒙德拉贡大学成绩单MULetter文凭样本
xxxihn4u
 
Artificial-Intelligence-in-Daily-Life (2).pptx
nidhigoswami335
 
Slides Powerpoint: Eco Economic Epochs.pptx
Steven McGee
 
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
UI/UX Developer Guide: Tools, Trends, and Tips for 2025
Penguin peak
 
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 

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