-
Notifications
You must be signed in to change notification settings - Fork 1
/
polygons_centroid.py
58 lines (40 loc) · 1.66 KB
/
polygons_centroid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import rasterio
import rasterio.mask
import numpy as np
import pandas as pd
import fiona
raster = "NGA_ppp_v2c_2020.tif"
shapefile = "nga_admbnda_adm1_osgof_20161215.shp"
destination = "test_G.csv"
def polygons_centroid(raster, shapefile, destination):
"""Calculates the weighted centroid of a list of polygons
Args:
raster: The raster file that countains the spatial weights (ex: Population raster)
shapefile: The shapefile that countains the list of polygons.
destination: The destination path for the resulting csv.
Returns:
A pandas DataFrame with the x, y coordinates of each polygon centroid.
"""
x = []
y = []
id = []
with rasterio.open(raster) as src:
with fiona.open(shapefile, "r") as shapefile:
for feature in shapefile:
features = feature["geometry"]
id.append(feature["id"])
out_image, out_transform = rasterio.mask.mask(src, [features], crop=True)
X_sum = out_image.sum(axis=1)[0, :]
X_ind = np.arange(len(X_sum))
i_centroid = int(np.average(X_ind, weights=X_sum))
Y_sum = out_image.sum(axis=2)[0, :]
Y_ind = np.arange(len(Y_sum))
j_centroid = int(np.average(Y_ind, weights=Y_sum))
x1, y1 = out_transform * (i_centroid, j_centroid)
x.append(x1)
y.append(y1)
print(x1, y1)
df = pd.DataFrame({"id": id, "x": x, "y": y})
df.to_csv(destination)
return df
polygons_centroid(raster, shapefile, destination)