Creating and working with map polygons using Google’s My Maps and Python
Published 2021-02-27
Table of Contents
In this (very) short blog post I will be discussing how we can use Google’s My Maps to draw a polygon on a map and export it. I will then show you how to load this exported map polygon into python and check if a given point lies within the polygon (a point-in-polygon calculation) calculation.
This sort of thing can be incredibly useful if you are building any app/script that will pull latitude and longitude information from an API and make some decision based on that. An example of this would be scraping data from a property listings website, and generating alerts for properties which fall within an area you want to live.
If you would prefer a more interactive version of this tutorial I have created a Jupyter notebook version of this tutorial in my GitHub repo.
Create a map polygon using My Maps
Before we get started with the Python side of things, we will begin by drawing a polygon on a map using My Maps. To Start we can use the “draw a line” option on the top left of the map to draw a basic rectangle around the area you want to draw your polygon on.

From here, you can use the white circles along the line to reshape the rectangle into a polygon of your choosing. In my example below I have drawn a polygon around an area surrounding Dublin City Centre.

Once you are happy with your polygon you can export the map layer using the “Export to KML/KMZ” button.

Note: Be sure to check the “Export as KML instead of KMZ” option when exporting your polygon layer.

Some installations
Now that we have our map polygon created and saved locally, we are ready to get stuck into the coding side of things.
Since we are dealing with geometric data, we will need to install some libraires to assist us with understanding the data, the first library we will need to install is GDAL. I will not cover the entire installation process for this library here, however instructions can be found for your operating system at the following locations:
Once GDAL is installed, you will also need to install the osgeo and geotable packages however these can be easily installed via pip.
Load the polygon from KML using Geotable
Once we have the installs done, we can import our .kml file using the geotable.load() function. This will load the polygon shape data, as well as some metadata about our layer into a GeoTable object
import geotable
t = geotable.load('map_polygon.kml')Check point in polygon
Now that we have our polygon loaded, we can perform some simple point-in-polygon calculations. To do this we will use the shapely library to create a Point object. From here we can use the contains method of our polygon to check if the point we passed in are inside our polygon.
It’s worth noting that when creating the Point object it expects the input in long, lat (x, y) format instead of lat, long y, x which is often returned when searching for coordinates.
from shapely.geometry import Point
sample_location_in_polygon = Point(-6.263387, 53.344357) # Dublin city
sample_point_not_in_polygon = Point(-6.929850, 53.642509) # Random location in Meath
print(f"Dublin point in poly: {t.geometries[0].contains(sample_location_in_polygon)}")
print(f"Meath point in poly: {t.geometries[0].contains(sample_point_not_in_polygon)}")
# Output
# Dublin point in poly: True
# Meath point in poly: False