This tutorial will explain to the user how to connect QGIS to your PostgreSQL/PostGIS database and spatially view any of your tables containing a geometry column. PostGIS is a powerful database for GIS functions and data storage so it is nice to have a program to visually see the GIS data stored within the database. QGIS is an easy to use open source GIS application and is ideal for data viewing, editing, and making maps.
You are required to have a PostgreSQL database with PostGIS installed. This database will need a GIS table (contains a geometry type column). Start bydownloading a sample shapefile here and follow the next few steps to import the shapefile to PostgreSQL using QGIS.
Importing Shapefile Data to PostGIS using QGIS
Open Quantum GIS and click the “Import Shapefiles to PostgreSQL"
button. This will open SPIT (Shapefile to PostGIS Import Tool).
![QGIS Import Shapefiles to PostgreSQL QGIS Import Shapefiles to PostgreSQL](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_importshapefiletopostgresql.png)
Start by creating a connection to your PostgreSQL database. Under “PostgreSQL Connections” click the “New” button. Add a name for your connection, and then enter the parameters for your database connection (Host, Database, Port, Username, and Password). Click the “Test Connection” button and you should see a message saying “Connection to databasename was successful”. Once this is successful, click OK.
![QGIS PostGIS Connection QGIS PostGIS Connection](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_postgisconnetion.png)
Select your server from the dropdown list and click “Connect”, a list of your schemas will appear in the Global schema dropdown menu. Choose the schema where you would like to import the data. Now click the “Add” button and add your shapefile. Ensure the information is correct concerning the shapefile and click “OK”.
![QGIS SPIT (Shapefile to PostGIS Import Tool) QGIS SPIT (Shapefile to PostGIS Import Tool)](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_spit.png)
Your shapefile is now added to your database. Next, we will view the newly added PostGIS data.
Viewing PostGIS Layers using QGIS
Click the “Add PostGIS Layer” button
. This will open up a window.
![QGIS Add PostGIS Layer QGIS Add PostGIS Layer](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_addpostgis.png)
Under “PostgreSQL Connections” select your server name from the dropdown list and click “Connect”. You will see a list of your PostgreSQL schemas. Open the schema containing your GIS data to see a list of all available layers. Select the n_america layer click “Add”.
![QGIS Add PostGIS Table QGIS Add PostGIS Table](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_addpostgistable.png)
The GIS data for North America will appear in the data viewing area. Use the “Zoom In” tool
to draw a box closely around the US, Canada, and Mexico.
![QGIS Zoom In QGIS Zoom In](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_zoomin.png)
![QGIS Layer View QGIS Layer View](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_dataview.png)
To view the attribute table for this layer, right-click the layer and choose “Open Attribute Table” or click the icon
when the layer is selected.
![QGIS Open Attribute Table QGIS Open Attribute Table](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_openattributetable.png)
To edit your data, turn on the editor by clicking the “Toggle Editing” icon
. Save your edits using the “Save Edits” button
.
![QGIS Toggle Editing QGIS Toggle Editing](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_toggleediting.png)
![QGIS Save Edits QGIS Save Edits](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_saveedits.png)
A simple way to identify features or query the attributes of a location is to use the “Identify Features” tool
. Click a location on the map to view its attributes.
![QGIS Identify Features QGIS Identify Features](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_identifyfeatures.png)
Classifying your Data
Right-click the n_america layer and choose “Properties”. Under Symbology -> Legend Type, choose “Unique Value”. Under “Classification field”, choose “country” and click Classify. Each unique value in the country column will be given a random color. Click “OK”.
![QGIS Symbology QGIS Symbology](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_symbology.png)
Now let’s add some labels. Select the n_america layer and click the “Labeling” button
. Put a check next to “Label this layer”. Under “Field with labels”, choose “country” and click “OK”.
![QGIS Labeling QGIS Labeling](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_labeling.png)
![QGIS Map QGIS Map](http://www.gistutor.com/images/stories/gistutor/qgis/qgis_map.png)
This concludes our tutorial on working with PostGIS data with QGIS. The user should now know how to import shapefile data into a PostGIS database, load that layer in QGIS, view/edit the attributes, and symbolize the map according to its values.
0 commentaires