Skip to content
Permalink
Browse files

[asf] Fix parsing of polygons

  • Loading branch information
nirvn committed Mar 5, 2019
1 parent adf4a32 commit 766d7e4721f3bfc145c15c2c78007d51d089ddae
@@ -18,23 +18,13 @@
#include "qgslogger.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsrectangle.h"
#include "geometry/qgsabstractgeometry.h"
#include "geometry/qgscircularstring.h"
#include "geometry/qgscompoundcurve.h"
#include "geometry/qgscurvepolygon.h"
#include "geometry/qgslinestring.h"
#include "geometry/qgsmultipoint.h"
#include "geometry/qgsmulticurve.h"
#include "geometry/qgspolygon.h"
#include "geometry/qgspoint.h"
#include "qgsfeedback.h"
#include "qgssymbol.h"
#include "qgssymbollayer.h"
#include "qgsauthmanager.h"
#include "qgssettings.h"
#include "qgslinesymbollayer.h"
#include "qgsfillsymbollayer.h"
#include "qgsmarkersymbollayer.h"
#include "qgsrenderer.h"
#include "qgssinglesymbolrenderer.h"
#include "qgscategorizedsymbolrenderer.h"
@@ -88,7 +78,7 @@ QgsWkbTypes::Type QgsArcGisRestUtils::mapEsriGeometryType( const QString &esriGe
else if ( esriGeometryType == QLatin1String( "esriGeometryPolyline" ) )
return QgsWkbTypes::MultiCurve;
else if ( esriGeometryType == QLatin1String( "esriGeometryPolygon" ) )
return QgsWkbTypes::Polygon;
return QgsWkbTypes::MultiPolygon;
else if ( esriGeometryType == QLatin1String( "esriGeometryEnvelope" ) )
return QgsWkbTypes::Polygon;
// Unsupported (either by qgis, or format unspecified by the specification)
@@ -109,7 +99,7 @@ QgsWkbTypes::Type QgsArcGisRestUtils::mapEsriGeometryType( const QString &esriGe
return QgsWkbTypes::Unknown;
}

static std::unique_ptr< QgsPoint > parsePoint( const QVariantList &coordList, QgsWkbTypes::Type pointType )
std::unique_ptr< QgsPoint > QgsArcGisRestUtils::parsePoint( const QVariantList &coordList, QgsWkbTypes::Type pointType )
{
int nCoords = coordList.size();
if ( nCoords < 2 )
@@ -124,7 +114,7 @@ static std::unique_ptr< QgsPoint > parsePoint( const QVariantList &coordList, Qg
return qgis::make_unique< QgsPoint >( pointType, x, y, z, m );
}

static std::unique_ptr< QgsCircularString > parseCircularString( const QVariantMap &curveData, QgsWkbTypes::Type pointType, const QgsPoint &startPoint )
std::unique_ptr< QgsCircularString > QgsArcGisRestUtils::parseCircularString( const QVariantMap &curveData, QgsWkbTypes::Type pointType, const QgsPoint &startPoint )
{
const QVariantList coordsList = curveData[QStringLiteral( "c" )].toList();
if ( coordsList.isEmpty() )
@@ -145,7 +135,7 @@ static std::unique_ptr< QgsCircularString > parseCircularString( const QVariantM
return curve;
}

static std::unique_ptr< QgsCompoundCurve > parseCompoundCurve( const QVariantList &curvesList, QgsWkbTypes::Type pointType )
std::unique_ptr< QgsCompoundCurve > QgsArcGisRestUtils::parseCompoundCurve( const QVariantList &curvesList, QgsWkbTypes::Type pointType )
{
// [[6,3],[5,3],{"b":[[3,2],[6,1],[2,4]]},[1,2],{"c": [[3,3],[1,4]]}]
std::unique_ptr< QgsCompoundCurve > compoundCurve = qgis::make_unique< QgsCompoundCurve >();
@@ -186,7 +176,7 @@ static std::unique_ptr< QgsCompoundCurve > parseCompoundCurve( const QVariantLis
return compoundCurve;
}

static std::unique_ptr< QgsPoint > parseEsriGeometryPoint( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
std::unique_ptr< QgsPoint > QgsArcGisRestUtils::parseEsriGeometryPoint( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
{
// {"x" : <x>, "y" : <y>, "z" : <z>, "m" : <m>}
bool xok = false, yok = false;
@@ -199,7 +189,7 @@ static std::unique_ptr< QgsPoint > parseEsriGeometryPoint( const QVariantMap &ge
return qgis::make_unique< QgsPoint >( pointType, x, y, z, m );
}

static std::unique_ptr< QgsMultiPoint > parseEsriGeometryMultiPoint( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
std::unique_ptr< QgsMultiPoint > QgsArcGisRestUtils::parseEsriGeometryMultiPoint( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
{
// {"points" : [[ <x1>, <y1>, <z1>, <m1> ] , [ <x2>, <y2>, <z2>, <m2> ], ... ]}
const QVariantList coordsList = geometryData[QStringLiteral( "points" )].toList();
@@ -230,7 +220,7 @@ static std::unique_ptr< QgsMultiPoint > parseEsriGeometryMultiPoint( const QVari
return multiPoint;
}

static std::unique_ptr< QgsMultiCurve > parseEsriGeometryPolyline( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
std::unique_ptr< QgsMultiCurve > QgsArcGisRestUtils::parseEsriGeometryPolyline( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
{
// {"curvePaths": [[[0,0], {"c": [[3,3],[1,4]]} ]]}
QVariantList pathsList;
@@ -253,7 +243,7 @@ static std::unique_ptr< QgsMultiCurve > parseEsriGeometryPolyline( const QVarian
return multiCurve;
}

static std::unique_ptr< QgsCurvePolygon > parseEsriGeometryPolygon( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
std::unique_ptr< QgsMultiSurface > QgsArcGisRestUtils::parseEsriGeometryPolygon( const QVariantMap &geometryData, QgsWkbTypes::Type pointType )
{
// {"curveRings": [[[0,0], {"c": [[3,3],[1,4]]} ]]}
QVariantList ringsList;
@@ -263,26 +253,56 @@ static std::unique_ptr< QgsCurvePolygon > parseEsriGeometryPolygon( const QVaria
ringsList = geometryData[QStringLiteral( "ringPaths" )].toList();
if ( ringsList.isEmpty() )
return nullptr;
std::unique_ptr< QgsCurvePolygon > polygon = qgis::make_unique< QgsCurvePolygon >();
std::unique_ptr< QgsCompoundCurve > ext = parseCompoundCurve( ringsList.front().toList(), pointType );
if ( !ext )
{
return nullptr;
}
polygon->setExteriorRing( ext.release() );
for ( int i = 1, n = ringsList.size(); i < n; ++i )

QList< QgsCompoundCurve * > curves;
for ( int i = 0, n = ringsList.size(); i < n; ++i )
{
std::unique_ptr< QgsCompoundCurve > curve = parseCompoundCurve( ringsList[i].toList(), pointType );
if ( !curve )
{
return nullptr;
continue;
}
curves.append( curve.release() );
}
if ( curves.count() == 0 )
return nullptr;

std::sort( curves.begin(), curves.end(), []( const QgsCompoundCurve * a, const QgsCompoundCurve * b )->bool{ double a_area = 0.0; double b_area = 0.0; a->sumUpArea( a_area ); b->sumUpArea( b_area ); return std::abs( a_area ) > std::abs( b_area ); } );
std::unique_ptr< QgsMultiSurface > result = qgis::make_unique< QgsMultiSurface >();
while ( !curves.isEmpty() )
{
QgsCompoundCurve *exterior = curves.takeFirst();
QgsCurvePolygon *newPolygon = new QgsCurvePolygon();
newPolygon->setExteriorRing( exterior );
std::unique_ptr<QgsGeometryEngine> engine( QgsGeometry::createGeometryEngine( newPolygon ) );
engine->prepareGeometry();

QMutableListIterator< QgsCompoundCurve * > it( curves );
while ( it.hasNext() )
{
QgsCompoundCurve *curve = it.next();
QgsRectangle boundingBox = newPolygon->boundingBox();
if ( boundingBox.intersects( curve->boundingBox() ) )
{
QgsPoint point = curve->startPoint();
if ( engine->contains( &point ) )
{
newPolygon->addInteriorRing( curve );
it.remove();
engine.reset( QgsGeometry::createGeometryEngine( newPolygon ) );
engine->prepareGeometry();
}
}
}
polygon->addInteriorRing( curve.release() );
result->addGeometry( newPolygon );
}
return polygon;
if ( result->numGeometries() == 0 )
return nullptr;

return result;
}

static std::unique_ptr< QgsPolygon > parseEsriEnvelope( const QVariantMap &geometryData )
std::unique_ptr< QgsPolygon > QgsArcGisRestUtils::parseEsriEnvelope( const QVariantMap &geometryData )
{
// {"xmin" : -109.55, "ymin" : 25.76, "xmax" : -86.39, "ymax" : 49.94}
bool xminOk = false, yminOk = false, xmaxOk = false, ymaxOk = false;
@@ -628,7 +648,7 @@ std::unique_ptr<QgsFillSymbol> QgsArcGisRestUtils::parseEsriFillSymbolJson( cons
return symbol;
}

QgsSimpleMarkerSymbolLayerBase::Shape parseEsriMarkerShape( const QString &style )
QgsSimpleMarkerSymbolLayerBase::Shape QgsArcGisRestUtils::parseEsriMarkerShape( const QString &style )
{
if ( style == QLatin1String( "esriSMSCircle" ) )
return QgsSimpleMarkerSymbolLayerBase::Circle;
@@ -15,10 +15,23 @@
#ifndef QGSARCGISRESTUTILS_H
#define QGSARCGISRESTUTILS_H

#include <QStringList>
#include <QVariant>
#include "qgswkbtypes.h"
#include "qgsrectangle.h"
#include "qgsmarkersymbollayer.h"
#include "geometry/qgsabstractgeometry.h"
#include "geometry/qgscircularstring.h"
#include "geometry/qgscompoundcurve.h"
#include "geometry/qgscurvepolygon.h"
#include "geometry/qgsgeometryengine.h"
#include "geometry/qgslinestring.h"
#include "geometry/qgsmultipoint.h"
#include "geometry/qgsmulticurve.h"
#include "geometry/qgsmultisurface.h"
#include "geometry/qgspolygon.h"
#include "geometry/qgspoint.h"

#include <QStringList>
#include <QVariant>

class QNetworkReply;
class QgsNetworkAccessManager;
@@ -51,6 +64,15 @@ class QgsArcGisRestUtils
static QByteArray queryService( const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, QgsFeedback *feedback = nullptr );
static QVariantMap queryServiceJSON( const QUrl &url, const QString &authcfg, QString &errorTitle, QString &errorText, QgsFeedback *feedback = nullptr );

static std::unique_ptr< QgsPoint > parsePoint( const QVariantList &coordList, QgsWkbTypes::Type pointType );
static std::unique_ptr< QgsCircularString > parseCircularString( const QVariantMap &curveData, QgsWkbTypes::Type pointType, const QgsPoint &startPoint );
static std::unique_ptr< QgsCompoundCurve > parseCompoundCurve( const QVariantList &curvesList, QgsWkbTypes::Type pointType );
static std::unique_ptr< QgsPoint > parseEsriGeometryPoint( const QVariantMap &geometryData, QgsWkbTypes::Type pointType );
static std::unique_ptr< QgsMultiPoint > parseEsriGeometryMultiPoint( const QVariantMap &geometryData, QgsWkbTypes::Type pointType );
static std::unique_ptr< QgsMultiCurve > parseEsriGeometryPolyline( const QVariantMap &geometryData, QgsWkbTypes::Type pointType );
static std::unique_ptr< QgsMultiSurface > parseEsriGeometryPolygon( const QVariantMap &geometryData, QgsWkbTypes::Type pointType );
static std::unique_ptr< QgsPolygon > parseEsriEnvelope( const QVariantMap &geometryData );

static std::unique_ptr< QgsSymbol > parseEsriSymbolJson( const QVariantMap &symbolData );
static std::unique_ptr< QgsLineSymbol > parseEsriLineSymbolJson( const QVariantMap &symbolData );
static std::unique_ptr< QgsFillSymbol > parseEsriFillSymbolJson( const QVariantMap &symbolData );
@@ -60,6 +82,7 @@ class QgsArcGisRestUtils
static QColor parseEsriColorJson( const QVariant &colorData );
static Qt::PenStyle parseEsriLineStyle( const QString &style );
static Qt::BrushStyle parseEsriFillStyle( const QString &style );
static QgsSimpleMarkerSymbolLayerBase::Shape parseEsriMarkerShape( const QString &style );

static QDateTime parseDateTime( const QVariant &value );

@@ -25,6 +25,8 @@
#include "qgsmarkersymbollayer.h"
#include "qgssinglesymbolrenderer.h"
#include "qgscategorizedsymbolrenderer.h"
#include "geometry/qgsmultisurface.h"

#include <QObject>

class TestQgsArcGisRestUtils : public QObject
@@ -39,6 +41,7 @@ class TestQgsArcGisRestUtils : public QObject
void testMapEsriFieldType();
void testParseSpatialReference();
void testMapEsriGeometryType();
void testParseEsriGeometryPolygon();
void testParseEsriFillStyle();
void testParseEsriLineStyle();
void testParseEsriColorJson();
@@ -109,12 +112,27 @@ void TestQgsArcGisRestUtils::testMapEsriGeometryType()
QCOMPARE( QgsArcGisRestUtils::mapEsriGeometryType( QStringLiteral( "esriGeometryMultipoint" ) ), QgsWkbTypes::MultiPoint );
//unsure why this maps to multicurve and not multilinestring
//QCOMPARE( QgsArcGisRestUtils::mapEsriGeometryType( QStringLiteral("esriGeometryPolyline") ),QgsWkbTypes::MultiCurve );
QCOMPARE( QgsArcGisRestUtils::mapEsriGeometryType( QStringLiteral( "esriGeometryPolygon" ) ), QgsWkbTypes::Polygon );
QCOMPARE( QgsArcGisRestUtils::mapEsriGeometryType( QStringLiteral( "esriGeometryPolygon" ) ), QgsWkbTypes::MultiPolygon );
QCOMPARE( QgsArcGisRestUtils::mapEsriGeometryType( QStringLiteral( "esriGeometryEnvelope" ) ), QgsWkbTypes::Polygon );

QCOMPARE( QgsArcGisRestUtils::mapEsriGeometryType( QStringLiteral( "xxx" ) ), QgsWkbTypes::Unknown );
}

void TestQgsArcGisRestUtils::testParseEsriGeometryPolygon()
{
QVariantMap map = jsonStringToMap( "{"
"\"rings\": ["
"[[12,0],[13,0],[13,10],[12,10],[12,0]],"
"[[3,3],[9,3],[6,9],[3,3]],"
"[[0,0],[10,0],[10,10],[0,10],[0,0]]"
"]"
"}" );
QCOMPARE( map[QStringLiteral( "rings" )].isValid(), true );
std::unique_ptr<QgsMultiSurface> geometry = QgsArcGisRestUtils::parseEsriGeometryPolygon( map, QgsWkbTypes::Point );
QVERIFY( geometry.get() );
QCOMPARE( geometry->asWkt(), QStringLiteral( "MultiSurface (CurvePolygon (CompoundCurve ((0 0, 10 0, 10 10, 0 10, 0 0)),CompoundCurve ((3 3, 9 3, 6 9, 3 3))),CurvePolygon (CompoundCurve ((12 0, 13 0, 13 10, 12 10, 12 0))))" ) );
}

void TestQgsArcGisRestUtils::testParseEsriFillStyle()
{
QCOMPARE( QgsArcGisRestUtils::parseEsriFillStyle( QStringLiteral( "esriSFSBackwardDiagonal" ) ), Qt::BDiagPattern );

0 comments on commit 766d7e4

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