What is the inverse operation of QgsGeometry.asGeometryCollection() as documented here: http://qgis.org/api/classQgsGeometry.html#a7cb1cbf8cfa441456067a66ad3a25a94
I'd like to do something like:
from qgis.core import (QgsFeature, QgsGeometry,
QgsVectorLayer, QgsMapLayerRegistry,
QgsField)
from PyQt4.QtCore import QVariant
from qgis.utils import iface
import math
def myFunction(arg):
return arg
layer=iface.mapCanvas().currentLayer()
feature = layer.selectedFeatures()[0]
geom = feature.geometry()
if geom.isMultipart():
newgeom=QgsGeometry.INVERSE([myFunction(part) for part in geom.asGeometryCollection ()])
else:
newgeom=myFunction(geom)
changeGeometry(feature.id(),newgeom)
Things that do not work:
# doesn't work:
newgeom=QgsGeometry.combine([part for part in geom.asGeometryCollection ()])
# doesn't work:
newgeom=([part for part in geom.asGeometryCollection ()])
# doesn't work:
newgeom = [part for part in geom.asGeometryCollection()]
# doesn't work: argument 1 has unexpected type 'list'
newgeom = QgsGeometry.fromPolyline([part for part in geom.asGeometryCollection()])
# QgsGeometry has no attribute 'MultiPolyline':
newgeom = QgsGeometry.MultiPolyline([part for part in geom.asGeometryCollection()])
# MuliPolyline is not defined:
newgeom = MultiPolyline([part for part in geom.asGeometryCollection()])
newgeom = QgsGeometry.fromMultiPolyline([part for part in geom.asGeometryCollection()])
newgeom = QgsGeometry.fromMultiPolyline([part.asPolyline for part in geom.asGeometryCollection()])
newgeom = QgsGeometry.fromMultiPolyline([part.asPolyline for part in geom])
newgeom = QgsGeometry.fromMultiPolyline([part.asPolyline for part in geom.asMultiPolyline()])
Works with Polylines / LineStrings:
newgeom = QgsGeometry.fromMultiPolyline([part for part in geom.asMultiPolyline()])
newgeom = QgsGeometry.fromMultiPolyline([userFunction(part,*args).asPolyline() for part in geom.asGeometryCollection()])
http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html has some documentation on accessing and creating the various types. http://qgis.org/api/classQGis.html lists the enumerations of WKB* types and plain geometry types.
It appears that each multipart type must be treated separately, and the multi-* types can't be disassembled and reassembled generally. In other words, you need to spell out and process the individual geometries separately like:
parts = ([userFunction(part,*args) for part in geom.asGeometryCollection()])
if geom.wkbType() == Qgis.WKBMultiLineString:
newgeom= QgsGeometry.fromMultiPolyline([x.asPolyline() for x in parts])
elif geom.wkbType() == QGis.WKBMultiPolygon:
newgeom= QgsGeometry.fromMultiPolygon([x.asPolygon() for x in parts])
elif geom.wkbType() == QGis.WKBLineString25D:
newgeom= QgsGeometry.fromMultiPolyline25D([x.asPolyline25D() for x in parts])
elif geom.wkbType() == QGis.WKBMultiPolygon25D:
newgeom= QgsGeometry.fromMultiPolygon25D([x.asPolygon25D() for x in parts])
...
layer.changeGeometry(feature.id(),newgeom)
Is there a function or combination of functions that would simplify the above? Something like:
newgeom = CAST_AS_GEOMETRY_TYPE([part for part in geom.asGeometryCollection()],geom.WKBType())