2

I'm trying to modify a feature in a WFS insert transaction using the Geoserver Scripting Extension with python. Basically I'm able to change the feature attributes, but I'm unsure of how to pass this feature along the pipeline so that the modified attributes will end up in the database.

def preInsert(inserted, req, context):
    iterator = inserted.features()
    while iterator.hasNext():
        feature = iterator.next()
        feature.setAttribute('userid', 'placeholder')

The above code doesn't have the userid field populated once it gets to the database.

Really I'm just confused as to whether or not I need to return something from these hook functions or if I'm allowed to mutate the objects themselves because the documentation is sparse and examples are few and far between.

0

Well after, mucking around with this for a couple of days I've finally figured out the reason why it wasn't working. Apparently the inserted parameter was a ReprojectingFeatureCollection, which upon calling the next method returns a copied version of the Feature with reprojected geometries. Instead I needed to grab the Insert Element from the Request object to mutate (which i suspected from the beginning and tried, but due to another bug and poor debugging support here i missed).

Code ended up looking like this:

from org.geoserver.wfs.request import Insert

def get_features(request):
    features = []
    for e in request.getElements():
        if isinstance(e, Insert):
            features += e.getFeatures()
    return features    

def preInsert(inserted, req, context):
    features = get_features(req)
    for feature in features:
        feature.setAttribute('userid', 'placeholder')

Now when it comes to actually getting the user out of the current request I'm not even sure that's possible.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.