4

I have some updated waypoint in one layer that correspond to waypoints in another (the 'Master') and I have a script that is supposed to update the geometry in the 'master' from the update layer.

layers = QgsMapLayerRegistry.instance().mapLayers()

# find the update and master layers

master = update = None
for name, layer in layers.iteritems():
    if 'wp_master' == layer.name():
        master = layer
    elif 'changed'  == layer.name():
        update = layer

update_features = {}  # indexed by feature attribute name
for f in update.getFeatures():
    update_features[f.attribute('name')] = f

# now search the master layer for the ones that have changed

master.startEditing()
for m in master.getFeatures():
    n = m.attribute('name')
    if update_features.get(n, 'none') != 'none':
        u = update_features[n]
        old = m.geometry().asPoint()
        new = u.geometry().asPoint()
        print 'master', n, old.x(), old.y()
        print 'new', n, new.x(), new.y()
        # if not m.geometry().equals( u.geometry()) :
        print 'updating', n
        print 'time', time.strftime("%Y-%m-%d %H:%M:%S")
        m['updated'] = time.strftime("%Y-%m-%d %H:%M:%S")
        master.dataProvider().changeGeometryValues({m.id(): u.geometry()})
        master.updateFeature(m)
        master.commitChanges()
        new = m.geometry().asPoint()
        print 'nex x,y ', new.x(), new.y()

gives this output:

Running mod-test.py in: /Users/rful011/src/qgis/scripts/bin
master T J B03 3 1768944.68311 5947703.25154
new T J B03 3 1768943.24096 5947702.72496
updating T J B03 3
time 2018-02-25 11:00:38
new x,y  1768944.68311 5947703.25154
Completed script: mod-test.py

We note that the printed geometry of the point in master is unchange

What am I missing and is there a better way of doing this without iterating over the master layer?

4
  • Did you enable editing of the layer (master.startEditing()) and committing the changes (master.commitChanges())?
    – Joseph
    Commented Feb 19, 2018 at 10:19
  • Yes and the changes to the ‘updated’ field work, it is just the geometry changes the fail. It used to work so it is odd. Commented Feb 20, 2018 at 17:50
  • 1
    I have just moved the commit inside the loop, immediately after the updateFeature call but it makes no difference. Time to build a nice small test script. BTW I am running this from script_runner, not that that should matter. Commented Feb 21, 2018 at 19:38
  • I tried this script with a different update layer with two features and it worked for the second feature but not the first. Weird. Commented Feb 24, 2018 at 22:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.