Please do not use this as is in a production environment!
There are some sample database users that are being created by the steps below
with passwords that are hardcoded in this repo!
This is a geopreview sample stack based on the one used in HDX
- docker - tested on version 20.10 , should work with >17.04
- docker-compose - tested on version 1.25.5 , should work with >1.11.2
NOTE: this was tested with Docker for Linux and Mac.
This will create a total of 3 containers.
- postgis - This is the container that runs the postgres database. The actual database files are mapped outside the container as explained further below.
- gisapi - This is the NodeJS application that actually serves that vector tiles / PBF. It's based on gisrestapi which in turn was forked from https://github.com/spatialdev/PGRestAPI and slightly modified.
- ogr2ogr - This is a container that has everything needed to run the ogr2ogr command line tool. The container's image was actually created to hold a python application that manages/runs the transformations so it contains additional software not actually needed for this example.
The gisapi server comes with a way of viewing the geodata from postgis as vector tile layers (described below). To use the vector tiles / PBF in a separate web application with Leaflet we used the following plugin: https://github.com/SpatialServer/Leaflet.MapboxVectorTile
-
Clone this git repo
git clone https://github.com/alexandru-m-g/vector-tiles-sample.git
and enter the newly created foldercd vector-tiles-sample
-
Start the docker stack
docker-compose up -d
-
Initialize the database, only needed the first time. A persistent folder called postgis was created in the current folder where the database files will be stored. The path for this folder is specified in .env
cat postgis/create.sql | docker-compose exec -T postgis psql -U postgres
Alternatively, you can run
docker-compose exec postgis psql -U postgres
and copy paste the lines from create.sqlNOTE: this creates a postgres user and database
-
Restart the gisapi container so that it can connect to the newly created postgis db, only needed the first time
docker-compose restart gisapi
-
Import sample shapefile and geojson file into postgis. The ogr2ogr/samples folder in this project is mapped inside the ogr2ogr container and already contains 2 samples: 1 (unzipped) shapefile and 1 geojson.
In order to run the examples below you will need to be inside the ogr2ogr container:
# get a shell inside the container docker-compose exec ogr2ogr /bin/sh
Example 1, shapefile:
# run the import from inside the container ogr2ogr --config PG_USE_COPY NO -f "PostgreSQL" "PG:host=postgis dbname=testdb port=5432 user=test password=test" /srv/samples/sample_adm3.shp -nln sample_adm3_shp -overwrite -lco OVERWRITE=YES -fieldTypeToString Real -t_srs EPSG:4326
Example 2, geojson:
# run the import from inside the container ogr2ogr --config PG_USE_COPY NO -f "PostgreSQL" "PG:host=postgis dbname=testdb port=5432 user=test password=test" /srv/samples/sample.geojson -nln sample_geojson -overwrite -lco OVERWRITE=YES -fieldTypeToString Real -t_srs EPSG:4326
Example 3, shapefile:
# run the import from inside the container ogr2ogr --config PG_USE_COPY NO -f "PostgreSQL" "PG:host=postgis dbname=testdb port=5432 user=test password=test" /srv/samples/sample_adm1.shp -nln sample_adm1_shp -overwrite -lco OVERWRITE=YES -fieldTypeToString Real -t_srs EPSG:4326 -nlt MultiPolygon
To try additional files one could just add them in the ogr2ogr/samples folder. Please note that the zipped shapefiles need to be unzipped before running the ogr2ogr import command.
-
See the newly imported layers as vector tiles in a browser.
You might neeed to restart the gisapi container for the new tables to show up, see (4). This would not be needed if you'd try to directly access the PBF URLs
Go to http://localhost:8888/services/tables and click on the table name that you would like to see. This will load a page with information about the selected table. In order to view the vector tiles based map, click on the "Dynamic Vector Tile Service - wkb_geometry column" link.
The above ogr2ogr commands might not work on all shapefiles, geojson, etc. Othere command line arguments that could be useful are:
- specifying MultiPolygon geometry type:
-nlt MultiPolygon
- specifying MultiLineString geometry type:
-nlt MultiLineString
- I haven't tested this, but it seems like ogr2ogr can automatically enable the above 2 options by using
-nlt PROMOTE_TO_MULTI
. From the ogr2ogr docs "PROMOTE_TO_MULTI can be used to automatically promote layers that mix polygon or multipolygons to multipolygons, and layers that mix linestrings or multilinestrings to multilinestrings" - forcing source SRS:
-s_srs EPSG:4326