Different types of vector geometry with lines and polygons, loading shape files to PostGIS with QGIS

A free screencast (video) course is available for this post but you need to be signed in order to view it, you can sign in here if you already have an account or register here if you don't have one.

So far, we only worked with points vectors, the simplest geometry type. In terms of vectors (we will cover rasters in future posts), there are three main types of geometry: points, lines, and polygons.

The point is like the atomic unit of vector data; all vector geometry types are collections of points. A line is composed of a collection of points in a specific order and a polygon the same, except that the first point must be the same as the last point, so it has a closed area. The following image shows an example:

Loading shape files in PostGIS with QGIS There is only one geometry per record in the same table in most GIS formats, and the geometry type must be homogeneous. It is to say, the monuments table in PostGIS has a geometry column of type point, so no monument will be allowed to be a line or a polygon. It is a limitation, but it dramatically helps for spatial indices. There are also two variations on points, lines, and polygons; they can be 3D (each point has a third coordinate for altitude or elevation). They can also be "multi" a multipolygon is composed of many polygons but still attached to only one record. A real-world example of this would be a road that is cut at some point and starts back a little farther; it's the same road but composed of two lines. Finally, there are some special geometries like polygons with holes. We will go with 99% of the cases and use 2D points, lines, polygons, and their "multi" variations.

We will load some data about the world countries' borders (multipolygons) and world rivers (multilines); this data comes from opendatasoft for the administrative boundaries and UNESCO for the world rivers. I downloaded the shapefile version for both of these layers.

The shapefile is a pretty old format; it is composed of at least three files, a .shp file which contains the geometries of each feature, the .dbf (which is an old dBase format) that contains the attributes for each geometry, and finally a .shx file which includes the spatial index. Even if it's old and quite limited, the shapefile is still widely used as an exchange format in the GIS world.

So I now have two shapefiles (committed in resources/geodata/shape); how can I open and/or convert them to PostGIS so we can share them with Geoserver, display them with OpenLayers, edit them in Laravel? First point, the shapefile is the most common type of geospatial file. Therefore any GIS application can open it, including Geoserver. There are a few good desktop GIS applications, ArcGIS, in the commercial world, for example, but we are fortunate to have QGIS in the open-source world. If you haven't done it yet, go and install it, it's available on Linux, Windows, and macOS.

We will first use QGIS to display our downloaded data, inspect it, and then export it directly to our PostGIS database:

  • Once QGIS is opened, on the left panel (Browser), navigate the project and the shapefiles, right-click on each one, and click "Add Layer to Project." You should see the layers on the map with random colors:

Loading shape files in PostGIS with QGIS

QGIS also supports direct connections to PostGIS and WFS; let's show our PostGIS monuments data on the map first. We need to create a new PostgreSQL connection to our database: right-click on PostgreSQL in the left panel (Browser) and click "New connection...". Fill the fields with the same database connection information we have in the .env file:

Loading shape files in PostGIS with QGIS

Now, we can browse into the connection; in the public schema, we can see the monuments table and add it to the map just like we did with the shapefiles:

Loading shape files in PostGIS with QGIS

Great, we can now mix data sources in a QGIS map. Now we can import the shapefiles in PostGIS by going to the menu "Database" and select "DB Manager". In the DB Manager window, navigate to the connection we created and select the public schema. Finally, click on "Import Layer/File...":

Loading shape files in PostGIS with QGIS

In the "Import vector layer" windows, select the world-rivers input and fill the rest of the form just like the image below:

Loading shape files in PostGIS with QGIS

You will see an "Import was successful" message. The table with the geometry field (and index) will be created and imported to PostgreSQL:

Loading shape files in PostGIS with QGIS

Repeat the same process with the "world-administrative-boundaries" shapefile. Once we have our layers in PostGIS, we can share it in Geoserver just like we did for the monuments table:

Loading shape files in PostGIS with QGIS

In the previous image, we can notice with the icon in front of the names of the layers that we have a point layer (laravelgis:monuments), a polygon layer (laravelgis:world_administrative_boundaries), and a line layer (laravelgis:world_rivers).

In the next post, we will style and load the new layers to the OpenLayers map in WFS. We will see more WFS specificities and understand its limitations in terms of performance.

The commit for this post is available here: loading-shape-files-in-postgis-with-qgis

First published 2 years ago
Latest update 1 year ago
fedot
Posted by fedot 1 year ago

Hello, I loaded world-rivers to the geoserver and published the Layer. I also changed in the map.js typeName

const paramsObj = { servive: "WFS", version: "2.0.0", request: "GetFeature", typeName: "laravelgis:world-rivers", outputFormat: "application/json", crs: "EPSG:4326", srsName: "EPSG:4326", };

but the http://localhost:8080/geoserver/wfs response is empty and the map is empty, for sure i forget something to misconfigure. Thanks!


webgisdev
Posted by webgisdev 1 year ago

Hello fedot,

Sorry for the late reply, I just published a new post that might help you. I think your problem was linked to the styleFunction expecting a name attribute not present in the world-rivers layer.

Cheers!

Arjun Kishore
Posted by Arjun Kishore 1 year ago

Hi,

Thanks for this valuable tutorial. Wondering if there is anyway to create shapefile from geojson format Are there any GDAL php composer package exist for the conversion. Will really appreciate it, if you could point me in right direction.

Cheers


webgisdev
Posted by webgisdev 1 year ago

Hello Arjun,

The easiest way is to use QGIS, add the geojson file to your project, then right-click on the layer, Export, Save feature as, select ESRI Shapefile as the format, and fill in the rest of the options. Otherwise, if you have GDAL installed, you can do it from the command line with the ogr2org command like this:

ogr2ogr -nlt POINT -skipfailures -F "ESRI Shapefile" monuments.shp monuments.geojson

Finally, gasparesganga/php-shapefile is a good PHP package supporting reading and writing to shape files; you can find the documentation here: PHP Shapefile.

I hope it helps!

Cheers!

You need to be signed in to post comments, you can sign in here if you already have an account or register here if you don't.