Commit d82cd8e9 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added function generate_country_industrial_cells, its tests, test files

parent 0e9f4756
LONGITUDE,LATITUDE,TAXONOMY,BUILDINGS,DWELLINGS,OCCUPANTS_PER_ASSET,COST_PER_AREA_EUR,ID_1,NAME_1,ID_2,NAME_2
12,50,BdgClassA,50,50,500,2000,1,State 1,11,Municipality 1
12,50,BdgClassB,30,30,400,2000,1,State 1,11,Municipality 1
12,50,BdgClassC,20,20,300,2000,1,State 1,11,Municipality 1
11.999,50.0082,BdgClassA,40,40,400,2000,1,State 1,11,Municipality 1
11.999,50.0082,BdgClassB,40,40,533,2000,1,State 1,11,Municipality 1
11.999,50.0082,BdgClassC,30,30,450,2000,1,State 1,11,Municipality 1
12.0082,50,BdgClassA,15,15,150,2000,1,State 1,11,Municipality 1
12.0082,50,BdgClassD,30,30,500,2000,1,State 1,11,Municipality 1
12.0084,50.0082,BdgClassA,20,20,200,2000,2,State 2,25,Municipality 5
12.0084,50.0082,BdgClassB,15,15,200,2000,2,State 2,25,Municipality 5
12.0084,50.0082,BdgClassC,25,25,375,2000,2,State 2,25,Municipality 5
12.0084,50.0082,BdgClassD,30,30,500,2000,2,State 2,25,Municipality 5
12.0162,50.0162,BdgClassB,45,45,600,2000,2,State 2,27,Municipality 7
12.0162,50.0162,BdgClassD,60,60,1000,2000,2,State 2,27,Municipality 7
\ No newline at end of file
LONGITUDE,LATITUDE,TAXONOMY,BUILDINGS,DWELLINGS,OCCUPANTS_PER_ASSET,COST_PER_AREA_EUR,ID_1,NAME_1,ID_2,NAME_2,ID_99,NAME_99
12,50,BdgClassA,50,50,500,2000,1,State 1,11,Municipality 1,CC_IND_1,CC_IND_1
12,50,BdgClassB,30,30,400,2000,1,State 1,11,Municipality 1,CC_IND_1,CC_IND_1
12,50,BdgClassC,20,20,300,2000,1,State 1,11,Municipality 1,CC_IND_1,CC_IND_1
11.999,50.0082,BdgClassA,40,40,400,2000,1,State 1,11,Municipality 1,CC_IND_0,CC_IND_0
11.999,50.0082,BdgClassB,40,40,533,2000,1,State 1,11,Municipality 1,CC_IND_0,CC_IND_0
11.999,50.0082,BdgClassC,30,30,450,2000,1,State 1,11,Municipality 1,CC_IND_0,CC_IND_0
12.0082,50,BdgClassA,15,15,150,2000,1,State 1,11,Municipality 1,CC_IND_2,CC_IND_2
12.0082,50,BdgClassD,30,30,500,2000,1,State 1,11,Municipality 1,CC_IND_2,CC_IND_2
12.0084,50.0082,BdgClassA,20,20,200,2000,2,State 2,25,Municipality 5,CC_IND_3,CC_IND_3
12.0084,50.0082,BdgClassB,15,15,200,2000,2,State 2,25,Municipality 5,CC_IND_3,CC_IND_3
12.0084,50.0082,BdgClassC,25,25,375,2000,2,State 2,25,Municipality 5,CC_IND_3,CC_IND_3
12.0084,50.0082,BdgClassD,30,30,500,2000,2,State 2,25,Municipality 5,CC_IND_3,CC_IND_3
12.0162,50.0162,BdgClassB,45,45,600,2000,2,State 2,27,Municipality 7,CC_IND_4,CC_IND_4
12.0162,50.0162,BdgClassD,60,60,1000,2000,2,State 2,27,Municipality 7,CC_IND_4,CC_IND_4
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CA_IND_0,12,50,11.9975,49.997,12.004075,50.0041,"POLYGON ((11.9975 50.0041, 12.004075 50.0041, 12.004075 49.997, 11.9975 49.997, 11.9975 50.0041))"
CA_IND_1,12,50.0082,11.9975,50.0041,12.004075,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.004075 50.0122555555556, 12.004075 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CA_IND_2,12.0081,50.0082,12.004075,50.0041,12.0122222222222,50.0122555555556,"POLYGON ((12.004075 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.0041, 12.004075 50.0041, 12.004075 50.0122555555556))"
CA_IND_3,12.0082,50,12.004075,49.997,12.0122222222222,50.0041,"POLYGON ((12.004075 50.0041, 12.0122222222222 50.0041, 12.0122222222222 49.997, 12.004075 49.997, 12.004075 50.0041))"
CA_IND_4,12.0162,50.0162,12.0122222222222,50.0122555555556,12.018,50.018,"POLYGON ((12.0122222222222 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CA_IND_0,12,50,11.9975,49.997,12.004075,50.0041,"POLYGON ((11.9975 50.0041, 12.004075 50.0041, 12.004075 49.997, 11.9975 49.997, 11.9975 50.0041))"
CA_IND_1,12,50.0082,11.9975,50.0041,12.004075,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.004075 50.0122555555556, 12.004075 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CA_IND_2,12.0081,50.0082,12.004075,50.0041,12.0122222222222,50.0122555555556,"POLYGON ((12.004075 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.0041, 12.004075 50.0041, 12.004075 50.0122555555556))"
CA_IND_3,12.0082,50,12.004075,49.997,12.0122222222222,50.0041,"POLYGON ((12.004075 50.0041, 12.0122222222222 50.0041, 12.0122222222222 49.997, 12.004075 49.997, 12.004075 50.0041))"
CA_IND_4,12.0162,50.0162,12.0122222222222,50.0122555555556,12.018,50.018,"POLYGON ((12.0122222222222 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CA_IND_0,12,50,11.9975,49.997,12.004075,50.0041,"POLYGON ((11.9975 50.0041, 12.004075 50.0041, 12.004075 49.997, 11.9975 49.997, 11.9975 50.0041))"
CA_IND_1,12,50.0082,11.9975,50.0041,12.004075,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.004075 50.0122555555556, 12.004075 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CA_IND_2,12.0081,50.0082,12.004075,50.0041,12.0122222222222,50.0122555555556,"POLYGON ((12.004075 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.0041, 12.004075 50.0041, 12.004075 50.0122555555556))"
CA_IND_3,12.0082,50,12.004075,49.997,12.0122222222222,50.0041,"POLYGON ((12.004075 50.0041, 12.0122222222222 50.0041, 12.0122222222222 49.997, 12.004075 49.997, 12.004075 50.0041))"
CA_IND_4,12.0162,50.0162,12.0122222222222,50.0122555555556,12.018,50.018,"POLYGON ((12.0122222222222 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CB_IND_0,12,50,11.9975,49.997,12.0040444444444,50.0041,"POLYGON ((11.9975 50.0041, 12.0040444444444 50.0041, 12.0040444444444 49.997, 11.9975 49.997, 11.9975 50.0041))"
CB_IND_1,12,50.0082,11.9975,50.0041,12.0046055555556,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.0046055555556 50.0122555555556, 12.0046055555556 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CB_IND_2,12.0081,50.0082,12.0046055555556,50.0041,12.0122222222222,50.0122555555556,"POLYGON ((12.0046055555556 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.0041, 12.0046055555556 50.0041, 12.0046055555556 50.0122555555556))"
CB_IND_3,12.0082,50,12.0040444444444,49.997,12.0122222222222,50.0041,"POLYGON ((12.0040444444444 50.0041, 12.0122222222222 50.0041, 12.0122222222222 49.997, 12.0040444444444 49.997, 12.0040444444444 50.0041))"
CB_IND_4,12.0162,50.0162,12.0122222222222,50.0122555555556,12.018,50.018,"POLYGON ((12.0122222222222 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CB_IND_0,12,50,11.9975,49.997,12.0040444444444,50.0041,"POLYGON ((11.9975 50.0041, 12.0040444444444 50.0041, 12.0040444444444 49.997, 11.9975 49.997, 11.9975 50.0041))"
CB_IND_1,12,50.0082,11.9975,50.0041,12.0051666666667,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.0051666666667 50.0122555555556, 12.0051666666667 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CB_IND_2,12.0081,50.0082,12.0040444444444,50.0041,12.0122222222222,50.0122555555556,"POLYGON ((12.0040444444444 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.0041, 12.0040444444444 50.0041, 12.0040444444444 50.0122555555556))"
CB_IND_3,12.0082,50,12.0040444444444,49.997,12.0122222222222,50.0041,"POLYGON ((12.0040444444444 50.0041, 12.0122222222222 50.0041, 12.0122222222222 49.997, 12.0040444444444 49.997, 12.0040444444444 50.0041))"
CB_IND_4,12.0162,50.0162,12.0122222222222,50.0122555555556,12.018,50.018,"POLYGON ((12.0122222222222 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CB_IND_0,12,50,11.9975,49.997,12.0040444444444,50.0041,"POLYGON ((11.9975 50.0041, 12.0040444444444 50.0041, 12.0040444444444 49.997, 11.9975 49.997, 11.9975 50.0041))"
CB_IND_1,12,50.0082,11.9975,50.0041,12.0051666666667,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.0051666666667 50.0122555555556, 12.0051666666667 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CB_IND_2,12.0081,50.0082,12.0040444444444,50.0041,12.0122222222222,50.0122555555556,"POLYGON ((12.0040444444444 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.0041, 12.0040444444444 50.0041, 12.0040444444444 50.0122555555556))"
CB_IND_3,12.0082,50,12.0040444444444,49.997,12.0122222222222,50.0041,"POLYGON ((12.0040444444444 50.0041, 12.0122222222222 50.0041, 12.0122222222222 49.997, 12.0040444444444 49.997, 12.0040444444444 50.0041))"
CB_IND_4,12.0162,50.0162,12.0122222222222,50.0122555555556,12.018,50.018,"POLYGON ((12.0122222222222 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122222222222 50.0122555555556, 12.0122222222222 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CC_IND_0,11.999,50.0082,11.9975,50.0041,12.0036555555556,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.0036555555556 50.0122555555556, 12.0036555555556 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CC_IND_1,12,50,11.9975,49.997,12.0041444444444,50.0041,"POLYGON ((11.9975 50.0041, 12.0041444444444 50.0041, 12.0041444444444 49.997, 11.9975 49.997, 11.9975 50.0041))"
CC_IND_2,12.0082,50,12.0041444444444,49.997,12.0122,50.0041,"POLYGON ((12.0041444444444 50.0041, 12.0122 50.0041, 12.0122 49.997, 12.0041444444444 49.997, 12.0041444444444 50.0041))"
CC_IND_3,12.0084,50.0082,12.0036555555556,50.0041,12.0125666666667,50.0122555555556,"POLYGON ((12.0036555555556 50.0122555555556, 12.0125666666667 50.0122555555556, 12.0125666666667 50.0041, 12.0036555555556 50.0041, 12.0036555555556 50.0122555555556))"
CC_IND_4,12.0162,50.0162,12.0122,50.0122555555556,12.018,50.018,"POLYGON ((12.0122 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122 50.0122555555556, 12.0122 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CC_IND_0,11.999,50.0082,11.9975,50.0041,12.0031666666667,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.0031666666667 50.0122555555556, 12.0031666666667 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CC_IND_1,12,50,11.9975,49.997,12.0041444444444,50.0041,"POLYGON ((11.9975 50.0041, 12.0041444444444 50.0041, 12.0041444444444 49.997, 11.9975 49.997, 11.9975 50.0041))"
CC_IND_2,12.0082,50,12.0041444444444,49.997,12.0122,50.0041,"POLYGON ((12.0041444444444 50.0041, 12.0122 50.0041, 12.0122 49.997, 12.0041444444444 49.997, 12.0041444444444 50.0041))"
CC_IND_3,12.0084,50.0082,12.0041444444444,50.0041,12.0125666666667,50.0122555555556,"POLYGON ((12.0041444444444 50.0122555555556, 12.0125666666667 50.0122555555556, 12.0125666666667 50.0041, 12.0041444444444 50.0041, 12.0041444444444 50.0122555555556))"
CC_IND_4,12.0162,50.0162,12.0122,50.0122555555556,12.018,50.018,"POLYGON ((12.0122 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122 50.0122555555556, 12.0122 50.018))"
\ No newline at end of file
id,LONGITUDE,LATITUDE,lon_w,lat_s,lon_e,lat_n,geometry
CC_IND_0,11.999,50.0082,11.9975,50.0041,12.0031666666667,50.0122555555556,"POLYGON ((11.9975 50.0122555555556, 12.0031666666667 50.0122555555556, 12.0031666666667 50.0041, 11.9975 50.0041, 11.9975 50.0122555555556))"
CC_IND_1,12,50,11.9975,49.997,12.0041444444444,50.0041,"POLYGON ((11.9975 50.0041, 12.0041444444444 50.0041, 12.0041444444444 49.997, 11.9975 49.997, 11.9975 50.0041))"
CC_IND_2,12.0082,50,12.0041444444444,49.997,12.0122,50.0041,"POLYGON ((12.0041444444444 50.0041, 12.0122 50.0041, 12.0122 49.997, 12.0041444444444 49.997, 12.0041444444444 50.0041))"
CC_IND_3,12.0084,50.0082,12.0041444444444,50.0041,12.0125666666667,50.0122555555556,"POLYGON ((12.0041444444444 50.0122555555556, 12.0125666666667 50.0122555555556, 12.0125666666667 50.0041, 12.0041444444444 50.0041, 12.0041444444444 50.0122555555556))"
CC_IND_4,12.0162,50.0162,12.0122,50.0122555555556,12.018,50.018,"POLYGON ((12.0122 50.018, 12.018 50.018, 12.018 50.0122555555556, 12.0122 50.0122555555556, 12.0122 50.018))"
\ No newline at end of file
......@@ -33,6 +33,7 @@ import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon
import shapely.wkt
import GDE_TOOLS_create_industrial_cells as gdet_cr_ind
......@@ -1291,3 +1292,190 @@ def test_get_relative_area_range():
function_out = gdet_cr_ind.get_relative_area_range(dummy_gdf)
assert np.isinf(function_out)
def test_generate_country_industrial_cells():
"""
The test reads both inputs and expected outputs from a series of data files.
Three ficticious input files are considered.
"""
# Path to data files:
pathname = os.path.join(
os.path.dirname(__file__), "data", "GDE_TOOLS_create_industrial_cells"
)
# Parameters common to all three ficticious cases:
col_lon = "LONGITUDE"
col_lat = "LATITUDE"
width_EW = 30.0 / (60.0 * 60.0) # 30 arcsec
width_NS = 30.0 / (60.0 * 60.0) # 30 arcsec
precision_points = 4
boundaries_type = "shp"
in_crs = "EPSG:4326"
consistency_tol_dist = 0.05
consistency_tol_area = 0.05
# Auxiliary dictionary definining the precision to use to convert coordinates into strings:
dec_precision_EW = int("{:E}".format(width_EW).split("-")[1])
dec_precision_NS = int("{:E}".format(width_NS).split("-")[1])
precision_cells = {}
precision_cells["lon_w"] = "{:.%sf}" % (dec_precision_EW)
precision_cells["lat_s"] = "{:.%sf}" % (dec_precision_NS)
precision_cells["lon_e"] = "{:.%sf}" % (dec_precision_EW)
precision_cells["lat_n"] = "{:.%sf}" % (dec_precision_NS)
# The three ficticious countries to test:
countries = ["Country_A", "Country_B", "Country_C"]
id_strings = ["CA_IND", "CB_IND", "CC_IND"]
# For each country, the following combinations of input parameters are considered:
consistency_options = [True, True, False]
autoadjust_options = [True, False, False]
# Expected output for simple variables for which the output is not defined by a data file:
expected_country_id = "278"
expected_overlap_found = np.array(
[
["False", "False", "Not_Checked"], # Country_A
["False", "True", "Not_Checked"], # Country_B
["False", "False", "Not_Checked"], # Country_C
]
)
expected_gap_found = np.array(
[
["False", "False", "Not_Checked"],
["False", "False", "Not_Checked"],
["False", "True", "Not_Checked"],
]
)
expected_big_dist_diff = np.array(
[
["False", "False", "Not_Checked"],
["False", "False", "Not_Checked"],
["False", "False", "Not_Checked"],
]
)
expected_big_area_diff = np.array(
[
["False", "False", "Not_Checked"],
["False", "False", "Not_Checked"],
["False", "False", "Not_Checked"],
]
)
# Run the tests:
for k, country in enumerate(countries):
expected_aggr_filepath = os.path.join(
pathname, "Exposure_Ind_%s_output.csv" % (country)
)
expected_aggr_mod_df = pd.read_csv(expected_aggr_filepath, sep=",")
for i in range(0, len(consistency_options)):
consistency_checks = consistency_options[i]
autoadjust_overlap_gap = autoadjust_options[i]
# Run the function:
res = gdet_cr_ind.generate_country_industrial_cells(
country,
col_lon,
col_lat,
width_EW,
width_NS,
id_strings[k],
precision_points,
precision_cells,
pathname,
pathname,
boundaries_type,
consistency_checks,
autoadjust_overlap_gap,
in_crs,
consistency_tol_dist,
consistency_tol_area,
)
(
function_cells_gdf,
function_aggr_mod_df,
function_overlap_found,
function_gap_found,
function_big_dist_diff,
function_big_area_diff,
function_country_id,
) = res
# Compare against expected results (simple variables):
assert function_overlap_found == expected_overlap_found[k, i]
assert function_gap_found == expected_gap_found[k, i]
assert function_big_dist_diff == expected_big_dist_diff[k, i]
assert function_big_area_diff == expected_big_area_diff[k, i]
assert function_country_id == expected_country_id
# Compare against expected updated aggregated model file:
numerical_columns = [
"LONGITUDE",
"LATITUDE",
"BUILDINGS",
"DWELLINGS",
"OCCUPANTS_PER_ASSET",
"COST_PER_AREA_EUR",
]
non_numerical_columns = [
"TAXONOMY",
"ID_1",
"NAME_1",
"ID_2",
"NAME_2",
"ID_99",
"NAME_99",
]
for col in numerical_columns:
np.testing.assert_allclose(
function_aggr_mod_df[col].values,
expected_aggr_mod_df[col].values,
rtol=0.0,
atol=1e-08,
)
for col in non_numerical_columns:
assert np.all(
function_aggr_mod_df[col].values == expected_aggr_mod_df[col].values
)
# Compare against expected final cell geometries and IDs:
expected_cells_filepath = os.path.join(
pathname, "expected_cells_%s_%s.csv" % (country, str(i))
)
aux_df = pd.read_csv(expected_cells_filepath, sep=",")
geoms = []
for poly in aux_df["geometry"].values:
geoms.append(shapely.wkt.loads(poly))
expected_cells_gdf = gpd.GeoDataFrame(aux_df, geometry=geoms, crs=in_crs)
assert np.all(function_cells_gdf["id"].values == expected_cells_gdf["id"].values)
for row in range(0, expected_cells_gdf.shape[0]):
np.testing.assert_allclose(
function_cells_gdf["geometry"].values[row].bounds,
expected_cells_gdf["geometry"].values[row].bounds,
rtol=0.0,
atol=1e-08,
)
np.testing.assert_allclose(
function_cells_gdf["lon_w"].values,
expected_cells_gdf["lon_w"].values,
rtol=0.0,
atol=1e-08,
)
np.testing.assert_allclose(
function_cells_gdf["lon_e"].values,
expected_cells_gdf["lon_e"].values,
rtol=0.0,
atol=1e-08,
)
np.testing.assert_allclose(
function_cells_gdf["lat_s"].values,
expected_cells_gdf["lat_s"].values,
rtol=0.0,
atol=1e-08,
)
np.testing.assert_allclose(
function_cells_gdf["lat_n"].values,
expected_cells_gdf["lat_n"].values,
rtol=0.0,
atol=1e-08,
)
Supports Markdown
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