We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Creating GIS Rest APIS using Geodjango under 30 minutes

00:00

Formal Metadata

Title
Creating GIS Rest APIS using Geodjango under 30 minutes
Title of Series
Number of Parts
351
Author
License
CC Attribution 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language
Production Year2022

Content Metadata

Subject Area
Genre
Abstract
We're living in the world of APIs. CRUD operations are base of lot of operations. Many smart frameworks such as Django, Flask, Laravel provides out of the box solutions to filter the data, which covers almost all needs to separate data based on column values. When it comes to Geospatial data, we expect to filter data based on their location property instead of metadata. This is where things get complicated, if you are using framework that doesn't have package, library built to handle such use cases, you are likely to be dependent on either database or any external package to handle it. Fortunately Geodjango [docs.djangoproject.com/en/4.0/ref/contrib/gis/] (Django's extension) allows us to create databases which understands geometry and can process it [docs.djangoproject.com/en/4.0/ref/contrib/gis/geoquerysets/#gis-queryset-api-reference]. It also provides support to write APIs using Rest Framework extension [pypi.org/project/djangorestframework-gis/] which takes this to next level allowing user to output the data in various formats, creating paginations inside geojson, create TMSTileFilters, etc. In this talk we'll scratch the surface of this python package and see how to build basic CRUD APIs to push, pull GIS data along with filtering it to the PostgreSQL database
Keywords
202
Thumbnail
1:16:05
226
242
Representational state transferSoftware frameworkOpen setScale (map)Relational databaseObject (grammar)Object-relational mappingStatisticsInterface (computing)Interior (topology)Endliche ModelltheorieExtension (kinesiology)Data modelQuery languageDatabaseRaster graphicsDynamic HTMLMean value theoremDatabaseServer (computing)Type theoryInformation securityRepresentational state transferMeta elementRight angleData structureNormal (geometry)Template (C++)Goodness of fitDifferent (Kate Ryan album)YouTubeWeb pageInformationEndliche ModelltheorieSystem administratorSoftware frameworkSocial classContent (media)Physical systemBitOpen sourceView (database)WritingAuthenticationToken ringQuery languageCustomer relationship managementCASE <Informatik>Functional (mathematics)Extension (kinesiology)Mobile appLogicMereologyMetadata2 (number)Communications protocolComputer fileBlogDebuggerTwitterUniform resource locatorGeometrySoftware developerAutomationDynamical systemLevel (video gaming)Front and back endsProjective planeSerial portRaw image formatXMLComputer animation
Revision controlComputer programSource codeField extensionAxonometric projectionGeometryOpen setLibrary (computing)AbstractionNeuroinformatikRaster graphicsInformationDatabaseLibrary (computing)Installation artDefault (computer science)Projective planeWindowWebsiteRadical (chemistry)Physical systemGeometryMultiplication signGeometryAddress spaceTranslation (relic)Operating systemIntegrated development environmentComputer fileVector spaceIP addressComputer animation
Installation artLibrary (computing)Mobile appElectronic mailing listCASE <Informatik>GeometryProjective planeComputer animation
Endliche ModelltheoriePolygonData modelGeometryRaw image formatView (database)StatisticsQuery languageGoogolOpen setPoint (geometry)DistanceGeometrySlide ruleDatabaseSerial portDefault (computer science)File formatLine (geometry)Point (geometry)Field (computer science)PolygonEndliche ModelltheorieSoftware frameworkMetadataRight angleDistancePhysical systemQuery languageBit rateRepresentational state transferWKB-MethodeIntegerMultiplicationParameter (computer programming)Multiplication signRaster graphicsDimensional analysisComputer animation
Set (mathematics)Computer fileAuthorizationMobile appDefault (computer science)Software frameworkAuthenticationRepresentational state transferComputer animation
Coordinate systemPoint (geometry)GeometrySoftware frameworkRepresentational state transferSet (mathematics)File formatResultantComputer animation
Endliche ModelltheorieData conversionGeometryComputer fileGeometryField (computer science)Serial portRepresentational state transferFile formatProper mapCASE <Informatik>Endliche ModelltheorieSoftware frameworkComputer animation
Coordinate systemPoint (geometry)Data conversionEndliche ModelltheorieField (computer science)GeometryRow (database)Computer animation
GeometryPoint (geometry)Coordinate systemFile formatResultantField (computer science)GeometrySerial portComputer animation
Point (geometry)GeometryCoordinate systemRepresentation (politics)EmailAuthorizationDifferent (Kate Ryan album)Row (database)Key (cryptography)Category of beingSoftware frameworkComputer animation
GeometryMechatronicsData bufferMeta elementData modelField (computer science)PolygonEndliche ModelltheorieGeometryPoint (geometry)PolygonFunctional (mathematics)Field extensionBuffer solutionCASE <Informatik>Field (computer science)Serial portComputer animation
GeometryPoint (geometry)Coordinate systemGeometrySet (mathematics)NumberVideo gameDefault (computer science)Representational state transferCategory of beingUniform resource locatorLimit (category theory)Computer fileSoftware frameworkComputer animation
GeometryCoordinate systemPoint (geometry)Mechanism designDigital filterInstallation artWeb pageParameter (computer programming)Software frameworkDefault (computer science)Type theoryIntegerGeometryFront and back endsRepresentational state transferComputer animation
Generic programmingObject (grammar)CuboidLine (geometry)Default (computer science)PolygonComputer animation
Point (geometry)Nachlauf <Strömungsmechanik>Query languageEmailAuthorizationSoftware testingScripting languageGeneric programmingObject (grammar)Maxima and minimaFile formatCuboidPower (physics)Representational state transferSoftware frameworkComputer animation
Generic programmingObject (grammar)TesselationSoftware frameworkRepresentational state transferZoom lensComputer animation
Point (geometry)Coordinate systemSoftware testingEmailHTTP cookieScripting languageAuthorizationQuery languageGeometryClient (computing)Level (video gaming)Open setZoom lensCASE <Informatik>Library (computing)CuboidComputer animation
DistanceGeneric programmingObject (grammar)Functional (mathematics)DistanceMetrePoint (geometry)Computer animation
DistanceGeneric programmingObject (grammar)State of matterEmailHTTP cookieSoftware testingScripting languageAuthorizationFile formatPoint (geometry)Order (biology)DistanceSlide ruleComputer animation
MechatronicsDistanceHTTP cookieSoftware testingEmailAuthorizationScripting languageGeneric programmingObject (grammar)Representational state transferCASE <Informatik>View (database)AuthenticationElectronic mailing listType theoryComputer animation
Open sourceSoftware developerContent (media)Source codeNewsletterVideoconferencingRandom numberGeometryRepresentational state transferYouTubeSoftware frameworkNewsletterComputer animation
System programmingCoordinate systemXMLUML
Transcript: English(auto-generated)
Hello, everyone. Thank you for attending this talk. In this talk, we are going to go through creating how exactly we can create special REST APIs under 30 minutes using GeoDjango. So of course, this is like an exaggeration. Of course, when you have to create very simple APIs, you can do it under 30 minutes.
But we will see what are different things that are possible with GeoDjango and how exactly we can work on that. This is possible because GeoDjango is an extension to a very famous Python framework known as Django. And with using GDAL, GeoSpyProj, it's possible to actually use geospatial data in a way
it is meant to be in Django. A little bit of information about me. So I'm a full stack developer. I'm an open source contributor. I started contributing to a few projects. Can you go to the next slide?
So I'm a freelance GIS developer. I work with front end, back end, both. I started contributing to a few of the open source projects, such as GeoStyler in past couple of months. And hopefully, I'll be contributing more.
Apart from development, I also create a lot of content around open source GIS. You can check out my blogs, my YouTube videos. And for all social handles, it will be Twitter, LinkedIn, YouTube. You can search for my social handle and send me a request. I'll be more than happy to talk to you. So I'm assuming that you know these four things, Django,
Django REST framework, GIS, and REST protocols. We will be just going through a quick recap about all these things, but I'm hoping that you have already worked with all of this. So for example, Django is a Python framework
that is meant to scale, meant to be secured. And it actually works on mvt model, which stands for model view and template. So whatever your database structure is, you actually create models out of that. And then whatever is your logic that you want to write and execute.
Whenever you are hitting any URL, let it be static or dynamic. You write that in view. And your HTML part actually goes inside template. Django uses Jinja 2 template engine, which is very amazing because it helps you to write dynamic HTML content.
That means that if you have to create a website and if you have navbar and footer and sidebar, you just create one HTML and you actually put it as, let's say, like navbar HTML, and then you can reuse it whenever you want. Not just entire HTML, but you can, of course, also change the content inside that HTML.
And that is what makes it SEO optimized because you can just create a base HTML, add some meta tags, and then it'll be used in every page. Apart from that, Django also has a very good community support, and you can see that it's already very famous on GitHub as well.
It has its own server, so to run a Django project, you don't need to rely on any other server. You just go python manage.py run server, and Django automatically starts its own server. Django is built on top of apps, so whenever you will install Django, and when you go to settings.py file, you will see that it's all apps.
So even if you add new app to Django, you will have to mention that. Even if you add any other Python package, you will have to mention that, and that's what makes it robust and very easy to use. A lot of famous companies, such as Instagram, Mozilla, Pinterest, all of them use Django.
Second thing that I expect that you have worked with is Django REST Framework, which is a very famous Python package built to help you to take all the magic that Django has and actually create RESTful APIs on top of it. There are various things that actually goes into creating a REST API, such as creating
serializers, which actually can help you to import and export your model data as a JSON data. Apart from that, REST framework also allows us to create authentication. Let it be session authentication or token authentication right out of the back, but you're not only limited to that.
You can obviously use any other authentication system as well. DRF also allows you to create permission classes. So for example, if you want to create a REST API where only admin can use it to edit, while other people can go ahead and just read the data,
you can do that by simply adding permission classes. Django has a lot of permission classes inbuilt, but you're not, again, limited to that. You are more than welcome to create your own permission classes and then use them. It also supports filtering and pagination right out of the back. And as we move forward, we will have a greater look at that.
The one thing that I want to talk most about over here is GeoDjango, which is a GIS extension of Django, which actually allows us to use GIS-enabled models. That means we can create different types of geometries, raster data inside Django. We can also create special queries.
As I mentioned, with Django and Django REST framework, we can create filters. We can create normal metadata queries. But actually, by using GeoDjango, we can write queries which runs on a special data. One thing I forgot to mention about Django is that Django comes with an admin panel right out of the back.
You don't need to install anything. You can just start using admin panel, which allows you to create users and also talk with all the database models that you have created. By installing GeoDjango, you can also see map, and then on that map, you can do things such as add your data, let it be geometry,
or even raster files. Next, GeoDjango supports a lot of databases, as you can see. Suppose PostGIS, Specialite, MySQL, and Oracle. And the official documentation will walk you through all the different things that you can do with different types of databases.
So far, PostGIS is the most supported one. I would say because 100% of the functions that are written in GeoDjango are supported by PostGIS. And of course, depending upon your use case and what functions you want to use, you can check out the official documentation.
To install GeoDjango, it can be a little bit tricky because a lot of these libraries depend upon the operating system. But again, official documentation is very crisp, very clear, and works almost every time. So if you're on Linux or Mac, you can simply use something like a conda environment to create your own environment and install all the libraries.
If you're on Windows, you can use OS Geo 4W installer, and that will do the trick for you. So geos, we actually need for computations of geometry. Proj, we need for projection system. GDAL, as we all know, is a translational library
for vector and raster data. Geo IP is for getting the geolocation information based on the terminal's IP address. And PostGIS and Specialites, obviously, these are the databases that we usually work with. When you create a new Django project, by default, we'll have SQLite support inbuilt
and SQLite file inbuilt. But if you want to move from SQLite to PostGIS, that is not a problem. You can definitely do that. To set up the Geo Django project, you only need to add this django.contrib.gis
in the installed apps. As I said, everything is apps in Django, so whenever you will install any library or anything like that, you will need to mention that in the installed apps list. So this is not the only thing that we need to do. When we actually work on more detailed case scenario,
we can actually add more things to settings.py. But this is obviously the most important one. Next we have is models. So let's have a look at how exactly models can be created using geometry field. So here you can see that we have Pokemon centers
where name, master, and ratings are something that is metadata. Of course, it's character field and integer field, but the last one that we have is a center which is actually a point field. So Django supports point, line, polygon, multi-point, multi-line, multi-polygon, and also raster. But raster is just for PostGIS,
so you have to keep that in mind whenever you are working with Geo Django. Raster is also supported. And you have to keep in mind that the data that you want to send to the database must be always in a long lag format. So this is one of the confusion that I personally feel all the time,
but that's why I decided to mention it over here. And data can be exposed as a WKT, WKB, GeoJSON, right out of the bag. So once the data is, once you feed the data, you can actually start using that data, and by default, Django will support WKT format,
so whenever you are getting the data back, or whenever you want to push the data, you will have to do it using WKT. But of course, there are workarounds, and we will have a look at them now. When we create the model for, like, let's say point field or polygon field,
there are a few arguments that we can pass, such as SRID. We can define what SRID do we want to work with. We can also define the dimension, whether the data is going to be 2D, or whether the data is going to be 3D. We can also define whether we want that particular column
in our database as a geometry or a geography. So this is very important that if you want to work with geography, you simply mention geography is equal to true and it will do the trick for you. But again, keep in mind that geography will only work with the lat long and not with any other coordinate systems.
Next we have is a Django REST framework specifically designed for GIS. So using this, we can actually also create REST APIs, which uses GeoDjango's capabilities to work with the spatial data. So if you remember, as I said, that when we work with Django REST framework,
we create serializers, which actually allows us to talk with the data in a JSON format. And with GIS, we can create actually geo serializers, which the use of that, we will definitely see in upcoming slides. Apart from that, we can also create spatial filters and point and distance queries and things like that.
Again, to set up the Django REST framework GIS Python package, we need to mention that in installed apps. And there are, again, many settings that we can do. So for example, when we work with authentication or filter or pagination, these are all extra settings
that we can mention in the settings.py file, if you want to change, of course, the default settings. And as soon as you install Django REST framework, as soon as you install GeoDjango, you will immediately see that the results that you are getting as a WKT, you will start seeing at least the geometry
as a JSON format. So that's one improvement that we get right out of the bat when we install GeoDjango. And now let's talk about serializers. How exactly can we get the data from model in a proper GeoJSON format?
So we generally, using REST framework, we create serializers.py file where we mention that we will be dealing with the Pokemon centers, which is our model, and here we can mention what will be our GeoField. So in this case, center is my GeoField because I'm using center as my geometry column name as well.
Once we do that, we can also assign ID field, which actually will create whatever is there in this particular column in our row as the ID of that particular feature. So once we do that, you can immediately see the results that now not only the geometry,
but the entire data is properly in a GeoJSON format. And here you can see that the name, which we defined as an ID field, is now the actual ID over there. And this is how, by just adding a serializer, we can get the data from earlier as a WKT.
We were getting the data as WKT, but now we are getting the data as a GeoJSON. And we can also see this difference while posting the data. So earlier, as I mentioned, that whenever we are posting the data, we have to send the WKT back, but once we use GeoRest framework, we can actually send the feature as a body,
and it will still take that as a data. But we have to be sure that the properties, the name of the properties, the key of that particular row is actually matching with the column name because this data will be again converted back to columns and then will be stored.
If you don't have geometry in your model, that's not a problem. You can also create geometry on the fly. So to do that, we actually use geometry serializer method field. So for example, here I have created this buffer, and in this case, I'm particularly using the center,
which was also a geometry, but what I want to do is I want to actually, instead of sending the point, I want to send the buffer. So what do I do is I actually create this getBuffer function, where I'm returning the polygon, and this is just a random extent where I'm adding one degree and removing one degree.
And here you can see that I can use that geofield buffer, which I mentioned over here. So if you have, let's say, lat and long as your column names, that is still fine. You can actually create geometry on the fly and then use it. Next is we'll see how pagination works. This is one of the problem with GeoJSON is that as the number of features increases,
it really makes GeoJSON heavy. So this is a very good approach where we can actually set the pagination and it will take limited number of features in each request. By default, Django REST framework supports 100 features in each request,
but you can change that by mentioning how many features do you want in each request in settings.py file. And you can see that not only pagination is working, but also we get these two new properties known as next and previous, which makes our life very easy because we can just hit that particular URL
and get next or previous data. And not only that, here you can see that the page is equal to two. That page, particularly the parameter, we can also change it to whatever name that we want. Filters, I think this is going to be one of the most important thing because we want to see how exactly
we can filter our data using special capabilities. So geofilters, as I mentioned that in REST framework, by just mentioning that what type of filters that you want, so for example, the default is Django backend filter. You are able to actually use any of your columns and then create filtering on top of that.
So if it's a character column, then you can do filtering such as whether whatever you want to search is equal to that or is like that or starts with that, end with that. If it's an integer, then you can do less than, greater than. Or if you want to create your own filter, for example, if you want to see whether the date given is between two dates,
you can actually create your own custom filters as well. And with geofilters, you can actually use the special capabilities to create filters. So for example, this one is a filter based on bounding box where we will just define a filter that is available by default which is in bbox filter.
And using this filter, we will be able to check whatever bounding box that we are passing in the URL, whatever data that we get inside that bounding box, only that will be returned. And at the last, we add this bbox filter include overlapping which means that if your data is polygon or line
and even if it is overlapping, it will return. That is totally up to you if you want to keep it true or false. And this will work something like this where you pass bounding box and of course, minimum longitude, minimum latitude, max long and max lat and that's how the format will be and it will return the data in a GeoJSON format.
Now again, here as you can see that the reason why I said that we can create APIs under 30 minutes is that you don't have to do anything, you just have to mention that what filter do you want to use and it will automatically start using it. So that's the power of GeoDjango and REST framework. Next we have is a filter based on XYZ tile.
So if you want to use your data as a TMS, you can also do that by providing TMS tile filter and here we will be providing zoom X and Y of that particular region and it will return the data for you. For example, in this case, I am at five zoom level and X value is 22 and Y is 14
and it will return the data for you. To get these values, of course, depending upon what client you're using, let it be leaflet or open layers or map libre, map box, all of them have features where you can get this data. Then we have filter based on distance from point.
So here you can see that even though the point is in lat long, I am passing the distance in meters. This is because, again, this is a inbuilt function, distance to point filter and this will just work. So it will take long and lat as 78.45 and 11.81 and distance as 10,000 meters,
that means like 10 kilometers and it will return the data. Just like that, we also have one more filter. I'm not sure if that's in the slide, which actually when you give your lat long and when you give the distance, it will actually give you the data
in order of which is the closest to your point. So these are all things that we generally use whenever we are creating REST API. There are so many things that you can directly add to this type of list API view such as authentication, permissions to actually make it more robust
and specific for your use case. If you want to learn more about it, you can follow my newsletter and my YouTube channel where I'll be uploading a Geo Django and a GIS REST framework related course very soon. And of course, if you want to talk to me about anything
in this, feel free to connect with me on my channels. So yeah, that's all, any questions? Thank you.