<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Try the QgsGeometry::combine() method.</span><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
<a href="http://www.qgis.org/api/classQgsGeometry.html#a63244d3435cc99794b82a6cbd0af0eb5" target="_blank">http://www.qgis.org/api/classQgsGeometry.html#a63244d3435cc99794b82a6cbd0af0eb5</a><br></div><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">Quoting class doc</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><dt style="line-height:16px;background-color:rgb(251,252,253);font-family:'Lucida Grande',Verdana,Geneva,Arial,sans-serif;font-weight:bold">
<b>Note:</b></dt><dd style="line-height:16px;background-color:rgb(251,252,253);font-family:'Lucida Grande',Verdana,Geneva,Arial,sans-serif">this operation is not called union since its a reserved word in C++.</dd>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Oct 5, 2013 at 8:07 AM, gene <span dir="ltr"><<a href="mailto:martin.laloux@gmail.com" target="_blank">martin.laloux@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I also do not see union in  Geometry Handling<br>
<<a href="http://www.qgis.org/en/docs/pyqgis_developer_cookbook/geometry.html" target="_blank">http://www.qgis.org/en/docs/pyqgis_developer_cookbook/geometry.html</a>>  :<br>
<br>
the layer:<br>
<br>
    for elem in layer.getFeatures():<br>
         geom= elem.geometry()<br>
         print geom.exportToGeoJSON()<br>
    { "type": "LineString", "coordinates": [ [203271.93800293002277613,<br>
89444.43836556003952865],   [204056.96179387057782151,<br>
89149.26942016639804933], [204590.77797171016572975,<br>
89111.58827820124861319], [204823.14501382855814882,<br>
89456.99874621508934069] ] }<br>
    { "type": "LineString", "coordinates": [ [204541.79248715547146276,<br>
89757.64295613372814842], [204474.17871974196168594,<br>
90086.05268357080058195] ] }<br>
<br>
<br>
Using the  see <<a href="https://github.com/inky/see" target="_blank">https://github.com/inky/see</a>>   module (alternative to<br>
Python's dir()):<br>
<br>
     from see import see<br>
     see(geom)<br>
    hash()                 help()                 repr()<br>
str()<br>
    .Error()               .addPart()             .addRing()<br>
    .adjacentVertices()    .area()                .asGeometryCollection()<br>
    .asMultiPoint()        .asMultiPolygon()      .asMultiPolyline()<br>
    .asPoint()             .asPolygon()           .asPolyline()<br>
    .asWkb()               .avoidIntersections()  .boundingBox()<br>
    .buffer()              .centroid()<br>
.closestSegmentWithContext()<br>
    .closestVertex()       .closestVertexWithContext()<br>
    .combine()             .contains()            .convertToMultiType()<br>
    .convexHull()          .crosses()             .deletePart()<br>
    .deleteRing()          .deleteVertex()        .difference()<br>
    .disjoint()            .distance()            .equals()<br>
    .exportToGeoJSON()     .exportToWkt()         .fromMultiPoint()<br>
    .fromMultiPolygon()    .fromMultiPolyline()   .fromPoint()<br>
    .fromPolygon()         .fromPolyline()        .fromRect()<br>
    .fromWkb()             .fromWkt()             .insertVertex()<br>
    .interpolate()         .intersection()        .intersects()<br>
    .isGeosEmpty()         .isGeosEqual()         .isGeosValid()<br>
    .isMultipart()         .length()              .makeDifference()<br>
    .moveVertex()          .overlaps()            .reshapeGeometry()<br>
    .simplify()            .splitGeometry()       .sqrDistToVertexAt()<br>
    .symDifference()       .touches()             .transform()<br>
    .translate()           .type()                .validateGeometry()<br>
    .vertexAt()            .within()              .wkbSize()<br>
    .wkbType()<br>
<br>
I see .intersection(), .touches() but no .union()<br>
<br>
Whereas with Shapely:<br>
--------------------------<br>
<br>
    from shapely.geometry import LineString, shape<br>
    from json import loads<br>
    # empty shapely geometry<br>
    myunion = LineString()<br>
    for elem in layer.getFeatures():<br>
           geom = elem.geometry()<br>
           # transformation of the QGIS geometry to shapely geometry with<br>
the geo_interface<br>
           geoms = shape(jloads(elem.geometry().exportToGeoJSON()))<br>
           myunion = myunion.union(geoms)<br>
   print myunion<br>
   MULTILINESTRING ((203271.9380029300227761 89444.4383655600395286,<br>
204056.9617938705778215 89149.2694201663980493, 204590.7779717101657297<br>
89111.5882782012486132, 204823.1450138285581488 89456.9987462150893407),<br>
(204541.7924871554714628 89757.6429561337281484, 204474.1787197419616859<br>
90086.0526835708005819))<br>
<br>
and<br>
      gem = QgsGeometry.fromWkt(myunion.wkt)<br>
      gem<br>
      <qgis.core.QgsGeometry object at 0x12986fef0><br>
       print gem.exportToGeoJSON()<br>
      { "type": "MultiLineString", "coordinates": [ [<br>
[203271.93800293002277613, 89444.43836556003952865],<br>
[204056.96179387057782151, 89149.26942016639804933],<br>
[204590.77797171016572975, 89111.58827820124861319],<br>
[204823.14501382855814882, 89456.99874621508934069] ], [<br>
[204541.79248715547146276, 89757.64295613372814842],<br>
[204474.17871974196168594, 90086.05268357080058195] ] ] }<br>
<br>
or with ogr (same result):<br>
-------------<br>
    from osgeo import ogr<br>
    # empty ogr geometry<br>
    myunion = ogr.Geometry(ogr.wkbLineString)<br>
    for elem in layer.getFeatures():<br>
           geom = elem.geometry()<br>
           # transformation of the QGIS geometry to ogr geometry<br>
           geoms = ogr.CreateGeometryFromWkb(geom.asWkb())<br>
           myunion = myunion.Union(geoms)<br>
<br>
Is there a comparable solution with PyQGIS ?<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://osgeo-org.1560.x6.nabble.com/Python-Union-selected-polygons-in-a-layer-tp5081862p5081959.html" target="_blank">http://osgeo-org.1560.x6.nabble.com/Python-Union-selected-polygons-in-a-layer-tp5081862p5081959.html</a><br>

Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Daniel Vaz
</div>