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.

Conflicts:
	src/core/geometry/qgsmultipointv2.cpp
	tests/src/core/testqgsgeometry.cpp
  • Loading branch information
rldhont committed Nov 20, 2015
1 parent 4f4fe59 commit 8d31372
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/core/geometry/qgsmultipointv2.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,20 +63,19 @@ QDomElement QgsMultiPointV2::asGML3( QDomDocument& doc, int precision, const QSt


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

QList<QgsPointV2> pts;
foreach ( const QgsAbstractGeometryV2 *geom, mGeometries ) foreach ( const QgsAbstractGeometryV2 *geom, mGeometries )
{ {
if ( dynamic_cast<const QgsPointV2*>( geom ) ) if ( dynamic_cast<const QgsPointV2*>( geom ) )
{ {
const QgsPointV2* point = static_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 );
{ json += " }";
json.chop( 2 ); // Remove last ", "
}
json += "] }";
return json; return json;
} }


Expand Down
47 changes: 47 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class TestQgsGeometry : public QObject
void bufferCheck(); void bufferCheck();
void smoothCheck(); void smoothCheck();


void exportToGeoJSON();

private: private:
/** A helper method to do a render check to see if the geometry op is as expected */ /** A helper method to do a render check to see if the geometry op is as expected */
bool renderCheck( QString theTestName, QString theComment = "", int mismatchCount = 0 ); bool renderCheck( QString theTestName, QString theComment = "", int mismatchCount = 0 );
Expand Down Expand Up @@ -706,6 +708,51 @@ void TestQgsGeometry::smoothCheck()
QVERIFY( QgsGeometry::compare( multipoly, expectedMultiPoly ) ); QVERIFY( QgsGeometry::compare( multipoly, expectedMultiPoly ) );
} }


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( QString theTestName, QString theComment, int mismatchCount ) bool TestQgsGeometry::renderCheck( QString theTestName, QString theComment, int mismatchCount )
{ {
mReport += "<h2>" + theTestName + "</h2>\n"; mReport += "<h2>" + theTestName + "</h2>\n";
Expand Down

4 comments on commit 8d31372

@m-kuhn
Copy link
Member

@m-kuhn m-kuhn commented on 8d31372 Nov 26, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has broken travis for 2.10 https://travis-ci.org/qgis/QGIS/builds/92230210

There are no more bugfix releases officially planned.
But you still seem to be backporting, so in case you plan an additional release, keep this in mind.

@rldhont
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this issue.

I know that, there's no bugfix releases officialy planned.
But some customers can't update to release 2.12.
I'll fix it

@m-kuhn
Copy link
Member

@m-kuhn m-kuhn commented on 8d31372 Nov 26, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tell them the 2.12 upgrade is a free one ;)

@m-kuhn
Copy link
Member

@m-kuhn m-kuhn commented on 8d31372 Nov 26, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These commits certainly are interesting as well. I did not backport them since they did not apply cleanly, but if you have customers that are bound to 2.10, you may want to look into it
436c30f 62591d9

Please sign in to comment.