Fetch shapes
Data fetch operation for region/province/country shapes
build_nodes(prefectures, nodes_cfg)
Build the nodes, either directly at provincial (admin1) level or from adminlvk2 subregions
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefectures
|
GeoDataFrame
|
|
required |
Source code in workflow/scripts/fetch_shapes.py
cut_smaller_from_larger(row, gdf, overlaps)
Automatically assign overlapping area to the smaller region
Example
areas_gdf.apply(cut_smaller_from_larger, args=(areas_gdf, overlaps), axis=1)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row
|
GeoSeries
|
the row from pandas apply |
required |
gdf
|
GeoDataFrame
|
the geodataframe on which the operation is performed |
required |
overlaps
|
DataFrame
|
the boolean overlap table |
required |
Raises:
Type | Description |
---|---|
ValueError
|
in case areas are exactly equal |
Returns:
Type | Description |
---|---|
GeoSeries
|
gpd.GeoSeries: the row with overlaps removed or not |
Source code in workflow/scripts/fetch_shapes.py
eez_by_region(eez, province_shapes, prov_key='region', simplify_tol=0.5)
Break up the eez by admin1 regions based on voronoi polygons of the centroids
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eez
|
GeoDataFrame
|
description |
required |
province_shapes
|
GeoDataFrame
|
description |
required |
prov_key
|
str
|
name of the provinces col in province_shapes. Defaults to "region". |
'region'
|
simplify_tol
|
float
|
tolerance for simplifying the voronoi polygons. Defaults to 0.5. |
0.5
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: description |
Source code in workflow/scripts/fetch_shapes.py
fetch_country_shape(outp_path)
Fetch the country shape from natural earth and save it to the outpath
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outp_path
|
PathLike
|
the path to save the country shape (geojson) |
required |
Source code in workflow/scripts/fetch_shapes.py
fetch_gadm(country_code='CHN', level=2)
fetch GADM shapefile for a given country and administrative level. https://gadm.org/download_country.html
Parameters:
Name | Type | Description | Default |
---|---|---|---|
country_code
|
str
|
ISO3 country code (e.g., 'CHN', 'USA'). |
'CHN'
|
level
|
int
|
Administrative level (0=country, 1=region, etc.). |
2
|
Returns:
Type | Description |
---|---|
geopandas.GeoDataFrame: Loaded shapefile as GeoDataFrame. |
Source code in workflow/scripts/fetch_shapes.py
fetch_maritime_eez(zone_name)
Fetch maritime data for a country from Maritime Gazette API# (Royal marine institute of Flanders data base)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
zone_name
|
str
|
the country's zone name, e.g "Chinese" for china |
required |
Raises:
Type | Description |
---|---|
HTTPError
|
if the request fails |
Returns: dict: the maritime data
Source code in workflow/scripts/fetch_shapes.py
fetch_natural_earth_shape(dataset_name, filter_key, filter_value='China', region_key=None)
Fetch region or country shape from natural earth dataset and filter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name
|
str
|
the name of the natural earth dataset to fetch |
required |
filter_key
|
str
|
key to filter the records by |
required |
filter_value
|
str | list
|
filter pass value. Defaults to "China". |
'China'
|
Example
china country: build_natural_earth_shape("admin_0_countries", "ADMIN", "China") china provinces: build_natural_earth_shape("admin_1_states_provinces", "iso_a2", "CN", region_key="name_en")
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: the filtered records |
Source code in workflow/scripts/fetch_shapes.py
fetch_prefecture_shapes(fixes={GDAM_LV1: {'Nei Mongol': 'InnerMongolia', 'Xinjiang Uygur': 'Xinjiang', 'Hong Kong': 'HongKong', 'Ningxia Hui': 'Ningxia'}})
Fetch county-level shapefiles for China.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fixes
|
(dict, Optional)
|
Dictionary mapping old names to new names for specific columns. |
{GDAM_LV1: {'Nei Mongol': 'InnerMongolia', 'Xinjiang Uygur': 'Xinjiang', 'Hong Kong': 'HongKong', 'Ningxia Hui': 'Ningxia'}}
|
Source code in workflow/scripts/fetch_shapes.py
fetch_province_shapes()
Fetch the province shapes from natural earth and save it to the outpath
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: the province shapes |
Source code in workflow/scripts/fetch_shapes.py
has_overlap(gdf)
Check for spatial overlaps across rows
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf
|
GeoDataFrame
|
the geodataframe to check |
required |
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
Index x Index boolean dataframe |
Source code in workflow/scripts/fetch_shapes.py
remove_overlaps(gdf)
Remove inter row overlaps from a GeoDataFrame, cutting out the smaller region from the larger one
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf
|
GeoDataFrame
|
the geodataframe to be treated |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: the treated geodataframe |
Source code in workflow/scripts/fetch_shapes.py
split_provinces(prefectures, node_config)
Split Inner Mongolia into East and West regions based on prefectures.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefectures
|
GeoDataFrame
|
Gall chinese prefectures. |
required |
node_config
|
dict
|
the configuration for node build |
required |
Returns: gpd.GeoDataFrame: Updated GeoDataFrame with Inner Mongolia split EAST/WEST.
Source code in workflow/scripts/fetch_shapes.py
validate_split_cfg(split_cfg, gdf)
validate the province split configuration. The province (admin level 1) is split by admin level 2 {subregion: [prefecture names],..}. The prefecture names must be unique and cover all admin2 in the admin1 level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
split_cfg
|
dict
|
the configuration for the prefecture split |
required |
gdf
|
GeoDataFrame
|
the geodataframe with prefecture shapes |
required |
Raises: ValueError: if the prefectures are not unique or do not cover all admin2 in the admin1 level