diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2023-09-28 00:33:05 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2023-09-28 00:44:55 +0200 |
commit | 9113d206d35a3f74733e1cbc216037ff8907ad0a (patch) | |
tree | c106baa097e8c67298bd549efca2c6ede764aca3 | |
parent | 8cd26788627f44f100d5fd0f7bee0299c71f505e (diff) |
argparse: Use type=Path.
-rwxr-xr-x | gis-observation-map | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/gis-observation-map b/gis-observation-map index e02eef5..196fea1 100755 --- a/gis-observation-map +++ b/gis-observation-map @@ -61,6 +61,9 @@ config.read(Path(xdg_config_home()).joinpath(programName).joinpath('config')) if 'QGIS' not in config.keys() or config['QGIS'] is None: config['QGIS'] = {} +def ePath(v): + return Path(v).expanduser() + parser = argparse.ArgumentParser( description='Create a QGIS project with observations from Artdatabanken.', prog = programName, @@ -68,13 +71,13 @@ parser = argparse.ArgumentParser( %(prog)s {--geometry=FILE|--point=X,Y} ...''' ) parser.add_argument('--margin', type=int, help='Margin (in meters) from geometry envelopes') -parser.add_argument('--topo-basedir', default=config['QGIS']['topo-basedir'], help='Base directory for "Topografi 10", "Topografi 50", etc.') +parser.add_argument('--topo-basedir', default=config['QGIS']['topo-basedir'], type=ePath, help='Base directory for "Topografi 10", "Topografi 50", etc.') parser.add_argument('--project-name', help='Project filename (and title)') -parser.add_argument('--project-home', help='Project home directory') -parser.add_argument('--geometry', nargs='*', default=[], help='Geometry filename(s)') +parser.add_argument('--project-home', type=ePath, help='Project home directory') +parser.add_argument('--geometry', nargs='*', default=[], type=ePath, help='Geometry filename(s)') parser.add_argument('--point', nargs='*', metavar='X,Y', default=[], help=f'Coordinates of interest (in {target_srs.GetName()})') parser.add_argument('--observation-format', default='GPKG', help='Format for the observation file (default: %(default)s)') -parser.add_argument('--style', default=config['QGIS']['style'], help='QGIS Layer Style File (*.qml) to apply to the observation layer (default: %(default)s)') +parser.add_argument('--style', default=config['QGIS']['style'], type=ePath, help='QGIS Layer Style File (*.qml) to apply to the observation layer (default: %(default)s)') args = parser.parse_args() @@ -83,11 +86,8 @@ if args.project_home is not None and args.project_name is not None: qgs = QgsApplication([], False) qgs.initQgis() - projectHome = Path(args.project_home) - projectHome = projectHome.expanduser() - projectHome.mkdir(mode=0o755, exist_ok=True) - - projectPath = projectHome.joinpath(args.project_name) + args.project_home.mkdir(mode=0o755, exist_ok=True) + projectPath = args.project_home.joinpath(args.project_name) if projectPath.suffix not in ['.qgs', '.qgz']: projectPath = projectPath.with_suffix('.qgz') if projectPath.exists(): @@ -122,8 +122,6 @@ if projectInstance is not None: sourceGroup.setItemVisibilityChecked(False) for arg_idx, path in enumerate(args.geometry): - path = Path(path) - path = path.expanduser() layer = QgsVectorLayer(path.as_posix(), path.stem, 'ogr') if not layer.isValid(): raise Exception(f'ERROR: {path}: failed to load in QGIS') @@ -238,8 +236,6 @@ for pt in args.point: geometries.append(bbox) for path in args.geometry: - path = Path(path) - path = path.expanduser() ds = ogr.Open(path.as_posix(), update=0) for i in range(ds.GetLayerCount()): layer = ds.GetLayerByIndex(i) @@ -403,7 +399,7 @@ def getObservations(taxonLists, taxonRedlistCategories, searchFilter): drv = ogr.GetDriverByName(args.observation_format) if drv.name == geojson_drv.name: - path = projectHome.joinpath(layername.lower() + '.geojson') + path = args.project_home.joinpath(layername.lower() + '.geojson') with path.open(mode='w') as fp: json.dump(obs, fp, indent=2, ensure_ascii=False) else: @@ -412,7 +408,7 @@ def getObservations(taxonLists, taxonRedlistCategories, searchFilter): fp.flush() suffix = drv.GetMetadataItem(gdal.DMD_EXTENSIONS).split(' ')[0] - path = projectHome.joinpath(layername.lower()).with_suffix('.' + suffix) + path = args.project_home.joinpath(layername.lower()).with_suffix('.' + suffix) gdal.VectorTranslate(path.as_posix(), fp.name, reproject=True, dstSRS=target_srs, format=drv.name) obs = None @@ -422,8 +418,7 @@ def getObservations(taxonLists, taxonRedlistCategories, searchFilter): if not layer.isValid(): raise Exception(f'ERROR: {path}: failed to load in QGIS') if args.style is not None: - style = Path(args.style).expanduser() - layer.loadNamedStyle(style.as_posix()) + layer.loadNamedStyle(args.style.as_posix()) layer.setReadOnly(True) layer.setFlags(QgsMapLayer.Identifiable | QgsMapLayer.Searchable) projectInstance.addMapLayer(layer) @@ -668,17 +663,14 @@ getObservations(taxonLists, taxonRedlistCategories, searchFilter) topo_maps = ['Topografi 10', 'Topografi 50', 'Topografi 100'] if args.topo_basedir is not None: - basedir = Path(args.topo_basedir) - basedir = basedir.expanduser() - for topo in topo_maps: qlr_paths = [] - d = basedir.joinpath(topo) + d = args.topo_basedir.joinpath(topo) if not d.is_dir(): continue if args.project_home is not None: - l = Path(args.project_home).expanduser().joinpath(topo) + l = args.project_home.expanduser().joinpath(topo) l.symlink_to(d, target_is_directory=True) d = l |