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:
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:
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:
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:
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...":
In the "Import vector layer" windows, select the world-rivers input and fill the rest of the form just like the image below:
You will see an "Import was successful" message. The table with the geometry field (and index) will be created and imported to PostgreSQL:
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:
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
Hello, I loaded world-rivers to the geoserver and published the Layer. I also changed in the map.js typeName
but the http://localhost:8080/geoserver/wfs response is empty and the map is empty, for sure i forget something to misconfigure. Thanks!
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!