Adding Geo into your Django by Corryn Smith

This video features Corryn Smith at DjangoCon US 2015 in Austin, Texas, USA.

Adding Geo into your Django by Corryn Smith
0:24:17
Published November 3, 2017
7,702 views

Adding Geo into your Django by Corryn Smith

This presentation will teach the audience the basics of GIS and GeoDjango. Once you know how to set up a Django project, putting a map on your webpage hasn't been easier. During my talk, I will show you how to navigate through the GeoDjango documentation as well as other handy tools such as Mapbox and Leaflet. In addition, I will show the audience two GeoDjango examples – a project about my Study Abroad trip and an interactive campus map! The overall objective is for the audience to know that GeoDjango exists, how to use it, and to promote creativity for their next project!

Help us caption & translate this video!

http://amara.org/v/HH7w/

Summary

Corryn Smith explains how to add geographic data to Django with GeoDjango. She recommends PostgreSQL with PostGIS, shows how to install the GIS app, match Django models to shapefile fields, load shapefiles, and manage points and polygons through the Django admin. She then demonstrates exposing the data as GeoJSON through an API and displaying it with Mapbox and JavaScript libraries such as Leaflet, arguing that map-based presentation makes spatial data more useful and understandable. In the questions, she points to GIS data sources and notes that existing Django projects need a spatial database and a few GIS-specific additions.

Key takeaways

  • GIS represents, manipulates, and analyzes geographic data, including points, lines, and polygons.
  • GeoDjango works best with PostgreSQL and the PostGIS extension, and requires spatial data such as shapefiles.
  • A shapefile’s fields should correspond to the fields in the Django model, and all files in the shapefile archive must be kept together.
  • GeoDjango’s admin integration provides a map for adding or editing geographic features, while GeoJSON APIs can expose them to front-end applications.
  • Mapbox and Leaflet can display GeoDjango data interactively, including custom markers, popups, map bounds, and user location.
  • Spatial data can be found through sources such as GIS clearinghouses, TIGER, and NHGIS, or converted from spreadsheets into shapefiles.

Summarised automatically from the transcript.

Transcript

3,700 words · auto-generated Show

Automatically transcribed, so expect mistakes in names and technical terms.

0:16

Speaker 1: Hello everyone. Um today I will be talking about how you can add Geo to your Django And uh first off, I want to apologize if my slides are slightly askew. I originally created my PowerPoint on an Ubuntu. I transferred it to a PC and I don't have a computer so I'm using Anna's MacBook. So it's touch three OS You can follow along on a on the um PDF version. You just have to go to bit. ly slash Geojango15. So give you a little information about myself. I will I will uh uh introduce you guys to what GIS may be if you're not familiar with it. how

1:01

Speaker 1: GIS and Python interact with each other and then what Geojo is all about and what you can do with it. So my overall objective is to introduce you guys to what Geojango is and hopefully you guys can make your own project with it. So a little about a little bit about myself. I attended the University of North Georgia from 2011 to 2014. I was originally a computer science major. I took two classes, intro to Jython and Java. I wasn't really feeling it. I went to a GIS day and they told me how you can make maps, analyze maps, and even use information technology to do more with your maps. So I changed my major To environmental and spatial analysis where I dip my toes in remote sensing, GIS, environmental studies, Python, Django

1:47

Speaker 1: Right now, I currently live in Flagstaff, Arizona, pursuing my master's in geography. I'm not really doing anything with Django, but I do plan on maybe making a project using Django. And uh definitely want to spend more time, free time using Django. So I keep saying this. What is GIS? Well GIS stands for Geographical Information Systems. Some people may tell you it stands for science, some people may say it stands for geographical information studies. Pretty much what a GIS is, it's a program that allows you to create, manipulate, analyze your geographic data. So pretty much if you have a point on Earth, you can represent that point on a GIS.

2:33

Speaker 1: So just some applications of GIS. Starbucks may be the best or the worst at GIS. Every time I want to go to Starbucks, I cannot find one. But if there is a Starbucks, I guarantee you there are three in a half mile radius. Zip codes when you go to the department store. You can create a map of trails or bikes or urban trail systems in your city. If anyone uses Waze on their smartphone, that uses GIS. So pretty much if you have spatial data, you can display it on a map and people love visuals. Here's one example, just uh urban development over a five-year span in uh Gainesville, Georgia. A simple tracking map to track textbooks, or you can make a map of your campus.

3:23

Speaker 1: So why GIS in Python? Well, um there's this one popular program called ArcMap that's by Esri that a lot of people in the GIS field use. Um Python is installed in Arcmap and you can go into the Python in Arcmap and write a You can use it to automate your tools. So you can uh write a script here that runs like five tools and you can have all of your stuff that you want to do on a um a project done really fast instead of manually clicking going through each tool on each uh shapefile or whatnot, um they use ArcPy library to do that So what about GIS and Django?

4:09

Speaker 1: Well together they can make GeoDjango. So first before you start your Geojo project, you need to have a spatial database. I highly recommend using Postgres because you can create the extension PostGIS to get all the spatial reference stuff you need to help store your GIS data. So when you create your project, you want to go into your settings folder, and under your installed app, you want to make sure that you have Django. contrib. gis. That's pretty much the only difference. So now you want to have your Geojenga project, right? Well you need spatial data to have a Geojango project. I highly recommend going to the Geojjango tutorial.

4:57

Speaker 1: They have the world border shape file there for your convenience. If you don't like World Borders for some reason, you can go off in the fancy internet and find other shapefiles. For you non-GIS people Shape files normally are zipped and contain seven other files. Sometimes they have four, but it's it's four to seven files. I can't tell you what they all stand for, but dot PRJ means projection. The DBF is the database. You just want to have everything zip. You can't just pull out the. shp or it won't work. Um if you have your own uh GIS on your computer like ArcMap or QGIS or GRASS, you can create your own shapefile

5:44

Speaker 1: there. GIS people. So um your model should pretty much duplicate what's in your shapefile. The shape file um is Pretty much a model. You have uh all your different fields like the uh I have an example here, like the name of the school, how many students. uh field for your longitude, field for your latitude. You want your model to pretty much uh be what's in your shapefile. The image on the left is from the Geojango tutorial about the worlds. The uh image on the right is my uh Georgia example. So as you can notice on the top of the world border example it has Django. contrib. gis.

6:29

Speaker 1: db. So you want to make sure you have that. gis So the Geojo tutorial is really great, but I think if you're new to Geojo, it can be very intimidating. It goes through a lot of spatial queries that you can run and it's it's kind of fatty so what I'm gonna go over is I'm cutting the fat for you guys So now that you have your spatial database and you have spatial data in a shapefile form, you can now load your spatial data. So this is what I did, and I'm pretty sure the Geojo tutorial does the same way. I made a data folder in my app and I just pasted everything that was in my my zip file

7:15

Speaker 1: So you can see all seven files are in there. And then this load. py is directly off of the Geojang tutorial. I just changed the GA mapping fields to match my model and shapefile. And uh just change some of the names like GA shape instead of worlds underscore border underscore shape underscore string of numbers. So then you want to open up your Python shell and uh import the load and then run the load. py. And as you can see, it saved, I believe there's 10 schools that was in my uh Georgia shape file. So now let's go into our Django admin, and you can see that the schools had successfully loaded into

8:04

Speaker 1: my Django project. You want to also go into your admin and make sure that it's geo-friendly by adding the. gis to your Django. contrib. And then with your admin. site. register, I have this admin. geo model admin. So this is a very fancy thing that will add a map to your admin. And then you want to do the same thing with your uh your main URLs. py, not your uh app project. Just make sure that it has the. gis, of course. So um let's say that I forgot to add a school and instead of going into my GIS, my computer creating that second or my last uh school, reloading it.

8:51

Speaker 1: I can go straight into the admin and go to add uh Georgia, kind of like what you can do with a normal Django project. But this time we have this fancy map in our admin. So I have the name of the school. I used Tess State College, uh University. There's one student And then I placed a point in the middle of Lake Lanier, because there's no school there. This is kind of tricky the way that it's set up. So you have to put a latitude and longitude number, a string, in your admin, or else it'll be like, can't do it, gotta fill me in. So you put the point on the map, you hit you have to uh put mumble jumble in the Latin long, hit save, and it will override

9:37

Speaker 1: the strings that you put up there with the correct coordinates. of the point that you put on the map. Now you can also do this the other way. You can rig it where you can copy and paste your real Latin long and place a point in the ocean Save it, reload it, and then it'll go to the point that you had set your Latin long to. But those are I think two or three other strings that gotta put an admin, but for the keeping it simple, we're just gonna do it this way So as I mentioned, the point on the map overrides the Latin long. It automatically geo it finds that point on the earth and geocodes that location. Same thing if you had a polygon, you can just draw your polygon and it'll uh

10:22

Speaker 1: find the geometry for that shape file or that file. Um so I guess you can do this without having a shapefile to begin with. You can just create a model and keep adding and adding and adding. But um you really should have a shapefile to begin with if you wanted to share that with GIS folk. or if you wanted to bring your shapefile into Arcmap or QGIS to do further analyst on it. So I highly suggest having a shapefile first. But if for some reason you forgot to add something, you can always add it in. So you want to display your spatial data, right? I highly suggest you create an API so you can call your API's spatial data to your project.

11:08

Speaker 1: So I'm not going to get into really big details. It's simply setting up a normal API that you do with regular data. You just want to make sure that you have REST underscore framework underscore GIS. And then import serializers as geo-serializers. And you'll have this geo field that's equal to m poly, and then the fields that you want shown on your API, such as name and number of students. Same thing, it uses JSON data, but um it's called GeoJSON data. And same thing, you just Want to have it GIS. Actually, the way I'm looking at it, it looks like a regular JSON uh setup, so I don't even think you need to add GIS anywhere.

11:57

Speaker 1: And then you just want to make sure you have it on your um URL, API URLs. And then voila, I've pulled up my API. And you can see that under the geometry it has the point coordinates and it has my latitude and longitude of the schools that are called for my spatial data. And then here's that test point that I made. You can see that it's not 1, 2, 3, 4, 5, 6, 7, 8. It's actually negative 83 and 34. So once you create an API, you can use third-party websites such as Mapbox to display your data onto your website. Mapbox is free to use. There is a paying option.

12:43

Speaker 1: You can limit the size of your map and put constraints. So if you want your user to only look at a certain area, they can't scroll all the way to France or something. Like I said, it's free and you you just create the project. You pretty much Zoom into an area of interest. So if you're interested in Cuba, you just zoom into Cuba and it it saves it there for you. So when you open up your Django project, it's saved on that one map tile. So once you save your project in Mapbox, you just need to get the map ID that's underneath the project tab. So that's really the only thing you need. So now it's time to use the Mapbox

13:31

Speaker 1: API. You just need to open up your template page that you're going to be using to display your spatial data. And you just need these two lines right here. One's the Mapbox JavaScript and one's the Mapbox CSS. And I'm pretty sure the line of code is on the Mapbox website. And then of course you just want to have your uh map CSS there. And more map CSS. I'm going to point out that the variable our icon is going to be the icon that displays my spatial data on my map, such as the school points. So you can just mess around with that. And then underneath your CSS for your icon, this is where you want to put in your

14:16

Speaker 1: mapbox. map. with your username dot ID of that map box tile. Underneath that, this is where we get into the API that we created. So it's the JSON and then we have our API name, which was mine was saved as the API Georgia. And you want the GeoJSON pointing to your uh Georgia API so it knows what data to look for and display on the map If you're really into JavaScript, I highly recommend looking at leaflet. A lot of companies use leaflet to display their spatial data with their fancy um

15:02

Speaker 1: movements or icons, like you click on the point and it shows you my in my example would be like the name of the school, the students. You can have that hyperlinked to another profile. So if you're really into JavaScript, I highly recommend looking at Leaflet. So this is the finished project. This is what happens after I've loaded the data. I added a point on the map because I forgot something. I created my API and then I went on Mapbox and created a map tile and then put that into my uh template. You can now see that there are schools on my Georgia Mapbox tile. And if you look close enough, there is the imaginary school that's on an island in Lake Lanier. And what I was mentioning before with the JavaScript, uh You can tell that when you point or click on the point on the map it has the name of the school

15:51

Speaker 1: and m bound of students. So let's get fancy. You want to create something interactive, right? So last semester in my webmaster class, I teamed up with Crystal Lilliston and Will Cox. And we kind of messed around and created an interactive campus map for the University of North Georgia, Gainesville campus. Feel free to go to this website. It's, I'm warning you, it's still in the works. We only worked on it for one semester. It's ungmap. com. So um if you're going to the website, I recommend going to the Gainesville campus. Um when you go to the Gainesville campus It will display you our mat box tile of the Gainesville Campus School. We have a restriction on it, like I mentioned earlier. You can't go no more than like two miles northwest

16:39

Speaker 1: Uh east and south. Um just looking at this, I don't know how well you guys can see it, but the parking lots are grayed out. The buildings have 10, or not 10, the buildings have red , shape file, and then the emergency call boxes are blue points on the map. You can use the HTML5J location to hit find me and it works very well on the cell phones. I we tested it out in the science building and it had me right on the corner of the science building where I was standing on the second level. Um here's some zoomed-in detail of our shape file. I hand digitized all the parking lot

17:24

Speaker 1: spaces. Yeah, there are a lot of them. And you can keep zooming in. So that's just an example of some shape files you can make. So, like I said, my objective of this talk was to introduce you guys to what Geo Django is. how you can use it and hopefully we can promote some more ideas. Maps are great. I mean people love visuals. If you have data that can be placed on a map, put it on a map. So if you have a figure out a problem and solve it with a map, like I said, maps were used everywhere I'd like to give a special thanks to my professor, Zach Miller

18:11

Speaker 1: from the University of North Georgia. Obviously, I couldn't be here without him. We've had him for three semesters, and he does a lot with GIS and Django and Gojo. I want to thank Brandon Remiser for setting up the Linux machine for me to make this PowerPoint presentation. Crystal and Will for teaming up with me to make our interactive web map. And then uh DjangoCon US, the Django Software Foundation, for letting me be here today because I literally moved across country from Atlanta to Flag South, Arizona last or three weeks ago and I wouldn't have been here without them. And of course Anna for letting me use her laptop for today's talk. I will now take questions. We have 10 minutes left

19:06

Speaker 2: Hi.

19:07

Speaker 1: Hello.

19:08

Speaker 2: I I wonder if there might be some time during the conference to just kind of sit down and do a little mini install-a-thon or something like that to. If there's a time that people want to just try it.

19:22

Speaker 1: Unfortunately, no. I have class on Wednesday, so I'm leaving tomorrow night. We can maybe meet up sometime tomorrow And I can go over some of the stuff that you will need to install to get your Geojo up and running. And I can point you to great references that I used in the past.

19:41

Speaker 2: Do you want to pick a time and a place? Maybe where people can do it?

19:48

Speaker 1: If you uh follow me on Twitter, I'm sure I can pick up my busy schedule. And I'm sorry, by what? I should doodle? Oh is it? I haven't heard of that. Okay.

20:02

Speaker 3: Uh yeah. I was actually wondering and maybe you'll go over this. If you do the install a thon. But I was wondering how difficult it is to integrate this into an existing Django app or just to have a Django app like maybe that's A little bit separate because I know this one or when I was looking at it, um I haven't used it at all, but when I was looking at it you have to use a specific uh database engine for it. So is there a way to just integrate it in without making that the default engine or Um

20:30

Speaker 1: you'll need a spatial database to have this Geojle working. Um Are you using Postgres right now?

20:42

Speaker 3: Yeah, yeah.

20:43

Speaker 1: Yeah. Um I I'm not positive, but maybe you can try doing the create extension post-GIS. I'm I'm not positive on how that's going to work on your existing project Um, I mean you can try it, it may or may not work. It shouldn't affect the project. Cool. Okay. So there you go. All right. Yeah, like I mentioned, uh there's some points in the uh project that where you will need to add the dot GIS, but it shouldn't uh hurt your current project.

21:19

Speaker 4: Are there any handy um references or resources online to be able to find data for particular problems.

21:27

Speaker 1: Okay, where we can find spatial data? All right. The GIS Clearing House website has some data that we can use. If you're into roads, I highly recommend Tiger. If you're into CentS data, you want to go to NH N H GIS, the National Historic GIS. They have all sorts of census data, I think all the way into the 1800s. Uh trying to think of what other places we can get spatial data. I mean if you just Google something and put shapefile, you might be lucky Um some companies have their GIS data open to the public. If you can get uh

22:13

Speaker 1: data in an Excel sheet, you can put that into like a CSV. You can in put that into your uh GIS such as arc map and create a shapefile there and then put that onto your Geojle web app. So it's a multi-step process, but if you have the data, you can definitely put it on a map.

22:36

Speaker 5: Hello.

22:36

Speaker 1: Hey.

22:37

Speaker 5: Uh two questions. One, that admin weirdness. I'm from a developer point of perspective, that just seems like it's screaming for a pull request. Is it just something that needs to be done? You know, where you enter gibberish and latlong and click the map?

22:52

Speaker 1: Um I I think it's how the shapefile is set up because uh you have to have the lat and long on your model for it to pull in your shapefile.

23:07

Speaker 5: Okay.

23:07

Speaker 1: So

23:09

Speaker 5: something in the admin could be fixed where it's a good thing.

23:18

Speaker 1: Get that taken care of. Because like I mentioned earlier, I did another project where I had switched it. I put in the coordinates and then put a gibberish point on the map.

23:27

Speaker 5: Um related, I guess, about the shapefile. So you've got the loader for the shapefile to put all the stuff in the GIS database. Is there any way to with Geo Django or some other extension? To write a shapefile out? Because if you're adding more data into that database, can you

23:43

Speaker 1: Yeah, I guess a way that you can uh pull the GeoJSON information and um You can I guess then write a program for it to maybe using ArcPy to make a shapefile using the GeoJSON data with that. Okay.

23:58

Speaker 4: Thanks.

23:59

Speaker 1: You're welcome. Thank you. Thank you.

Questions this talk answers

What is GIS and what can you use it for?

GIS (Geographic Information Systems) lets you create, manipulate, and analyze geographic data, such as points, trails, campus maps, and urban-development maps.

Discussed at 1:47

What do I need to set up a GeoDjango project?

You need a spatial database; the speaker recommends PostgreSQL with the PostGIS extension. You also need to add `django.contrib.gis` to the project’s installed apps and provide spatial data such as a shapefile.

Discussed at 4:09

How do I load a shapefile into GeoDjango?

Put all of the shapefile’s associated files in a data directory, adapt the tutorial’s loading script so its mapping fields match your model, then import and run that script from the Python shell.

Discussed at 6:29

How can I add and edit geographic data in the Django admin?

Register the model with GeoDjango’s admin support, including `GeoModelAdmin`, and add the GIS URL configuration. The admin then provides a map for placing points or drawing polygons, which supplies the geometry and coordinates.

Discussed at 8:04

How do I expose GeoDjango spatial data through an API?

Set up a normal Django REST API using the GIS REST framework support and return the geometry as GeoJSON, along with the fields you want to expose, such as a school’s name and student count.

Discussed at 11:08

How do I display GeoDjango data on a website map?

Use a mapping library or service such as Mapbox: include its JavaScript and CSS, specify the map ID, and point the map at your GeoJSON API. Leaflet can add interactive behavior such as clickable points and popups.

Discussed at 12:43

Can I add GeoDjango to an existing Django app?

Yes, but the app needs a spatial database. With an existing PostgreSQL project, the speaker suggests enabling PostGIS and adding the required GIS settings; the GIS additions should not harm the existing project, though she was not certain about the exact migration path.

Discussed at 20:30

Where can I find free spatial data and shapefiles?

The speaker recommends the GIS Clearinghouse, TIGER for roads, and NHGIS for historical census data. Search engines, public company datasets, or converting spreadsheet data to CSV and then to a shapefile are other options.

Discussed at 21:27

How can I export GeoDjango data back to a shapefile?

The suggested approach is to retrieve the data as GeoJSON and write a program—potentially using ArcPy—to convert that GeoJSON into a shapefile.

Discussed at 23:43

Presenters

Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.

More videos from DjangoCon US