Gis programming in python

Getting Started: Building Location-Based (GIS) REST APIs with Python

We are in the era of fully integrating GIS with IT operations to have a positive impact on a wider population. The biggest resource to help one achieve that is the knowledge of building REST APIs. One of the best institutions that have integrated GIS with IT operations is Utah AGRC and their work is publicly accessible here:

Utah Mapping Portal

Established in State law in 1991, Utah’s State Geographic Information Database (SGID) provides one-stop download and…

REST simply stands for REpresentational State Transfer and it’s an approach of building APIs on top of the HTTP Protocol. Meaning you can perform the CRUD (Create, Read, Update, Delete)Databases tasks via the web. Additionally, REST APIs return data in JSON or XML format.

API simply stands for Application Programming Interface. Based on the image above, you can see that the API has got the business logic which is being utilized by users on multiple platforms. An example of a GIS business Logic is let’s assume you have a clothing line but you want to have different pricing based on a Customer’s location. You can implement that business logic and let the API share it to Mobile Software Engineers and Web Software Engineers to consume it.

Читайте также:  Python django admin templates

In this article, we will be using Python to implement a REST API. We will focus on Django Framework because it has GeoDjango but it doesn’t mean it’s the best because the same can be implemented using Flask, FastAPI with GeoAlchemy2. Let’s get started:

1. Project Definition

The project will build a GIS REST API for mainly two datasets i.e. Nairobi Health Facilities and Nairobi Sub Counties. An add-on will be GIS abilities to query the nearest health facilities when given a user location and also query health facilities by a Sub County boundary.

Here is the link to the dataset used in this project(Click here)

2. Project Set-Up

You need to have Python 3 installed on your computer. We can then set up a working directory and virtual environment. I am using a windows operating system.

> pip install virtualenvironment> cd Desktop/> mkdir gis-rest-api-with-python

After making our working directory, we set up a virtual environment, and activate it

> cd gis-rest-api-with-python/> py -m venv env> .\env\Scripts\activate

We can now install all the dependencies

> cd gis-rest-api-with-python/> pip install django> pip install djangorestframework> pip install djangorestframework-gis> pip install psycopg2> pip install django-filter

Pyscopg2 helps with PostgreSQL database connections and the rest are quite clear 🙂.

We can now create our project and app using the Django commands, and then we can see if this setup works.

> mkdir src> cd src/> django-admin startproject gis_rest_project> cd gis_rest_project/> python manage.py startapp nairobi_hospitals_api> python manage.py runserver

You should now be able to see the screenshot below on your web browser using http://127.0.0.1:8000/

Not really, Lol, there is a Django setting.py file in your project folder that you need to add some of your dependencies and app

INSTALLED_APPS = [
.
###
'rest_framework','rest_framework_gis','nairobi_hospitals_api',]

NB: Django has got a project which can have multiple apps. For our case, the project folder is gis_rest_project while our app folder is nairobi_hospitals_api. To run a manage.py command, you should be in the project folder.

Читайте также:  Libreoffice java 64 bit

3. Generating Django Models from existing Database

PostgreSQL with PostGIS as a GIS extension is currently the most popular open-source way of storing geospatial data in both development and production. If you ain’t familiar with PostGIS, I can recommend a book PostGIS in Action.

Based on the PGAdmin interface above, I can be able to query and visualize my spatial data using Geometry Viewer.

You can set up a Database connection in the settings.py file on your gis_rest_project folder.

Now to try out our GIS rest API, we need to start up the server:

> python manage.py runserver

We can also access the two REST API endpoints on our localhost:

"nairobihealthfacilities": "http://127.0.0.1:8000/nairobihealthfacilities/""nairobisubcounties": 
"http://127.0.0.1:8000/nairobisubcounties/"

It will look like the image below on your web browser:

You are also able to filter the Health facilities by the sub country boundary using either the id or name.

GET /nairobihealthfacilities/?subcounty=12GET /nairobihealthfacilities/?subcounty=Kasarani

The above queries will return the hospital data in Kasarani (Nairobi) as shown below:

We can also be able to get 5 nearest hospitals by querying the GPS coordinates of a user location

http://127.0.0.1:8000/nairobihealthfacilities/get_nearest_facilities/?x=36.8219&y=-1.2921orGET /nairobihealthfacilities/get_nearest_facilities/?x=36.8219&y=-1.2921

The result for getting 5 nearest hospitals including distance to (in metres):

8. API Documentation

I haven’t done API documentation for this demo, but it’s a good practice and a necessity. As it will help those consuming your APIs to understand what you have built. You can select either Swagger or Redocly.

Источник

Walkthrough: Mapping GIS Data in Python

It seems geographic data has never been more important than at this moment in history. This article will improve your understanding of geospatial information, allowing you an entry point to the rich world of geographic information science (GIS) through neat, easy-to-work with pandas DataFrames.

By following this code-along, you’ll learn to navigate geographic shapefiles in a Jupyter Notebook hosted in Google Colab’s virtual environment. You’ll end up with some pretty sweet maps highlighting the housing situation in Washington, DC.

Читайте также:  Файлы python имеют расширение

Geographic information science (GIS) can be quite a complex field, so consider this walkthrough a soft primer. In order to follow along, you’ll need access to a free Google Drive account — so go get set up if you don’t have one already. Google provides 15GB of free storage, so at the very least you’ll have more space on your hard drive for dank GIS memes.

In the walkthrough, you’ll be uploading data from DC’s open data portal, and then connecting to these datasets from Google’s Colab platform. In this environment, we’ll conduct some analysis on the datasets and ultimately do our mapmaking.

All the resources mentioned in this tutorial, including Google Drive, Google Colab, and Tableau Public, are always free — no request for your credit card, no sign-up trial period, no “what is the name of your firstborn child, Rumpelstiltskin?” nonsense.

If you’d prefer not to take part in the code-along, click here for the link to completed Colab notebook and the other materials used in this exercise. But you’ve heard what they say about the journey being more important than the destination? Let’s get started with the walkthrough!

Table of Contents

  1. Introduction | Maps into Tables | Packages | Data Structures
  2. Activity | Virtual Environment | Data Collection | Code-Along
  3. Wrap-Up | Resources

Источник

Оцените статью