Skip to content
Permalink
Browse files

#8725R: fix collapsed polygons by ST_simplify in postgis

  • Loading branch information
ahuarte47 committed Jan 22, 2014
1 parent f6f0384 commit 01a733ba3f8864667e07e072f12ac4f33a43480e
Showing with 38 additions and 1 deletion.
  1. +38 −1 src/providers/postgres/qgspostgresfeatureiterator.cpp
@@ -14,6 +14,7 @@
***************************************************************************/
#include "qgspostgresfeatureiterator.h"
#include "qgspostgresprovider.h"
#include "qgsgeometry.h"

#include "qgslogger.h"
#include "qgsmessagelog.h"
@@ -292,6 +293,7 @@ QString QgsPostgresFeatureIterator::whereClauseRect()
bool QgsPostgresFeatureIterator::declareCursor( const QString& whereClause )
{
mFetchGeometry = !( mRequest.flags() & QgsFeatureRequest::NoGeometry ) && !P->mGeometryColumn.isNull();
bool simplifyGeometry = false;

try
{
@@ -305,7 +307,8 @@ bool QgsPostgresFeatureIterator::declareCursor( const QString& whereClause )
? ( P->mConnectionRO->majorVersion() < 2 ? "simplify" : "st_simplify" )
: ( P->mConnectionRO->majorVersion() < 2 ? "simplifypreservetopology" : "st_simplifypreservetopology" );

double tolerance = simplifyMethod.tolerance();
double tolerance = simplifyMethod.tolerance() * 0.8; //-> Default factor for the maximum displacement distance for simplification, similar as GeoServer does
simplifyGeometry = simplifyMethod.methodType() == QgsSimplifyMethod::OptimizeForRendering;

query += QString( "%1(%5(%2%3,%6),'%4')" )
.arg( P->mConnectionRO->majorVersion() < 2 ? "asbinary" : "st_asbinary" )
@@ -366,6 +369,17 @@ bool QgsPostgresFeatureIterator::declareCursor( const QString& whereClause )
query += delim + P->mConnectionRO->fieldExpression( P->field( idx ) );
}

// query BBOX of geometries to redefine the geometries collapsed by ST_Simplify()
if ( simplifyGeometry && !( P->mConnectionRO->majorVersion() >= 2 && P->mConnectionRO->minorVersion() >= 1 ) && QGis::flatType( QGis::singleType( P->geometryType() ) ) == QGis::WKBPolygon )
{
query += QString( ",%1(%5(%2)%3,'%4')" )
.arg( P->mConnectionRO->majorVersion() < 2 ? "asbinary" : "st_asbinary" )
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
.arg( P->mSpatialColType == sctGeography ? "::geometry" : "" )
.arg( P->endianString() )
.arg( P->mConnectionRO->majorVersion() < 2 ? "envelope" : "st_envelope" );
}

query += " FROM " + P->mQuery;

if ( !whereClause.isEmpty() )
@@ -589,6 +603,29 @@ bool QgsPostgresFeatureIterator::getFeature( QgsPostgresResult &queryResult, int
getFeatureAttribute( idx, queryResult, row, col, feature );
}

// fix collapsed geometries by ST_Simplify() using the BBOX fetched from the current query
const QgsSimplifyMethod& simplifyMethod = mRequest.simplifyMethod();
if ( mFetchGeometry && !simplifyMethod.forceLocalOptimization() && simplifyMethod.methodType() == QgsSimplifyMethod::OptimizeForRendering && QGis::flatType( QGis::singleType( P->geometryType() ) ) == QGis::WKBPolygon )
{
QgsGeometry* geometry = feature.geometry();

if ( !( P->mConnectionRO->majorVersion() >= 2 && P->mConnectionRO->minorVersion() >= 1 ) && ( !geometry || geometry->length() == 0 ) )
{
int returnedLength = ::PQgetlength( queryResult.result(), row, col );

if ( returnedLength > 0 )
{
unsigned char *featureGeom = new unsigned char[returnedLength + 1];
memcpy( featureGeom, PQgetvalue( queryResult.result(), row, col ), returnedLength );
memset( featureGeom + returnedLength, 0, 1 );

QgsGeometry *envelope = new QgsGeometry();
envelope->fromWkb( featureGeom, returnedLength + 1 );
feature.setGeometry( envelope );
}
}
}

return true;
}
catch ( QgsPostgresProvider::PGFieldNotFound )

0 comments on commit 01a733b

Please sign in to comment.
You can’t perform that action at this time.