Skip to content

Commit

Permalink
Fix JSON exported bbox and precision
Browse files Browse the repository at this point in the history
Fixes #31095
  • Loading branch information
elpaso committed Aug 5, 2019
1 parent 3235393 commit b4d597f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/core/qgsjsonutils.cpp
Expand Up @@ -119,12 +119,12 @@ json QgsJsonExporter::exportFeatureToJsonObject( const QgsFeature &feature, cons

if ( QgsWkbTypes::flatType( geom.wkbType() ) != QgsWkbTypes::Point )
{
featureJson[ "bbox" ] = { {
box.xMinimum(),
box.yMinimum(),
box.xMaximum(),
box.yMaximum()
}
featureJson[ "bbox" ] =
{
qgsRound( box.xMinimum(), mPrecision ),
qgsRound( box.yMinimum(), mPrecision ),
qgsRound( box.xMaximum(), mPrecision ),
qgsRound( box.yMaximum(), mPrecision )
};
}
featureJson[ "geometry" ] = geom.asJsonObject( mPrecision );
Expand Down
16 changes: 15 additions & 1 deletion tests/src/core/testqgsjsonutils.cpp
Expand Up @@ -212,7 +212,7 @@ void TestQgsJsonUtils::testExportFeatureJson()

QgsJsonExporter exporter { &vl };

const auto expectedJson { QStringLiteral( "{\"bbox\":[[1.12,1.12,5.45,5.33]],\"geometry\":{\"coordinates\":"
const auto expectedJson { QStringLiteral( "{\"bbox\":[1.12,1.12,5.45,5.33],\"geometry\":{\"coordinates\":"
"[[[1.12,1.34],[5.45,1.12],[5.34,5.33],[1.56,5.2],[1.12,1.34]],"
"[[2.0,2.0],[3.0,2.0],[3.0,3.0],[2.0,3.0],[2.0,2.0]]],\"type\":\"Polygon\"}"
",\"id\":0,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
Expand All @@ -222,6 +222,20 @@ void TestQgsJsonUtils::testExportFeatureJson()
QCOMPARE( QString::fromStdString( j.dump() ), expectedJson );
const auto json { exporter.exportFeature( feature ) };
QCOMPARE( json, expectedJson );

QgsJsonExporter exporterPrecision { &vl, 1 };

const auto expectedJsonPrecision { QStringLiteral( "{\"bbox\":[1.1,1.1,5.5,5.3],\"geometry\":{\"coordinates\":"
"[[[1.1,1.3],[5.5,1.1],[5.3,5.3],[1.6,5.2],[1.1,1.3]],"
"[[2.0,2.0],[3.0,2.0],[3.0,3.0],[2.0,3.0],[2.0,2.0]]],\"type\":\"Polygon\"}"
",\"id\":0,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
",\"type\":\"Feature\"}" ) };

const auto jPrecision( exporterPrecision.exportFeatureToJsonObject( feature ) );
QCOMPARE( QString::fromStdString( jPrecision.dump() ), expectedJsonPrecision );
const auto jsonPrecision { exporterPrecision.exportFeature( feature ) };
QCOMPARE( jsonPrecision, expectedJsonPrecision );

}

void TestQgsJsonUtils::testExportGeomToJson()
Expand Down
10 changes: 4 additions & 6 deletions tests/src/python/test_qgsjsonutils.py
Expand Up @@ -196,12 +196,10 @@ def testJSONExporter(self):

expected = """{
"bbox": [
[
5.0,
6.0,
15.0,
16.0
]
5.0,
6.0,
15.0,
16.0
],
"geometry": {
"coordinates": [
Expand Down

0 comments on commit b4d597f

Please sign in to comment.