Turning Coordinates Into a GeoPandas Shape

Greg Feliu
4 min readMar 1, 2020

Geo-plotting nearly always requires a shape object to show the boundaries of an area. In almost all cases, when trying to make a Geopandas shape you already have your data nicely formatted in a shape object. In almost all cases. Recently I had the unfortunate situation where the boundary points for a Polygon shape were simply coordinate pairs. How should one turn these into a Polygon shape? This seemingly simple question caused me much more pain that it should have. So, in order for you to avoid this same pain, allow me to explain how to transform coordinates into a shape object.

Goal: Map Population Density of NYC

This blog arose out of an attempt to make a map of the population density of the New York City (NYC) metro area. The goal of doing this was to shed more light on one of the main dividers of NYC neighborhoods. Are there clear boundaries between the boundaries of NYC and its suburbs? If one wanted to define the NYC metro area by population density, this map would be extremely helpful in making the distinction.

Photo credits to https://matadornetwork.com/destinations/north-america/united-states/new-york/

Data

Luckily for us, the uszipcode library is a fantastic source of information for zip codes in the US. In its simplest form it will give you the name, population density, area, etc. of a zipcode. If so desired, one can also get the coordinate boundaries needed to make a shapely object for mapping the zipcode. With a few lines of code I was able to collect the 500 zip codes in and around NYC.

from uszipcode import SearchEnginesearch = SearchEngine(simple_zipcode = False)
metro_nyc = search.by_coordinates(lat = 40.73, lng = -73.99, radius = 75, returns = 500)

This data was turned into a DataFrame with the zipcode itself as the index.

The only downside of this request, from my perspective, is the fact that the bordering coordinates are in a list, and not a shapely object. What’s a shapely object? It’s the basis for all points and shapes in Geopandas. After a deep search for information on how to do this, I eventually came across an answer in an issue raised in the shapely documentation. (Thank you so much!). In essence, you make each coordinate into a Point (the basic type for a shapely object) and combine these points into a Polygon object (also a shapely object).

import shapelypoint_list = [shapely.geometry.Point(x) for x in [lat, long]]
coords = [(p.x, p.y) for p in point_list]
polygon1 = shapely.geometry.Polygon(coords)
final_list.append(polygon1)

Once we have a Polygon object, we are able to use Geopandas to project the images(s). First, I added a column for these Polygon shape objects. Next, I converted the DataFrame into a GeoDataFrame and specified the column that contains the shapely objects (specified as ‘geometry’ in Geopandas). Once I did this, I was able to plot the objects simply by writing the name of the GeoDataFrame, and adding .plot(). In my example, I wanted to highlight the differences in population density across metro NYC, so I specified the “Density” column as the differentiator of the different shapes.

Population Density in and around NYC

Pretty neat, huh? The concentration of people in Manhattan is very apparent once one plots them. The gradient away from this center is in a somewhat odd pattern (not a circle with lower density rings from the center as in the Concentric Zone Model) because of the bodies of water in and around NYC and transportation links into NYC.

Since the differences are so extreme and there are only so many shades of red to use, we should probably transform the data so the differences are clearer. To do this, I’ll add a plot where the square root of the population density is taken so the extremes are less extreme, so to speak.

Slightly clearer picture of the differences in population density in an and around NYC

Here, the differences in population density within the suburbs are more clearly seen. The population density around NYC is certainly a complex and intricate issue. This map can be used as a starting point for that research!

Conclusion

I hope with this blog you can walk away knowing slightly more about the realities of making a geopandas plot. The details of making raw coordinates into a shapely object then into a geodataframe are not particularly hard, but are somewhat tricky. As long as you follow the steps outlined here, you can do this in no time!

--

--

Greg Feliu

Data Analyst | Data Engineer — Interests in language, sports, marketing and geographic visualizations