Proximity Analysis with Python

Proximity analysis is a way to analyze the locations of features by measuring the distance between them and other features in the area. The distance between point A and point B can be measured in a straight line or along a network path, such as a road network. In this article, I will introduce you to the proximity analysis with Python.

In this article, you will explore several proximity analysis techniques. In particular, you will learn to do things such as:

Also, Read – Interactive Maps with Python.

  • how to calculate the distance between points on a map, and
  • select all points within a radius of an entity.

Proximity Analysis with Python

I will start this task of proximity analysis with python by importing the necessary libraries:

import folium from folium import Marker, GeoJson from folium.plugins import HeatMap import pandas as pd import geopandas as gpd
Code language: JavaScript (javascript)

I will be working on the python proximity scan task using a US Environmental Protection Agency (EPA) dataset that tracks toxic chemical releases in Philadelphia, PA, USA. You can download the dataset from here. Now let’s read the data:

releases = gpd.read_file("toxic_release_pennsylvania.shp")
Code language: JavaScript (javascript)

I will also be using another dataset that contains readings from air quality monitoring stations in the same city. You can download this dataset from here. Let’s read this data:

stations = gpd.read_file("PhillyHealth_Air_Monitoring_Stations.shp")
Code language: JavaScript (javascript)

Calculating Distance

Now To calculate the distances between the points of two different Data Frames, we first need to make sure that they use the same Coordinate Reference System. Fortunately, this is the case here, where both use EPSG 2272:

print( print(
Code language: CSS (css)
{'init': 'epsg:2272'}
{'init': 'epsg:2272'}

We also check the CRS to see what units it uses. In our case, EPSG 2272 is in feet. It is relatively easy to calculate distances in GeoPandas:

# Select one release incident in particular recent_release = releases.iloc[360] # Measure distance from release to each station distances = stations.geometry.distance(recent_release.geometry) distances
Code language: PHP (php)
0     44778.509761
1     51006.456589
2     77744.509207
3     14672.170878
4     43753.554393
5      4711.658655
6     23197.430858
7     12072.823097
8     79081.825506
9      3780.623591
10    27577.474903
11    19818.381002
dtype: float64

Using the calculated distances we can get statistics like the average distance to each station:

print('Mean distance to monitoring stations: {} feet'.format(distances.mean()))
Code language: PHP (php)
Mean distance to monitoring stations: 33516.28487007786 feet

Creating a Buffer

If we want to understand all the points on a map that are within a certain radius of a point, the easiest way is to create a stamp. Let’s see how to do that:

two_mile_buffer = stations.geometry.buffer(2*5280) two_mile_buffer.head()
0    POLYGON ((2721944.640797138 257149.3104284704,...
1    POLYGON ((2682494.289907977 271248.9000113755,...
2    POLYGON ((2744886.638220146 280980.2466903776,...
3    POLYGON ((2703638.579968393 233247.1013432145,...
4    POLYGON ((2726959.772827223 251134.9763285518,...
dtype: object

We use folium.GeoJson() to plot each polygon on a map. Note that since the folium requires latitude and longitude coordinates, we need to convert the CRS to EPSG 4326 before plotting:

# Create map with release incidents and monitoring stations m = folium.Map(location=[39.9526,-75.1652], zoom_start=11) HeatMap(data=releases[['LATITUDE', 'LONGITUDE']], radius=15).add_to(m) for idx, row in stations.iterrows(): Marker([row['LATITUDE'], row['LONGITUDE']]).add_to(m) # Plot each polygon on the map GeoJson(two_mile_buffer.to_crs(epsg=4326)).add_to(m) # Show the map m
Code language: PHP (php)
image for post

Also, Read – Random Sampling with Python.

So this is how we can perform a task of Proximity Analysis with Python. I hope you liked this article on Proximity Analysis with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.

Follow Us:

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply