File size: 864 Bytes
d3ea3dd
 
 
f33a0f8
d3ea3dd
f33a0f8
d3ea3dd
 
 
 
 
 
 
 
 
 
f33a0f8
 
 
 
 
 
d3ea3dd
 
f33a0f8
10fa920
 
f33a0f8
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

from typing import List, Tuple
from pyproj import Geod
from shapely.geometry import Polygon

def get_density(polygon_coordinates: List[Tuple[float, float]], population: int) -> float:
    """
    Calculate the population density of a polygon with geographic coordinates.

    Args:
        polygon_coordinates (List[Tuple[float, float]]): List of (longitude, latitude) coordinates defining the polygon.
        population (int): Population within the polygon.

    Returns:
        float: Population density (population per square meter).
    """
    if polygon_coordinates[0] != polygon_coordinates[-1]:
        polygon_coordinates.append(polygon_coordinates[0])

    polygon = Polygon(polygon_coordinates)

    area = polygon.area
    if area == 0:
        raise ValueError("The polygon area is zero. Cannot calculate density.")

    return population / area