Managing GeoData with PostGIS
Volodymyr Gamula
KhmelnytskyiPy #1
Agenda:• Current situation with GeoData
• What is PostGIS?
• Spatial Data types in PostGIS?
• Trivial problem
• SRID
• Indexing
• Another ways to use PostGIS for
• How to start working with PostGIS now?
• Program interfaces
• Conclusions
GeoData is an information about geographic location,
stored in a specific format that can be used in geographic information systems (GIS)
What is GeoData?
Current situation with GeoData
GeoData is everywhere!
Suggested timeslots
What is PostGIS?
– Paul Ramsey, Chair of PostGIS Steering Committee
“It is a Relational Database System that in addition to text and numbers and dates, it can also index spatial objects - points, lines, and
polygons.”
History
• PostgreSQL: May 1, 1995
• PostGIS: April 19, 2005
• Very well tested
• Massively used in production
Spatial Data types in PostGIS
Point• (x, y, z) coordinates
• Represents locations such as buildings, train stations, etc.
• Also represents GPS latitute and longitude
• POINT(-51.1231325 12.213155)
LineString
• Multiple points connected by straight lines
• Represents things such as roads, routes, cables, etc.
• LINESTRING(30 10, 10 30, 30 40)
Polygon
• Multiple points which encloses an area
• Represents boundaries such as properties, towns, countries.
• POLYGON(30 10, 10 20, 20 40, 40 40, 30 10)
Trivial problem
Distance between two points
2 dimensions:
Geographical distance:
What if we need to recalculate distance for
every request?
Standard formula brings a lot of complexity :(
So, what should we do?
Geohashing
PostGIS has a function ST_Distance
ST_Distance can work with the next types, which are basic PostGIS types:
• Geometry • Geography
We should represent (lat, lng) to PostGIS
Geography field
There are a lot of ways to do it:• 'SRID=4326;POINT(83.106560 54.838971)'::geography
• ST_GeographyFromText('SRID=4326;POINT(-110 30)’)
• ST_GeomFromEWKT('SRID=4269;POINT(-110 30)’);
• ST_GeomFromGeoJSON(‚{"type":"Point","coordinates":[-48.23456,20.12345]}')
• ST_GeomFromGML(' <gml:Point srsName="http://www.opengis.net/def/crs/EPSG/0/4326"> <gml:coordinates>45.67, 88.56</gml:coordinates></gml:Point>’);
• etc.
SRID
Query:
Why will this query be so fast?
Indexes!
Indexes:
• B-Tree
• R-Tree
• GiST (Generalized Search Trees)
GiST Indexing for GeoData with calculations
Another PosGIS calculations:
• Area
• Distance
• Length
• Perimeter
• Union
• Difference
• Symmetric difference
How to start work with PostGIS now?
• Install PostgreSQL
• Install PostGIS
• CREATE EXTENSION postgis;
• CREATE EXTENSION btree_gist;
• ALTER TABLE geodata_test ADD COLUMN location geography(Point, 4326);
• CREATE INDEX ix_geodata_test_geohash ON geodata_test USING GIST(location);
Program interfaces:GeoAlchemy
GeoDjango
Conclusions
Thanks!
@vgamula