[Qgis-developer] Python: Union selected polygons in a layer

gene martin.laloux at gmail.com
Sat Oct 5 05:14:47 PDT 2013


Finally, I found the solution: it is 

*geom.combine(another_geom)* and not union, why ?

   gLine = QgsGeometry.fromPolyline( [ QgsPoint(1,1), QgsPoint(2,2) ] )
   gLine2 = QgsGeometry.fromPolyline( [ QgsPoint(1,2), QgsPoint(2,1) ])
   gLine2.combine(gLine).exportToGeoJSON()
   u'{ "type": "MultiLineString", "coordinates": [ [ [1, 1], [1.5, 1.5] ], [
[1, 2], [1.5, 1.5] ], [ [1.5, 1.5], [2, 2] ], [ [1.5, 1.5], [2, 1] ] ] }'

Control with Shapely:

   from shapely.geometry import asShape, mapping
   from json import loads
   geom1 = asShape(loads(gLine.exportToGeoJSON())) 
   geom2 = asShape(loads(gLine2.exportToGeoJSON())) 
   print mapping(geom1.union(geom2))
  {'type': 'MultiLineString', 'coordinates': (((1.0, 1.0), (1.5, 1.5)),
((1.5, 1.5), (2.0, 2.0)), ((1.0, 2.0), (1.5, 1.5)), ((1.5, 1.5), (2.0,
1.0)))}



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Python-Union-selected-polygons-in-a-layer-tp5081862p5081965.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list