2

I am trying to put a default value of a field so that the value always updates once any change is make item. Either the expression builder or function editor are fine. I'm more familiar with the expression builder so I went that path. There should only ever be 1 pole within 5m of the start point so I can use concatenate but if there is a way to find the closest pole that would be best.

If I use the code below I get no info no matter how large I make the distance.

 aggregate( layer:='Poles', aggregate:=' concatenate' , expression:="Pole Id", filter:=buffer(start_point( geometry(@parent)) ,0.00005),',')

With this code I get every item no matter how small I make the distance.

aggregate( layer:='Poles', aggregate:=' concatenate' , expression:="Pole Id", filter:=geom_to_wkt(buffer(start_point( geometry(@parent)) ,0.00005)),',')

I tried to find an answer but this was the closest I could find. I don't have enough rep to add a comment so I had to post a new question. @csk Close but not quite answer

3
  • FYI, when you "at" someone in a question, they don't get a message or any kind of notification. @username only works in the comment section, when that user has previously commented on that same post. (SE is kindof an anti-social media website in that way.)
    – csk
    Commented Jun 27, 2019 at 19:58
  • 1
    For the filter input, you need a function that gives true/false as its output, but the output of the buffer function is a geometry. Try something along the lines of: intersects($geometry,buffer(start_point( geometry(@parent)) ,0.00005))
    – csk
    Commented Jun 27, 2019 at 20:02
  • I know the "at" function didn't work cause I had to manually do the link. There is also no way to message you either but I though it would get your attention that way. aggregate( 'lakeland_power_poles_2ea67cd7_23c8_42f9_92e8_564dc5d422cc', 'concatenate',"Pole Id",intersects($geometry,buffer(start_point( geometry(@parent)) ,0.00005))) seems to work
    – Andrew
    Commented Jun 28, 2019 at 13:08

2 Answers 2

4

When the Aggregate functions are getting you down, try RefFunctions.

  1. Install the RefFunctions plugin. This adds a variety of useful custom functions to the expression builder, including geomnearest.

    geomnearest function

    Retrieve target field value from the nearest target feature in target layer. Syntax: geomnearest(targetLayer,targetField)

    Arguments

    targetLayer → the name of a currently loaded layer, for example 'myLayer'.

    targetField → a field in target layer we want as result when source feature is within target feature, for example 'myField'. If targetField is equal to '$geometry' The WKT geometry of targetFeature willbe retrieved. If otherwise is equal to '$distance' the calculated distance between source and target features will be returned

  2. For the default field value, use geomnearest to auto-fill the field with the targetField of the nearest point, no matter how far away that point is. (note: I tested this expression, and it worked)

    geomnearest('Poles','Pole Id')
    

I tried to find a solution using geomdistance to auto-fill the field with the targetField of the nearest point, as long as that point is within a specified distance. However, I couldn't quite make it work. I'm including my best attempt; feel free to use it as a starting point. Comment if you find a solution, or feel free to post as a separate answer.

  if( distance( start_point($geometry), geomnearest('Poles',$geometry))<5, geomnearest('Poles','Pole Id'), NULL)

The error seems to come from using geomnearest to get a feature's geometry. I can get other RefFunctions to work with $geometry as a parameter, but not geomnearest. When I use the recommended syntax from the function description, geomnearest('Poles','$geometry'), I get this error:

Eval Error: 'QgsGeometry' object has no attribute 'exportToWkt'

But if I use the syntax that seems right to me, geomnearest('Poles',$geometry), I get this error:

Eval Error: error: targetFieldName not present.

You can probably get around this by adding the WKT geometry as a text field, using the field calculator and geom_to_WKT($geometry). Then substitute for the second parameter in the distance function above: geom_from_WKT(geomnearest('Poles','mygeometryfield'))

1
  • This definitely helps get me on the right path. Only issue is I can't pick start point and it seems just to take the nearest two features from all the poles and lines and only uses that point with geomnearest('Poles','Pole Id'). Is there a way you can specify geomneareast to use start_point?
    – Andrew
    Commented Jun 28, 2019 at 13:03
0

What ended up working thanks to @csk suggestion was

 aggregate( 'Poles', 'concatenate',"Pole Id",intersects($geometry,buffer(start_point( geometry(@parent)) ,0.00005)))

The nearest option would still be best but I can't seem to get that working with start_point.

Your Answer

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

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