Skip to content

Commit

Permalink
Merge pull request #1131 from ahuarte47/Issue_8725R-ST_SnapToGrid
Browse files Browse the repository at this point in the history
Fix bug #8725R: replace ST_Simplify by ST_SnapToGrid to avoid collapsed geometries by postgis
  • Loading branch information
jef-n committed Jan 31, 2014
2 parents d050b0a + db1192f commit e846521
Showing 1 changed file with 1 addition and 51 deletions.
52 changes: 1 addition & 51 deletions src/providers/postgres/qgspostgresfeatureiterator.cpp
Expand Up @@ -293,7 +293,6 @@ QString QgsPostgresFeatureIterator::whereClauseRect()
bool QgsPostgresFeatureIterator::declareCursor( const QString& whereClause )
{
mFetchGeometry = !( mRequest.flags() & QgsFeatureRequest::NoGeometry ) && !P->mGeometryColumn.isNull();
bool simplifyGeometry = false;

try
{
Expand All @@ -304,11 +303,10 @@ bool QgsPostgresFeatureIterator::declareCursor( const QString& whereClause )
if ( mFetchGeometry && !simplifyMethod.forceLocalOptimization() && simplifyMethod.methodType() != QgsSimplifyMethod::NoSimplification && QGis::flatType( QGis::singleType( P->geometryType() ) ) != QGis::WKBPoint )
{
QString simplifyFunctionName = simplifyMethod.methodType() == QgsSimplifyMethod::OptimizeForRendering
? ( P->mConnectionRO->majorVersion() < 2 ? "simplify" : "st_simplify" )
? ( P->mConnectionRO->majorVersion() < 2 ? "snaptogrid" : "st_snaptogrid" )
: ( P->mConnectionRO->majorVersion() < 2 ? "simplifypreservetopology" : "st_simplifypreservetopology" );

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" )
Expand Down Expand Up @@ -369,17 +367,6 @@ 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 ) )
{
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() )
Expand Down Expand Up @@ -603,43 +590,6 @@ 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::WKBPoint )
{
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 );

if ( QGis::flatType( QGis::singleType( P->geometryType() ) ) == QGis::WKBPolygon )
{
feature.setGeometry( envelope );
}
else
{
QgsPolyline polyline;
polyline.append( envelope->vertexAt( 0 ) );
polyline.append( envelope->vertexAt( 2 ) );
delete envelope;

geometry = QgsGeometry::fromPolyline( polyline );
feature.setGeometry( geometry );
}
}
}
}

return true;
}
catch ( QgsPostgresProvider::PGFieldNotFound )
Expand Down

0 comments on commit e846521

Please sign in to comment.