Measuring Geographic Distributions with GeoPandas: Standard Distance

Table of Contents

Introduction

The Standard Distance, also know as the Standard Distance Deviation, is the average distance all features vary from the Mean Center and measures the compactness of a distribution. The Standard Distance is a value representing the distance in units from the Mean Center and is usually plotted on a map as a circle for a visual indication of dispersion, the Standard Distance is the radius.

The Standard Distance works best in the absence of a strong directional trend. According to Andy Mitchell, if a directional trend is present you are better off using the Standard Deviational Ellipse.

You can use the Standard Distance to compare territories between species, which has the wider/broader territory, or to compare changes over time such as the dispersion of burglaries for each calendar month.

In a Normal Distribution you would expect around 68% of all points to fall within the Standard Distance.

Sources:
The Esri Guide to GIS Analysis, Volume 2: Spatial Measurements and Statistics.
An Introduction to Statistical Problem Solving in Geography

This course is designed to instill the basics of Python Programming by incrementally increasing your knowledge session-upon-session. In each section you will be given new material for a workbook to fill out and by the end of this course you will have your very own Python reference handbook. So how does this course have a GIS focus? Simple, most elements of the course have GIS and geospatial data in mind. Instead of using non-descript variables and values, we will use terms such as population, city, x_coord, y_coord, and so on. This will aid participants with pinpointing how they can relate geospatial data to Python. 

The Formula

The mean x-coordinate is subtracted from the x-coordinate value for each point and the difference is squared. The sum of all the squared values for x minus the x-mean is divided by the number of points. This is also performed for y-coordinates. The resulting values for x and y are summed and then we take the square root of this value to return the value to original distance units.

First we get the mean X and Y…

Mean Center formula

…and then the Standard Distance.

Standard Distance Formula

For Point features the X and Y coordinates of each feature is used, for Polygons the centroid of each feature represents the X and Y coordinate to use, and for Linear features the mid-point of each line is used for the X and Y coordinate.

Using GeoPandas to Calculate the Standard Distance

The code below represents previous endeavours from when we used GeoPandas and Shapely to find the mean center for a dataset. We require the mean center for the standard distance calculations. In our example we will use a Shapefile, but you can use any input and output filetypes that you have available with your GeoPandas setup. 

The code is heavily commented for ease of understanding the workflow. For a Point, we now we need get the mean value for all x values and y values. For a Polyline, we need to first get the midpoint of each line, and then get the mean value for all x values and y values for the midpoints. For a Polygon, we need to the the centroid value for each polygon, and then get the mean value for all x values and y values for the centroids.

				
					import geopandas as gpd
from shapely.geometry import Point
import math

## input shapefile path
in_shp = r"path\to\input\shapefile\input.shp"

## the output shapefile path for the standard distance polygon
out_shp = r"path\to\output\shapefile\output.shp"

## read in the shapefile to a GeoDataFrame
gdf = gpd.read_file(in_shp)

## get the geometry type from the first record
geom_type = gdf.geom_type[0]

## get the EPSG code
crs = gdf.crs

## required for the formula
num_features = len(gdf)

## for Point geometry
if geom_type == "Point":
    ## get all x and y values
    gdf["x"] = gdf.geometry.x
    gdf["y"] = gdf.geometry.y

## for LineString geometry
elif geom_type == "LineString":
    ## get all x and y values for the midpoints
    gdf["midpoint"] = gdf.geometry.interpolate(0.5, normalized=True)
    gdf["x"] = gdf["midpoint"].x
    gdf["y"] = gdf["midpoint"].y

## for Polygon geometry
elif geom_type == "Polygon":
    ## get all x and y values for the centroids
    gdf["centroid"] = gdf.geometry.centroid
    gdf["x"] = gdf["centroid"].x
    gdf["y"] = gdf["centroid"].y

## get the mean of the x and y values
mean_x = gdf['x'].mean()
mean_y = gdf['y'].mean()

## calculate the sum of the squared differences for x and y
sum_of_sq_diff_x = sum([math.pow(x - mean_x, 2) for x in gdf["x"]])
sum_of_sq_diff_y = sum([math.pow(y - mean_y, 2) for y in gdf["y"]])

## get the sum of the results
sum_of_results = (sum_of_sq_diff_x/num_features) + (sum_of_sq_diff_y/num_features)

## get the square root of the sum of the results to find the standard distance
standard_distance = math.sqrt(sum_of_results)

## create a point geometry representing the mean center
mean_center = Point(mean_x, mean_y)

## buffer the mean center by the standard distance
standard_distance_geom = mean_center.buffer(standard_distance)

## create a GeoDataFrame with the polygon geometry
gdf_standard_distance = gpd.GeoDataFrame(geometry=[standard_distance_geom], crs=crs)

## add fields for the CenterX, CenterY, and StdDist
gdf_standard_distance["CenterX"] = mean_x
gdf_standard_distance["CenterY"] = mean_y
gdf_standard_distance["StdDist"] = standard_distance

## write the standard distance polygon to the output shapefile
gdf_standard_distance.to_file(out_shp, driver="ESRI Shapefile")
				
			

Standard Distance in Action

I downloaded crime data from DATA.POLICE.CO.UK for all crimes in the West Midlands which were a series of CSV files. I further processed the data for burglaries only and you can downland the shapefile for the example below here

Birmingham Burglaries 2023

Running the script produces a Shapefile that contains the Standard Distance polygon for all burglaries in 2023

Standard Distance ArcGIS Pro GeoPandas Python Geostatistics

Below is a comparison between our GeoPandas tool and the Standard Distance tool output from ArcGIS Pro. Spot on!

GeoPandas Standard Distance Polygon
ArcGIS Pro Standard Distance Polygon

At Final Draft Mapping we provide comprehensive courses for automating tasks within ArcGIS Pro and ArcGIS Online with ArcPy and the ArcGIS API for Python. Courses range from beginner to advanced workflows and all paid courses provide extra support where you can ask questions. Automation within ArcGIS is a highly sought after skill, by adding these skills to your arsenal you are placing yourself at the forefront of that demand. 

We appreciate our blog readers, you can get 25% off any (non-sale) course at any time with the code FDMBLOG25

Also in this series

Leave a Comment

Your email address will not be published. Required fields are marked *