Automating Geospatial Tasks with GeoServer and Python
During my work at Airbus as a Deep Learning Scientist, I had to automate geospatial tasks for some projects, like the extraction of image tiles from a GeoServer WMS/WCS service, ingestion of large satellite imagery, and more. In this article, I will show you how to automate these tasks using Python only.
Context
Geospatial data come in various formats and sizes, and it can be challenging to manage them. Among the tools available to manage geospatial data, GeoServer is a popular open-source server software that allows users to share, edit and serve geospatial data. GeoServer supports various data formats, including vector data formats such as ESRI Shapefile, and raster data formats such as GeoTIFF.
However, there are no official Python client to interact with GeoServer. To automate geospatial tasks with GeoServer, I created a Python library called geoserver-py
.
Introducing GeoServer-Py
Why?
The goal of geoserver-py
is to provide a simple and efficient way to interact with GeoServer using Python. The library is designed to follow the REST API of GeoServer without abstracting it too much. This allows users to have full control over the requests and responses.
Features
This library comes with the following features:
- Full support of the GeoServer REST API
- Full type hints and documentation
- Support for JSON and XML responses and requests
Automating Geospatial Tasks
In this section, I will show you how to automate geospatial tasks using geoserver-py
. First, you will have to install the library using pip
:
Tip
You may want to create a virtual environment before installing the library.
Connecting to GeoServer
First, you will need to connect to your GeoServer instance. Here is how you can do it:
Because this library depends on requests
, you can pass additional parameters such as timeout
or proxies
to the GeoServer
class, as you would do with requests
.
Managing Workspaces
First, you may find it convenient to manage your workspaces. Here is how you can create a new workspace:
To facilitate the management of workspaces, you can also list, update, or delete them:
Managing Data Stores
There are different ways to create / add data in a GeoServer instance. You can either upload directly your data (from a local file, external URL, PostGIS, etc.) or create a new data store from a data already present in the GeoServer instance (e.g. you can reference a file that is already present in the data directory of the geoserver).
You can also create a data store from a PostGIS database:
And of course you can list, update, or delete data stores:
Managing Coverage Stores
Coverage stores are a type of data store that contains raster data. You can create a coverage store from a GeoTIFF file:
You can also list, update, or delete coverage stores:
More Examples
Check out the rest of the examples in the documentation! You will find examples on how to manage layers, styles, and more.