geoapplications development  · 2018. 11. 25. · postgis –spatial objects (2) 18 sql-mm part 3:...

30
Geoapplications development http://rgeo.wikience.org Higher School of Economics, Moscow, www.cs.hse.ru

Upload: others

Post on 22-Jan-2021

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Geoapplications development

http://rgeo.wikience.org

Higher School of Economics, Moscow, www.cs.hse.ru

Page 2: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Agenda2

Page 3: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Spatial queries: motivating examples3

http://www.spatial.cs.umn.edu/Book/sdb-chap1.pdf

Page 4: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Spatial SQL4

OpenGIS Implementation Specification for Geographic information -Simple feature access - Part 2: SQL option

Page 5: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Spatial database management system (SDBMS)5

Page 6: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Discussion6

Page 7: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Data platforms map: June 2015, by 451 Research7

Page 8: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostgreSQL8

Page 9: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostGIS 9

http://postgis.net/features/

Page 10: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostGIS: generic workflow10

Page 11: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Enabling PostGIS11

http://postgis.net/install/

-- Enable PostGIS (includes raster)CREATE EXTENSION postgis;-- Enable TopologyCREATE EXTENSION postgis_topology;-- Enable PostGIS Advanced 3D-- and other geoprocessing algorithms-- sfcgal not available with all distributionsCREATE EXTENSION postgis_sfcgal;-- fuzzy matching needed for TigerCREATE EXTENSION fuzzystrmatch;-- rule based standardizerCREATE EXTENSION address_standardizer;-- example rule data setCREATE EXTENSION address_standardizer_data_us;-- Enable US Tiger GeocoderCREATE EXTENSION postgis_tiger_geocoder;

Page 12: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Enabling PostGIS (2)12

Page 13: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Basic SQL statements13

-- Create table with spatial columnCREATE TABLE geotable (

id SERIAL PRIMARY KEY,geom GEOMETRY(Point, 26910),name VARCHAR(128)

);

-- Add a spatial indexCREATE INDEX geotable_gix

ON geotableUSING GIST (geom);

See next trainings for indexing spatial data

-- Add a pointINSERT INTO geotable (geom) VALUES (

ST_GeomFromText('POINT(0 0)', 26910));

-- Query for nearby pointsSELECT id, nameFROM geotableWHERE ST_DWithin(

geom,ST_GeomFromText('POINT(0 0)', 26910),1000

);

Page 14: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostGIS at a glance14

Item Description

Spatial data types (for table columns)

geometry, geography, box3d, box2d, spheroid, raster, gidx, topology.TopoGeometry, etc.

Spatial objects (to be stored in table fields)

Defined using POINT(0 0)LINESTRING(0 0,1 1,1 2)

Indexing strategy GiST (Generalized Search Tree)

Functions (loads of them!)

Egenhofer topological relationship, map algebra, other raster operations, etc.

Command line tools

shp2pgsql, pgsql2shp, raster2pgsql, etc.

Formats Vector and raster data export in various formats

Other 3D object, network topology, geocoder/reverse geocoder/(US Census Tiger data)

See previous trainings to revise WKT + its UML, Egenhofer matrix, map algebra, etc.

Page 15: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostGIS – spatial data types15

<skipped> ………CREATE OR REPLACE FUNCTION geometry_send(geometry)

RETURNS byteaAS '$libdir/postgis-2.2','LWGEOM_send'LANGUAGE 'c' IMMUTABLE STRICT;

-- Availability: 0.1.0CREATE TYPE geometry (

internallength = variable,input = geometry_in,output = geometry_out,send = geometry_send,receive = geometry_recv,typmod_in = geometry_typmod_in,typmod_out = geometry_typmod_out,delimiter = ':',alignment = double,analyze = geometry_analyze,storage = main

);

-- Availability: 1.5.0CREATE TYPE geography (

internallength = variable,input = geography_in,output = geography_out,receive = geography_recv,send = geography_send,typmod_in = geography_typmod_in,typmod_out = geography_typmod_out,delimiter = ':',analyze = geography_analyze,storage = main,alignment = double

);

-- Availability: 0.5.0CREATE TYPE spheroid (

alignment = double,internallength = 65,input = spheroid_in,output = spheroid_out);

Page 16: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostGIS – spatial objects16

ST_GeomFromText('POINT(-126.4 45.32)', 26910) -- WKTST_GeomFromEWKT('SRID=26910;POINT(-126.4 45.32)') -- EWKT

Page 17: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Recall EPSG17

Page 18: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostGIS – spatial objects (2)18

SQL-MM Part 3:• SQL Multimedia Applications Spatial specification• Introduces circularly interpolated curves• not yet fully supported by PostGIS

CIRCULARSTRING(0 0, 1 1, 1 0)CIRCULARSTRING(0 0, 4 0, 4 4, 0 4, 0 0)

More details are at http://postgis.net/docs/manual-2.2/using_postgis_dbmanagement.html

Page 19: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

GEOMETRY VS GEOGRAPHY19

CREATE TABLE global_points (id SERIAL PRIMARY KEY,name VARCHAR(64),location GEOGRAPHY(POINT,4326) -- restrict to store POINTs only and SRID=4326=WGS84

);

INSERT INTO global_points (name, location) VALUES ('London',ST_GeographyFromText('SRID=4326;POINT(0 49)') );

http://postgis.net/docs/manual-2.2/using_postgis_dbmanagement.html

Page 20: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

JDBC & PostGIS: before we proceed20

Page 21: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

JDBC21

http://www.developersbook.com/jdbc/images/JDBC-Architecture.gif

Page 22: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

JDBC & PostGIS22

// Load the JDBC driver and establish a connection.

Class.forName("org.postgresql.Driver");

String url = "jdbc:postgresql://localhost:5400/postgis_example";

java.sql.Connection conn = DriverManager.getConnection(url, "postgres", "1111");

// <skipped adding PostGIS types to connection> -- see course site for complete code

// Create a statement and execute a select query

Statement s = conn.createStatement();

ResultSet r = s.executeQuery("select geom,id from geotable");

while (r.next()) {

// Retrieve the geometry as an object then cast it to the geometry type.

PGgeometry geom = (PGgeometry) r.getObject(1);

int id = r.getInt(2);

System.out.println("Row " + id + ":");

System.out.println(geom.toString());

} s.close(); conn.close();

Page 24: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

PostGIS functions24

Page 26: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Jaspa26

http://jaspa.upv.es/blog/about-jaspa/

Page 28: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Readings28

www.di.unipi.it/~confor/talks/SDBMS.ppt

• Spatial Databases and Spatial Data Management by Tatyana Budanskaya

Page 29: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly

Practical task29

Page 30: Geoapplications development  · 2018. 11. 25. · PostGIS –spatial objects (2) 18 SQL-MM Part 3: • SQL Multimedia Applications Spatial specification • Introduces circularly