Skip to content

Commit

Permalink
[BUGFIX] Multipoint asJSON
Browse files Browse the repository at this point in the history
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 69eb659 commit a860af5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/core/geometry/qgsmultipointv2.cpp
Expand Up @@ -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;
}

Expand Down
47 changes: 47 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -81,6 +81,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 */
Expand Down Expand Up @@ -739,6 +741,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";
Expand Down

0 comments on commit a860af5

Please sign in to comment.