Table of Contents
Introduction
The Mean Center is the average X coordinate and Y coordinate for all features in a study area and is the simplest descriptor of a geographic distribution. The Mean Center is generally used to track the changes in a features distribution over time and can also be used to compare the distribution of multiple features.
The Mean Center is also known as the Geographic Center or Center of Concentration for a set of features.
You would calculate the Mean Center for features where there is no travel interaction between the Center and the features of the study. Basically, use it for a study where each event that happens is a recorded location, for example a burglary for crime analysis, or the sighting of a wombat for wildlife studies.
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
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 Mean Center
The code below uses GeoPandas and Shapely to find the mean center for a dataset and create an output file. 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
## input shapefile path
in_shp = r"path\to\input\shapefile\input.shp"
## the output shapefile path for the mean center point
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
## 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()
## create a point geometry representing the mean center
mean_center = Point(mean_x, mean_y)
## create a GeoDataFrame with the point geometry
gdf_mean_center = gpd.GeoDataFrame(geometry=[mean_center], crs=crs)
## add fields for the XCoord and YCoord
gdf_mean_center["XCoord"] = mean_x
gdf_mean_center["YCoord"] = mean_y
## write the mean center point to the output shapefile
gdf_mean_center.to_file(out_shp, driver="ESRI Shapefile")
Mean Center 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.
Running the script produces a Shapefile that contains the Mean Center for all burglaries in 2023.
Below is a comparison between our GeoPandas tool and the Mean Center tool output from ArcGIS Pro. Spot on!
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...
- Central Feature
- Median Center
- Standard Distance
- Weighted Mean Center
- Weighted Central Feature
- Standard Distance by Case