Commit 1ebb7357 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added exporting functions to GDE_TOOLS_create_industrial_cells.py

parent 28929517
......@@ -1567,3 +1567,68 @@ def generate_country_industrial_cells(
big_area_diff,
country_id,
)
def export_modified_aggregated_model_file(aggr_mod_df, out_pathname, out_filename, separ=","):
"""This file writes the aggr_mod_df DataFrame to a CSV file with name out_filename, under
the path out_pathname. If the file already exists, it will overwrite it. If the path does
not exist, it will create it.
Args:
aggr_mod_df (DataFrame): Pandas DataFrame to be exported.
out_pathname (str): Path to where the output CSV will be written.
out_filename (str): File name of the output CSV file.
separ: Separator to use in the CSV file (default=',').
"""
# Create out_pathname if it does not exist:
if not os.path.exists(out_pathname):
os.makedirs(out_pathname)
aggr_mod_df.to_csv(os.path.join(out_pathname, out_filename), sep=separ)
def export_cells_to_geodatafile(
country_name, country_id, cells_gdf, out_pathname, out_filename, out_driver="ESRI Shapefile"
):
"""This file creates a geospatial data file of format given by out_driver, with the IDs and
geometries of cells_gdf. Additional fields are written with the country name and ID. If the
file already exists, it will overwrite it. If the path does not exist, it will create it.
Args:
country_name (str): Name of country (as in the files of the aggregated model).
country_id (int): ID of the country as per the geospatial data files of the input
aggregated model.
cells_gdf (GeoDataFrame): GeoPandas GeoDataFrame with the cells geometry. This function
assumes that it contains at least the following columns:
- id: ID of the cell, given by id_str and an incremental
integer.
- geometry: (Shapely) polygons of the output cells.
out_pathname (str): Path to where the geospatial data file will be written.
out_filename (str): File name of the geospatial data file.
out_driver (str): "ESRI Shapefile", "GeoJSON", "GPKG", etc. Format of the output
geospatial data file.
"""
# Check that in_gdf has the needed columns and terminate otherwise:
if ("id" not in cells_gdf.columns) or ("geometry" not in cells_gdf.columns):
print("ERROR!! One or more of id, geometry missing as columns of cells_gdf")
return
# Create out_pathname if it does not exist:
if not os.path.exists(out_pathname):
os.makedirs(out_pathname)
# Start a new GeoDataFrame with the desired columns/features of cells_gdf:
shp_gdf = gpd.GeoDataFrame({}, geometry=cells_gdf["geometry"])
shp_gdf["ID_99"] = deepcopy(cells_gdf["id"])
shp_gdf["NAME_99"] = deepcopy(cells_gdf["id"])
# Add values of admin unit level 0:
id_0 = np.array([country_id for i in range(shp_gdf.shape[0])])
name_0 = np.array([country_name for i in range(shp_gdf.shape[0])])
shp_gdf["ID_0"] = id_0
shp_gdf["NAME_0"] = name_0
# Write to ShapeFile or other format (as per out_driver):
shp_gdf.to_file(os.path.join(out_pathname, out_filename), driver=out_driver)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment