Chap 4 - Spatial - DBMS - Exercise - PostGIS
Chap 4 - Spatial - DBMS - Exercise - PostGIS
POSTGIS
Spatial RDBMS
1 HOW TO START POSTGIS
Navigate to the PostgreSQL 9.1 menu item in Start menu and run PgAdmin III.
Double click on the “PostgreSQL Database Server” tree entry. You will be
prompted for the super user password to connect to the “template1” database.
Navigate to SpatialDB Schemas public Views. You can see four views which
are created by PostGIS.
3 LOAD DATA IN POSTGIS DATABASE
After installing the PostGIS 2.0 + will install Shp2pgsql-gui ESRI Shapefile and DBF
Loader Graphical User Interface in pgAdmin III
Step 1: open PgAdmin III and your Plug-in menu should look like:
Step 2: If you don't have a database selected, the menu options will be disabled, but once
you have a database selected, then you should be able to click the loader icon and get a
screen like this.
Step 3: Click on “View Connection details…”. Get a screen like this. Take the default
connection parameter or change connection parameter according to requirement. Click OK
Step 3: Click Add File. Browse the shapefile from desired location. Put 32643 in SRID. The
SRID (http://www.beginningspatial.com/common_srids ) denote projection of Shapefile.
Step 4: Just click on button Import to load the shapefile in SpatialDB database.
Step 5: Navigate to SpatialDB Schemas public Tables. Now you can see imported
Shapefile.
4 VIEWING DATA IN POSTGIS
There are a number of open source options for desktop viewers / editors of PostGIS data:
QGIS, a C++ / Qt program;
uDig, a Java / Eclipse program; and,
gvSIG, a Java / Swing program.
Step 1: To view the data in uDig fire up the uDig application and select: ->Map,
Step 2: Select PostGIS Data Source from given different data sources.
Step 3: Enter the connection information into the dialog box and click on Next.
Step 4: Select the database from Database List Box and click on List button which will
display the all table in selected database.
Step 5: Select the table from given Table name. And Click Next. Again Click Next
Once you have selected the layers you want to view, they will be added to the map, and you
can zoom, pan, edit, do table views, styling, and all the other desktop functionality you
generally get in a GIS viewer.
5 SPATIAL ANALYSIS IN SQL
A surprising number of traditional GIS analysis questions can be answered using a spatial
database and SQL. GIS analysis is generally about filtering spatial objects with conditions,
and summarizing the results – and that is exactly what databases are very good at. GIS
analysis also tests interactions between spatially similar features, and uses the interactions
to answer questions – with spatial indexes and spatial functions, databases can do that too!
Query 1:
“What is the total length of all roads in the Himachal Pradesh, in kilometers?”
SELECT Sum (ST_Length (geom))/1000 AS km_roads
FROM road_network;
km_roads
------------------
3395.32154976064
(1 row)
Query 2:
”How large is the Kullu Tehsil, in hectares?”
hectares
------------------
363987.756444798
(1 row)
Query 3:
”What is the largest District in the State, by area?”
District_n | hectares
--------------------------------
"Lahaul&Spiti" 1384465.95943495
(1 row)
The last one is particularly tricky. There are several ways to do it, including a two step
process that finds the maximum area, and then finds the municipality that has that area.
The suggested way uses the PostgreSQL “LIMIT” statement and a reverse ordering to pull
just the top area.
Query 4:
“What is the perimeter of the Shimla District?”
perimeter
------------------
521720.778361923
(1 row))
Query 5:
”What is the total area of Himachal Pradesh in hectares?”
hectares
------------------
5571989.74501868
(1 row)
Query 6:
“What is the total area of all Districts with more than 10, 00,000person in them?”
hectares
------------------
571570.96084352
(1 row)
Query 7:
”What is the length in kilometers of all roads where description of road is not known?”
kilometers
------------------
78.5513505360546
(1 row)
Query 8:
“What is the extent of CHAMBA Tehsil”?
SELECT ST_Extent(geom)
FROM TEHSIL
WHERE tehsil_nam = 'CHAMBA';
st_extent
------------------
"BOX(593828.3601 3577520.2759,633979.4296 3620236.5801)"
(1 row)
Query 9:
Counts the number of invalid geometries in the Tehsil table.
SELECT gid
FROM TEHSIL
WHERE NOT ST_IsValid(geom);
gid
------------------
(0 row)
Query 10:
Find out where the Dal Lake is.
SELECT ST_AsText(geom)
FROM lakes
WHERE lake_name='Dal'
st_extent
------------------
"POINT(622897.662884603 3568927.38474516)"
(1 row)
Query 11:
Give the name of lakes in Chamba District.
SELECT DISTRICT.district_n,lakes.lake_name
FROM DISTRICT,lakes
WHERE ST_Contains(DISTRICT.geom,lakes.geom)
AND district_n='Chamba';
district_n lake_name
------------------
"Chamba";"ManiMahesh"
"Chamba";"GauriKund Lake"
"Chamba";"Khajjiar"
"Chamba";"Gadasaru"
"Chamba";"Mahakali"
"Chamba";"Lam Dal Lake"
"Chamba";"KhundiMaral"
(7 row)
Query 12:
What is spatial reference ID of State Himachal Pradesh?
st_srid
------------------
32643
(1 row)
ST_AREA()
ST_Area() returns the area of a polygon or multipolygon.
ST_LENGTH()
ST_Length() returns the length of an ST_LineString or ST_MultiLineString.
ST_PERIMETER()
ST_Perimeter() returns the perimeter of a polygon or multipolygon.
ST_EXTENT()
ST_EXTENT() returns the extent of shape.
ST_ASTEXT()
ST_AsText() takes an ST_Geometry object and returns its well-known text representation.
ST_CONTAINS()
ST_Contains() takes two geometry objects and returns t (TRUE) if the first object
completely contains the second; otherwise, it returns f (FALSE).
ST_SRID()
ST_SRID() takes an ST_Geometry object and returns its spatial reference ID.