As part of a project I was working on, I had to scrape data from Open Street Map. I scraped data from Lima, the city where I come from. As always with cleaning scraped data, the process can be painstaking. You can check it here.

Once having cleaned the data, I was ready for doing some exploratory analysis. As I was dealing with georeferenced data, I thought it would be appropriate to take advantage of it plotting some nice maps. How difficult could it be?

The answer, as with most of the things, is that it depends. GeoPandas makes working with shape files and geo data easier. However, there are some things I have learnt during the process. Here are some tips I wanted to share:

When plotting on a map chances are you will be dealing with shape files. A shape file is a number of files that basically contain the geometrical shapes that describe a location. For example, for my project, I used a shape file of Peru with all its districts (the smallest governmental unit.) You can download the file here.

GeoPandas makes importing the shape file really easy. You get back a data frame, just like in pandas. The interesting thing is that it comes with an extra columnn named geometry. This column contains all of the shapes related to a location.

Once you have your dataframe, you can proceed as you would do with any other dataset. For example, as I only wanted to work on the city of Lima and its related districts, I subset the data correspondingly.

Once there, what I should have done, is to know what the projection of my data was. Believe me, it would have saved me from a lot of pain! Basically, all of the 2D images of maps that we see use some kind of projection to translate the shape of the earth to a plane. I was really surprised to see how many projections there are. That lead to an obvious question: well, what kind of projection is used in this file. After some head scratching, I realized that GeoPandas comes with a method called .crs (reading the docs ahead could save you from this pain1 ), which will give you the type of projection being used. This will become very handy, trust me.

Having said that, there is no need to know what your projections are if all you want to do is a plot of the file. You simply need to do shp_lima.plot(). and voilà:

However, chances are that you want to do a little bit more than having an image of the location you are plotting. In my case, I wanted to use the data I scraped from Open Street maps to observe some patterns in the map.

To start with, the data I had collected from Open Street Map where a bunch of latitude and longitude points per observation. If you want to draw these points, you need to pass the appropriate shape figure to them. Once you have the appropriate shape figure you can pass it to a GeoPandas data frame.

Now you’d think: well I have my points and my shapes, let’s bring it all together into a nice map. Well, not really. Remember the thing about the coordinate system. The problem is that your recently created GeoPandas dataframe is coordinate system ignorant. You need to give it a proper coordinate system so the plotting runs smoothly. This means that both the data set, the one that contains your map and the one that has your points, should be in the same coordinate system. In my case, I used the WGS84 for both.

From then on, plotting is not that complicated. Here I am plotting the location of banks in the city of Lima.

Note that basically the plotting takes up one line. That’s really awesome.

## Merging Spatial Data

Afterwards, I wanted to have an idea of what was the concentration of banks per district. Having plotted the points on the map was interesting, however there was so much data that it made impossible to observe tendencies by districts. This is where merging comes in handy. So far, I just got a data set with the shapes of the districts and one with points of the banks. I needed a way to put everything together. I wanted all those banks that lie within the boundaries of a district to be matched to that particular district. This is fairly easy to do with GeoPandas sjoin() method.

sjoin() performs a spatial join. In my case, it basically checked if the points of the banks where within the boundaries of the districts’ shapes. Let me be more clear. For example, in my geobanks dataset, I have the following point belonging to a bank.

With this information, we don’t really know to which district this point belongs to. This is why we need to merge this data with the districts data.

Now, each bank is related to the district where its coordinate points lay on. You can see an example below.

Inspecting the map, you can see that there are multiple big districts on the periphery that do not seem to have many banks. Also, there are small districts within the city that seem very relevant. However, given that they are so small in comparison to these big districts on the periphery, we do not really get to have a clear picture of them.

If I wanted a close inspection of these smaller and central districts, I had two options: First, given that I had a list with the name of the districts, I could select the districts I wanted to include in my analysis. This would be definitely have been time consuming.

Another option was using GeoPandas methods. Checking the documentation, I noticed two interesting methods: centroid and distance(). The centroid method gives you the center of a Polygon. I used this method to calculate the center of San Isidro, Lima’s financial district. With this result, I used distance() to calculate the distance between the districts and this center. I then use this result to pick the districts within a certain distance.

From then on, plotting is simple. Here is the result:

I was really impressed with all the possibilities GeoPandas offers. You can check all the code in this notebook. I hope this has whet your appetite for plotting maps. At least for me, it definitely has. I see maps in my future!

1. Note that, at the time of writing, there were some broken links and images in the docs. However, this does not prevent a proper understanding of the library.