Created
October 28, 2022 09:11
-
-
Save agiudiceandrea/366c4b7d1d4939fdb899daf0928058a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
*************************************************************************** | |
punti_lungo_linea.py | |
--------------------- | |
Version : 1.0 | |
Date : 28/10/2022 | |
Copyright : (C) 2022 by Andrea Giudiceandrea | |
Email : andreaerdna at libero dot it | |
*************************************************************************** | |
* * | |
* This program is free software; you can redistribute it and/or modify * | |
* it under the terms of the GNU General Public License as published by * | |
* the Free Software Foundation; either version 2 of the License, or * | |
* any later version. * | |
* * | |
*************************************************************************** | |
""" | |
def punti_lungo_linea(lista_distanze): | |
layer = iface.activeLayer() | |
if not layer: return | |
selected_features = layer.selectedFeatures() | |
if not len(selected_features) > 0: return | |
geometria_linea = selected_features[0].geometry() | |
lunghezza_linea = geometria_linea.length() | |
punti = [] | |
for dist in lista_distanze: | |
if 0 <= dist <= lunghezza_linea: | |
punti.append([dist, geometria_linea.interpolate(dist)]) | |
if not len(punti) > 0: return | |
features = [] | |
for dist, punto in punti: | |
feat = QgsFeature() | |
feat.setGeometry(punto) | |
feat.setAttributes([dist]) | |
features.append(feat) | |
uri = "Point?crs={}&field=dist:double".format(layer.sourceCrs().authid()) | |
virt_layer = QgsVectorLayer(uri, "Punti lungo la linea", "memory") | |
virt_layer.dataProvider().addFeatures(features) | |
virt_layer.updateExtents() | |
QgsProject.instance().addMapLayer(virt_layer) | |
iface.setActiveLayer(layer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment