[QGIS-Developer] geom.isValid() does not work in python

Raymond Nijssen r.nijssen at terglobo.nl
Sun Jul 18 22:47:33 PDT 2021


Thank you Andrea!

I also asked this on the QGIS Community Telegram group and got a few 
answers. Still a bit confusing though. Here's a test script:



wkt = 'MULTIPOLYGON(((-9 0, 0 9, 9 0, 0 -9, -9 0),(-8 0, 0 8, 8 0, 0 -8, 
-8 0)),((-7 0, 0 7, 7 0, 0 -7, -7 0)))'

geom = QgsGeometry.fromWkt(wkt)
print(geom)

print( geom.validateGeometry() ) # 0 = QGIS (Returns an error)
print( geom.validateGeometry(0) ) # 0 = QGIS (Returns an error)
print( geom.validateGeometry(1) ) # 1 = GEOS
print( geom.isGeosValid() ) # Why does this one even exist???

geom2 = geom.constGet()
print(geom2)
print( geom2.isValid() )
print( geom2.isValid(0) )
print( geom2.isValid(1) )

geom3 = geom.get()
print(geom3)
print( geom2.isValid() )
print( geom3.isValid(0) )
print( geom3.isValid(1) )



I think geom2 and geom3 are the exact same thing. And the parameter 0 or 
1 (or none at all) don't seem to change anything to the validation type, 
since they don't raise errors.

(BTW I'm doing this cause QGIS validation raises an error for 
multipolygons having a polygon within an inner of another polygon, which 
should be fixed. That's also the example wkt here.)



On 18-07-2021 13:49, Andrea Giudiceandrea wrote:
> Il 18/07/2021 11:46, Raymond Nijssen ha scritto:
>> How can I run ths isValid() function on a geometry. I would think my 
>> Polygon Geometry would inherit all functions from the 
>> QgsAbstractGeometry base class, but it seems it doesn't.
> 
>> I get:
>> AttributeError: 'QgsGeometry' object has no attribute 'isValid'
> 
> Hi Raymond,
> the QGIS Python API documentation clarify that: "The actual geometry 
> representation is stored as a QgsAbstractGeometry within the container, 
> and can be accessed via the get() method"[1].
> 
> So, I think
> 
> geom.get().isValid()
> 
> will work without errors.
> 
> Best regards.
> 
> Andrea
> 
> [1] https://qgis.org/pyqgis/3.16/core/QgsGeometry.html#module-QgsGeometry
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list