Skip to content
Permalink
Browse files

[BUGFIX] Multipoint asJSON

Fixes #13855
Multipoint asJSON made MultiPoint as Multilinestring with only 1 point by linestring.

This bugfix adds tests.
  • Loading branch information
rldhont committed Nov 20, 2015
1 parent 4203a7c commit 00ea968347d40912ffa2a8954f298c015c664d29
Showing with 55 additions and 5 deletions.
  1. +8 −5 src/core/geometry/qgsmultipointv2.cpp
  2. +47 −0 tests/src/core/testqgsgeometry.cpp
@@ -73,20 +73,23 @@ QDomElement QgsMultiPointV2::asGML3( QDomDocument& doc, int precision, const QSt

QString QgsMultiPointV2::asJSON( int precision ) const
{
QString json = "{\"type\": \"MultiPoint\", \"coordinates\": [";
QString json = "{\"type\": \"MultiPoint\", \"coordinates\": ";

QList<QgsPointV2> pts;
Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
{
if ( dynamic_cast<const QgsPointV2*>( geom ) )
{
const QgsPointV2* point = static_cast<const QgsPointV2*>( geom );
json += QgsGeometryUtils::pointsToJSON( QList<QgsPointV2>() << *point, precision ) + ", ";
pts << *point;
}
}
if ( json.endsWith( ", " ) )
json += QgsGeometryUtils::pointsToJSON( pts, precision );
/*if ( json.endsWith( ", " ) )
{
json.chop( 2 ); // Remove last ", "
}
json += "] }";
}*/
json += " }";
return json;
}

@@ -83,6 +83,8 @@ class TestQgsGeometry : public QObject
void smoothCheck();

void dataStream();

void exportToGeoJSON();

private:
/** A helper method to do a render check to see if the geometry op is as expected */
@@ -1050,6 +1052,51 @@ void TestQgsGeometry::dataStream()
QVERIFY( resultGeometry.isEmpty() );
}

void TestQgsGeometry::exportToGeoJSON()
{
//Point
QString wkt = "Point (40 50)";
QScopedPointer<QgsGeometry> geom( QgsGeometry::fromWkt( wkt ) );
QString obtained = geom->exportToGeoJSON();
QString geojson = "{\"type\": \"Point\", \"coordinates\": [40, 50]}";
QCOMPARE( obtained, geojson );

//MultiPoint
wkt = "MultiPoint (0 0, 10 0, 10 10, 20 10)";
geom.reset( QgsGeometry::fromWkt( wkt ) );
obtained = geom->exportToGeoJSON();
geojson = "{\"type\": \"MultiPoint\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]] }";
QCOMPARE( obtained, geojson );

//Linestring
wkt = "LineString(0 0, 10 0, 10 10, 20 10)";
geom.reset( QgsGeometry::fromWkt( wkt ) );
obtained = geom->exportToGeoJSON();
geojson = "{\"type\": \"LineString\", \"coordinates\": [ [0, 0], [10, 0], [10, 10], [20, 10]]}";
QCOMPARE( obtained, geojson );

//MultiLineString
wkt = "MultiLineString ((0 0, 10 0, 10 10, 20 10),(30 30, 40 30, 40 40, 50 40))";
geom.reset( QgsGeometry::fromWkt( wkt ) );
obtained = geom->exportToGeoJSON();
geojson = "{\"type\": \"MultiLineString\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [20, 10]], [ [30, 30], [40, 30], [40, 40], [50, 40]]] }";
QCOMPARE( obtained, geojson );

//Polygon
wkt = "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0 ),(2 2, 4 2, 4 4, 2 4, 2 2))";
geom.reset( QgsGeometry::fromWkt( wkt ) );
obtained = geom->exportToGeoJSON();
geojson = "{\"type\": \"Polygon\", \"coordinates\": [[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]] }";
QCOMPARE( obtained, geojson );

//MultiPolygon
wkt = "MultiPolygon (((0 0, 10 0, 10 10, 0 10, 0 0 )),((2 2, 4 2, 4 4, 2 4, 2 2)))";
geom.reset( QgsGeometry::fromWkt( wkt ) );
obtained = geom->exportToGeoJSON();
geojson = "{\"type\": \"MultiPolygon\", \"coordinates\": [[[ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]], [[ [2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]]] }";
QCOMPARE( obtained, geojson );
}

bool TestQgsGeometry::renderCheck( const QString& theTestName, const QString& theComment, int mismatchCount )
{
mReport += "<h2>" + theTestName + "</h2>\n";

0 comments on commit 00ea968

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