Skip to content

Instantly share code, notes, and snippets.

@aborruso
Last active April 14, 2017 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aborruso/70f499780f7ac636d78f47abfb4c9747 to your computer and use it in GitHub Desktop.
Save aborruso/70f499780f7ac636d78f47abfb4c9747 to your computer and use it in GitHub Desktop.
QGIS: selezionare geometrie da una tabella correlata, via una action in Python. Un GIST a servizio di questo post https://medium.com/tantotanto/qgis-selezionare-geometrie-da-una-tabella-di-attributi-correlata-bea37747a7e2

Qui sotto un video che mostra come usare un action Python, per selezionare una geometria a partire da una tabella con valori correlati al layer con le geometrie. Il campo di join è id.

Sotto trovate pure:

  • la action in Python;
  • il file con le geometrie;
  • il file con la tabella con i dati correlati.

from qgis.utils import iface
# inserire qui il nome del layer geometrico
vl = QgsMapLayerRegistry.instance().mapLayersByName('layerGeometrico OGRGeoJSON Point')[0]
iface.setActiveLayer(vl)
cLayer = iface.mapCanvas().currentLayer()
#inserire qui il campo su cui fare la query per selezionare la feature del layer geometrico
expr = QgsExpression("\"id\"=[% "id" %]")
it = cLayer.getFeatures( QgsFeatureRequest( expr ) )
ids = [i.id() for i in it]
cLayer.setSelectedFeatures( ids )
# zoom alla selezione
box = cLayer.boundingBoxOfSelected()
iface.mapCanvas().setExtent(box)
iface.mapCanvas().refresh()
Display the source blob
Display the rendered blob
Raw
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"bbox": [ 12.8430348202616, 37.7670112159098, 13.0423397711376, 37.9278380328105 ],
"features": [
{ "type": "Feature", "properties": { "id": 1 }, "bbox": [ 12.843034820261613, 37.825638740790964, 12.843034820261613, 37.825638740790964 ], "geometry": { "type": "Point", "coordinates": [ 12.843034820261613, 37.825638740790964 ] } },
{ "type": "Feature", "properties": { "id": 2 }, "bbox": [ 13.039430209810979, 37.927838032810534, 13.039430209810979, 37.927838032810534 ], "geometry": { "type": "Point", "coordinates": [ 13.039430209810979, 37.927838032810534 ] } },
{ "type": "Feature", "properties": { "id": 3 }, "bbox": [ 13.042339771137623, 37.767011215909811, 13.042339771137623, 37.767011215909811 ], "geometry": { "type": "Point", "coordinates": [ 13.042339771137623, 37.767011215909811 ] } }
]
}
id particella
1 253a
2 253b
3 254a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment