spatial databases: lecture 4 dt211-4 dt228-4 semester 2 2009-10 pat browne...

65
Spatial Databases: Spatial Databases: Lecture 4 Lecture 4 DT211-4 DT228-4 DT211-4 DT228-4 Semester 2 Semester 2 2009-10 2009-10 Pat Browne Pat Browne //www.comp.dit.ie/pbrowne/Spatial%20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005

Upload: elaine-booker

Post on 26-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Spatial Databases: Lecture 4Spatial Databases: Lecture 4DT211-4 DT228-4 DT211-4 DT228-4 Semester 2 Semester 2

2009-102009-10

Pat BrownePat Browne

http://www.comp.dit.ie/pbrowne/Spatial%20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Page 2: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

OutlineOutline

We will look in more detail on what happens We will look in more detail on what happens when a spatial table is constructed in when a spatial table is constructed in PostgreSQL/PostGIS.PostgreSQL/PostGIS.

We will describe how construct a table for a We will describe how construct a table for a sub-set of the historical data setsub-set of the historical data set11..

We take a closer look at a range of OGC queries We take a closer look at a range of OGC queries that can be used in PostGISthat can be used in PostGIS22..

A reminder of viewing and querying using A reminder of viewing and querying using OpenJumpOpenJump

We will look at map accuracy. We will look at map accuracy.

Page 3: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

PostgreSQL-PostGISPostgreSQL-PostGIS

An object-relational DBMS with PostGIS spatial extensions.An object-relational DBMS with PostGIS spatial extensions. Is completely Open Source Is completely Open Source Compliant with OGS’s Simple Features for SQLCompliant with OGS’s Simple Features for SQL Has a spaghetti-like spatial data model Has a spaghetti-like spatial data model Spatial indexing Spatial indexing Supports OGC types and ‘native types’: point, line, box, path, Supports OGC types and ‘native types’: point, line, box, path,

polygon, and circle geometric types polygon, and circle geometric types Topology is under development.Topology is under development. Can perform overlay function Can perform overlay function Simple features are based on 2D geometry with linear Simple features are based on 2D geometry with linear

interpolation between vertices.interpolation between vertices.

Page 4: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

PostGIS levels of representationPostGIS levels of representation

Page 5: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

PostGIS S/W componentsPostGIS S/W components PostGiS provides PostGiS provides Open Database Connectivity Open Database Connectivity ODBC connectivity. ODBC connectivity.

PostGIS includes extensions to the underlying PostgreSQL ODBC PostGIS includes extensions to the underlying PostgreSQL ODBC drivers which allow transparent access to GIS objects from PostGIS drivers which allow transparent access to GIS objects from PostGIS via the ODBC protocol. ODBC connectivity is part of the OGC via the ODBC protocol. ODBC connectivity is part of the OGC standard.standard.

PostGIS also provides Java Database Connectivity (JDBC), which is PostGIS also provides Java Database Connectivity (JDBC), which is not part of the OGC standard. not part of the OGC standard.

GiST (Generalized Search Tree) provides high speed spatial GiST (Generalized Search Tree) provides high speed spatial indexing.indexing.

PROJ.4 is an open source library that provides PROJ.4 is an open source library that provides coordinate coordinate reprojection reprojection to convert between geographic coordinate systems.to convert between geographic coordinate systems.

GEOS (Geometry Engine, Open Source) is a library used by GEOS (Geometry Engine, Open Source) is a library used by PostGIS to perform all the operations in the OpenGIS Simple PostGIS to perform all the operations in the OpenGIS Simple Features for SQL Specification. The GEOS library is used to provide Features for SQL Specification. The GEOS library is used to provide geometry tests (ST_Touches(), ST_Contains(), ST_Intersects()) and geometry tests (ST_Touches(), ST_Contains(), ST_Intersects()) and operations (ST_Buffer(), ST_Union(),ST_Intersection() operations (ST_Buffer(), ST_Union(),ST_Intersection() ST_Difference()) within PostGIS.ST_Difference()) within PostGIS.

Page 6: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

PostGIS and OGC standardPostGIS and OGC standard

1.1. PostGIS implements and is compliant with the PostGIS implements and is compliant with the OGC’s SimpleOGC’s Simple Features for SQL standard.Features for SQL standard.

2.2. PostGIS supports all OGC types: Point, Line, PostGIS supports all OGC types: Point, Line, Polygon, MultiPoint, MultiLine, MultiPolygon, Polygon, MultiPoint, MultiLine, MultiPolygon, GeometryCollection and operations on those GeometryCollection and operations on those typestypes

3.3. PostGIS uses OGC well-known text format on PostGIS uses OGC well-known text format on the SQL command-line to represent GIS the SQL command-line to represent GIS features.features.

Page 7: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a table.Creating a table.

Recall from the labs that the basic steps to Recall from the labs that the basic steps to create a spatially enable table:create a spatially enable table:

Create a table with the desired non-spatial Create a table with the desired non-spatial attributes.attributes.

Add a spatial column with as Add a spatial column with as PostGIS/OGC extension PostGIS/OGC extension AddGeometryColumnAddGeometryColumn

Insert the geometry with a SQL Insert the geometry with a SQL insert insert && selectselect statements. statements.

Page 8: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial tableCreating a spatial table

On the following slides we will describe how to make a On the following slides we will describe how to make a table containing a subset of the historical data set from table containing a subset of the historical data set from National Monuments ServiceNational Monuments Service11. We assume that the . We assume that the countycounty22 table exists and that Dublin is a single region in table exists and that Dublin is a single region in the the countycounty table. table.

Note a system generated identified (Note a system generated identified (gidgid) is used as the ) is used as the primary key.primary key.

It is possible to include a geometry column at table It is possible to include a geometry column at table creation time, but the system would not generate creation time, but the system would not generate integrity constraints so we will stick with this method.integrity constraints so we will stick with this method.

Page 9: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 1Creating a spatial table, step 1

CREATE TABLE "public"."dublin_historical“CREATE TABLE "public"."dublin_historical“ (gid serial PRIMARY KEY,(gid serial PRIMARY KEY,"rmp_prop" int8,"rmp_prop" int8,"map_symbol" int8,"map_symbol" int8,"entity_id" varchar(7),"entity_id" varchar(7),"co_id" int8,"co_id" int8,"smr_val0" numeric,"smr_val0" numeric,"nat_grid_e" numeric,"nat_grid_e" numeric,"class_desc" varchar(255),"class_desc" varchar(255),"nat_grid_n" numeric,"nat_grid_n" numeric,"objectid" int8,"objectid" int8,"townlands" varchar(255),"townlands" varchar(255),"scope_n1" varchar(255),"scope_n1" varchar(255),"smrs" varchar(255));"smrs" varchar(255));

Page 10: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 1Creating a spatial table, step 1

PostgreSQL/PostGIS will respond:PostgreSQL/PostGIS will respond: NOTICE: CREATE TABLE will create NOTICE: CREATE TABLE will create implicit sequence implicit sequence "dublin_historical_gid_seq" for "dublin_historical_gid_seq" for serial column serial column "dublin_historical.gid""dublin_historical.gid"

NOTICE: CREATE TABLE / PRIMARY KEY NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index will create implicit index "dublin_historical_pkey" for table "dublin_historical_pkey" for table "dublin_historical""dublin_historical"

Page 11: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 1Creating a spatial table, step 1

Examine the table (\d):Examine the table (\d):

Table "public.dublin_historical"Table "public.dublin_historical"

Column | Type | ModifiersColumn | Type | Modifiers

--------+---------+-------------------------------+---------+-----------------------

gid | integer | not null default gid | integer | not null default nextval('dublin_historical_gid_seq'::regclass)nextval('dublin_historical_gid_seq'::regclass)

Reset of dataReset of data

Indexes:Indexes:"dublin_historical_pkey"PRIMARY KEY,btree (gid)"dublin_historical_pkey"PRIMARY KEY,btree (gid)

Page 12: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 2Creating a spatial table, step 2

Here we add the geometry column, PostGIS will Here we add the geometry column, PostGIS will automatically generate integrity constraints.automatically generate integrity constraints.

SELECT SELECT AddGeometryColumn('public','dublin_hAddGeometryColumn('public','dublin_historical','the_geom','29900','POINTistorical','the_geom','29900','POINT',2);',2);

We use an version of the Irish National Grid with We use an version of the Irish National Grid with SRID = 29900. The type of the geometry to be SRID = 29900. The type of the geometry to be stored in the column must be included, in this stored in the column must be included, in this case it is POINT.case it is POINT.

Page 13: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 2Creating a spatial table, step 2

First system generated constraintFirst system generated constraint

ALTER TABLE ALTER TABLE dublin_historical dublin_historical DROP DROP CONSTRAINT CONSTRAINT enforce_dims_the_geom;enforce_dims_the_geom;

ALTER TABLE ALTER TABLE dublin_historicaldublin_historical

ADD CONSTRAINT ADD CONSTRAINT enforce_dims_the_geom CHECK enforce_dims_the_geom CHECK (ndims(the_geom) = 2);(ndims(the_geom) = 2);

Page 14: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 2Creating a spatial table, step 2

Second system generated constraintSecond system generated constraint

ALTER TABLE ALTER TABLE dublin_historicaldublin_historical

ADD CONSTRAINT ADD CONSTRAINT enforce_geotype_the_geom CHECK enforce_geotype_the_geom CHECK (geometrytype(the_geom) = (geometrytype(the_geom) = ‘POINT'::text OR the_geom IS ‘POINT'::text OR the_geom IS NULL);NULL);

Page 15: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 2Creating a spatial table, step 2

Third system generated constraintThird system generated constraint

ALTER TABLE ALTER TABLE dublin_historicaldublin_historical

ADD CONSTRAINT ADD CONSTRAINT enforce_srid_the_geom CHECK enforce_srid_the_geom CHECK (srid(the_geom) = 29900);(srid(the_geom) = 29900);

Page 16: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 2Creating a spatial table, step 2

The Primary Constraint was created in The Primary Constraint was created in step1 step1

CONSTRAINT CONSTRAINT dublin_historicaldublin_historical_pkey _pkey PRIMARY KEY(gid);PRIMARY KEY(gid);

Page 17: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 3Creating a spatial table, step 3

Next we insert the data from the all Ireland Next we insert the data from the all Ireland historicalhistorical table into the newly created table into the newly created dublin_istoricaldublin_istorical table. table.

Only data is contained in Dublin is inserted Only data is contained in Dublin is inserted into the new table. Check the OGC & into the new table. Check the OGC & PostGIS documentation on the PostGIS documentation on the containscontains predicate AKA ‘a spatial relationship predicate AKA ‘a spatial relationship function’.function’.

Page 18: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Creating a spatial table, step 3Creating a spatial table, step 3

insertinsert into "dublin_historical" into "dublin_historical" ("rmp_prop","map_symbol", "entity_id", ("rmp_prop","map_symbol", "entity_id", "co_id","smr_val0", "co_id","smr_val0", "nat_grid_e“ ,"class_desc","nat_grid_n","o"nat_grid_e“ ,"class_desc","nat_grid_n","objectid", "townlands","scope_n1", "smrs", bjectid", "townlands","scope_n1", "smrs", the_geom) the_geom)

selectselect "rmp_prop", "map_symbol", "rmp_prop", "map_symbol", "entity_id", "co_id","smr_val0", "entity_id", "co_id","smr_val0", "nat_grid_e", "class_desc", "nat_grid_n", "nat_grid_e", "class_desc", "nat_grid_n", "objectid", "townlands", "scope_n1", "objectid", "townlands", "scope_n1", "smrs", h.the_geom "smrs", h.the_geom

fromfrom "county" c, historical h where "county" c, historical h where contains(c.the_geom,h.the_geom) and contains(c.the_geom,h.the_geom) and

name = 'Dublin';name = 'Dublin';

Page 19: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

PostGIS system tables PostGIS system tables

The next few slides describe the built The next few slides describe the built PostGIS tables that provide the spatial PostGIS tables that provide the spatial functionality. We only outline the main functionality. We only outline the main features. For further details, please see features. For further details, please see the PostGIS 1.5 manual that was handed the PostGIS 1.5 manual that was handed out in lectures, also available at;out in lectures, also available at;

http://postgis.refractions.net/docs/http://postgis.refractions.net/docs/

Page 20: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

geometry_columns tablegeometry_columns table

Column | Type | ModifiersColumn | Type | Modifiers-----------------------------+--------------------------------+----------------------------------------+--------------------------------+----------- f_table_catalog | character varying(256) | not nullf_table_catalog | character varying(256) | not null f_table_schema | character varying(256) | not nullf_table_schema | character varying(256) | not null f_table_name | character varying(256) | not nullf_table_name | character varying(256) | not null f_geometry_column | character varying(256) | not nullf_geometry_column | character varying(256) | not null coord_dimension | integer | not nullcoord_dimension | integer | not null srid | integer | not nullsrid | integer | not null type | character varying(30) | not nulltype | character varying(30) | not nullIndexes:Indexes: "geometry_columns_pk" PRIMARY KEY, btree (f_table_catalog, "geometry_columns_pk" PRIMARY KEY, btree (f_table_catalog,

f_table_schema, ff_table_schema, f_table_name, f_geometry_column)_table_name, f_geometry_column)

Page 21: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

spatial_ref_sys tablespatial_ref_sys table

Displaying a spherical earth on a flat surface Displaying a spherical earth on a flat surface requires a requires a projectionprojection. This table uses a standard . This table uses a standard numbering, called the EPGSnumbering, called the EPGS11, to describe , to describe various projections. Using PostgreSQL’s various projections. Using PostgreSQL’s expanded display we can examine the details for expanded display we can examine the details for a particular projection representing the Irish a particular projection representing the Irish National Grid:National Grid:

\x\xselect * from spatial_ref_sys where srid=29900;select * from spatial_ref_sys where srid=29900;

Page 22: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

spatial_ref_sys tablespatial_ref_sys table

Column | Type | ModifiersColumn | Type | Modifiers-----------------+----------------------------------+----------------------------+----------------------------------+----------- srid | integer | not nullsrid | integer | not null auth_name | character varying(256) |auth_name | character varying(256) | auth_srid | integer |auth_srid | integer | srtext | character varying(2048) |srtext | character varying(2048) | proj4text | character varying(2048) | proj4text | character varying(2048) | Indexes:Indexes: "spatial_ref_sys_pkey" PRIMARY KEY, btree (srid)"spatial_ref_sys_pkey" PRIMARY KEY, btree (srid)

Page 23: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Valid Geometry?Valid Geometry? (Lakes table on CD) (Lakes table on CD)

tpostgis=# select IsValid(the_geom) from lakes;tpostgis=# select IsValid(the_geom) from lakes;NOTICE: Self-intersection at or near point 114275 271699NOTICE: Self-intersection at or near point 114275 271699NOTICE: Self-intersection at or near point 124552 240642NOTICE: Self-intersection at or near point 124552 240642NOTICE: Self-intersection at or near point 121283 305664NOTICE: Self-intersection at or near point 121283 305664 isvalidisvalid------------------ tt tt ff ff ff tt tt tt

Page 24: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Finding the centre point of a countyFinding the centre point of a county

To find the centre point of each county:To find the centre point of each county: select name, select name, asText(Centroid(the_geom)) from asText(Centroid(the_geom)) from county;county;

Page 25: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Finding a bounding boxFinding a bounding box

To find the bounding box of each county:To find the bounding box of each county: select name, select name, asText(envelope(the_geom)) from asText(envelope(the_geom)) from countycounty;;

OrOr select name, extent(the_geom)) from select name, extent(the_geom)) from county group by name;county group by name;

Must use ‘Must use ‘group bygroup by’ which can handle multiple ’ which can handle multiple polygons, also polygons, also extentextent returns the geometry as returns the geometry as text.text.

Page 26: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Finding the dimensions of a Finding the dimensions of a bounding boxbounding box

select name, extent(the_geom) from county select name, extent(the_geom) from county where name= 'Dublin' group by namewhere name= 'Dublin' group by name;;

ReturnsReturns Dublin | BOX(297049.5 215877.484375,330201.8125 266854.40625)Dublin | BOX(297049.5 215877.484375,330201.8125 266854.40625)

Using the above, we can measure the Using the above, we can measure the dimensions of the bounding box:dimensions of the bounding box:

select distance(select distance(geomFromText(‘Point….)’),geomFromText(‘Point….)’),geomFromText(‘Point….)’));geomFromText(‘Point….)’));

Page 27: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Point in PolygonPoint in Polygon

We can use a non-OGC function We can use a non-OGC function &&&& to find to find objects. objects.

SELECT name FROM original_county SELECT name FROM original_county where GeomFromText('POINT(309612.0 where GeomFromText('POINT(309612.0 233192.0)', 29900) && the_geom; 233192.0)', 29900) && the_geom;

Finds a given point in Dublin.Finds a given point in Dublin. When constructing a query it is important to When constructing a query it is important to

remember that only the bounding-box-based remember that only the bounding-box-based operators such as operators such as &&&& can take advantage of the can take advantage of the PostgreSQL’s GiST spatial index.PostgreSQL’s GiST spatial index.

Page 28: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Find things near a pointFind things near a point

select townlands from dublin_historical WHERE distance(the_geom, GeomFromText('POINT(309612.0 233192.0)', 29900)) < 100;

When we submit such a query we should have some idea of the type of the result set (use PostGIS manual, OGGSFSQL)

Page 29: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Find things near a pointFind things near a point

SELECT c.name, h.townlands FROM county AS c,dublin_historical AS h WHERE distance(h.the_geom, GeomFromText('POINT(317431.0 231704.0)', 29900)) < 1000 and c. Name = 'Dublin County Borough';

Do we know the type of result set here?

Page 30: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Find things near a pointFind things near a point

SELECT c.name, h.townlands

FROM county AS c,

dublin_historical AS h

WHERE st_dwithinst_dwithin(h.the_geom, PointFromText('POINT(317431.0 231704.0)', 29900),1000) and c. Name = 'Dublin County Borough';

Similar to previous query but using st_within.

Page 31: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Find things near a pointFind things near a point

SELECT gid,townlands FROM dublin_historical

WHERE st_dwithin(the_geom, GeomFromText('POINT(317431.0 231704.0)',29900),500)

ORDER BY st_dwithin(the_geom, GeomFromText('POINT(317431.0 231704.0)',29900),500)

LIMIT 100;

Page 32: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Finding the largest county in IrelandFinding the largest county in Ireland

SELECT name, area(the_geom)/10000 AS hectares FROM county ORDER BY hectares DESC LIMIT 1;

Leaving out Northern Ireland SELECT name, area(the_geom)/10000 AS hectares FROM county

where name != 'Northern Ireland' ORDER BY hectares DESC LIMIT 1;

Page 33: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

What is the length of roads fully contained within Dublin Borough?

SELECT c.name, sum(length(r.the_geom))/1000 as roads_km FROM roads AS r, county AS c WHERE r.the_geom && r.the_geom

AND contains(c.the_geom,r.the_geom)

AND c.name = 'Dublin County Borough'

GROUP BY c.name

ORDER BY roads_km;

Page 34: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

What historical objects are near Dublin roads?

SELECT townlands FROM dublin_historical h, roads r, county c WHERE

distance(h.the_geom,r.the_geom) < 1000

AND c.name = 'Dublin County BoroughDublin County Borough';

We say near is less than a kilometre from the road.

Page 35: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Overlays Overlays 

We should distinguish the overlay We should distinguish the overlay operationoperation and and teststests for overlap. The OGCSFSQL use two for overlap. The OGCSFSQL use two similar keyword. Table-on-table overlays are similar keyword. Table-on-table overlays are possible with the ST_Intersection() possible with the ST_Intersection() functionfunction ST_Intersects(a,b) returns BOOLEAN ST_Intersects(a,b) returns BOOLEAN ST_Intersection(a,b) returns GEOMETRY ST_Intersection(a,b) returns GEOMETRY

  ST_Intersects(a,b)=TRUE | FALSE

ST_Intersection()=GEOMETRY

Page 36: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Efficiency of SearchEfficiency of Search11

It is expensive to process the exact geometry of an object.Therefore approximations such as bounding boxes (BB) or a convex s (CH) are used to help to examine candidate objects and decided whether a candidate fulfils the query or not. The are used as ‘geometric filter’

Page 37: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Efficiency of SearchEfficiency of Search11

Page 38: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Efficiency of SearchEfficiency of Search11

Page 39: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Network QueriesNetwork Queries

In order to execute network queries, we In order to execute network queries, we need to augment the spatial information need to augment the spatial information used in the OGC Simple Features for SQL used in the OGC Simple Features for SQL standard. We will use pgRoutingstandard. We will use pgRouting11..

Page 40: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Network QueriesNetwork Queries11

Shortest Path from Dublin to Waterford

Page 41: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Viewing available Network Queries Viewing available Network Queries using pgAdmin IIIusing pgAdmin III

Page 42: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Network QueriesNetwork Queries

-- The start node 381 is in Dublin-- The destination is node 660 in Waterford-- Must return columns 'id', 'source', 'target' and 'cost'SELECT * FROM shortest_path('SELECT gid as id,source::integer,target::integer,length::double precision as costFROM all_roads_e',381, 660, false, false);

Page 43: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Network QueriesNetwork Queries

The edges are stored in all_roads_eThe source and target information was created in OpenJump using Tools | Analysis | Planar Graph.Details will be described in labs:

----------+------------------+-------------------- gid | integer | not null default nextval('egclass) id | bigint source | bigint target | bigint the_geom | geometry length | double precision

Page 44: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

OpenJumpOpenJump11

OpenJump GIS can act as a client for OpenJump GIS can act as a client for PostgreSQL/PostGIS. SQL queries can be PostgreSQL/PostGIS. SQL queries can be issued and view directly from OpenJump. issued and view directly from OpenJump. If the data is required in the database for If the data is required in the database for later use, then the user must explicitly later use, then the user must explicitly create a table from the result set. In short create a table from the result set. In short PostgreSQL/PostGIS queries bring a layer PostgreSQL/PostGIS queries bring a layer into OpenJump, the do into OpenJump, the do notnot update update PostgreSQL/PostGIS. PostgreSQL/PostGIS.

Page 45: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Using OpenJump with Using OpenJump with PostgreSQL/PostGISPostgreSQL/PostGIS

To add a PostGIS table to OpenJump select To add a PostGIS table to OpenJump select Layer | Layer | Run Datastore Query Run Datastore Query The first time you do this you will have to The first time you do this you will have to

establish a connection with establish a connection with PostgreSQL/PostGIS.PostgreSQL/PostGIS.

In the Run Datastore Query Window click on the In the Run Datastore Query Window click on the icon on the top right (2 small disks)icon on the top right (2 small disks)

This will open the Connection ManagerThis will open the Connection Manager Click AddClick Add

Page 46: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Using OpenJump with Using OpenJump with PostgreSQL/PostGISPostgreSQL/PostGIS

To make a new connection click here

Page 47: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Using OpenJump with Using OpenJump with PostgreSQL/PostGISPostgreSQL/PostGIS

Page 48: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Using OpenJump with Using OpenJump with PostgreSQL/PostGISPostgreSQL/PostGIS

To run a PostGIS spatial To run a PostGIS spatial queryquery in in OpenJump select OpenJump select

Layer | Run Datastore QueryLayer | Run Datastore Query

Page 49: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Irish Road WorkIrish Road Work

We will use a simplified version of the Irish We will use a simplified version of the Irish Road Network (IRN), consisting of national Road Network (IRN), consisting of national primary roads augmented with motorways primary roads augmented with motorways and some dual carriage –ways.and some dual carriage –ways.

Instructions on how to construct topology Instructions on how to construct topology were re given in Labs.were re given in Labs.

We used pgRouting to compute the We used pgRouting to compute the shortest path according to Dijkstra's shortest path according to Dijkstra's algorithm. algorithm.

Page 50: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

How to construct a networkHow to construct a network

Start with a single road table that consists of the Start with a single road table that consists of the Irish primary roads.Irish primary roads.

Calculate the length for each road segment and Calculate the length for each road segment and store it in a newly created store it in a newly created lengthlength column. column.

Generate the topology using PostgreSQLGenerate the topology using PostgreSQL1 1 or or OpenJump.OpenJump.

We can now apply various network queries such We can now apply various network queries such as shortest path.as shortest path.

These steps were covered in a lab.These steps were covered in a lab.

Page 51: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Sligo to CorkSligo to Cork

Page 52: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Length of all roads sections fully Length of all roads sections fully contained incontained in Dublin County Dublin County

Borough.Borough. SELECT  SELECT  

  Sum(ST_Length(r.the_geom))/1000   Sum(ST_Length(r.the_geom))/1000     AS kilometers      AS kilometers  FROM  FROM    all_roads r,    all_roads r,   dublin_regions dr  dublin_regions dr WHERE  WHERE    ST_Contains(dr.the_geom, r.the_geom)   ST_Contains(dr.the_geom, r.the_geom) AND AND    dr.name = ‘Dublin County Borough';    dr.name = ‘Dublin County Borough';

Page 53: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

What two houses within 500 meters of the What two houses within 500 meters of the Chester Beatty Library have the most Chester Beatty Library have the most

residents?residents? SELECT b1.residentsSELECT b1.residents FROM buildings b1, buildings b2FROM buildings b1, buildings b2 WHERE WHERE b2.name = 'CHESTER BEATTY LIBRARY' b2.name = 'CHESTER BEATTY LIBRARY'

andand ST_DWithin(b2.the_geom,b1.the_geom, ST_DWithin(b2.the_geom,b1.the_geom,

500) ORDER BY residents DESC LIMIT 2;500) ORDER BY residents DESC LIMIT 2;

Page 54: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Latitude of most northern and most Latitude of most northern and most southern buildings.southern buildings.

select st_y(st_transform(the_geom,4326)) select st_y(st_transform(the_geom,4326)) as latitude from buildings order by latitude as latitude from buildings order by latitude desc limit 1;desc limit 1;

select st_y(st_transform(the_geom,4326)) select st_y(st_transform(the_geom,4326)) as latitude from buildings order by latitude as latitude from buildings order by latitude asc limit 1;asc limit 1;

select st_y(the_geom) as easting from select st_y(the_geom) as easting from buildings order by easting desc limit 1;buildings order by easting desc limit 1;

Page 55: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Counties that have exactly 1 neighbour

SELECT c1.name FROM all_counties c1, all_counties c2 WHERE touches(c1.the_geom,

c2.the_geom) = 'TRUE' GROUP BY c1.name HAVING count(c2.name) =1;

Page 56: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Proportions

SELECT saps_label,primary_degree10_4,(male1_1 + female1_1) SELECT saps_label,primary_degree10_4,(male1_1 + female1_1) as Pop,as Pop,

((cast (primary_degree10_4 as float)) / (cast ((male1_1 + ((cast (primary_degree10_4 as float)) / (cast ((male1_1 + female1_1) as float)) * 100) as Grad_percentfemale1_1) as float)) * 100) as Grad_percent

FROM dublin_edsFROM dublin_edsWHERE primary_degree10_4 IS NOT NULLWHERE primary_degree10_4 IS NOT NULLORDER BY Grad_percentORDER BY Grad_percent DESC LIMIT 1;DESC LIMIT 1; saps_label | primary_degree10_4 | pop | grad_percentsaps_label | primary_degree10_4 | pop | grad_percent----------------------+--------------------+------+----------------------------------------+--------------------+------+------------------ 117 Mansion House A | 279 | 3802 | 7.33824302998422117 Mansion House A | 279 | 3802 | 7.33824302998422

Page 57: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

AccuracyAccuracy

You should calculate the area of county You should calculate the area of county Meath and compare the result to the area Meath and compare the result to the area stored in the stored in the all_counties all_counties table, you table, you should have get two different figures for should have get two different figures for the area of Meath. Which area is correct? the area of Meath. Which area is correct? To answer this question we would need to To answer this question we would need to know the accuracy of the area stored in know the accuracy of the area stored in the database and the accuracy the map the database and the accuracy the map that we used to calculate the area.that we used to calculate the area.

Page 58: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

AccuracyAccuracy

Obviously no spatial database or GIS can Obviously no spatial database or GIS can increase the positional accuracy of a increase the positional accuracy of a spatial dataset. The accuracy does not spatial dataset. The accuracy does not change as the viewing scale changes.change as the viewing scale changes.

So we need to know the scale of the So we need to know the scale of the original survey and the expected accuracy original survey and the expected accuracy at at thatthat scale. scale.

Page 59: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

AccuracyAccuracy

The accuracy of a The accuracy of a mapmap is dependent on is dependent on the differences between the the differences between the true positiontrue position of features and their of features and their representative representative position in the mapposition in the map. .

To find the To find the truetrue position requires highly position requires highly accurate devices such as industrial grade accurate devices such as industrial grade Global Positioning Systems (GPS) and Global Positioning Systems (GPS) and sophisticated mathematical software.sophisticated mathematical software.

Page 60: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

AccuracyAccuracy

A common of way of defining positional A common of way of defining positional accuracy for maps is to place limits on the root accuracy for maps is to place limits on the root mean square error (RMSE) for individual mean square error (RMSE) for individual position components (the X,Y and possibly Z i.e. position components (the X,Y and possibly Z i.e. height)height)

The RMSE is derived from the square root of the The RMSE is derived from the square root of the average of the squared discrepancies when average of the squared discrepancies when compared to a higher level independent survey. compared to a higher level independent survey. The RMSE is normally defined in terms of The RMSE is normally defined in terms of ground scale errors (e.g. +- one metre).ground scale errors (e.g. +- one metre).

Page 61: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

A Rough Guide to AccuracyA Rough Guide to Accuracy

ScaleScale Expected RMSEExpected RMSE

1:10001:1000 RMSE < 0.5 metresRMSE < 0.5 metres

1:25001:2500 0.5 < RMSE < 20.5 < RMSE < 2

1:10,0001:10,000 4 < RMSE < 54 < RMSE < 5

1:250,000 1:250,000 100 < RMSE < 120100 < RMSE < 120

Page 62: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Accuracy Accuracy

RMSE can be viewed as RMSE can be viewed as oneone criteria for criteria for positional accuracy. It is measured with respect positional accuracy. It is measured with respect to some more precise truth (often GPS). RMSE to some more precise truth (often GPS). RMSE is a measure of is a measure of absolute accuracyabsolute accuracy, with respect , with respect to a more precise framework.to a more precise framework.

Together with an acceptable RMSE a map Together with an acceptable RMSE a map must be consistent within itself. That is the must be consistent within itself. That is the various components must various components must fit togetherfit together. This is a . This is a measure of measure of relative accuracyrelative accuracy..

Page 63: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Accuracy Accuracy

If data has a nominal scale of say 1:250,000, it still may be more precise than the rough guidelines on slide 57 would indicate.

For example it could be derived from more accurate maps e.g. 1:10,560, 1:50,000, or 1:126,720.

Without meta-data it is difficult for humans or computers to know a map’s accuracy.

Page 64: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Accuracy Accuracy

The following table shows the expected The following table shows the expected absolute and relative accuracy values for absolute and relative accuracy values for well defined points within each accuracy well defined points within each accuracy category. The relative values apply up to category. The relative values apply up to the stated maximum measured distances the stated maximum measured distances quoted in the table.quoted in the table.

Page 65: Spatial Databases: Lecture 4 DT211-4 DT228-4 Semester 2 2009-10 Pat Browne 20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

Accuracy Accuracy

From The British Ordnance Survey (OSGB)

http://www.ordnancesurvey.co.uk/oswebsite/products/ossitemap/techinfo.html