Measuring Distance

In spatial analysis, we often want to know the shortest distance between two features. For example, we may want to know the distance from residence to pharmacy store to see if the distance affects people’s health. Or, we may be interested in whether the distance to airport or highway affects population growth. In this example, we will measure the nearest distance from airport in Pennsylvania, USA.

As usual, we need to import libraries we will be using.

[1]:
%matplotlib inline

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt

Then, we load the example data and airport data and explore the data by plotting them together.

[2]:
df = pd.read_csv('../data/example.csv')
[3]:
from gps2space import geodf
gdf = geodf.df_to_gdf(df, x='longitude', y='latitude')
gdf.head()
[3]:
pid timestamp latitude longitude geometry
0 P2 2020-04-27 10:42:22.162176000 40.993799 -76.669419 POINT (-76.66942 40.99380)
1 P2 2020-06-02 01:12:45.308505600 39.946904 -78.926234 POINT (-78.92623 39.94690)
2 P2 2020-05-08 23:47:33.718185600 41.237403 -79.252317 POINT (-79.25232 41.23740)
3 P2 2020-04-26 14:31:12.100310400 41.991390 -77.467769 POINT (-77.46777 41.99139)
4 P2 2020-03-31 15:53:27.777897600 41.492674 -76.542921 POINT (-76.54292 41.49267)
[4]:
airport = gpd.read_file('../data/paairport.shp')
airport.head()
[4]:
STATE NAME geometry
0 Pennsylvania Erie International POINT (-80.17600 42.08208)
1 Pennsylvania Bradford Regional POINT (-78.63987 41.80313)
2 Pennsylvania Venango Regional POINT (-79.86014 41.37793)
3 Pennsylvania Wilkes-Barre/Scranton International POINT (-75.72390 41.33823)
4 Pennsylvania Williamsport Regional POINT (-76.92144 41.24205)
[5]:
pacounty = gpd.read_file('../data/pacounty.shp')
[6]:
ax = pacounty.boundary.plot(figsize=(12, 12), edgecolor='black', linewidth=0.6)
gdf.plot(ax=ax, color='r')
airport.plot(ax=ax, color='g', marker='*', markersize=60)

plt.show();
_images/measuring_distance_9_0.png

The red dots are the footprints of Person 1 (P1) and Person 2 (P2) while the green stars are the airports in Pennsylvania, USA.

We can calculate the distance from each point of P1 and P2 to the nearest airport using the dist_to_point function in the dist module. The dist_to_point function takes three parameters:

  • gdf_a: This is the GeoDataFrame of P1 and P2’s footprints
  • gdf_b: This is the landmark from where you want to measure the distance
  • proj: This is the EPSG identifier you want to use to project your spatial data and will be applied to gdf_a and gdf_b

Because the airport data come from other source, we do not know if it has been projected or what is the projection system. So we want to check the projection system for airport data.

[7]:
airport.crs

It returns nothing, which means this data do not have projection. We will give it an initial projection of EPSG:4326.

[8]:
airport.crs = ("epsg:4326")

Now, we can import the dist function to calculate the distance from each point of P1 and P2 to the nearest airport.

[9]:
from gps2space import dist
[10]:
dist_to_airport = dist.dist_to_point(gdf, airport, proj=2163)
[11]:
dist_to_airport.head()
[11]:
pid timestamp latitude longitude geometry STATE NAME dist2point
0 P2 2020-04-27 10:42:22.162176000 40.993799 -76.669419 POINT (1926745.083 -169042.499) Pennsylvania Williamsport Regional 34579.711173
1 P2 2020-06-02 01:12:45.308505600 39.946904 -78.926234 POINT (1774126.223 -333525.438) Pennsylvania Johnstown-Cambria County 42187.331826
2 P2 2020-05-08 23:47:33.718185600 41.237403 -79.252317 POINT (1712951.727 -200231.269) Pennsylvania Du Bois-Jefferson County 30051.080354
3 P2 2020-04-26 14:31:12.100310400 41.991390 -77.467769 POINT (1833671.313 -79623.054) Pennsylvania Williamsport Regional 94804.346362
4 P2 2020-03-31 15:53:27.777897600 41.492674 -76.542921 POINT (1921659.985 -112174.444) Pennsylvania Williamsport Regional 42388.435141

The dist2point column represents the distance from each point to the nearest airport measured in meters. Likewise, you can then save the GeoDataFrame to a spatial dataset or non-spatial dataset as we did in the last section.