Creating GIS Rest APIS using Geodjango under 30 minutes
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
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 | 10.5446/69083 (DOI) | |
Publisher | ||
Release Date | ||
Language | ||
Production Year | 2022 |
Content Metadata
Subject Area | ||
Genre | ||
Abstract |
| |
Keywords |
FOSS4G Firenze 2022161 / 351
1
7
13
22
25
31
33
36
39
41
43
44
46
52
53
55
58
59
60
76
80
93
98
104
108
127
128
133
135
141
142
143
150
151
168
173
176
178
190
196
200
201
202
204
211
219
225
226
236
242
251
258
263
270
284
285
292
00:00
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
07:03
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
08:15
Installation artLibrary (computing)Mobile appElectronic mailing listCASE <Informatik>GeometryProjective planeComputer animation
08:48
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
11:41
Set (mathematics)Computer fileAuthorizationMobile appDefault (computer science)Software frameworkAuthenticationRepresentational state transferComputer animation
12:08
Coordinate systemPoint (geometry)GeometrySoftware frameworkRepresentational state transferSet (mathematics)File formatResultantComputer animation
12:29
Endliche ModelltheorieData conversionGeometryComputer fileGeometryField (computer science)Serial portRepresentational state transferFile formatProper mapCASE <Informatik>Endliche ModelltheorieSoftware frameworkComputer animation
12:56
Coordinate systemPoint (geometry)Data conversionEndliche ModelltheorieField (computer science)GeometryRow (database)Computer animation
13:14
GeometryPoint (geometry)Coordinate systemFile formatResultantField (computer science)GeometrySerial portComputer animation
13:38
Point (geometry)GeometryCoordinate systemRepresentation (politics)EmailAuthorizationDifferent (Kate Ryan album)Row (database)Key (cryptography)Category of beingSoftware frameworkComputer animation
14:21
GeometryMechatronicsData bufferMeta elementData modelField (computer science)PolygonEndliche ModelltheorieGeometryPoint (geometry)PolygonFunctional (mathematics)Field extensionBuffer solutionCASE <Informatik>Field (computer science)Serial portComputer animation
15:14
GeometryPoint (geometry)Coordinate systemGeometrySet (mathematics)NumberVideo gameDefault (computer science)Representational state transferCategory of beingUniform resource locatorLimit (category theory)Computer fileSoftware frameworkComputer animation
16:06
GeometryCoordinate systemPoint (geometry)Mechanism designDigital filterInstallation artWeb pageParameter (computer programming)Software frameworkDefault (computer science)Type theoryIntegerGeometryFront and back endsRepresentational state transferComputer animation
17:08
Generic programmingObject (grammar)CuboidLine (geometry)Default (computer science)PolygonComputer animation
17:48
Point (geometry)Nachlauf <Strömungsmechanik>Query languageEmailAuthorizationSoftware testingScripting languageGeneric programmingObject (grammar)Maxima and minimaFile formatCuboidPower (physics)Representational state transferSoftware frameworkComputer animation
18:15
Generic programmingObject (grammar)TesselationSoftware frameworkRepresentational state transferZoom lensComputer animation
18:36
Point (geometry)Coordinate systemSoftware testingEmailHTTP cookieScripting languageAuthorizationQuery languageGeometryClient (computing)Level (video gaming)Open setZoom lensCASE <Informatik>Library (computing)CuboidComputer animation
18:57
DistanceGeneric programmingObject (grammar)Functional (mathematics)DistanceMetrePoint (geometry)Computer animation
19:24
DistanceGeneric programmingObject (grammar)State of matterEmailHTTP cookieSoftware testingScripting languageAuthorizationFile formatPoint (geometry)Order (biology)DistanceSlide ruleComputer animation
19:47
MechatronicsDistanceHTTP cookieSoftware testingEmailAuthorizationScripting languageGeneric programmingObject (grammar)Representational state transferCASE <Informatik>View (database)AuthenticationElectronic mailing listType theoryComputer animation
20:04
Open sourceSoftware developerContent (media)Source codeNewsletterVideoconferencingRandom numberGeometryRepresentational state transferYouTubeSoftware frameworkNewsletterComputer animation
20:25
System programmingCoordinate systemXMLUML
Transcript: English(auto-generated)
00:01
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.
00:21
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
00:43
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?
01:09
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.
01:21
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,
01:45
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
02:01
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.
02:21
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.
02:41
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.
03:01
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.
03:20
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.
03:42
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.
04:01
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
04:23
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.
04:41
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,
05:01
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.
05:23
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.
05:42
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.
06:03
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,
06:22
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.
06:41
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.
07:02
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.
07:23
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
07:42
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
08:03
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
08:22
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,
08:41
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
09:00
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,
09:21
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,
09:42
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,
10:01
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,
10:22
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
10:41
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.
11:01
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,
11:22
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.
11:46
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
12:03
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
12:22
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?
12:40
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.
13:03
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,
13:20
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.
13:40
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,
14:04
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.
14:24
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,
14:42
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.
15:00
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,
15:22
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,
15:42
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
16:03
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
16:22
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.
16:42
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,
17:02
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.
17:22
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
17:41
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.
18:02
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.
18:20
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
18:42
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.
19:02
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,
19:21
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
19:42
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
20:02
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
20:20
in this, feel free to connect with me on my channels. So yeah, that's all, any questions? Thank you.