3

I have a vector layer (PostGIS) with line geometries. Some of these geometries have a length of 0.0, but they have two points with the same coordinate. I want to handle this features within a python script and tried to fetch all of them inside a polygon. When I use a QgsFeatureRequest,

feats = layer.getFeatures(QgsFeatureRequest().setFilterRect(myPolygon.boundingBox()))

the result contains only features with a length > 0.0, but not "zero-length-lines". How can I fetch these lines, too?

In the QGIS application I can see those "zero-length-lines"and I can select them manually. I checked this with a small script:

layer = iface.activeLayer()
fs = layer.selectedFeatures()
for f in fs:
 g = f.geometry()
 print("WKB type",g.type())
 if g.type() == QgsWkbTypes.LineGeometry:
 print("LineGeom")
 print("length: ", g.length())

It returns for the selected features:

WKB type 1
LineGeom
length: 0.0 <----
WKB type 1
LineGeom
length: 2.996556190491762
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 13, 2022 at 14:46
5
  • loop through the features and check the length Commented Jun 13, 2022 at 15:15
  • I've just made a layer with two features in a unit square, one of which is a zero-length line, and something like your first bit of code, feats = layer.getFeatures(QgsFeatureRequest().setFilterRect(QgsRectangle(-1, -1, 2, 2))) gets both of them. Are you sure your mypolygon contains everything? Commented Jun 13, 2022 at 17:03
  • @IanTurton the Q assertion is that when got with a setFilterRect on a polygon the zero-length features aren't returned, so can't be looped over. But my little test seems to imply they are returned (at least with a QgsRectangle query, which should be equivalent to a bounding box). Something's not right... Commented Jun 13, 2022 at 17:21
  • 1
    Such linestrings are invalid. Example: select ST_IsValid('LINESTRING(1 1, 1 1)') returns "false". Would you like to find them for fixing the geometries or why? Commented Jun 13, 2022 at 18:51
  • Thanks for your help! QgsFeatureRequest.GeometryNoCheck was it ... Yes, I want to make valid geometries from this lines (the second point should get a new coordinate depending on other geometries. Commented Jun 14, 2022 at 6:11

1 Answer 1

4

To also get invalid geometries you can try to set the QgsFeatureRequest() to GeometryNoCheck actively:

# just get some test features...
lines = QgsProject.instance().mapLayersByName('lines')[0]
polygons = QgsProject.instance().mapLayersByName('polygon')[0]
mypolygon = polygons.getFeature(1)
# setup the QgsFeatureRequest():
req = QgsFeatureRequest()
req.setFilterRect(mypolygon.geometry().boundingBox())
req.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)
# get the features and iterate over them, it should also print length 0 of the invalid ones
linefeatures = lines.getFeatures(req)
for linefeature in linefeatures:
 print(linefeature.geometry().length())

Other attributes than GeometryNoCheck would be GeometryAbortOnInvalid or GeometrySkipInvalid, see https://qgis.org/pyqgis/3.0/core/Feature/QgsFeatureRequest.html. I guess you may have set GeometrySkipInvalid as default in your settings or something like that.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Jun 13, 2022 at 21:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.