# 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:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```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(stations.crs)
print(releases.crs)```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)
```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)
for idx, row in stations.iterrows():

# Plot each polygon on the map