From e44d4f728d780f9e524d29aa97f027b148492f93 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 28 Sep 2023 01:14:58 +0200 Subject: Add option --geometry-style to style subsequent geometries. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Example: `gis-observation-map … --geometry-style ~/gruvor/style.qml --geometry ~/gruvor/jokkmokk.geojson` --- gis-observation-map | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/gis-observation-map b/gis-observation-map index 41af068..b04d0cd 100755 --- a/gis-observation-map +++ b/gis-observation-map @@ -64,6 +64,16 @@ if 'QGIS' not in config.keys() or config['QGIS'] is None: def ePath(v): return Path(v).expanduser() +class geometryAction(argparse.Action): + def __init__(self, option_strings, dest, nargs=None, **kwargs): + super().__init__(option_strings, dest, **kwargs) + def __call__(self, parser, namespace, values, option_string=None): + items = getattr(namespace, self.dest, None) + items = items.copy() + style = getattr(namespace, 'geometry_style', None) # get current style + items.append({'path': values, 'style': style}) + setattr(namespace, self.dest, items) + parser = argparse.ArgumentParser( description='Create a QGIS project with observations from Artdatabanken.', prog = programName, @@ -78,7 +88,9 @@ parser.add_argument('--project-name', help='Project filename (and title)') parser.add_argument('--project-home', type=ePath, help='Project home directory') -parser.add_argument('--geometry', nargs='*', default=[], type=ePath, +parser.add_argument('--geometry-style', type=ePath, dest='geometry_style', + help='QGIS Layer Style File (*.qml) to apply to subsequent geometry files') +parser.add_argument('--geometry', nargs='*', default=[], type=ePath, action=geometryAction, help='Geometry filename(s)') parser.add_argument('--point', nargs='*', metavar='X,Y', default=[], help=f'Coordinates of interest (in {target_srs.GetName()})') @@ -129,14 +141,20 @@ if projectInstance is not None: sourceGroup = layerTreeRoot.addGroup('Source geometry') sourceGroup.setItemVisibilityChecked(False) - for arg_idx, path in enumerate(args.geometry): - layer = QgsVectorLayer(path.as_posix(), path.stem, 'ogr') + for i, geom in enumerate(args.geometry): + path = geom['path'] + ds = ogr.Open(geom['path'].as_posix(), update=0) + layername = ds.GetLayer().GetName() + ds = None + layer = QgsVectorLayer(path.as_posix(), layername, 'ogr') if not layer.isValid(): raise Exception(f'ERROR: {path}: failed to load in QGIS') layer.setReadOnly(True) layer.setFlags(QgsMapLayer.Identifiable | QgsMapLayer.Searchable) - if layer.geometryType() == QgsWkbTypes.PointGeometry: + if geom['style'] is not None: + layer.loadNamedStyle(geom['style'].as_posix()) + elif layer.geometryType() == QgsWkbTypes.PointGeometry: symbol = layer.renderer().symbol().symbolLayers()[0] symbol.setSize(2.0) symbol.setFillColor(QColor('#db1e2a')) @@ -161,7 +179,7 @@ if projectInstance is not None: else: projectInstance.addMapLayer(layer, False) sourceGroup.addLayer(layer) - if arg_idx == 0: + if i == 0: #layerTreeModel = QgsLayerTreeModel(layerTreeRoot) #layerTreeView = QgsLayerTreeView() #layerTreeView.setModel(layerTreeModel) @@ -243,8 +261,8 @@ for pt in args.point: geometries.append(bbox) -for path in args.geometry: - ds = ogr.Open(path.as_posix(), update=0) +for geom in args.geometry: + ds = ogr.Open(geom['path'].as_posix(), update=0) for i in range(ds.GetLayerCount()): layer = ds.GetLayerByIndex(i) source_srs = layer.GetSpatialRef() -- cgit v1.2.3