0% found this document useful (0 votes)
35 views17 pages

Chap 4 - Spatial - DBMS - Exercise - PostGIS

PostGIS allows spatial data to be stored and analyzed within a PostgreSQL database. This chapter discusses how to install and configure PostGIS, load spatial data from shapefiles, view the data in a desktop GIS application, and perform spatial analysis using SQL queries. A variety of examples demonstrate calculating areas and lengths, finding the largest or most populated regions, determining spatial relationships, and more. Spatial functions in PostGIS such as ST_Area(), ST_Length(), and ST_Contains() enable powerful analysis of location-based data within the database.

Uploaded by

Ankur Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views17 pages

Chap 4 - Spatial - DBMS - Exercise - PostGIS

PostGIS allows spatial data to be stored and analyzed within a PostgreSQL database. This chapter discusses how to install and configure PostGIS, load spatial data from shapefiles, view the data in a desktop GIS application, and perform spatial analysis using SQL queries. A variety of examples demonstrate calculating areas and lengths, finding the largest or most populated regions, determining spatial relationships, and more. Spatial functions in PostGIS such as ST_Area(), ST_Length(), and ST_Contains() enable powerful analysis of location-based data within the database.

Uploaded by

Ankur Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CHAPTER4: BEGINNING WITH

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.

 Now you are connected to server.


2 HOW TO CREATE DATABASE IN POSTGIS

2.1 HOW TO CREATE A POSTGIS ENABLED DATABASE IN GUI


 Navigate to the “Databases” section of the database tree and open “Edit New
Object New Database”.Add a new database
named “SpatialDB”, with “postgres” as the owner. “template_postgis_20” as the
template.

By using the “template_postgis_20” database as the template, you get a new


database with spatial capabilities already installed and enabled.
Note on table-spaces: PostgreSQL installs with two default table-spaces, pg_default
and pg_global. The pg_global table-space is used for system tables and other objects
that need cluster-wide visibility do not use it for your working databases, use
pg_default (which is what will be chosen if you do not specify a table-space
explicitly).

 Open up the new “SpatialDB” database.


 Navigate to SpatialDB Schemas public Tables and see what tables exist. You
should see “spatial_ref_sys” tables, which standard tables are created by PostGIS.

 Navigate to SpatialDB Schemas public Views. You can see four views which
are created by PostGIS.
3 LOAD DATA IN POSTGIS DATABASE

3.1 LOAD DATA (SHAPEFILES) IN POSTGIS USING THE POSTGIS SHP2PGSQL


GRAPHICAL LOADER

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.

But for this exercise we will use uDig.

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!

5.1 BASIC EXERCISES


These exercises will be easier if you first peruse the data dictionary and functions list for
information about the data columns and available functions. You can enter these exercises
in order or for a challenge, cover up your page with another piece of paper and try to figure
out the answer yourself before looking at the SQL.

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?”

SELECT ST_Area (geom)/10000 AS hectares


FROM TEHSIL
WHERE tehsil_nam = 'KULLU';

hectares
------------------
363987.756444798
(1 row)

Query 3:
”What is the largest District in the State, by area?”

SELECT District_n,ST_Area(geom)/10000 AS hectares


FROM DISTRICT
ORDER BY hectares DESC
LIMIT 1;

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?”

SELECT ST_Perimeter (geom) AS Perimeter


FROM DISTRICT
WHERE District_n = 'Shimla';

perimeter
------------------
521720.778361923
(1 row))

Query 5:
”What is the total area of Himachal Pradesh in hectares?”

SELECT Sum (ST_Area (geom))/10000 AS hectares


FROM STATE;

hectares
------------------
5571989.74501868
(1 row)

Query 6:
“What is the total area of all Districts with more than 10, 00,000person in them?”

SELECT Sum (ST_Area (geom))/10000 AS hectares


FROM DISTRICT
WHERE total_popu>1000000;

hectares
------------------
571570.96084352
(1 row)

Query 7:
”What is the length in kilometers of all roads where description of road is not known?”

SELECT Sum (ST_Length(geom))/1000 AS kilometers


FROM Road_network
WHERE med_descri = 'Unknown';

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?

SELECT ST_SRID ("geom")


FROM STATE

st_srid
------------------
32643
(1 row)

Some of the SQL Function:

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.

You might also like