AlexanderSvarfdal commited on
Commit
d3ea3dd
·
1 Parent(s): 6b881ab

Added get_density

Browse files
Files changed (1) hide show
  1. data_processing/get_density.py +25 -0
data_processing/get_density.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from typing import List, Tuple
3
+ from shapely.geometry import Polygon
4
+ from pyproj import Geod
5
+
6
+
7
+ def get_desity(polygon_coordinates: List[Tuple], population: int):
8
+ """
9
+ Calculate the population density of a polygon with geographic coordinates.
10
+
11
+ Args:
12
+ polygon_coordinates (List[Tuple[float, float]]): List of (longitude, latitude) coordinates defining the polygon.
13
+ population (int): Population within the polygon.
14
+
15
+ Returns:
16
+ float: Population density (population per square meter).
17
+ """
18
+ geod = Geod(ellps="WGS84")
19
+
20
+ area, _ = geod.polygon_area_perimeter(polygon_coordinates)
21
+ area = abs(area)
22
+ if area == 0:
23
+ raise ValueError("The polygon area is zero. Cannot calculate density.")
24
+
25
+ return population / area