From 0d56538429885a5b2a80d549f5026712b9e5cdc8 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Mon, 2 Oct 2023 03:24:18 +0200 Subject: =?UTF-8?q?Add=20support=20for=20extra=20layers=20(such=20as=20?= =?UTF-8?q?=E2=80=9CV=C3=A4rdek=C3=A4rnor=20skog=E2=80=9D).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gis-observation-map | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'gis-observation-map') diff --git a/gis-observation-map b/gis-observation-map index 9c58b8e..248b63a 100755 --- a/gis-observation-map +++ b/gis-observation-map @@ -36,6 +36,7 @@ from qgis.core import ( QgsApplication, QgsCoordinateReferenceSystem, QgsLayerDefinition, + QgsLayerTreeLayer, QgsLayerTreeModel, QgsMapLayer, QgsPointXY, @@ -827,6 +828,59 @@ if not args.no_observations: getObservations(taxonLists, taxonRedlistCategories, searchFilter) +if args.project_home is not None and projectInstance is not None: + for item in config['QGIS'].get('NatureValue', []): + name = item['Name'].title() + srcPath = Path(item['Source']).expanduser() + dstPath = args.project_home.joinpath(srcPath.name) + + fmt = item.get('Format') + if fmt is not None: + drv = ogr.GetDriverByName(fmt) + if drv is None: + raise Exception(f'Invalid format {fmt}') + suffixes = drv.GetMetadataItem(gdal.DMD_EXTENSIONS).split(' ') + if dstPath.suffix[1:] not in suffixes: + dstPath = dstPath.with_suffix('.' + suffixes[0]) + + layerName = item.get('LayerName') + srcDs = gdal.OpenEx( + srcPath.as_posix(), + gdal.OF_VECTOR, + open_options=item.get('SourceOpenOptions', []) + ) + + dstPath = dstPath.as_posix() + gdal.VectorTranslate( + dstPath, srcDs, format=fmt, + accessMode='overwrite', + reproject=True, dstSRS=target_srs, + clipDst=clipGeometry.ExportToWkt() if item.get('Clip', True) else None, + datasetCreationOptions=item.get('DatasetCreationOptions', []), + layerCreationOptions=item.get('LayerCreationOptions', []), + geometryType=item.get('GeometryType'), + skipFailures=True, + layers=[layerName] if layerName is not None else None + ) + srcDs = None + + if layerName is not None: + dstPath = f'{dstPath}|layername={layerName}' + layer = QgsVectorLayer(dstPath, name, 'ogr') + if not layer.isValid(): + raise Exception(f'ERROR: {dstPath}: failed to load in QGIS') + projectInstance.addMapLayer(layer, False) + layerTreeRoot.insertChildNode(-1, QgsLayerTreeLayer(layer)) # last + layer.setReadOnly(True) + + isVisible = item.get('Visible', True) + layerTreeRoot.findLayer(layer.id()).setItemVisibilityChecked(isVisible) + + style = item.get('Style') + if style is not None: + style = Path(style).expanduser() + layer.loadNamedStyle(style.as_posix()) + topo_maps = ['Topografi 10', 'Topografi 50', 'Topografi 100'] if args.topo_basedir is not None and projectInstance is not None: idxContains = {} -- cgit v1.2.3