Functions
Maths calculations used in the PyPSA-China workflow.
HVAC_cost_curve(distance)
Calculate the cost of HVAC lines based on distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distance
|
float
|
distance in km |
required |
Returns: float: cost in currency
Source code in workflow/scripts/functions.py
area_from_lon_lat_poly(geometry)
For shapely geometry in lon-lat coordinates, returns area in m^2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
Polygon
|
Polygon geometry in lon-lat coordinates. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
Area of the polygon in m^2. |
Source code in workflow/scripts/functions.py
cartesian(s1, s2)
Compute the Cartesian product of two pandas Series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s1
|
Series
|
first series |
required |
s2
|
Series
|
second series |
required |
Returns: pd.DataFrame: A DataFrame representing the Cartesian product of s1 and s2.
Examples:
>>> s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
>>> s2 = pd.Series([4, 5, 6], index=["d", "e", "f"])
>>> cartesian(s1, s2)
d e f
a 4 5 6
b 8 10 12
c 12 15 18
Source code in workflow/scripts/functions.py
get_poly_center(poly)
Get the geographic centroid of a polygon geometry.
Extracts the centroid coordinates from a polygon object, typically used for plotting and spatial analysis in geographic applications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
Polygon
|
A (shapely) polygon geometry object with a centroid attribute that has x and y coordinate arrays. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple containing (x, y) coordinates of the polygon centroid as floating point numbers. |
Example
from shapely.geometry import Polygon polygon = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) center = get_poly_center(polygon) print(center) (0.5, 0.5)
Note
This function assumes the polygon object has a centroid attribute with xy arrays containing coordinate data.
Source code in workflow/scripts/functions.py
haversine(p1, p2)
Calculate the great circle distance between two points on Earth.
Uses the Haversine formula to compute the shortest distance over the Earth's surface between two points specified in decimal degrees latitude and longitude. This is useful for calculating distances between geographic locations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p1
|
Point
|
location 1 in decimal deg |
required |
p2
|
Point
|
location 2 in decimal deg |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Great circle distance between the two points in kilometers. |
Example
from shapely.geometry import Point beijing = Point(116.4074, 39.9042) # longitude, latitude shanghai = Point(121.4737, 31.2304) distance = haversine(beijing, shanghai) print(f"Distance: {distance:.1f} km") Distance: 1067.1 km
Note
The function assumes the Earth is a perfect sphere with radius 6371 km.