3

I am developing a dimensioning tool, and I would like that the "son" feature been deleted, when the father is deleteted, (on the fly). How can I do this?

It can be done via application or PyQGIS.

The code: (Note that the dimensioning layer is setted as this:Digitizing dimensioning and annotations)


##User need to select polygon features and run this code
import processing

id_pai_dict={}

#### Save feature in temporary layer, convert polygon to line and explode lines
layer = iface.activeLayer()
selected = processing.run("qgis:saveselectedfeatures", {'INPUT':layer, 'OUTPUT':'memory:'})["OUTPUT"]
temp_lines = processing.run("qgis:polygonstolines", {'INPUT':selected,'OUTPUT':'memory:'})["OUTPUT"]
result = processing.run("qgis:explodelines", {'INPUT':temp_lines,'OUTPUT':'memory:'})["OUTPUT"]


####Save feature in temporary layer, apply buffer, convert polygon to line and explode lines
selected2 = processing.run("qgis:saveselectedfeatures", {'INPUT':layer, 'OUTPUT':'memory:'})["OUTPUT"]
buffered2 = processing.run("qgis:buffer", {'INPUT':selected,'DISTANCE':-10000,'SEGMENTS':5,'END_CAP_STYLE':2,'JOIN_STYLE':1,'MITER_LIMIT':2,'DISSOLVE':False,"OUTPUT":"memory:"})["OUTPUT"]
temp_lines2 = processing.run("qgis:polygonstolines", {'INPUT':buffered2,'OUTPUT':'memory:'})["OUTPUT"]
result2 = processing.run("qgis:explodelines", {'INPUT':temp_lines,'OUTPUT':'memory:'})["OUTPUT"]

#### Calculate the length of each exploded line and save in 
d = QgsDistanceArea()
d.setSourceCrs(layer.crs(), QgsProject.instance().transformContext())
d.setEllipsoid(QgsProject.instance().ellipsoid())
feature = result2.getFeatures()
result2.startEditing()
for i,feat in enumerate(feature):
    dimension = (d.convertLengthMeasurement(d.measureLength(feat.geometry()),1))*1000
    result2.changeAttributeValue(i+1, 2, dimension)
layer.commitChanges()

    ###Add gid field, from polygonlayer, to id_pai field, from dimensioning
result_selected_feature = result2.selectedFeatures()
layer.startEditing()
for feature in result_selected_feature:
    gid = feature["gid"]
    id_pai_dict.update({i:gid})
    result2.updateFeature(feature)


####Start editing dimensioning layer and add the result layer features
layer_dimensioning = QgsProject.instance().mapLayersByName("dimensioning")[0]
layer_dimensioning.startEditing()
provider_layer_dimensioning = layer_dimensioning.dataProvider()
for i,feature in enumerate(result2.getFeatures()):
    provider_layer_dimensioning.addFeature(feature)
layer_dimensioning .commitChanges()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.