I want to create a full-stack web application to let users query parks and greenspace in the UK, something like this: https://www.barnet.gov.uk/directories/parks.
I want to build a Django RESTful API to query parks and greenspaces data.
I am still a beginner and I am still learning. I have never imported large amounts of data to my app/database before. Usually, I manually input the data one by one for my RESTful APIS.
This time, I don't want to manually input all the data. I want to build a project that is based on real-world data. Therefore, I found a public dataset for parks and greenspace (?), https://www.data.gov.uk/dataset/4c1fe120-a920-4f6d-bc41-8fd4586bd662/os-open-greenspace. I find it hard to understand what is the page about. I downloaded the Geopackage/gpkg file and I don't know how to use it.
Here are some questions I have in mind:
- Is this the data I am looking for?
- Does the data contains info about the park, such as the park name? Or does it only store the geo location of the park?
What I want to do is somehow transform this gpkg file into data I can play with in my Django RESTful API. And then I will write some API endpoints for querying parks, and output JSON data such as getting the park name, location, etc. How can I do that?
Creating a geo based API is complex with a lot of potential decision points, however, here are the basics:
Source the data / shapefiles. Looks like you've done this as your source includes shape files.
Create a POSTGIS server. Postgis is an extension of Postgresql and is fairly easy to install.
Upload your geo data to postgis. I use the ogr2ogr package to accomplish this. This is it's own subject and may require transforming the shape data from your source into one that Postgis can work with.
Create your Django data models for the geo data. It's probably best to point inspectdb at the tables created by your ogr2ogr data loader. You'll want the managed=False Meta attribute as Django has nothing built in to work with POSTGIS or the shape files directly. I also suggest removing the shapefile out of the model that Django sees as it tries to port it to a textfield and the data is useless to look at that way. Even without the shapefile in the Django model you'll still be able to query against it on the tables below because we're going to have to drop into raw SQL
Create your other models and geo queries for them. The Django ORM again has no context for shapes or POSTGIS type queries, but you can write raw sql and get your list of django objects that way. For example if I'm trying to see if a lat/long is within the shape I've defined I might do something like:
In the example above "geom" would point to the field that contains the shapefile.
I realize this doesn't directly answer your question and includes a lot of steps that can have umpteen different implementation paths; however, a reasonable attempt at a GEO based API example in a StackOverflow post is likely a fools' errand.