Managing GeoData with PostGIS @ KhmelnytskyiPy #1
-
Upload
volodymyr-gamula -
Category
Software
-
view
129 -
download
9
Transcript of Managing GeoData with PostGIS @ KhmelnytskyiPy #1
![Page 1: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/1.jpg)
Managing GeoData with PostGIS
Volodymyr Gamula
KhmelnytskyiPy #1
![Page 2: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/4.jpg)
Current situation with GeoData
![Page 5: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/5.jpg)
GeoData is everywhere!
![Page 6: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/6.jpg)
![Page 7: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/7.jpg)
Suggested timeslots
![Page 8: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/8.jpg)
What is PostGIS?
![Page 9: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/9.jpg)
– 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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/10.jpg)
History
• PostgreSQL: May 1, 1995
• PostGIS: April 19, 2005
• Very well tested
• Massively used in production
![Page 11: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/11.jpg)
Spatial Data types in PostGIS
![Page 12: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/15.jpg)
Trivial problem
![Page 16: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/16.jpg)
Distance between two points
![Page 17: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/17.jpg)
2 dimensions:
![Page 18: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/18.jpg)
Geographical distance:
![Page 19: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/19.jpg)
What if we need to recalculate distance for
every request?
![Page 20: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/20.jpg)
Standard formula brings a lot of complexity :(
![Page 21: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/21.jpg)
So, what should we do?
![Page 22: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/22.jpg)
Geohashing
![Page 23: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/23.jpg)
![Page 24: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/24.jpg)
![Page 25: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/25.jpg)
PostGIS has a function ST_Distance
![Page 26: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/26.jpg)
ST_Distance can work with the next types, which are basic PostGIS types:
• Geometry • Geography
![Page 27: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/27.jpg)
We should represent (lat, lng) to PostGIS
Geography field
![Page 28: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/29.jpg)
SRID
![Page 30: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/30.jpg)
Query:
![Page 31: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/31.jpg)
Why will this query be so fast?
![Page 32: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/32.jpg)
Indexes!
![Page 33: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/33.jpg)
Indexes:
• B-Tree
• R-Tree
• GiST (Generalized Search Trees)
![Page 34: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/34.jpg)
GiST Indexing for GeoData with calculations
![Page 35: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/35.jpg)
Another PosGIS calculations:
• Area
• Distance
• Length
• Perimeter
• Union
• Difference
• Symmetric difference
![Page 36: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/37.jpg)
Program interfaces:GeoAlchemy
![Page 38: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/38.jpg)
GeoDjango
![Page 39: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/39.jpg)
Conclusions
![Page 40: Managing GeoData with PostGIS @ KhmelnytskyiPy #1](https://reader034.fdocuments.net/reader034/viewer/2022042522/55d06932bb61eb2d7e8b4637/html5/thumbnails/40.jpg)
Thanks!
@vgamula