Commit 40281e0e authored by Nicolas Garcia Ospina's avatar Nicolas Garcia Ospina
Browse files

Let geometry retrieval to be optional

parent 1613ba35
Pipeline #24913 passed with stage
in 5 minutes and 18 seconds
......@@ -15,6 +15,7 @@ obm_output_pathname: ./obm_results
import_pathname: ./results
number_cores: 1
batch_size: 1000
get_geometry: False
roads_database:
host: your_host.dir.request_data
......
......@@ -20,6 +20,7 @@
import os
import logging
import pandas
import geopandas
# Initialize log
logger = logging.getLogger(__name__)
......@@ -27,21 +28,45 @@ logger = logging.getLogger(__name__)
class FileProcessor:
@staticmethod
def write_tiles_to_csv_no_geom(list_of_dictionaries, output_pathname):
"""Write a csv file from a list of dictionaries without geometries.
def write_tiles_to_csv(
list_of_dictionaries,
output_pathname,
get_geometry=False,
column_geometry="built_area",
crs="epsg:4326",
):
"""Write a csv file from a list of dictionaries.
Args:
list_of_dictionaries (list): List of dictionaries with built-up areas to
write.
output_pathname (str): Target path name for the csv file.
get_geometry (bool): Set if the geometry will be written
column_geometry (str): Name of the field that contains geometries.
Default = "built_area"
crs (str): EPSG code of the data projection. Default = "epsg:4326"
"""
tiles_df = pandas.DataFrame(list_of_dictionaries)
tiles_df = tiles_df.drop_duplicates(keep="first")
filepath_out = os.path.join(
output_pathname,
"{}_{}.csv".format(tiles_df.quadkey.iloc[0], len(tiles_df.index)),
)
logger.info("Creating {}".format(filepath_out))
tiles_df.to_csv(filepath_out, index=False)
if get_geometry is False:
tiles_df = pandas.DataFrame(list_of_dictionaries)
tiles_df = tiles_df.drop_duplicates(keep="first")
filepath_out = os.path.join(
output_pathname,
"{}_{}.csv".format(tiles_df.quadkey.iloc[0], len(tiles_df.index)),
)
logger.info("Creating {}".format(filepath_out))
tiles_df.to_csv(filepath_out, index=False)
else:
tiles_gdf = geopandas.GeoDataFrame(
list_of_dictionaries, geometry=column_geometry, crs=crs
)
filepath_out = os.path.join(
output_pathname,
"{}_{}.csv".format(tiles_gdf.quadkey.iloc[0], len(tiles_gdf.index)),
)
logger.info("Creating {}".format(filepath_out))
tiles_gdf.to_csv(filepath_out, index=False)
......@@ -65,6 +65,8 @@ if args.import_csv:
target_db_config = config["target_database"]
import_pathname = os.path.abspath(config["import_pathname"])
get_geometry = config["get_geometry"]
def multiprocess_built_estimation_batch(quadkey_batch):
"""
......@@ -116,7 +118,9 @@ def multiprocess_built_estimation_batch(quadkey_batch):
if built_up_areas:
# Write output into a csv file
FileProcessor.write_tiles_to_csv_no_geom(built_up_areas, output_pathname)
FileProcessor.write_tiles_to_csv(
built_up_areas, output_pathname, get_geometry=get_geometry
)
del built_up_areas
roads_database.connection.close()
......@@ -164,7 +168,8 @@ def multiprocess_buildings_batch(quadkey_batch):
if obm_built_up_areas:
# Write output into a csv file
FileProcessor.write_tiles_to_csv_no_geom(
FileProcessor.write_tiles_to_csv(
get_geometry=get_geometry,
list_of_dictionaries=obm_built_up_areas,
output_pathname=obm_output_pathname,
)
......
......@@ -277,7 +277,8 @@ class TileProcessor:
associated to the Tile and a given DataSource.
Contains:
quadkey (str): Tile quadkey
source_id (int): Integer associated to a predefined method
source_id (int): Integer associated to a predefined method.
built_area (str): Polygon string projected to WGS84 coordinates.
built_area_size (float): Area measured in squared meters.
last_update (str): Date when the pickle was generated.
......@@ -301,6 +302,7 @@ class TileProcessor:
results = {
"quadkey": tile.quadkey,
"source_id": datasource.source_id,
"built_area": TileProcessor.reproject_polygon(built_polygon, tile.crs, "epsg:4326"),
"built_area_size": TileProcessor.albers_area_calculation(built_polygon, tile.crs),
"last_update": str(date.today()),
}
......
......@@ -36,6 +36,7 @@ setup(
"babelgrid",
"fiona",
"rtree",
"pandas",
"geopandas",
"rasterio",
"psycopg2-binary",
......
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