[Qgis-developer] geos help(union polygons)

Marco Hugentobler marco.hugentobler at sourcepole.ch
Thu Jan 24 00:36:14 PST 2013


Hi Vinayan

It is recommended to use the C-api of geos, not the C++ one (C-api is 
more stable). Therefore, the qgis code uses only the C-api and you can 
use QgsGeometry::asGeos() in the plugin instead of wkt-export / wkt-import.
For cascaded union, there is the method  GEOSUnaryUnion(const 
GEOSGeometry* g1) in the c-api. The argument seems to be a geometry 
collection, which can be created with GEOSGeom_createCollection.

Regards,
Marco

On 24.01.2013 06:32, vinayan wrote:
> Hi,
>
> I am trying to find gaps inside a polygon layer much like the ESRI 
> topology rule("must not have gaps"). I did this initially by making an 
> extent polygon and then subtracting the union of all geometries from 
> this extent polygon.
>
> All this worked well and for around 100 polygons in the extent the 
> performance is ok. But when the count goes up, the performance is 
> drastically down. The issue, I think is because I am creating the 
> union iteratively as below.
>
> for ( it = mFeatureList1.begin(); it != FeatureListEnd; ++it )
> {
> g2 = it->feature.geometry();
> g1 = g1->combine( g2 );
> }
>
>
> From what I read from Martin Davis' 
> blog(http://lin-ear-th-inking.blogspot.in/2007/11/fast-polygon-merging-in-jts-using.html), 
> the cascaded polygon union improves the performance of union by a 
> large ratio.
>
> I want to use the below method that exist in Geos. But I am not sure 
> how to use it.
>
> I got this far(also pasted in pastebin http://pastebin.com/rniMLuZW),
>
> std::vector<geos::geom::Polygon*> geomCollection;
>
> QList<FeatureLayer>::ConstIterator FeatureListEnd = mFeatureList1.end();
> for ( it = mFeatureList1.begin(); it != FeatureListEnd; ++it )
> {
> g1 = it->feature.geometry();
> const std::string wkt = g1->exportToWkt().toStdString();
> geos::geom::Polygon* geom = 
> dynamic_cast<geos::geom::Polygon*>(geos::io::WKTReader().read(wkt)); 
> //error is here....
> geomCollection.push_back(geom);
> }
>
> geos::geom::Geometry* unionedPoly = 
> geos::operation::geounion::CascadedPolygonUnion::Union(&geomCollection);
>
>
> The error is,
> /home/vinayan/QgisGitWorking/Quantum-GIS/src/plugins/topology/topolTest.cpp:746: 
> error: cannot dynamic_cast 
> ‘geos::io::WKTReader().geos::io::WKTReader::read((* & wkt))’ (of type 
> ‘struct geos::geom::Geometry*’) to type ‘struct geos::geom::Polygon*’ 
> (target is not pointer or reference to complete type)
>
>
> This has more to do with my c++ knowledge than anything i guess..but 
> if anyone can help where I am going wrong, it would be a good step 
> forward for me. If it works well, I am sure this function would find 
> use in other parts of Qgis where slow unions are involved.
>
> Thanks in advance,
> Vinayan
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer


-- 
Dr. Marco Hugentobler
Sourcepole -  Linux & Open Source Solutions
Weberstrasse 5, CH-8004 Zürich, Switzerland
marco.hugentobler at sourcepole.ch http://www.sourcepole.ch
Technical Advisor QGIS Project Steering Committee



More information about the Qgis-developer mailing list