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

gene martin.laloux at gmail.com
Sat Oct 5 04:07:28 PDT 2013


I also do not see union in  Geometry Handling
<http://www.qgis.org/en/docs/pyqgis_developer_cookbook/geometry.html>  :

the layer:

    for elem in layer.getFeatures():
         geom= elem.geometry()
         print geom.exportToGeoJSON() 
    { "type": "LineString", "coordinates": [ [203271.93800293002277613,
89444.43836556003952865],   [204056.96179387057782151,
89149.26942016639804933], [204590.77797171016572975,
89111.58827820124861319], [204823.14501382855814882,
89456.99874621508934069] ] }
    { "type": "LineString", "coordinates": [ [204541.79248715547146276,
89757.64295613372814842], [204474.17871974196168594,
90086.05268357080058195] ] }


Using the  see <https://github.com/inky/see>   module (alternative to
Python's dir()):

     from see import see
     see(geom)
    hash()                 help()                 repr()                
str()
    .Error()               .addPart()             .addRing()
    .adjacentVertices()    .area()                .asGeometryCollection()
    .asMultiPoint()        .asMultiPolygon()      .asMultiPolyline()
    .asPoint()             .asPolygon()           .asPolyline()
    .asWkb()               .avoidIntersections()  .boundingBox()
    .buffer()              .centroid()           
.closestSegmentWithContext()
    .closestVertex()       .closestVertexWithContext()
    .combine()             .contains()            .convertToMultiType()
    .convexHull()          .crosses()             .deletePart()
    .deleteRing()          .deleteVertex()        .difference()
    .disjoint()            .distance()            .equals()
    .exportToGeoJSON()     .exportToWkt()         .fromMultiPoint()
    .fromMultiPolygon()    .fromMultiPolyline()   .fromPoint()
    .fromPolygon()         .fromPolyline()        .fromRect()
    .fromWkb()             .fromWkt()             .insertVertex()
    .interpolate()         .intersection()        .intersects()
    .isGeosEmpty()         .isGeosEqual()         .isGeosValid()
    .isMultipart()         .length()              .makeDifference()
    .moveVertex()          .overlaps()            .reshapeGeometry()
    .simplify()            .splitGeometry()       .sqrDistToVertexAt()
    .symDifference()       .touches()             .transform()
    .translate()           .type()                .validateGeometry()
    .vertexAt()            .within()              .wkbSize()
    .wkbType()

I see .intersection(), .touches() but no .union()

Whereas with Shapely:
--------------------------

    from shapely.geometry import LineString, shape
    from json import loads
    # empty shapely geometry
    myunion = LineString()
    for elem in layer.getFeatures():  
           geom = elem.geometry() 
           # transformation of the QGIS geometry to shapely geometry with
the geo_interface     
           geoms = shape(jloads(elem.geometry().exportToGeoJSON()))
           myunion = myunion.union(geoms)
   print myunion
   MULTILINESTRING ((203271.9380029300227761 89444.4383655600395286,
204056.9617938705778215 89149.2694201663980493, 204590.7779717101657297
89111.5882782012486132, 204823.1450138285581488 89456.9987462150893407),
(204541.7924871554714628 89757.6429561337281484, 204474.1787197419616859
90086.0526835708005819))

and 
      gem = QgsGeometry.fromWkt(myunion.wkt)
      gem
      <qgis.core.QgsGeometry object at 0x12986fef0>
       print gem.exportToGeoJSON()
      { "type": "MultiLineString", "coordinates": [ [
[203271.93800293002277613, 89444.43836556003952865],
[204056.96179387057782151, 89149.26942016639804933],
[204590.77797171016572975, 89111.58827820124861319],
[204823.14501382855814882, 89456.99874621508934069] ], [
[204541.79248715547146276, 89757.64295613372814842],
[204474.17871974196168594, 90086.05268357080058195] ] ] }

or with ogr (same result):
-------------
    from osgeo import ogr
    # empty ogr geometry
    myunion = ogr.Geometry(ogr.wkbLineString)
    for elem in layer.getFeatures():  
           geom = elem.geometry()   
           # transformation of the QGIS geometry to ogr geometry     
           geoms = ogr.CreateGeometryFromWkb(geom.asWkb())
           myunion = myunion.Union(geoms)

Is there a comparable solution with PyQGIS ? 




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


More information about the Qgis-developer mailing list