Tugas Basis Data Spasial

Embed Size (px)

Citation preview

  • 8/10/2019 Tugas Basis Data Spasial

    1/35

    Applied Spatial Database

    Preparation of Spatial Database in PostgreSQL

    Import Shapefile into Spatial Databases

    Spatial Data Query

    Result's Visualization in Quantum GIS

    created by :

    Rd Achmad Faizal P S

    (11/319127/TK/38258)

    Department of Geodetic Engineering

    Faculty of Engineering

    Gadjah Mada University

  • 8/10/2019 Tugas Basis Data Spasial

    2/35

    I. Objective

    - Students are understand the concept of spatial database

    - Students can create a spatial database by importing shapefiles into spatial data tables

    that have an element of data geometry

    - Students can convert WKB into WKT

    - Students can create a query related to the Data Definition Language (DDL)

    - Student can work with spatial query

    - Students can displaying the results on Quantum GIS

    II. Equipment Needed

    - Laptop hardware

    -

    PostgreSQL 9.3 software installed (with PostGIS 2.0 extension)- Quantum GIS software 2.4 installed

    - Notepad ++ (optional)

    - Shapefile Dataset (data source = http://planning.hawaii.gov/gis/ ) :

    1. haw_centerlines.shp (polyline)2. hawcntrs100.shp (polyline)3. pubschool.shp (point)4. skilled_nursing.shp (point)5. tsunevac.shp (polygon)

    III. Practice1. 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 PostgreSQL s GUI.

    - By SQL Query:

    On SQL editor (execute arbitrary SQL queries),

    write down an SQL s script like this:

    create database databases name ;

    then execute it by clicking ( ) icon.

    http://planning.hawaii.gov/gis/http://planning.hawaii.gov/gis/http://planning.hawaii.gov/gis/http://planning.hawaii.gov/gis/
  • 8/10/2019 Tugas Basis Data Spasial

    3/35

    - 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 onspatial database. To make it become a database that supporting spatial data, we have tointegrating it with PostGIS extension. There are two ways:

    - By right click on extension belong to working database (bds_faizal) New Extension,then choose postgis on the names field, then click OK .

  • 8/10/2019 Tugas Basis Data Spasial

    4/35

    - 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

    Then, execute it.

    Do the same thing in the same directory location for spatial_ref_sys.sql.C:\ProgramFiles\PostgreSQL\9.3\share\contrib\postgis-2.1\spatial_ref_sys.sql

  • 8/10/2019 Tugas Basis Data Spasial

    5/35

    We can open that sql file (postgis.sql and spatial_ref_sys.sql) by notepad++, thencopy/paste it on SQL editor, its more efficiently I thought since we copying those file toour familiar directory.The reason why I explain the second ways (By open query file manually) above is tounderstand 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 databaseconcept (look at picture below).

    - Geometry_columns provide columns that able to addgeometry data type. This is the display ofgeometry_columns (showed using SQL script):

    select * from geometry_columns ;

    *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 asingle column.

    - Geography_columns has similar function asgeometry_columns . This is the display ofGeography_columns (showed using SQL script):

    select * from geography_columns ;

    What is best to use (between geometry_columns and geography_columns)?http://gis.stackexchange.com/questions/26082/what-is-the-difference-between-geometric-and-

    geographic-columns :"When choosing between the geometry and geography type for data storage, you should consider whatyoull be using it for. If all you do are simple measurements and relationship checks on your data, andyour data covers a fairly large area, then most likely youll be better o ff storing your data using the newgeography type. Although the new geography data type can cover the globe, the geometry type is farfrom obsolete. The geometry type has a much richer set of functions than geography, relationshipchecks are generally faster, and it has wider support currently across desktop and web-mapping tools."

    http://gis.stackexchange.com/questions/26082/what-is-the-difference-between-geometric-and-geographic-columnshttp://gis.stackexchange.com/questions/26082/what-is-the-difference-between-geometric-and-geographic-columnshttp://gis.stackexchange.com/questions/26082/what-is-the-difference-between-geometric-and-geographic-columnshttp://gis.stackexchange.com/questions/26082/what-is-the-difference-between-geometric-and-geographic-columnshttp://gis.stackexchange.com/questions/26082/what-is-the-difference-between-geometric-and-geographic-columns
  • 8/10/2019 Tugas Basis Data Spasial

    6/35

    - Spatial_ref_sys is PostGIS include and OGC compliant database table that lists over 3000known spatial reference systems and details needed to transform/project betweenthem. 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 aren tautomatically 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 PostGIS1. 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 clickopen .

  • 8/10/2019 Tugas Basis Data Spasial

    7/35

    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 attheir .prj file)

    5. Then click Import

  • 8/10/2019 Tugas Basis Data Spasial

    8/35

    - By QuantumGIS1. Open QuantumGIS Desktop

    2. Click on Databases Spit Import Shapefiles to PostgreSQL

    3. There will be spit dialog box , click new Then, there will be new entries display, determine name, host, port, databases,username, password (these are related with our postgreSQL configuration). Testconnection by click Test connect until there is a successful message. Then Ok.

    4. Add shapefile that will be converted by click Add, and choose them.

  • 8/10/2019 Tugas Basis Data Spasial

    9/35

    5. After that, determine the SRID of the shapefile.On QGIS, we can only choose one reference system in one PostGIS connections, so makesure that all converted shapefile have the same coordinate system.

    6. Choose connect and enter the postgreSQL password, then click ok . Shapefiles willbe imported to spatial databases automatically.

    These are the result of converting shapefile to databases on PostgreSQL. There will be newtable 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 ViewAll Rows.

  • 8/10/2019 Tugas Basis Data Spasial

    10/35

    We are also possible to displaying databases integrating/connected with PostGIS on QuantumGIS byusing DB Manager. There are three mod in there (info, table, preview)

    And displaying it visually by using layers add postgis layers or ctrl +shift+D . Make sure that QGIS andPostGIS databases are connected by choose connect and entry PoistgreSQL password.

    2. Info Mod 1. Table Mod

    3. Preview Mod

  • 8/10/2019 Tugas Basis Data Spasial

    11/35

    3. Case and Resolve- Case :

    Do queries to show :1) Data Modification or the application of SQL as DDL2) Query application for :

    a. Converting WKB to WKTb. Showing the data typec. Showing the geometric coordinate valued. Finding the closest distance between two pointe. Choosing Features with specific attributes (displayed on QGIS canvas)f. Presenting the overlay between databases tables with raster (displayed on

    QGIS canvas)3) Implementing existing spatial databases for proximity analyst and overlay analyst

    using SQL syntax, then display it on QGIS canvas!

    - 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 databaseschemas. 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 TablesThese are the explanation of DDL by SQL by using SQL editor on PostgreSQL,practically:

    - Add Columns :SQL command:ALTER TABLEtable name ADD COLUMNcolumn name data_type;

    In this case, I will add ownership field on skilled_nursing table. The SQL syntax willbe like this:

  • 8/10/2019 Tugas Basis Data Spasial

    12/35

    So, there will be a new column named ownership like picture below:

    - Remove Columns SQL command:ALTER TABLEtable_name DROP COLUMNcolumn name ;

    In this case, I will delete some of unused column from skilled_nursing table. The SQLsyntax will be like this:

  • 8/10/2019 Tugas Basis Data Spasial

    13/35

    So, this is skilled_nursing table after execution, some of selected columns areremoved:

    - Add Constraint: SQL constraints are used to specify rules for the data in a table. The specify rules areNOT NULL, UNIQUE , PRIMARY KEY , FOREIGN KEY , CHECKSQL command will be depended on specify rules :ALTER TABLEtable_name ADD [specify rules];

    In this case, Ill set set phone_no as Unique from skilled_nursing table. The SQL syntaxwill 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 TABLEtable_name DROP [specify rules];

  • 8/10/2019 Tugas Basis Data Spasial

    14/35

    In this case, I will remove the constraint that has been given before to phone_no field asa UNIQUE value. The SQL syntax will be like this:

    After execution, the phone_no column is no longer set as unique constraint, so it ispossible to be duplicated in the same field.

    - Changing a Column's Data TypeSQL command:ALTER TABLEtable_name ALTER COLUMNcolumn name TYPE data type;

    In this case, I will change ownership data type become varchar(15) . The SQL syntax willbe like this:

    So, the data type of ownership column will be set become varchar(15) , look at picturebelow:

  • 8/10/2019 Tugas Basis Data Spasial

    15/35

    - Change Default Values SQL command:ALTER TABLEtable_name ALTER COLUMNcolumn name SET DEFAULTdefaultvalue ;

    In this case, I will set the default values of ownership as government . The SQL syntaxwill be like this:

    So, the ownership column will be added government as default value. This doesn'taffect any existing rows in the table, it just changes the default for future INSERTcommands.

    - Rename ColumnSQL command:

    ALTER TABLETable_name RENAME COLUMNold column name TO new columnname ;

    In this case, I will rename contact column become contact_name column. The SQLsyntax will be like this:

    So, if we see the picture below, contact field name will be change to contact_name .

  • 8/10/2019 Tugas Basis Data Spasial

    16/35

    - Rename TableSQL command:ALTER TABLEtable_name RENAME TOnew 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 andtheir value will not change.

  • 8/10/2019 Tugas Basis Data Spasial

    17/35

    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 vectorgeometry objects on a map, spatial reference systems of spatial objects andtransformations between spatial reference systems. A binary equivalent, known

    as well-known binary (WKB) is used to transfer and store the same informationon databases, such as PostGIS, Microsoft SQL Server and DB2. The formats wereoriginally defined by the Open Geospatial Consortium (OGC) and described intheir Simple Feature Access and Coordinate Transformation Servicespecifications.These are the ways to convert WKB to WKT format by using SQL query:This is the basic SQL function related with WKB and WKT1. 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 3 rd and 4 th function for conversation betweenWKB 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 thatgeotable table has been made):

  • 8/10/2019 Tugas Basis Data Spasial

    18/35

    b. Showing the data typeWe are already knew that the data type of geometry for example above isPOINT, by looking at its statement.

    These are another WKT form, for LINESTRING and POLYGON

    c. Showing the geometric coordinate valuesGeometric coordinate values are showed after data type. For the exampleabove, we can directly know the geometric coordinates .

    Defined As POINT

    2. LINESTRING 1. POLYGON

    Defined As POINTsgeometric Coordinates

  • 8/10/2019 Tugas Basis Data Spasial

    19/35

    For advance and interoperability purposes , we can export those table into otherASCII format such as (.CSV), so the geometric coordinate are more easy to bedefined. These are following steps to export it:

    - On query window, select File Export

    - Do setting as needs, then choose the file directory and name to besaved

    This is the (.csv) format of table that has been exported, opened by notepad++ :

  • 8/10/2019 Tugas Basis Data Spasial

    20/35

    d. Finding the closest distance between two pointTo find the closest distance between two geospatial object (geometry datatype), we can use the function below:ST_Distance(geometry1,geometry2)

    For Example :

    The GEOMETRY type calculates meaningless Cartesian/planar distancebetween two geospatial object. If we use GEOGRAPHY type, so the distancewill be calculated by using great circle/orthodromic distance .

    In this case, I want to calculate the closet distance between two location that is2900 Pali Hwy hospital and Aliamanu Elementary School . So, the SQL querywill be like this:

    Base on the result of SQL queries, the closest distance between those locationsis 7672.74513788208 meters (planar distance).

    Comparing with GIS software (ArcGIS) distance calculate function :

  • 8/10/2019 Tugas Basis Data Spasial

    21/35

    e. Choosing Features with specific attributes (displayed on QGIS canvas)To answer this question, Ill use QGISs SQL user interface because the result willbe displayed directly on QGIS canvas. It means that we can do spatial query onQGIS with consequence that it must be connected with databases on PostGISbefore. These are the step to do queries on QGIS:

    1. Open QGIS2. If we never make an databases connection and importing PostGIS databases

    before, so we made it first (just like the answer no. 1 above about showingPostGIS databases on QGIS). In this situation, assumed that I used adatabases that have been made before as the answer of question no.1(tugas_bds databases ).

    3. Select Databases DB Manager DB

    4. It will be DB Manager windows, connecting it with PostGIS databases byclicking on refresh icon first, then choose PostGIS databases toconnected (tugas_bds)

  • 8/10/2019 Tugas Basis Data Spasial

    22/35

    5. Entries the PostgreSQLs username and password

    6. Then the databases will be connected, after that, click SQL window Icon tobegin spatial queries in QGIS .

  • 8/10/2019 Tugas Basis Data Spasial

    23/35

    7. Do queries to find specific attribute.In this case, I will do SQL query to find contours where the values (elevation)are located between 1000 until 8000 meter (msl). The SQL query will be likethis :

    Then select on Execute icon or F5

    8. So the table as result of that query will be like this:

    9. We can load it as a new layer, but before we load it, QGIS will ask for uniqueinteger value as a primary key and it must be integer numbers. We can usegid column for it, but it wouldnt be in sequence number because it willfollowing its original values from hawcntrs100 s table . As primary key thatdefine the unique value of its data, we have to make it unique and

    sequential.So, as a solu tion, Ill use SQL aggregate function (row_number) and it mustbe following by (over) .

  • 8/10/2019 Tugas Basis Data Spasial

    24/35

    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!

  • 8/10/2019 Tugas Basis Data Spasial

    25/35

    11. So, there will be new layer on QGIS canvas (see pictures below). This layerrepresented the querys result that displayed on QGIS directly.Hawcntrs100s contours have been filtered by query (wrote above) definedas elev , only showing the contours line s between 1000 and 8000 meter(msl). This is an example of SQL query implementation to find specifics

    attributes (elevation) that have been qualified.

    1. The visualization of elev layer (as queries result)

    2. Comparison with hawcntrs100 (before querying )

  • 8/10/2019 Tugas Basis Data Spasial

    26/35

    f. Presenting the overlay between databases t ables with raster (displayed onQGIS canvas) by using openLayer plugin.

    Quantum GIS provide many plugin to be installed and upgrade the QGISperformance to manage geospatial data. OpenLayer plugin is a QGIS plugin

    embedding OpenLayers functionality, by using this plugin, we can add rasterdata from servers or Web Map Service to be used directly on QGIS canvas and itdepended by inserted shapefiles and its covering coordinates.Web Map Service (WMS) is OGC standard that provides a simple HTTP interfacefor requesting geo-registered map images from one or more distributedgeospatial databases. A WMS request defines the geographic layer(s) and areaof interest to be processed.

    These are the following steps:1. Install OpenLayer Plugin

    - Open QGIS Plugin Manage and install plugins..

    - On plugins display windows, there will be many plugins provided bydevelopers. Searching for openLayer plugin, and install Plugin.

  • 8/10/2019 Tugas Basis Data Spasial

    27/35

    - After the installation, we can use OpenLayer plugin by select on webtoolbar then OpenLayer plugin and displaying map from web map serviceprovided

    2.

    2. Insert spatial databases into layer canvasIn this case, I will displaying the overlay between haw_centerlines databaseswith raster data provided by map service such as Bing Map, MapQuest,Google map, Open Street Map, and Apple map

    - On DB Managers SQL Windows. Write down the SQL syntax below, itwill minimalize the unused column, and load as new layer.

  • 8/10/2019 Tugas Basis Data Spasial

    28/35

    - So, it will be like this

    3. Then, add raster map layer from OpenLayer plugins.- Select on web toolbar Openlayer Plugins Then choose layer based onits provider (for example, bing map bing aerial).These are the example of overlaying between database table and rasterfrom many map service provider:

    From Bing Map (Bing Aerial)

  • 8/10/2019 Tugas Basis Data Spasial

    29/35

    From Google Map (Google Satellite)

    MapQuest ( MapQuest Open Aerial)

  • 8/10/2019 Tugas Basis Data Spasial

    30/35

    The best result is the raster map provided by Bing Map, the map resolution is stillgood and the rasters tiles are still provided well until high level of zooming.

  • 8/10/2019 Tugas Basis Data Spasial

    31/35

    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 therelationship between a selected spatial object and its neighbors.

    1. ST_BufferIn this case, Ill use ST_b uffer to make a buffer area with specified distance(100 meter) around the roads from haw_centerlines . The SQL syntax andthe result will be like this:

    The Buffer area (100 meter) around haw_centerlines

  • 8/10/2019 Tugas Basis Data Spasial

    32/35

    2. ST_DwithinIn this case, I ll use ST_DWithin to find the medical_center that are inwithin 100 meter around road (haw_centerline ). The SQL syntax and itsresults will be like this:

    The medical_center that are within 100 meter around haw_centerlines Italso can be proved by displaying 100 m buffer area.

  • 8/10/2019 Tugas Basis Data Spasial

    33/35

    - Overlay Analyst :Overlay Analyst is an analytical technique used to identifying the overlapbetween 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 belike this:

    Every pubschools inside tsunami evacuation zone, it also can be proved bydisplaying tusnevac as comparison

  • 8/10/2019 Tugas Basis Data Spasial

    34/35

    2. ST_IntersectsIn this case, I will identifying every road (haw_centerlines) and publicschools (pubschools) intersect with tsunami evacuation zone withMAPNUM = 1. The SQL syntax and its result will be like this:

    Every pubschools and haw_centerlines intersected with tsunavec with mapnum = 2 (yellow area)

  • 8/10/2019 Tugas Basis Data Spasial

    35/35

    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/

    http://postgis.net/docs/manual-2.1/http://postgis.net/docs/manual-2.1/http://workshops.boundlessgeo.com/postgis-intro/http://workshops.boundlessgeo.com/postgis-intro/http://www.postgresql.org/docs/8.4/static/http://www.postgresql.org/docs/8.4/static/http://gis.stackexchange.com/http://gis.stackexchange.com/http://gis.stackexchange.com/http://www.postgresql.org/docs/8.4/static/http://workshops.boundlessgeo.com/postgis-intro/http://postgis.net/docs/manual-2.1/