Introduction To Postgis: Ulanbek Turdukulov
Introduction To Postgis: Ulanbek Turdukulov
ULANBEK TURDUKULOV
Spatial Objects for PostgreSQL
LAN
Internet
Mapserver
uDig
OpenIMF
GeoServer
QGIS PostGIS Web
MapGuide Client
GRASS
uDig
ArcGIS
Who is using PostGIS?
Lots of people …
Windows Installer
PostgreSQL 8.2.4
PgAdmin III
Install as Service to allow automatic database start on boot
Demonstrate Installation …
2.1 – PostgreSQL Installation
2.1 – PostgreSQL Installation
(3 rows)
3.2 OGC Metadata Tables
GEOMETRY_COLUMNS
F_TABLE_CATALOG = ‘’
F_TABLE_SCHEMA = ‘public’
F_TABLE_NAME = ‘bc_roads’
F_GEOMETRY_COLUMN = ‘the_geom’
COORD_DIMENSION = 2
SRID = 3005
TYPE = ‘MULTILINESTRING’
3.2 OGC Metadata Tables
SPATIAL_REF_SYS
SRID = 3005
AUTH_NAME = ‘EPSG’
AUTH_SRID = 3005
SRTEXT = ‘PROJCS["NAD83 / BC Albers”, … ]’
PROJ4TEXT = ‘+proj=aea …’
3.3 – Loading Shape Files
Shape File
.shp = geometry
.dbf = attributes (string, number, date)
.shx = utility index
PostGIS/PostgreSQL Table
Columns can be geometry
Columns can be attributes
One Shape File = One PostGIS Table
3.3 – Loading Shape Files
notepad bc_pubs.sql
3.3.1 – Command Line Options
A && B = TRUE
A && B = FALSE
3.5.2 Using Spatial Indexes
A && B = TRUE
_ST_Intersects(A && B) = FALSE
ST_Intersects(A,B)
A && B AND _ST_Intersects(A,B)
3.5.2 Using Spatial Indexes
A && B
3.5.2 Using Spatial Indexes
A && B
3.5.2 Using Spatial Indexes
_ST_Intersects(A,B)
3.5.2 - Using Spatial Indexes
ST_Intersects(A, B)
3.7 - Spatial Analysis in SQL
ST_Contains(A, B)
ST_Within(B, A)
3.7 - Spatial Analysis in SQL
ST_Touches(A, B)
3.7 - Spatial Analysis in SQL
ST_Crosses(A, B)
3.7 - Spatial Analysis in SQL
ST_DWithin(A, B, D)
D
3.7 - Spatial Analysis in SQL
What is the total area of all landslides in kilometers (using Guass Kruger
projection?
SELECT
Sum( ST_Length( the_geom ) ) / 1000
AS km_roads
FROM bc_roads;
3.7 - Spatial Analysis in SQL
What is the total area of all voting areas with more than 100 voters in
them?
SELECT Sum(ST_Area(the_geom))/10000 AS hectares
FROM bc_voting_areas
WHERE vtotal > 100;
3.8 - Basic Exercises
Valid Invalid
SELECT gid
FROM bc_voting_areas
WHERE
NOT ST_IsValid(the_geom);
4.1 - Data Integrity
4897 is INVALID!
SELECT
ST_IsValid(ST_Buffer(the_geom, 0.0))
FROM bc_voting_areas
WHERE gid = 4897;
UPDATE bc_voting_areas
SET
the_geom = ST_Buffer(the_geom, 0.0)
WHERE gid = 4897;
4.2 - Distance Queries
ST_Intersects() = TRUE
ST_Intersection() =
4.4 - Overlays
Create a new table containing all voting areas that fall within
PRINCE GEORGE clipped to that municipality’s bounds …
4.4 - Overlays
4.4 - Overlays
• A quick check …
– Area of overlay should be same as area of
clipping polygon
• SELECT Sum(ST_Area(intersection_geom))
FROM pg_voting_areas;
• SELECT ST_Area(the_geom)
FROM bc_municipality
WHERE name = ‘PRINCE GEORGE’;
4.5 - Coordinate Projection
• PROJCS[“NAD83 / BC Albers",
GEOGCS["NAD83",
DATUM["North_American_Datum_1983",
SPHEROID["GRS 1980",6378137,298.257222101]],
PRIMEM["Greenwich",0],
UNIT["degree",0.01745329251994328],
AUTHORITY["EPSG","4269"]],
PROJECTION["Albers_Conic_Equal_Area"],
PARAMETER["latitude_of_center",45],
PARAMETER["longitude_of_center",-126],
PARAMETER["standard_parallel_1",50],
PARAMETER["standard_parallel_2",58.5],
PARAMETER["false_easting",1000000],
PARAMETER["false_northing",0],
UNIT["metre",1],
AUTHORITY["EPSG","3005"]]
4.5 - Coordinate Projection
MULTILINESTRING((
1004687.04355194 594291.053764096,
1004729.74799931 594258.821943696))
ST_Transform(the_geom)
MULTILINESTRING((
-125.9341 50.3640700000001,
-125.9335 50.36378))
4.6 - Advanced Exercises
What two pubs have the most Green Party supporters within 500
meters of them?
SELECT
p.name, p.city,
Sum(v.green) AS greens
FROM
bc_pubs p,
bc_voting_areas v
WHERE
ST_DWithin(v.the_geom, p.the_geom, 500)
GROUP BY p.name, p.city
ORDER BY greens DESC LIMIT 2;
4.6 - Advanced Exercises
http://localhost/postgis
c:\ms4w\apps\postgis\htdocs\template.html
5.1 Basic Configuration
LAYER
CONNECTIONTYPE postgis
NAME "bc_voting_areas”
CONNECTION "user=postgres
password=postgres dbname=postgis
host=localhost"
DATA "the_geom FROM bc_voting_areas"
STATUS ON
TYPE POLYGON
CLASS
COLOR 255 255 200
END
END
5.2 Filters and Expressions
• DATA statement
DATA "the_geom from (%sql%) as
foo using SRID=3005 using unique
gid"
• Template line
<textarea name="sql" rows=3
cols=50>[sql]</textarea>
• Only trick is choice of Mapserver LAYER
“TYPE”
– Arbitrary SQL can return any geometry type
5.5 - Very Dynamic SQL
Enter a SQL
query and see it
on the map
outlined in red
5.5 - Very Dynamic SQL
Ulanbek Turdukulov
turdukulov@itc.nl
PostGIS
http://www.postgis.org
Mapserver
http://mapserver.gis.umn.edu