Every day, millions of devices, sensors, and IoT devices paired with numerous satellites, remote sensing platforms, and analytical engines, generate exabytes of geospatial data. This is a result of big data combined with advancements in machine learning. This boom in rich datasets has enabled organizations and industry experts to build innovative products and capabilities.
Several companies provide mapping and site-inspection services with growing access to geospatial data. Legacy companies and startups are amassing large sets of highly conceptualized geo-environmental data from sensors to deliver the next innovation in smart mobility. Geo-environmental data is also being used to analyze climate risks, determine where and when it is safe to travel, and improve urban planning processes efficiently. As there are infinite applications to this dataset, it is an essential feat for stakeholders to understand exactly how it can be leveraged. Out of several ways to do it, the most easily accessible and simple method is using Geopandas. This article examines how to use the GeoPandas library to geographically restrict environmental data at Ambee.
How do you Restrict Services Based on Geographic Area Using GeoPandas?
GeoPandas is a geospatial data processing library for python based on pandas. GeoPandas is super helpful in processing shapefiles, GeoJSON data, plotting choropleth maps, and much more.
Let us assume that we run a service offered for a specific geographical location, while being unavailable for other areas. We need a way to restrict the incoming API requests to a particular geography. This is carried out using bounding boxes.
Two latitudes and two longitudes define a bounding box. It is a rectangle built using minimum longitude, minimum latitude, maximum longitude, and maximum latitude.
For this tutorial, we will use the example of France. An approximate bounding box for France is shown below.
The service needs to serve all the requests inside the bounding box and deny all requests outside the bounding box. This leads to an issue that the astute amongst you may have already observed.
Most countries are not perfect rectangles. Hence, some parts of England, Spain, Belgium. Germany, Switzerland, Andorra, and Luxembourg will fall inside the bounding box if we want to cover France completely. We also have part of the sea falling inside the bounding box.
Here is where Geopandas come in. Geopandas is a valuable library that can process geospatial data. We will make use of shapefiles and Geopandas to restrict requests to France.
Shapefiles are available online for all countries, continents, and states of major countries. They can also be created using tools like QGIS or ArcGIS.
Start by Importing the Libraries
import pandas as pd
import geopandas as gpd
We will now load the shapefile using the ‘read_file’ method in Geopandas. Let us see what the map looks like using the ‘plot’ method.
fr=gpd.read_file('FRA_adm0.shp')
fr.plot()
We can now use the ‘contains’ method to check if a pair of latitude and longitude is inside France. Let us take Paris for example.
Latitude and longitude coordinates for Paris are 48.864716, 2.349014.
We will now check if this pair of latitude and longitude is inside France. First, we need to express this pair of latitude and longitude as a ‘Point’ object. This can be done using the ‘shapely’ library. The Point object takes coordinates as a (Longitude, Latitude) pair.
Let us create a ‘Point’ object.
from shapely.geometry import Point
lng=2.349014
lat=48.864716
print(Point((lng,lat)))
POINT (2.349014 48.864716)
Now, we can use the ‘contains’ method to check if this point is inside France.
fr.contains(Point((lng,lat)))
0 True dtype: bool
Since Paris is inside France, the method returns True. Just for testing, let us now try coordinates for London.
lng=-0.118092
lat=51.509865
fr.contains(Point((lng,lat)))
0 False dtype: bool
Since the method returns a Boolean series, we can use this in the if-else statement to execute our service if the request is for a location inside France. The ‘contains’ method can take multiple points. Since we are only dealing with one point in our use case, we will use the ‘any’ method, which returns true if any of the points are inside France.
def execute_france(point):
if fr.contains(point).any():
#Your Code
print('Inside France')
else:
#Your Code
print('Outside France')
#Pass Paris Coordinates
execute_france(Point((lng,lat)))
Inside France
We see that the function executes the if part.
We can also add a buffer area around the shapefile using the ‘buffer’ method.
fr1=fr.buffer(0.5)
fr1.plot()
We can see that the method has added a buffer around our original shapefile. Since generating a buffer takes time, we can save this as a shapefile for future use.
fr1.to_file('fr_buffer.shp')
And That’s the End of the Tutorial
Ambee provides a range of rich datasets for different geospatial and climatic parameters. We have accurate, real-time weather, air quality, pollen, soil, and fire data available at a street-by-street level granularity for more than 90+ countries across the globe. Our clients come from all parts of the world and have different requests. The above method of restricting access to a geographical area with GeoPandas helps us accurately route the requests to appropriate locations and models.