#!/usr/bin/env python3 """Create a database containing all S-57 charts in the given directory. Optionally simplifies the geometries before saving them. See "S57ChartHandler" for supported nautical chart features. """ # Standard library from argparse import ArgumentDefaultsHelpFormatter from argparse import ArgumentParser # Typing from typing import Optional # Progress bar from tqdm import tqdm # Database and charts from pyrate.common.charts import S57ChartHandler from pyrate.common.charts import SpatialiteDatabase def create_db(path_to_raw_charts: str, path_to_db: str, simplify_tolerance: Optional[float] = None) -> None: """Creates a database from all charts in the given directory. Args: path_to_raw_charts: the path where to look for the source chart files path_to_db: the file of the target database simplify_tolerance: the tolerance within all new points shall lie wrt. to the old ones, in meters, non-negative """ files = list(S57ChartHandler.find_chart_files(path_to_raw_charts)) print("Scanned for relevant files") with SpatialiteDatabase(path_to_db) as database: with database.disable_synchronization(): if len(database) != 0: raise RuntimeError("writing to an already existing database, which might be an error") print("Created database") handler = S57ChartHandler() for file in tqdm(files, unit=" files"): database.write_geometries(handler.read_chart_file(file), update=False, raise_on_failure=False) if simplify_tolerance is not None: vertices_before = database.count_vertices() database.simplify_contents(simplify_tolerance) vertices_after = database.count_vertices() change = (vertices_before - vertices_after) / vertices_before * 100 print(f"Reduced the vertex count from {vertices_before} to {vertices_after} (-{change:.3f}%)") def _main() -> None: """The main function.""" parser = ArgumentParser(description=__doc__, formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument("path_to_raw_charts", type=str, help="will be searched recursively") parser.add_argument("path_to_db", type=str, help='usually ends with ".sqlite"') parser.add_argument( "--simplify_tolerance", type=float, default=25.0, help="the simplification tolerance in meters, set to zero to disable", ) args = parser.parse_args() simplify_tolerance = None if args.simplify_tolerance == 0.0 else args.simplify_tolerance create_db(args.path_to_raw_charts, args.path_to_db, simplify_tolerance) if __name__ == "__main__": _main()