Skip to content
Snippets Groups Projects

Add basic program structure and first use case

Merged Danijel Schorlemmer requested to merge basic_structure into main
Files
2
@@ -17,5 +17,108 @@
# along with this program. If not, see http://www.gnu.org/licenses/.
def main():
pass
import logging
import sys
import argparse
import configparser
import datetime
from exposurelib import SpatiaLiteExposure
# Add a logger printing error, warning, info and debug messages to the screen
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(sys.stdout))
def command_line_interface():
"""
Command-line interface of the exposure-share program. The first command-line option is the
command defining what type of extract will be created:
`full`: Full exposure will be extracted. This includes level-18 tile entities and
building entities.
`tile`: Exposure will be extracted at level-18 quad-tree tiles by aggregating all
entities (`building` and `residual`) within each tile.
The command require these mandatory options:
`-e`: Filepath of the exposure database.
`-c`: Config file (INI-type) describing the access to the tile database. Also needed
for `local` as the boundaries and tiles will be copied over to the local
SpatiaLite database.
`-i`: ISO 3166-1 alpha-3 code of the country for which the exposure is to be imported.
"""
# Create the argument parser
parser = argparse.ArgumentParser(description="Exposure-Share")
parser_shared = argparse.ArgumentParser(add_help=False)
parser_shared.add_argument(
"-i",
"--country-iso-code",
required=True,
type=str,
help="ISO 3166-1 alpha-3 code of the country",
)
parser_shared.add_argument(
"-e",
"--exposure-database",
required=True,
type=str,
help="Filepath to the SpatiaLite exposure database",
)
parser_shared.add_argument(
"-c",
"--config-file",
required=True,
type=str,
help="Filepath of the config file for accessing the Tiles database",
)
# Prepare for subparsers
subparsers = parser.add_subparsers(help="sub-command help", dest="command")
# Create the parser for the 'full' command
subparsers.add_parser(
"full",
help="Full exposure will be extracted. "
+ "This includes level-18 tile entities and building entities.",
parents=[parser_shared],
)
# Create the parser for the 'tile' command
subparsers.add_parser(
"tile",
help="Exposure will be extracted at level-18 quad-tree tiles "
+ "by aggregating all entities (`building` and `residual`) within each tile.",
parents=[parser_shared],
)
# Read arguments from command line.
args = parser.parse_args()
postgis_config = configparser.ConfigParser()
postgis_config.read(args.config_file)
# Initialize database
output_database_object = SpatiaLiteExposure(args.exposure_database)
output_database_object.connect()
output_database_object.create_tables()
exposure_source_db_config = postgis_config["Exposure"]
obm_source_db_config = postgis_config["OBM"]
start_time = datetime.datetime.now()
logger.info("Start time: %s" % start_time)
if args.command == "full":
output_database_object.import_from_postgis(
exposure_source_db_config,
obm_source_db_config,
country_iso_code=args.country_iso_code,
copy_buildings=False,
)
elif args.command == "tile":
raise ValueError("Option `tile` not yet implemented")
else:
raise ValueError(f"Unknown option `{args.command}`")
logger.info("Execution time: %s" % (datetime.datetime.now() - start_time))
if __name__ == "__main__":
command_line_interface()
Loading