created by :
Rd Achmad Faizal P S
(11/319127/TK/38258)
I.
Objective
-
Students can create a spatial database by importing shapefiles into spatial data tables
that have an element of data geometry
II.
Students can create a query related to the Data Definition Language (DDL)
Equipment Needed
-
Laptop hardware
Notepad ++ (optional)
haw_centerlines.shp (polyline)
hawcntrs100.shp (polyline)
pubschool.shp (point)
skilled_nursing.shp (point)
tsunevac.shp (polygon)
III. Practice
1. Create an spatial databases :
The first thing we have to do is create a databases. It can be done in two ways, such as by using
SQL query manually or by using PostgreSQLs GUI.
-
By SQL Query:
On SQL editor (execute arbitrary SQL queries),
) icon.
By PostgreSQL GUI:
On Databases, right click New Databases, there will be an entries display, write the
name of databases on Names field (for example bds_faizal), then click OK.
The result is there will be a new database named bds_faizal, This database isnt yet based on
spatial database. To make it become a database that supporting spatial data, we have to
integrating it with PostGIS extension. There are two ways:
-
By open query file manually, on Query display (execute arbitrary SQL queries icon) file
open, then open file postgis.sql located on
C:\ProgramFiles\PostgreSQL\9.3\share\contrib\postgis-2.1\postgis.sql
We can open that sql file (postgis.sql and spatial_ref_sys.sql) by notepad++, then
copy/paste it on SQL editor, its more efficiently I thought since we copying those file to
our familiar directory.
The reason why I explain the second ways (By open query file manually) above is to
understand how the spatial databases extension works. PostGIS extension can
supporting spatial data because they provide geography_columns, geometry_columns
and spatial_ref_sys table as a spatial environment to work with spatial database
concept (look at picture below).
-
*There are the different between PostGIS 2.0 and the late version,
Geometry_columns replaces on table, but at 2.0, it replaces at views as a
single column.
Spatial_ref_sys is PostGIS include and OGC compliant database table that lists over 3000
known spatial reference systems and details needed to transform/project between
them. Spatial_ref_sys provide spatial referenced (SRID) that needed to convert shapefile
(.dbf) to spatial databases.
This is the display of spatial_ref_sys table (showed using SQL script):
select * from spatial_ref_sys;
3911
I suggested to use the first ways, because raster_columns and raster_overview arent
automatically added, so there are some function that cant be used.
2. Converting shapefile to spatial databases
To convert shapefile to databases, there two ways. It can be converted by using PostGIS
directly or by using QGIS. These are the following stape:
-
By PostGIS
1. Open PostGIS desktop (PostGIS 2.0 shapefile and DBF loader exporter)
2. On Import mod, click add file, Choose shapefile that will be converted, then click
open.
3. Click on view connection detils, determine the username, password, server host,
and databases that will be used
4. Determine SRID based on shapefile SRID that will be converted (we can see it at
their .prj file)
By QuantumGIS
1. Open QuantumGIS Desktop
4. Add shapefile that will be converted by click Add, and choose them.
6. Choose connect and enter the postgreSQL password, then click ok. Shapefiles will
be imported to spatial databases automatically.
These are the result of converting shapefile to databases on PostgreSQL. There will be new
table based on shapefile that weve been import. We can see the table by using SQL script
(select * from table name) or by right click on the selected table name View Data View
All Rows.
2.
Info Mod
1.
3.
Table Mod
Preview Mod
And displaying it visually by using layers add postgis layers or ctrl +shift+D. Make sure that QGIS and
PostGIS databases are connected by choose connect and entry PoistgreSQL password.
Resolve :
1) A data definition language or data description language (DDL) is a syntax similar to
a computer programming language for defining data structures, especially database
schemas. There are some kind of modification that can be implemented, such as:
- Add Columns
- Remove Columns
- Add Constraint
- Remove Constraint
- Change Default Values
- Change Columns Data Type
- Rename Columns
- Rename Tables
These are the explanation of DDL by SQL by using SQL editor on PostgreSQL,
practically:
Add Columns :
SQL command:
ALTER TABLE table name ADD COLUMN column name data_type;
In this case, I will add ownership field on skilled_nursing table. The SQL syntax will
be like this:
So, there will be a new column named ownership like picture below:
Remove Columns
SQL command:
ALTER TABLE table_name DROP COLUMN column name;
In this case, I will delete some of unused column from skilled_nursing table. The SQL
syntax will be like this:
So, this is skilled_nursing table after execution, some of selected columns are
removed:
- Add Constraint:
SQL constraints are used to specify rules for the data in a table. The specify rules are
NOT NULL, UNIQUE , PRIMARY KEY , FOREIGN KEY , CHECK
SQL command will be depended on specify rules :
ALTER TABLE table_name ADD [specify rules];
In this case, Ill set set phone_no as Unique from skilled_nursing table. The SQL syntax
will be like this:
So, the phone_no column will be unique and it cant be duplicate value in the same field.
- Remove Constraint:
SQL command will be depended on specify rules :
ALTER TABLE table_name DROP [specify rules];
In this case, I will remove the constraint that has been given before to phone_no field as
a UNIQUE value. The SQL syntax will be like this:
So, the data type of ownership column will be set become varchar(15), look at picture
below:
So, the ownership column will be added government as default value. This doesn't
affect any existing rows in the table, it just changes the default for future INSERT
commands.
-
Rename Column
SQL command:
ALTER TABLE Table_name RENAME COLUMN old column name TO new column
name;
In this case, I will rename contact column become contact_name column. The SQL
syntax will be like this:
So, if we see the picture below, contact field name will be change to contact_name.
Rename Table
SQL command:
ALTER TABLE table_name RENAME TO new table name;
In this case, I will rename skilled_nursing table to be medical_center table. The SQL
syntax will be like this :
So, the result is, skilled_nursing table will rename to medical_center table. Column and
their value will not change.
2) These are the application of query to resolve some cases such as:
a. Converting WKB to WKT
Well-known text (WKT) is a text markup language for representing vector
geometry objects on a map, spatial reference systems of spatial objects and
transformations between spatial reference systems. A binary equivalent, known
as well-known binary (WKB) is used to transfer and store the same information
on databases, such as PostGIS, Microsoft SQL Server and DB2. The formats were
originally defined by the Open Geospatial Consortium (OGC) and described in
their Simple Feature Access and Coordinate Transformation Service
specifications.
These are the ways to convert WKB to WKT format by using SQL query:
This is the basic SQL function related with WKB and WKT
1. bytea WKB = ST_AsBinary(geometry);
2. text WKT = ST_AsText(geometry);
3. geometry = ST_GeomFromWKB(bytea WKB, SRID);
4. geometry = ST_GeometryFromText(text WKT, SRID);
For a single value , we can use the 3rd and 4th function for conversation between
WKB and WKT.
For Example (from single value WKT to WKB):
But, in this case I want to convert all WKB geometry values from one table
(pubschools) into new table. The SQL query will be like this (assumed that
geotable table has been made):
b.
Defined As POINT
LINESTRING
1.
POLYGON
Defined As POINTs
geometric Coordinates
For advance and interoperability purposes , we can export those table into other
ASCII format such as (.CSV), so the geometric coordinate are more easy to be
defined. These are following steps to export it:
-
This is the (.csv) format of table that has been exported, opened by notepad++ :
Base on the result of SQL queries, the closest distance between those locations
is 7672.74513788208 meters (planar distance).
Comparing with GIS software (ArcGIS) distance calculate function :
6. Then the databases will be connected, after that, click SQL window Icon to
begin spatial queries in QGIS .
9. We can load it as a new layer, but before we load it, QGIS will ask for unique
integer value as a primary key and it must be integer numbers. We can use
gid column for it, but it wouldnt be in sequence number because it will
following its original values from hawcntrs100 s table. As primary key that
define the unique value of its data, we have to make it unique and
sequential.
So, as a solution, Ill use SQL aggregate function (row_number) and it must
be following by (over).
So the right SQL query and its answer will be like this:
10. To load it as a new layer, click on mark (load as new layer) below.
Entries for column with unique integer values = id and geometry column
= the_geom. Then click Load now!
11. So, there will be new layer on QGIS canvas (see pictures below). This layer
represented the querys result that displayed on QGIS directly.
Hawcntrs100s contours have been filtered by query (wrote above) defined
as elev, only showing the contours lines between 1000 and 8000 meter
(msl). This is an example of SQL query implementation to find specifics
attributes (elevation) that have been qualified.
1.
2.
f.
The best result is the raster map provided by Bing Map, the map resolution is still
good and the rasters tiles are still provided well until high level of zooming.
3) Implementing existing spatial databases for proximity analyst and overlay analyst
using SQL syntax, then display it on QGIS canvas!
- Proximity Analyst :
Proximity Analysis is an analytical technique used to determine the
relationship between a selected spatial object and its neighbors.
1. ST_Buffer
In this case, Ill use ST_buffer to make a buffer area with specified distance
(100 meter) around the roads from haw_centerlines. The SQL syntax and
the result will be like this:
2. ST_Dwithin
In this case, Ill use ST_DWithin to find the medical_center that are in
within 100 meter around road (haw_centerline). The SQL syntax and its
results will be like this:
Overlay Analyst :
Overlay Analyst is an analytical technique used to identifying the overlap
between two or more selected spatial object.
1. ST_Contains
In this case, I will find every public schools (pubschools) that are inside the
tsunami evacuation zone (tsunevac). The SQL syntax and the result will be
like this:
2. ST_Intersects
In this case, I will identifying every road (haw_centerlines) and public
schools (pubschools) intersect with tsunami evacuation zone with
MAPNUM = 1. The SQL syntax and its result will be like this:
Every pubschools and haw_centerlines intersected with tsunavec with mapnum = 2 (yellow area)
IV.
References
http://postgis.net/docs/manual-2.1/
http://workshops.boundlessgeo.com/postgis-intro/
http://www.postgresql.org/docs/8.4/static/
http://gis.stackexchange.com/