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

2013-10-06 Thread Marco Bernasocchi
Hi Andi in inasafe i do something similar [0]
Please be warned that it is in the "messyest" part of our code :)

Ciao

[0]
https://github.com/AIFDR/inasafe/blob/master/safe_qgis/impact_statistics/aggregator.py#L1046
Hi,

I am trying to union selected geometries in a layer into one geometry. A
multpolygon as a result would be fine if the selected polygons aren't
contiguous.

I did not find a union command in the API. How can I do this from Python?

Thanks a lot for any hints!

Andreas
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2013-10-05 Thread Andreas Neumann
Hi Daniel,

Thanks - this should do the trick! And it explains why I did not find
"Union".

Will try on Monday when I am back in the office.

Andreas

Am 05.10.2013 11:55, schrieb Daniel:
> Try the QgsGeometry::combine() method.
> 
> http://www.qgis.org/api/classQgsGeometry.html#a63244d3435cc99794b82a6cbd0af0eb5
> 
> Quoting class doc
> 
> *Note:*this operation is not called union since its a reserved word in C++.
> 
> 
> On Sat, Oct 5, 2013 at 8:07 AM, gene  wrote:
> 
>> I also do not see union in  Geometry Handling
>>   :
>>
>> 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    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
>>   
>>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

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

2013-10-05 Thread gene
Ok, the reply is 

  this operation is not called union since its a reserved word in
C++.

Thanks to Daniel-2, we post  at the same time.




--
View this message in context: 
http://osgeo-org.1560.x6.nabble.com/Python-Union-selected-polygons-in-a-layer-tp5081862p5081967.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2013-10-05 Thread gene
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.
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2013-10-05 Thread Daniel
Try the QgsGeometry::combine() method.

http://www.qgis.org/api/classQgsGeometry.html#a63244d3435cc99794b82a6cbd0af0eb5

Quoting class doc

*Note:*this operation is not called union since its a reserved word in C++.


On Sat, Oct 5, 2013 at 8:07 AM, gene  wrote:

> I also do not see union in  Geometry Handling
>   :
>
> 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    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
>   
>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.
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Daniel Vaz
_

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

2013-10-05 Thread gene
I also do not see union in  Geometry Handling
  :

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    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
  
   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.
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2013-10-04 Thread Andreas Neumann
Hi,

I am trying to union selected geometries in a layer into one geometry. A
multpolygon as a result would be fine if the selected polygons aren't
contiguous.

I did not find a union command in the API. How can I do this from Python?

Thanks a lot for any hints!

Andreas
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer