Using Python to check geographic coordinates on a KML map

My name is Egmara and I’m a Data Scientist at Delivery Center. Python is a language that I use a lot on a day-to-day basis and now, I’m going to show you an example of how this powerful language can be useful!

The need to check geographic coordinates

Delivery Center is a Brazilian company that acts as an integrating platform for Marketplaces such as iFood, Rappi and several others. Through this platform, sellers are able to manage their orders, coming from all these marketplaces in a single environment. Therefore, when we receive a new order it is important that the platform is able to recognize whether the geographic location (latitude and longitude, postal code, etc.) belongs to the established delivery area.

In many approaches, it is desirable to verify whether a geographic location belongs to a specific map file type called KML (Keyhole Markup Language). KML is a file format used to display geographic data and maps. In order to check coordinates of a latitude and longitude dataset in a map KML file, let’s use Python!

How can Python help you?

GeoPandas is an open source project to work with geospatial data in Python. Geometric data types are used, such as Point or Polygon. GeoPandas extends the datatypes used by Pandas to allow spatial operations on these geometric types. The manipulation and analysis of objects are performed by the Shapely library.

Walk-through

You can start by installing Pandas, GeoPandas and Shapely:

python3 -m pip install geopandas
python3 -m pip install

To import libraries, you can do:

import geopandas as gpd
from shapely.geometry import Point, Polygon
import pandas as pd

Suppose you have a map KML file, called ‘my_map.kml’, and a latitude and longitude dataset, called ‘lat_long_dataset.csv’.

Use GeoPandas to read the KML map and assign the result to an object:

gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'
my_map = gpd.read_file('my_map.kml', driver='KML')
my_map
Image for post
Image for post

The my_map object is a GeoDataFrame and the column called ‘geometry’ is a geometric data type, a Polygon.

type(my_map)geopandas.geodataframe.GeoDataFrame

To read the latitude and longitude dataset, use Pandas:

locations = pd.read_csv('lat_long_dataset.csv')
locations
Image for post
Image for post

We’re almost ready! Now, you need to convert each location data (latitude and longitude columns) into a geometric point:

geometric_points = []
for xy in zip(locations['longitude'], locations['latitude']):
geometric_points.append(Point(xy))

Add the geometric_points list to a column of a new GeoDataFrame:

geo_locations = gpd.GeoDataFrame(locations,
crs = {'init': 'epsg:4326'},
geometry = geometric_points)
geo_locations
Image for post
Image for post

To check which locations of the dataset belong to the region of the polygon (KML map), the contains() method is the answer! This method returns ‘True’ if a data type Point belongs to the region and ‘False’, otherwise. Create a new Boolean column in your GeoDataFrame:

for i in range(0, len(geo_locations)):
point = geo_locations.geometry.loc[i]
check = my_map.contains(point).values
geo_locations['in_my_map'].loc[i] = check
geo_locations.head(5)
Image for post
Image for post

Therefore, the GeoDataFrame has a column that indicates whether the geometrical points belong to the KML map or not. In addition, the number of points that belong to the map is:

print(sum(geo_locations.in_my_map == True))99

and the number of points that do not belong to the map is:

print(sum(geo_locations.in_my_map == False))1

Viewing the KML map and dataset with Folium!

According to the previous step, only one place does not belong to the KML zone. Let’s see it?

Folium is a very useful library for viewing maps in the GeoJSON format. We can convert our KML file, ‘my_map.kml’, into a GeoJSON, ‘my_map.geojson’ using this tool. Following some steps here, you can view the map and the dataset!

To import Folium and create an image, you can do:

import folium
fig = folium.Map(width=900, height=600)

Read ‘my_map.geojson’ and add it to the image:

geo_json_map = json.load(open('my_map.geojson'))
folium.Choropleth(
geo_data = geo_json_map,
fill_color = "steelblue",
fill_opacity = 0.4,
line_color = "steelblue",
line_opacity = 0.9
).add_to(fig)

Now, add the points from the dataset:

points = geo_locations[['latitude', 'longitude']].values.tolist()
for point in range(0, len(points)):
folium.Marker(locationlist[point]).add_to(fig)
fig
Image for post
Image for post

Nice! Indeed, we can see that only one point is outside our KML zone, according to GeoPandas in the previous section.

I hope this helped! Thanks!

Graduated in Industrial Mathematics | Data Scientist at Delivery Center|

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store