45
PostGRES and Spatial SQL for Spatial Data Analysis

PostGIS and Spatial SQL

Embed Size (px)

Citation preview

Page 1: PostGIS and Spatial SQL

PostGRES and Spatial SQL for

Spatial Data Analysis

Page 2: PostGIS and Spatial SQL

Who Are You?

Page 3: PostGIS and Spatial SQL

Todd Barr

Spatial DataScientist

Page 4: PostGIS and Spatial SQL
Page 5: PostGIS and Spatial SQL
Page 6: PostGIS and Spatial SQL

What is “FOSS4G”

FREE OPEN SOURCE SOFTWARE “4” Geospatial/GIS

Page 7: PostGIS and Spatial SQL
Page 8: PostGIS and Spatial SQL

Why use FOSS4G Technology• Build on Open Standards so data and code are shareable and interoperable

• Increases Flexibility to respond to needs

• Rapid Innovation pushes Geospatial Technology forward

• Core Product can be customized to your need

• Lower Cost of Software Ownership

• Control When Patches are Introduced

Page 9: PostGIS and Spatial SQL
Page 10: PostGIS and Spatial SQL

What is Spatial SQL• Just like normal SQL but with Spatial Functions

• Geometry/Geography stored as a Binary Large Object (BLOB)

• Spatial Data is just another column in a database

• Attribution is maintained like other data within the database

Page 11: PostGIS and Spatial SQL

Drivers• Increase in Desire for Location Data (Social Media)

• Increased Use of Mobile

• Big Data/Large Amounts of Unstructured Data

• Increased understanding of Geography and its role in general data analysis

Page 12: PostGIS and Spatial SQL

Databases that Support Spatial SQL• Oracle• MySQL• SQL Server (starting in 2008)• Spatial Lite• PostGRES

Page 13: PostGIS and Spatial SQL

Functionality

Cost

s & F

ees

Page 14: PostGIS and Spatial SQL

PostGIS verse a “Geodatabase”(Personal, File, ArcSDE)

• Spatial SQL allows to query and analyze Spatial Data, a Geodatabase is just for data management – Still relies on GUI to preform analysis

• Spatial SQL is build on Open Geospatial Consortium (OGC), Geodatabases are not

• Spatial SQL can support a variety of applications, Geodatabases only supports desktop GIS software and proprietary Web Servers

Page 15: PostGIS and Spatial SQL

Shapefile verses PostGIS• Shapefiles cannot support more than 70m rows• Limitations for field names• Disk Space Hogs • Difficult to share data• Limitations to Date Field name• Do not support nulls• Files that exist in folders are difficult to manage (backups,size)• Only support Spatial Tools, not BI or non Spatial software

Page 16: PostGIS and Spatial SQL

Spatial Data Management with PostGIS• Writing SQL to query data opposed to generating new files• Increasing performance and response speeds through Indexing• PostGIS takes advantage of multiprocessing hardware• Users can access PostGIS through the network or Internet, without a

specialized web viewer or desktop software• PostGIS maintains data integrity and replication• PostGIS can handle multiple users accessing the same data• Can store Spatial Data as Text (geojson, WKT) or as Binary (BLOB or

WKB)

Page 17: PostGIS and Spatial SQL

PostGIS Spatial Capabilities• Supports all Geometry Types

• Point• Multipoint• Line• Linestring• Polygon• MultiPolygon• Collections

• Projections• Supports all known map projections• Allows reprojection within query

• Spatial Relationships• Measure• Serialize/Deserialize• Intersect• Buffer• Point n Polygon• Overlap/Touch/Cross

Page 18: PostGIS and Spatial SQL

PostGIS Spatial Capabilities• Can import/export any Spatial Data Type

• Spatial Indexing• Allows user to reduce query time

Page 19: PostGIS and Spatial SQL

Geometry Vs. Geography • Geography is always in WGS84• Geography has limited Spatial Functions• Geography views the world as 2D• Geography is faster for generating visualizations • Geometry allows for full analysis• Geometry allows for 3D Analysis

Page 20: PostGIS and Spatial SQL

Geography Geometry

Page 21: PostGIS and Spatial SQL

Metrics (Unscientific) • 400K buffers generated

• PostGIS: Shorter than a ½ mile walk with “the dog”• Proprietary Desktop GIS: 4 hours

• 1.5 Billion Point in Polygon Analyses

• PostGIS: Shorter than a commercial free episode of “The Walking Dead”• Proprietary Desktop GIS: Kept spinning, turned off after 24 hours

• 1.5 Billion Measurements

• PostGIS: a bit longer than a commercial free episode of “The Walking Dead”• Proprietary Desktop GIS – Error 99999 after 3 hours of the process

Page 22: PostGIS and Spatial SQL
Page 23: PostGIS and Spatial SQL

LET’S CODE

Page 24: PostGIS and Spatial SQL

Your Current Set up• “Lone Wolf” Analyst/Developer Style

• PostGRES 9.5• PostGIS 2.3.x

• QGIS 2.14 – for Map Making and Visualization

Page 25: PostGIS and Spatial SQL

Getting Comfortable With pgAdmin 4

Page 26: PostGIS and Spatial SQL

Finding the Database

Page 27: PostGIS and Spatial SQL

SQL Warm up

SELECT * FROM liquor_licenses

Page 28: PostGIS and Spatial SQL

The WHERE Clause

SELECT * FROM liquor_licensesWHERE license_ty='Tavern'

Page 29: PostGIS and Spatial SQL

Adding some Function

SELECTCOUNT(establish) FROM liquor_licenses WHERElicense_ty='Brew Pub’

Page 30: PostGIS and Spatial SQL

Group By

SELECTAVG(char_length(establish)), license_ty FROMliquor_licenses GROUP BYlicense_ty

Page 31: PostGIS and Spatial SQL

Standard Deviation (last non GEO one I promise)SELECTAVG(char_length(establish)),STDDEV(char_length(establish)),license_tyFROMliquor_licenses GROUP BYlicense_ty

Page 32: PostGIS and Spatial SQL

The GEOM

SELECTST_AsText(geom)FROM liquor_licenses

Page 33: PostGIS and Spatial SQL

ST_LengthSELECT ST_LENGTH (geom) ,strnameFROMstreet_centerlines

Page 34: PostGIS and Spatial SQL
Page 35: PostGIS and Spatial SQL

¯\_(ツ )_/¯ ST_Transform

SELECTST_LENGTH(ST_TRANSFORM(geom,2876),strnameFROM street_centerlines

Page 36: PostGIS and Spatial SQL

ST_AREA

SELECTobjectid,ST_AREA(ST_TRANSFORM(geom,2876))FROMbuildings

Page 37: PostGIS and Spatial SQL

ST_BUFFER

ALTER TABLEliquor_licensesADD Column buffer geometry(Polygon,4326)

Page 38: PostGIS and Spatial SQL

ST_BUFFER

UPDATE liquor_licensesSET Buffer=ST_TRANSFORM(ST_BUFFER(ST_TRANSFORM(geom,2876),30),4326)

Page 39: PostGIS and Spatial SQL

Exporting as Different Data Types

SELECT ST_AsGeoJSON(geom) FROM floodplain_city

SELECT ST_AsGML(geom) FROM floodplain_city

Page 40: PostGIS and Spatial SQL

ST_NPoints

SELECTST_NPoints(geom)FROMstreet_centerlines

Page 41: PostGIS and Spatial SQL

ST_IntersectSELECT a.strname,a.gid,a.geom, a.leftadd1,a.leftadd2,a.rgtadd1,a.rgtadd2 FROMstreet_centerlines a, floodplain_city b WHERE ST_Crosses(a.geom,b.geom)

Page 42: PostGIS and Spatial SQL
Page 43: PostGIS and Spatial SQL

ST_WithinSELECT a.establish, a.license_ty,a.gid,a.geomFROM liquor_licenses a, zoning b WHERE st_within(a.geom, b.geom) AND zone_name='LOW DENSITY MIXED-USE NEIGHBORHOOD DISTRICT'

Page 44: PostGIS and Spatial SQL

ST_Within - part 2SELECT COUNT(a.establish),a.license_ty, b.zone_name ,a.gid,a.geomFROM liquor_licenses a, zoning b WHERE ST_WITHIN(a.geom,b.geom) GROUP BYb.zone_name, a.license_ty

Page 45: PostGIS and Spatial SQL

ContactTWITTER:@Spatial_Punk

EMAIL: [email protected]

Slack:http://thespatialcommunity.slack.comHandle: truck