Skip to main content
edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Source Link
adamczi
  • 739
  • 5
  • 20

QgsGeometry Polygon incorrectly interpreted as 'list'

I'm writing a QGIS plugin in which input data is multiple small polygons taken from an existing memory layer and merge it into one single polygon (not multi) and return it as memory layer.

However, when I try to set a geometry for the new feature (single one in new memory layer) I get TypeError: QgsGeometry.fromPolygon(unknown-type): argument 1 has unexpected type 'list'. Below is my code and comments.

vl is the old layer and vl_combine is the new one, just as pr_combine is the new dataProvider:

      newGeometry = QgsGeometry.fromWkt('GEOMETRYCOLLECTION EMPTY')

      for feature in vl.getFeatures(): 
          newGeometry = newGeometry.combine(feature.geometry())

      fet_combine = QgsFeature()
    > fet_combine.setGeometry(QgsGeometry.fromPolygon([newGeometry])) # <-- this is the line I get error in

      fet_combine.setAttributes(['just text'])
    
      pr_combine.addFeatures(fet_combine)
    
      vl_combine.updateExtents()

      ## Add prepared layer with transparency
      QgsMapLayerRegistry.instance().addMapLayer(vl_combine)
      vl_combine.setLayerTransparency(50)

So what I do:

  • check the print type(newGeometry) and what I get is <class 'qgis._core.QgsGeometry'>.
  • by print newGeometry I see <qgis._core.QgsGeometry object at 0x9db0680>.
  • finally I print newGeometry.asPolygon() and get a structure of [[(16.9102,52.4036), (16.9105,52.4038), (16.9098,52.4034), (16.9102,52.4036)]] (shortened, but should be clear)

I can't think of more ways to troubleshoot, the object seems to be of correct QgsGeometry type. I tried to put newGeometry without [ ] in the > line, but it didn't work too.