managing geodata with postgis @ khmelnytskyipy #1

40
Managing GeoData with PostGIS Volodymyr Gamula KhmelnytskyiPy #1

Upload: volodymyr-gamula

Post on 16-Aug-2015

128 views

Category:

Software


9 download

TRANSCRIPT

Page 1: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Managing GeoData with PostGIS

Volodymyr Gamula

KhmelnytskyiPy #1

Page 2: Managing GeoData with PostGIS @ 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

Page 3: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

GeoData is an information about geographic location,

stored in a specific format that can be used in geographic information systems (GIS)

What is GeoData?

Page 4: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Current situation with GeoData

Page 5: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

GeoData is everywhere!

Page 6: Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Page 7: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Suggested timeslots

Page 8: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

What is PostGIS?

Page 9: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

– 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.”

Page 10: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

History

• PostgreSQL: May 1, 1995

• PostGIS: April 19, 2005

• Very well tested

• Massively used in production

Page 11: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Spatial Data types in PostGIS

Page 12: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Point• (x, y, z) coordinates

• Represents locations such as buildings, train stations, etc.

• Also represents GPS latitute and longitude

• POINT(-51.1231325 12.213155)

Page 13: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

LineString

• Multiple points connected by straight lines

• Represents things such as roads, routes, cables, etc.

• LINESTRING(30 10, 10 30, 30 40)

Page 14: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

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)

Page 15: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Trivial problem

Page 16: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Distance between two points

Page 17: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

2 dimensions:

Page 18: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Geographical distance:

Page 19: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

What if we need to recalculate distance for

every request?

Page 20: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Standard formula brings a lot of complexity :(

Page 21: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

So, what should we do?

Page 22: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Geohashing

Page 23: Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Page 24: Managing GeoData with PostGIS @ KhmelnytskyiPy #1
Page 25: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

PostGIS has a function ST_Distance

Page 26: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

ST_Distance can work with the next types, which are basic PostGIS types:

• Geometry • Geography

Page 27: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

We should represent (lat, lng) to PostGIS

Geography field

Page 28: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

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.

Page 29: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

SRID

Page 30: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Query:

Page 31: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Why will this query be so fast?

Page 32: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Indexes!

Page 33: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Indexes:

• B-Tree

• R-Tree

• GiST (Generalized Search Trees)

Page 34: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

GiST Indexing for GeoData with calculations

Page 35: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Another PosGIS calculations:

• Area

• Distance

• Length

• Perimeter

• Union

• Difference

• Symmetric difference

Page 36: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

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);

Page 37: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Program interfaces:GeoAlchemy

Page 38: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

GeoDjango

Page 39: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Conclusions

Page 40: Managing GeoData with PostGIS @ KhmelnytskyiPy #1

Thanks!

@vgamula