Get started with pygeoapi
pygeoapi is a python implementation ( Flask, Starlette, Django ) of OGC API Standards. pygeoapi allows users to connect their datasets of various formats (GeoJSON, Shapefile, PostGIS connection, Elasticsearch, etc.) and create RESTful end points following OGC Standards using OpenAPI, GeoJSON and HTML. This project is Open source and gaining quite a popularity now a day.
Let us start by installing project on local machine
Installation
Installing pygeoapi using git
pygeoapi is available on github and can be cloned easily. To install it locally, open terminal and follow commands.
- Head over to your desired location
- create python3 virtualenv and activate it
- clone repo in the folder and install dependencies and run setup
git clone https://github.com/geopython/pygeoapi.git
cd pygeoapi
pip3 install -r requirements.txt
python3 setup.py install
Installing pygeoapi using docker
pygeoapi official docker image is also available , which we can use to run docker instantly.
- Make sure that Docker is running on your machine
docker --version
- Run docker image at desired port (8050, in this example)
docker run --rm -p 8050:80 geopython/pygeoapi:latest
Setup
pygeoapi depends on a configuration .yml file to load everything including metadata, data stores,etc. While using docker, this file is included in the image and thus if you run docker image as per command above, you can go to browser and see pygeoapi local instance
But if you click on any link such as Collections, Processes, etc., you will see that you are redirected to port 5000 (which is official port as per config file).
If you have installed package via git, you will not be able to run the pygeoapi instance, until you declare path of config file as environment variable PYGEOAPI_CONFIG
.
Hence, it is important to understand the configuration file and it’s structure. You can download default config file here. If you have cloned the repo, you will find file with name pygeoapi-config.yml
.
Editing config file
This file can be briefly broken down into following parts.
- Server — Here all metadata related to instance can be found such as default port, allowed languages, default url, etc.
- metadata — Here information such as contact details, homepage link, etc. is provided
- resources — This is where all the data sources that we want to use in our app are written.
As we are focusing on just installation part, we’ll have a look at server and metadata. In upcoming blogs, we’ll dive deep into resources and various available settings along with it.
As we are interested in running our application at port 8050 , we’ll change port & url default values from 5000 to 8050
If you are using docker, the port for bind will be 80, url can be http://localhost:8050
Apart from this, if we want to use our own information for contact, we can do that by editing default information under metadata
Once file is edited as per requirement. Setup env variable by giving path to the file
(env) krishnaglodha@Krishnas-MacBook-Pro pygeoapi % export PYGEOAPI_CONFIG=pygeoapi-config.yml
Validating config file
It is always good practice to check whether the edits you made to the config file are valid or not, To do this we rely on openapi
Setup a new environment variable as follows
export PYGEOAPI_OPENAPI=example-openapi.yml
and then run following command
pygeoapi openapi generate $PYGEOAPI_CONFIG --output-file $PYGEOAPI_OPENAPI
so that if there are any errors, pygeoapi will catch and tell you.
e.g. In this case I’m missing one python package requests
, which then I can install and check again. If everything is correct, example-openapi.yml
file will be generated.
once checked , we can start the instance py running following command
pygeoapi serve
If you are on docker, use following command to connect your edited config file.
docker run -p 8050:80 \
-v $(pwd)/pygeoapi-config.yml:/pygeoapi/local.config.yml \
geopython/pygeoapi:latest
Once you get pygeoapi running, open http://localhost:8050/ and you’ll see pygeoapi homepage with custom configuration
In upcoming articles, we’ll learn more about
- Web Interface of pygeoapi
- How to use various datasets with pygeoapi
- Making pygeapi production ready, etc.