Table of Contents
Introduction
The Central Feature is the point that is the shortest distance to all other points in the dataset and thus identifies the most centrally located feature. The Weighted Central Feature considers weights when calculating the central feature, where points with higher weights have a larger influence on the result.
Sources:
The Esri Guide to GIS Analysis, Volume 2: Spatial Measurements and Statistics.
An Introduction to Statistical Problem Solving in Geography
The Formula
For each feature, calculate the distance to all other features and multiply each distance by the assigned weight. Get the sum of all the (distance x weigth). The feature with the minimal sum of weighted distances with the Weighted Central Feature.
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 Weighted Central Feature
The code below uses GeoPandas and Shapely to find the weighted central feature 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 and Polygon, we use the centroid. You could use the Point geometry itself for a Point shapefile, but in order to get the “total_distance” calculation on one line of code it was easier to assign the Point geometry to a column called “point” for each geometry type. For a Polyline, we use the midpoint of each line.
We calculate the distance from each point to all other points, multiplying each distance by a weight, and getting the sum of all distance * weight, and then find the point with the smallest summed distance * weight, this represents the weighted central feature. Although, there could be multiple features with the same smallest shortest-distance so we account for this.
Lastly, we export the central feature(s) from the original input Shapefile to a new Shapefile.
import geopandas as gpd
## input shapefile path
in_shp = r"path\to\input\shapefile\input.shp"
## the output shapefile path for the weighted central feature(s)
out_shp = r"path\to\output\shapefile\output.shp"
## the field that contains the numerical weight
weight_fld = "FIELD_NAME"
## 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]
## for Point and Polygon geometry get the centroid
if geom_type in ("Point", "Polygon"):
## get the centroid of each feature as a Point geometry
gdf["point"] = gdf.geometry.centroid
## for LineString geometry get the midpoint
elif geom_type == "LineString":
## get the midpoint of each line as a Point geometry
gdf["point"] = gdf.geometry.interpolate(0.5, normalized=True)
## calculate the weighted sum of distances for all points
gdf["weighted_sum_distance"] = gdf["point"].apply(lambda geom: (gdf["point"].distance(geom) * gdf[weight_fld]).sum())
## get the value of the minimum weighted sum distance
min_distance = gdf["weighted_sum_distance"].min()
## there could be multiple weighted central features with the same smallest
## cumulative distance
weighted_central_feature = gdf[gdf["weighted_sum_distance"] == min_distance]
## sanitize the weighted central feature(s) gdf and make ready for output
weighted_central_feature = weighted_central_feature.drop(columns=["point", "weighted_sum_distance"])
## write the weighted central feature(s) to the output shapefile
weighted_central_feature.to_file(out_shp, driver="ESRI Shapefile")
Weighted Central Feature in Action
Data for Primary School location was downloaded from the Department of Education (Ireland) and processed to contain Primary Schools in County Kildare in a projected coordinate system – Irish Transverse Mercator (EPSG:2157). You can download the Shapefile containing the data used below here.
Running the script produces a Shapefile that contains the Weighted Central Feature from the original Primary Schools Shapefile.
Below is a comparison between our GeoPandas tool and the Central Feature 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...
- Mean Center
- Central Feature
- Median Center
- Standard Distance
- Weighted Mean Center
- Mean Center by Case
- Standard Distance by Case