Post on 24-Nov-2015
description
transcript
Introduction to uDig 2/13/05
Refractions Research 1
d d G SG SIntroduction to Introduction to PostGISPostGIS
Dr. Harish Karnatak, Scientist, NRSC, ISRO Hyderabad
Source of Contents: www.refractions.net
Spatial Objects for Spatial Objects for PostgreSQLPostgreSQL
Adds support for geographic objects to theP t SQL bj t l ti l d t bPostgreSQL object-relational database
PostgreSQL already has geometric typesbut native geometries are too limited for GISdata and analysis.
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 2
PostGIS Overview / HistoryPostGIS Overview / History
June 2001 Version 0.1 Spatial types tied to GiSTp yp
July 2001 Version 0.5 Tie-in to UMN MapServer Wider ability to visualize data
May 2007 Version 1.2 Full support for OpenGIS SF-SQL Performance increases
harish_k@nrsc.gov.in
Performance increases Analysis and transformation functions
- Latest: 15.2
Why PostGIS?Why PostGIS?
Because databases are better than files
Unified Storage, Management, Access SQL Everywhere
Transactional Integrity
harish_k@nrsc.gov.in
g y Multiple Users, Multiple Edits
Introduction to uDig 2/13/05
Refractions Research 3
Internet
PostGIS in the Spatial StackPostGIS in the Spatial Stack
LAN
WebClient
Internet
uDig
OpenIMFMapserver
GeoServer
MapGuide
uDig
QGIS
GRASS
A GIS
PostGIS
harish_k@nrsc.gov.in
ArcGIS
PostgreSQLPostgreSQL Installation Installation
Windows Installer PostgreSQL 8.2.4 PgAdmin III
Install as Service to allow automatic database start on boot
harish_k@nrsc.gov.in
Demonstrate Installation
Introduction to uDig 2/13/05
Refractions Research 4
PostgreSQLPostgreSQL InstallationInstallation
harish_k@nrsc.gov.in
PostgreSQLPostgreSQL InstallationInstallation
Directories created during installation: \bin - Executables \include - Include files for compilation \lib - DLL shared library files \share - Extensions
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 5
2.1 2.1 PostgreSQL InstallationPostgreSQL Installation
Tools included with the install: PgAdmin III psql Command Line
harish_k@nrsc.gov.in
PostGISPostGIS InstallationInstallation
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 6
PostGISPostGIS InstallationInstallation
A PostgreSQL instance has one software version and one network port (5432)one network port (5432)
An instance contains many databases Connection string specifies a database
-h host U user d database p password
A database contains many schemas public
A h t i t bl
harish_k@nrsc.gov.in
A schema contains many tables public.geometry_columns
A table contains many tuples
Spatially Spatially Enable Enable PostgreSQLPostgreSQL
Connect to the instance User postgres, password postgres
Create a new database in our local instance Select template_postgis as the template
Connect to the database
harish_k@nrsc.gov.in
Check for the spatial system tables spatial_ref_sys geometry_columns
Introduction to uDig 2/13/05
Refractions Research 7
2.3 2.3 Spatially Enable PostgreSQLSpatially Enable PostgreSQL
harish_k@nrsc.gov.in
Without Without template_postgistemplate_postgis
Create a new database Select template1 as the template
Connect to the database Load/run the PostGIS extension
(lwpostgis.sql)
harish_k@nrsc.gov.in
Load/run the PostGIS spatial reference systems (spatial_ref_sys.sql)
Introduction to uDig 2/13/05
Refractions Research 8
2.3.1 2.3.1 Without template_postgis Without template_postgis
Run PG Admin III
harish_k@nrsc.gov.in
UNIX UNIX Installation NoteInstallation Note
PostGIS requires PL/PgSQLcreatelang plpgsql d postgis U postgrespsql -f lwpostgis.sql d postgis U
postgrespsql -f spatial_ref_sys.sql d postgis U
harish_k@nrsc.gov.in
postgres
Introduction to uDig 2/13/05
Refractions Research 9
Simple Simple Spatial SQLSpatial SQL
Manually create geometries
create table points (pt geometry, name varchar);insert into points values ('POINT(0 0)', 'Origin');insert into points values ('POINT(5 0)', 'X Axis');insert into points values ('POINT(0 5)', 'Y Axis');
harish_k@nrsc.gov.in
p ( ( ) )
select name, ST_AsText(pt), ST_Distance(pt, 'POINT(5 5)') from points;
Simple Simple Spatial SQLSpatial SQL
| | name | astext | distance --------- + -------------- + -----
-------------
Origin | POINT(0 0) | 7.071067 X Axis | POINT(5 0) | 5
harish_k@nrsc.gov.in
Y Axis | POINT(0 5) | 5
(3 rows)
Introduction to uDig 2/13/05
Refractions Research 10
OGC OGC Metadata TablesMetadata Tables
GEOMETRY_COLUMNS F_TABLE_CATALOG = F_TABLE_SCHEMA = public F_TABLE_NAME = bc_roads F_GEOMETRY_COLUMN = the_geom
COORD DIMENSION = 2
harish_k@nrsc.gov.in
COORD_DIMENSION = 2 SRID = 3005 TYPE = MULTILINESTRING
OGC OGC Metadata TablesMetadata Tables
SPATIAL_REF_SYS SRID = 3005 AUTH_NAME = EPSG AUTH_SRID = 3005 SRTEXT = PROJCS["NAD83 / BC Albers, ]
PROJ4TEXT = +proj=aea
harish_k@nrsc.gov.in
PROJ4TEXT = +proj=aea
Introduction to uDig 2/13/05
Refractions Research 11
Loading Loading Shape FilesShape Files
Shape File (Misnomer! 3+ Files!) .shp = geometry .dbf = attributes (string, number, date) .shx = utility index
PostGIS/PostgreSQL TableCol mns can be geometr
harish_k@nrsc.gov.in
Columns can be geometry Columns can be attributes
One Shape File = One PostGIS Table
Loading Loading Shape FilesShape Files
Run cmd.exe
harish_k@nrsc.gov.in
pg_setenv.bat
Introduction to uDig 2/13/05
Refractions Research 12
Loading Loading Shape FilesShape Files
shp2pgsql [opts] shapefile tablename shp2pgsql i s 3005 D bc_pubs.shp bc_pubs > bc_pubs.sql
Read in .shp file Write out sql file
harish_k@nrsc.gov.in
Write out .sql file Load .sql file into PostgreSQL
using psql using PgAdmin
Loading Loading Shape Files Shape Files shp2pgsqlshp2pgsql
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 13
Loading Loading Shape FilesShape Files
shp2pgsql -s 3005 bc pubs.shp shp2pgsql s 3005 bc_pubs.shp public.bc_pubs > bc_pubs.sql
harish_k@nrsc.gov.in
Loading Loading Shape FilesShape Files
notepad bc_pubs.sql
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 14
Command Command Line OptionsLine Options
-D = Use dump format-i = Do not use bigint, even for long
numbers-s = Use this SRID-W = Use this character
di
harish_k@nrsc.gov.in
encoding-a = Run in append mode
Loading Loading Shape FilesShape Files
pg_shpsql.bat
shp2pgsql -i -D -s 3005 bc_hospitals.shp bc_hospitals > bc_data.sql
shp2pgsql -i -D -s 3005 bc_municipality.shp bc_municipality >> bc_data.sql
shp2pgsql -i -D -s 3005 bc_voting_areas.shp bc voting areas >> bc data sql
harish_k@nrsc.gov.in
bc_voting_areas >> bc_data.sql shp2pgsql -i -D -s 3005 bc_pubs.shp bc_pubs
>> bc_data.sql shp2pgsql -i -D -s 3005 bc_roads.shp
bc_roads >> bc_data.sql
Introduction to uDig 2/13/05
Refractions Research 15
Loading Loading Shape FilesShape Files
psql d postgis U postgres f bc_data.sql
harish_k@nrsc.gov.in
Viewing Viewing Data in Data in PostGISPostGIS
Quick desktop viewing options uDig QGIS gvSIG CadCorp SIS* FME Viewer*
W b b d li ti ti
harish_k@nrsc.gov.in
Web based application options MapGuide Mapserver Geoserver
Introduction to uDig 2/13/05
Refractions Research 16
Viewing Viewing Data in Data in PostGISPostGIS
Program Files uDig 1.1-RC11 uDig
harish_k@nrsc.gov.in
Viewing Viewing Data in Data in PostGISPostGIS
File New Layer
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 17
Viewing Viewing Data in Data in PostGISPostGIS
harish_k@nrsc.gov.in
Viewing Viewing Data in Data in PostGISPostGIS
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 18
3.4 Viewing Data in PostGIS3.4 Viewing Data in PostGIS
harish_k@nrsc.gov.in
Creating Creating Spatial IndexesSpatial Indexes
- PostGIS implements R-Tree indexes on top of the GiST indexing system
- Organizes data into nesting rectangles for fast searching
harish_k@nrsc.gov.in
CREATE INDEX bc_roads_gidx ON bc_roads USING GIST (the_geom);
(now create indexes on the other tables too)
Introduction to uDig 2/13/05
Refractions Research 19
Using Using Spatial IndexesSpatial Indexes
- Indexes are brought into play when PostgreSQL recognizes an operator in the SQL statement. For example:
- SELECT * FROM mytable WHERE myname = Paul
i t
harish_k@nrsc.gov.in
= is an operator
- SELECT * FROM mytable WHERE age < 2< is an operator
Using Using Spatial IndexesSpatial Indexes
Spatial index operator is && Bounding Boxes Interact
harish_k@nrsc.gov.in
A && B = TRUEA && B = FALSE
Introduction to uDig 2/13/05
Refractions Research 20
Using Using Spatial IndexesSpatial Indexes
Bounding Boxes are not enough!
A && B = TRUE_ST_Intersects(A && B) = FALSE
harish_k@nrsc.gov.in
Two pass system required Use bounding boxes to reduce candidates Use real topological test to get final answer
Using Using Spatial IndexesSpatial Indexes
harish_k@nrsc.gov.in
ST_Intersects(A,B)A && B AND _ST_Intersects(A,B)
Introduction to uDig 2/13/05
Refractions Research 21
Using Using Spatial IndexesSpatial Indexes
harish_k@nrsc.gov.in
A && B
Using Using Spatial IndexesSpatial Indexes
harish_k@nrsc.gov.in
A && B
Introduction to uDig 2/13/05
Refractions Research 22
Using Using Spatial IndexesSpatial Indexes
harish_k@nrsc.gov.in
_ST_Intersects(A,B)
Using Using Spatial IndexesSpatial Indexes
Index operations (&&) are built into common f ti f t ti b t functions for automatic use, but you can use them separately if you like. ST_Intersects(G1,G2)
G1 && G2 AND _ST_Intersects(G1,G2)
ST_Contains(G1,G2) ST Within(G1 G2)
harish_k@nrsc.gov.in
ST_Within(G1,G2) ST_Touches(G1,G2) ST_DWithin(G1,G2,D)
G1 && ST_Expand(G2,D) AND ST_Distance(G1,G2) > D
Introduction to uDig 2/13/05
Refractions Research 23
Spatial Spatial Index TestIndex Test
Run query with un-indexed function SELECT gid, name FROM bc_roads
WHERE _ST_Crosses( the_geom, ST_GeomFromText(.., 3005) );
Run query with indexed function SELECT gid, name FROM bc_roads
harish_k@nrsc.gov.in
WHERE ST_Crosses( the_geom, ST_GeomFromText(.., 3005) );
Note speed difference
Indexes Indexes and Query Plansand Query Plans
Run queries using the Explain b tt i t d f th R button instead of the Run button
Note the graphical display of how the database is using indexes
Click on the icons to get more information b t h t f th
harish_k@nrsc.gov.in
about each step of the query Remember to try this later when we start
performing more complicated queries
Introduction to uDig 2/13/05
Refractions Research 24
Indexes Indexes and Query Plansand Query Plans
harish_k@nrsc.gov.in
Indexes Indexes and Query Plansand Query Plans
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 25
When When Query Plans go BadQuery Plans go Bad
The database builds plans based on t ti ti b t d t di t ib ti l d statistics about data distribution sampled
from the tables Always tries to be selective, to pull the fewest
number of records necessary to move on to the next step
The database builds bad plans when it has
harish_k@nrsc.gov.in
The database builds bad plans when it has bad statistics
Make sure your database has up-to-date statistics by running ANALYZE
PostgreSQLPostgreSQL OptimizationOptimization
PostgreSQL settings are controlled by th tg l f filthe postgresql.conf file
Programs => PostgreSQL 8.2 => Configuration Files => Edit postgresql.conf
harish_k@nrsc.gov.in
Some settings require a restart Some can be changed at run-time using
the SET command!
Introduction to uDig 2/13/05
Refractions Research 26
PostgreSQLPostgreSQL OptimizationOptimization
PostgreSQL ships with conservative tti gsettings
Uses very little memory
Runs on very limited hardware
Disk access is slow, so higher performance can be gained by using
harish_k@nrsc.gov.in
performance can be gained by using more memory to cache data! Increase shared_buffers Physical RAM - OS needs * 75%
PostgreSQLPostgreSQL OptimizationOptimization
Sorting is faster in memoryI Increase work_mem
Disk clean-up is faster with more memory Increase maintenance_work_mem
Allocated per connection
Also
harish_k@nrsc.gov.in
Also Increase wal_buffers Increase checkpoint_segments Decrease random_page_cost
Introduction to uDig 2/13/05
Refractions Research 27
Spatial Spatial Analysis in SQLAnalysis in SQL
Get out the data dictionary, flip to the back:
ST_Length(geometry) returns Float
ST_Area(geometry) returns Float
ST AsText(geometry) returns Text
harish_k@nrsc.gov.in
ST_AsText(geometry) returns Text
ST_GeomFromText(text) returns Geometry
Spatial Spatial Analysis in SQLAnalysis in SQL
ST_Intersects(A, B)( , )
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 28
Spatial Spatial Analysis in SQLAnalysis in SQL
ST_Contains(A, B) ST_Within(B, A)
harish_k@nrsc.gov.in
Spatial Spatial Analysis in SQLAnalysis in SQL
ST_Touches(A, B)( , )
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 29
Spatial Spatial Analysis in SQLAnalysis in SQL
ST_Crosses(A, B)( , )
harish_k@nrsc.gov.in
Spatial Spatial Analysis in SQLAnalysis in SQL
ST_DWithin(A, B, D)( , , )
D
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 30
Spatial Spatial Analysis in SQLAnalysis in SQL
What is the total length of all roads in the province of BC, in kilometers?
SELECTSum( ST_Length( the_geom ) ) / 1000
AS km_roads FROM bc roads;
harish_k@nrsc.gov.in
FROM bc_roads;
Spatial Spatial Analysis in SQLAnalysis in SQL
How large is the city of g yPRINCE GEORGE, in hectares?
SELECTST_Area(the_geom)/10000
AS hectares
harish_k@nrsc.gov.in
FROM bc_municipality WHEREname = PRINCE GEORGE;
Introduction to uDig 2/13/05
Refractions Research 31
Spatial Spatial Analysis in SQLAnalysis in SQL
What is the largest municipality in the i b ?province, by area?
SELECTname, ST_Area(the_geom)/10000
AS hectares
harish_k@nrsc.gov.in
FROM bc_municipality ORDER BY hectares DESCLIMIT 1;
Basic Basic ExercisesExercises
What is the perimeter of the municipality of VANCOUVER?
Hint name = VANCOUVER SELECT ST_Perimeter(the_geom)
FROM bc_municipality WHERE name 'VANCOUVER';
harish_k@nrsc.gov.in
WHERE name = VANCOUVER ;
Introduction to uDig 2/13/05
Refractions Research 32
Basic Basic ExercisesExercises
What is the total area of all voting areas in hectares?
Hint 1 hectare = 10,000 m2
SELECTSum(ST_Area(the_geom))/10000 AS hectares
harish_k@nrsc.gov.in
AS hectares FROM bc_voting_areas;
Basic Basic ExercisesExercises
What is the total area of all voting areas with more than 100 voters in them?
SELECTSum(ST_Area(the_geom))/10000 AShectares
harish_k@nrsc.gov.in
hectares FROM bc_voting_areas WHERE vtotal > 100;
Introduction to uDig 2/13/05
Refractions Research 33
Basic Basic ExercisesExercises
What is the length in kilometers of all roads named Douglas St?
SELECTSum(ST_Length(the_geom))/1000 ASkilometers FROM bc roads
harish_k@nrsc.gov.in
FROM bc_roads WHERE name = 'Douglas St';
Data Data IntegrityIntegrity
Spatial data algorithms embed t ti f t t expectations of structure
PostGIS expects data to conform to the OGC Simple Features for SQL standard Polygon rings dont cross other rings
or self-intersectM l i l l i
harish_k@nrsc.gov.in
Multi-polygons are non-overlapping Interior rings can touch exteriors, but only
at one point Ring orientation is unimportant
Introduction to uDig 2/13/05
Refractions Research 34
Data Data IntegrityIntegrity
Valid Invalid
harish_k@nrsc.gov.in
SELECT gid FROM bc_voting_areas WHERENOT ST_IsValid(the_geom);
Data Data IntegrityIntegrity
4897 is INVALID! SELECT
ST_IsValid(ST_Buffer(the_geom, 0.0))FROM bc_voting_areas WHERE gid = 4897;UPDATE bc voting areas
harish_k@nrsc.gov.in
UPDATE bc_voting_areas SETthe_geom = ST_Buffer(the_geom, 0.0)
WHERE gid = 4897;
Introduction to uDig 2/13/05
Refractions Research 35
Distance Distance QueriesQueries
How many BC Unity Party supporters live within 2 kilometers of the Tabor Arms pub?
SELECT ST_AsText(the_geom)FROM bc_pubs WHERE name ILIKE Tabor Arms%;
harish_k@nrsc.gov.in
WHERE name ILIKE Tabor Arms% ;
Distance Distance QueriesQueries
SELECT Sum(unity) AS unity_voters bFROM bc_voting_areas
WHEREST_Distance(
the_geom, ST_GeomFromText(POINT(), 3005),
) < 2000;
harish_k@nrsc.gov.in
) < 2000;
Introduction to uDig 2/13/05
Refractions Research 36
Distance Distance QueriesQueries
SELECT Sum(unity) AS unity_voters bFROM bc_voting_areas
WHEREST_DWithin(
the_geom, ST_GeomFromText(POINT(), 3005),2000
harish_k@nrsc.gov.in
2000);
Distance Distance QueriesQueries
ST_DWithin(geometry, geometry, float) SELECT
$1 && ST_Expand($2, $3) AND$2 && ST_Expand($1, $3) ANDST_Distance($1, $2) < $3
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 37
Distance Distance Not BufferNot Buffer
What pubs fall within a 1km buffer of the d ?roads?
You do not need to buffer to answer this question, because the return value is not a buffered road geometry, it is the pub.
SELECT p.name FROM b b b d
harish_k@nrsc.gov.in
FROM bc_pubs p, bc_roads rWHEREST_DWithin(p.the_geom, r.the_geom, 1000)
Distance Distance Not BufferNot Buffer
This is the WRONG way to do it: SELECT p.name
FROM bc_pubs p, bc_roads rWHEREST_Contains(
ST Buffer(r the geom 1000)
harish_k@nrsc.gov.in
ST_Buffer(r.the_geom, 1000),p.the_geom
);
Introduction to uDig 2/13/05
Refractions Research 38
Spatial Spatial JoinsJoins
Standard joins use a common keyStandard joins use a common key SELECT a.var1, b.var2
FROM a, b WHERE a.id = b.id
Spatial joins use the universal key, location
harish_k@nrsc.gov.in
location SELECT a.var1, b.var2
FROM a, b WHERE ST_Intersects(a.geom, b.geom)
Spatial Spatial JoinsJoins
Tabor Arms Pub distance query as joinTabor Arms Pub distance query as join SELECT Sum(v.unity) AS unity_voters
FROMbc_voting_areas v,bc_pubs p
WHERE
harish_k@nrsc.gov.in
ST_DWithin(v.the_geom, p.the_geom, 2000)ANDp.name ILIKE 'Tabor Arms%';
Introduction to uDig 2/13/05
Refractions Research 39
Spatial Spatial JoinsJoins
Where are all of the pubs located within 250 t f h it l?meters of a hospital?
SELECT bc_hospitals.name, bc_pubs.name FROMbc_hospitals, bc_pubs
WHERE
harish_k@nrsc.gov.in
ST_DWithin(bc_hospitals.the_geom, bc_pubs.the_geom, 250
);
Spatial Spatial JoinsJoins
Summarize the 2000 provincial election by municipality
SELECTm.name, Sum(v.ndp) AS ndp Sum(v.vtotal) AS total
FROMbc_voting_areas v,
harish_k@nrsc.gov.in
g ,bc_municipality m
WHERE ST_Intersects(v.the_geom, m.the_geom)GROUP BY m.nameORDER BY m.name;
Introduction to uDig 2/13/05
Refractions Research 40
OverlaysOverlays
Table-on-table overlays are possible ith th ST I t ti () f tiwith the ST_Intersection() function
ST_Intersects(a,b) returns BOOLEAN ST_Intersection(a,b) returns GEOMETRY
ST I t t () TRUE
harish_k@nrsc.gov.in
ST_Intersects() = TRUE
ST_Intersection() =
OverlaysOverlays
Create a new table containing all ti g th t f ll ithi voting areas that fall within
PRINCE GEORGE clipped to that municipalitys bounds
harish_k@nrsc.gov.in
Introduction to uDig 2/13/05
Refractions Research 41
OverlaysOverlays
harish_k@nrsc.gov.in
4.4 4.4 -- OverlaysOverlays CREATE TABLE pg_voting_areas ASSELECTST_Intersection(v.the_geom, m.the_geom)AS intersection_geom,
ST_Area(v.the_geom) AS va_area, v.*, m.name
FROMbc_voting_areas v, bc municipality m
harish_k@nrsc.gov.in
bc_municipality m WHEREST_Intersects(v.the_geom, m.the_geom) ANDm.name = PRINCE GEORGE;
Introduction to uDig 2/13/05
Refractions Research 42
A quick check
OverlaysOverlays
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)
harish_k@nrsc.gov.in
_ ( _g )FROM bc_municipality WHERE name = PRINCE GEORGE;
Coordinate Coordinate ProjectionProjection
View the SRID of geometries using the ST_SRID() function
SELECT ST_SRID(the_geom)FROM bc_roads LIMIT 1;
Whats 3005?
harish_k@nrsc.gov.in
SELECT srtext FROM spatial_ref_sys WHERE srid = 3005;
Ah, its BC Albers
Introduction to uDig 2/13/05
Refractions Research 43
Coordinate Coordinate ProjectionProjection
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]
harish_k@nrsc.gov.in
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"]]
Coordinate Coordinate ProjectionProjection
Whats 3005 again? SELECT proj4text
FROM spatial_ref_sys WHERE srid = 3005;
+proj=aea +ellps=GRS80 +datum=NAD83 +lat_0=45.0 +lon_0=-126.0
harish_k@nrsc.gov.in
+lat_1=50.0 +lat_2=58.5 +x_0=1000000 +y_0=0
PROJ4 is coordinate re-projection library used by PostGIS
Introduction to uDig 2/13/05
Refractions Research 44
Coordinate Coordinate ProjectionProjection
Coordinate Re-projection is done using the p j gST_Transform() function
SELECT ST_AsText(the_geom) FROM bc_roads LIMIT 1;
SELECT
harish_k@nrsc.gov.in
ST_AsText(ST_Transform(the_geom, 4326) )
FROM bc_roads LIMIT 1;
Coordinate Coordinate ProjectionProjection
MULTILINESTRING((MULTILINESTRING((1004687.04355194 594291.053764096,1004729.74799931 594258.821943696))
ST_Transform(the_geom)
harish_k@nrsc.gov.in
MULTILINESTRING((-125.9341 50.3640700000001,
-125.9335 50.36378))
Introduction to uDig 2/13/05
Refractions Research 45
Advanced Advanced ExercisesExercises
What is the length in kilometers of Douglas St in VICTORIA?Douglas St in VICTORIA ?
SELECTSum(ST_Length(r.the_geom))/1000AS kilometers
FROMbc_roads r, b i i lit
harish_k@nrsc.gov.in
bc_municipality m WHEREST_Contains(m.the_geom, r.the_geom) ANDr.name = 'Douglas St' ANDm.name = 'VICTORIA';
Advanced Advanced ExercisesExercises
What two pubs have the most Green Party supporters within 500 meters of them?supporters within 500 meters of them?
SELECTp.name, p.city, Sum(v.green) AS greens
FROMbc_pubs p, b ti
harish_k@nrsc.gov.in
bc_voting_areas v WHEREST_DWithin(v.the_geom, p.the_geom, 500)
GROUP BY p.name, p.city ORDER BY greens DESC LIMIT 2;
Introduction to uDig 2/13/05
Refractions Research 46
Advanced Advanced ExercisesExercises
What is the latitude of the most southerly hospital in the province?
Hint The SRID of lat/lon is 4326 SELECT
ST_Y(ST_Transform(the_geom,4326))AS latitude
harish_k@nrsc.gov.in
AS latitude FROM bc_hospitals ORDER BY latitude ASCLIMIT 1;
Advanced Advanced ExercisesExercises
What were the percentage NDP and Liberal vote within the city limits of Prince George in vote within the city limits of Prince George in the 2000 provincial election?
SELECT100*Sum(v.ndp)/Sum(v.vtotal) AS ndp, 100*Sum(v.liberal)/Sum(v.vtotal) AS liberal
FROMb ti
harish_k@nrsc.gov.in
bc_voting_areas v, bc_municipality m
WHEREST_Contains(m.the_geom, v.the_geom) ANDm.name = 'PRINCE GEORGE';
Introduction to uDig 2/13/05
Refractions Research 47
Advanced Advanced ExercisesExercises
What is the largest voting area polygon that has a hole?has a hole?
Hint A hole implies more than one ring SELECT
gid, id, ST_Area(the_geom) AS area
harish_k@nrsc.gov.in
( )FROM bc_voting_areas WHERE ST_NRings(the_geom) > 1 ORDER BY area DESCLIMIT 1;
Advanced Advanced ExercisesExercises
How many NDP voters live within 50 meters of Simcoe St in Victoria?Simcoe St in Victoria?
SELECTSum(v.ndp) AS ndp
FROMbc_voting_areas v, bc_municipality m, bc_roads r
WHERE
harish_k@nrsc.gov.in
WHEREST_DWithin(r.the_geom, v.the_geom, 50) ANDST_Contains(m.the_geom, r.the_geom) ANDr.name = 'Simcoe St' ANDm.name = 'VICTORIA';
Introduction to uDig 2/13/05
Refractions Research 48
Thank YouThank You
For any further information you may contact me- harish_k@nrsc.gov.in
harish_k@nrsc.gov.inAcknowledgement: Paul Ramsey