geoapplications development · 2018. 11. 25. · postgis –spatial objects (2) 18 sql-mm part 3:...
TRANSCRIPT
Geoapplications development
http://rgeo.wikience.org
Higher School of Economics, Moscow, www.cs.hse.ru
Agenda2
Spatial queries: motivating examples3
http://www.spatial.cs.umn.edu/Book/sdb-chap1.pdf
Spatial SQL4
OpenGIS Implementation Specification for Geographic information -Simple feature access - Part 2: SQL option
Discussion6
Data platforms map: June 2015, by 451 Research7
PostgreSQL8
PostGIS: generic workflow10
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;
Enabling PostGIS (2)12
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
);
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.
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);
PostGIS – spatial objects16
•
•
ST_GeomFromText('POINT(-126.4 45.32)', 26910) -- WKTST_GeomFromEWKT('SRID=26910;POINT(-126.4 45.32)') -- EWKT
Recall EPSG17
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
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
JDBC & PostGIS: before we proceed20
JDBC21
http://www.developersbook.com/jdbc/images/JDBC-Architecture.gif
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();
PostGIS functions24
Readings28
•
•
www.di.unipi.it/~confor/talks/SDBMS.ppt
• Spatial Databases and Spatial Data Management by Tatyana Budanskaya
Practical task29