DEV Community

Cover image for Sharing Geospatial Data with OGC API, pygeoapi and MongoDB
Joana Simoes
Joana Simoes

Posted on

Sharing Geospatial Data with OGC API, pygeoapi and MongoDB

Geospatial data can be defined as any dataset which has a location tag attached to it, i.e.: a pair of coordinates which allows us to position it precisely at the surface of the earth. In this post we are going to describe how we can make geospatial data available on the web, trough a RESTfull API, so that ourselves or others can do something with it, for instance creating a map. If it is true you can create a map using a static file, using an API has numerous advantages like scalability, reliability and updatability. In order to do this, we will use three things: a dataset, a standard and a stack of Free and Open Source Software (FOSS).


In this tutorial we will share a dataset with local shops and products in the city of Barcelona, kindly provided by sawcer.

record from sawcer dataset

Each record describes characteristics of the shop such as name, address, website, which products are sold (e.g.: Gluten free donut) and, last but not the least, its location as a point geometry, with x,y coordinates.


Standards are the glue in the server-client architecture. The Open Geospatial Consortium(OGC) is a member organisation, which publishes standards to ensure the maximum degree of interoperability between geospatial data and services. In practice interoperability would mean that you could write a client application that talks to any server, without knowing any details about that server's implementation.
In this tutorial we will use the OGC API Features standard, which was designed for sharing feature data over the web (if you are wondering what a feature is, you can check out this article). These are some of its main traits, which make it very convenient to use:

  • Modular
  • RESTfull
  • Recommended encodings (e.g.: GeoJSON, HTML)
  • Use of OpenAPI

Software Stack

In order to publish the dataset using the OGC API Features standard, we need a software which implements the standard. In this tutorial we will use pygeoapi, which is a python server implementation, released under a FOSS (MIT) license. pygeoapi needs a backend to store the data. For that we will use the MongoDB document oriented database. In order to make deployment easier, the complete stack was virtualised into a set of docker containers, and orchestrated using docker-compose.

Software stack

Putting it all together

In order to publish the dataset, you can clone this repository. If you navigate to the docker/examples/mongo folder, you will find this docker-composition:

version: '3.3'

    image: geopython/pygeoapi:latest

    container_name: pygeoapi_mongo

      - /

      - 5000:80

      - ./pygeoapi/docker.config.yml:/pygeoapi/local.config.yml
      - ./pygeoapi/
      - ./pygeoapi/
      - mongo
      - mongo

    image: mongo
    container_name: mongo
      - 27017:27017
      - ./docker-entrypoint-initdb.d/
      - ./mongo_data/:/mongo_data

    image: mongo-express
    restart: always
    container_name: mongo_express
      - mongo
      - mongo
      - 8081:8081

  mongo_data: {}

Enter fullscreen mode Exit fullscreen mode

Here is what happens behind the scenes, when you type docker-compose up:

  1. The mongo container starts, a database is initialised and the data is injected into a collection. The data is pulled from a GeoJSON file in the mongo_data folder. If you are trying this with your own data, it is worth to mention that mongo ingests the features array, without the outer element. You can transform a regular GeoJSON file into MongoDB-consumable JSON, using the jq command-line utility: jq --compact-output ".features" shops-orig.geojson > shops.geojson
  2. Once the database is live, the pygeoapi container starts. The configuration of the service is on this file, which is mounted by the container. In particular, this bit is where the sawcer dataset is exposed from the MongoDB backend:
        type: collection
        title: Shops and Products 
        description: SEARCH & FIND & SHARE where to get ingredients in local shops
            - cases
            - type: text/html
              rel: canonical
              title: information
              hreflang: en-US
                bbox: [-180, -90, 180, 90]
                begin: 2011-11-11
                end: null  # or empty
            - type: feature
              name: MongoDB
              data: mongodb://mongo:27017/sawcer
              collection: shops
Enter fullscreen mode Exit fullscreen mode

At this stage, the pygeoapi is running on port 5000: http://localhost:5000. You can access the sawcer dataset at: http://localhost:5000/collections/sawcer
You can retrieve the collection items at:
The default response is in html, but you can change it using the f (format) parameter. The server supports both, JSON and JSON-LD encodings. For instance:
You can access one particular item, using its id:

Collection item

If you want to explore the MongoDB collection, you can use the provided mongo-express user interface, running on port 8081: http://localhost:8081

What's next

Making your geospatial dataset available on the web using a standard, unlocks a world of possibilities. It means that many existing (or future) client applications will be able to read your data, out-of-the-box. For instance, anyone can use QGIS, Esri ArcGIS, React-leaflet, OpenLayers or Python OWSlib to pull your data and analyse it, or create products or services on top of it.

OGC API Features on QGIS

You can explore a live deployment of this dataset, at this endpoint:
On this OpenAPI document, you can try all the available endpoints:

Top comments (0)