Skip to content

Commit 64fdbe3

Browse files
committedApr 16, 2019
Added tests and doxy
1 parent 75537a4 commit 64fdbe3

File tree

10 files changed

+63
-21
lines changed

10 files changed

+63
-21
lines changed
 

‎python/core/auto_generated/geometry/qgsabstractgeometry.sip.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ Returns a GeoJSON representation of the geometry.
261261

262262
virtual QJsonObject asJsonObject( int precision = 17 ) const = 0;
263263
%Docstring
264-
Returns a GeoJSON object of the geometry.
264+
Returns a QJsonObject representation of the geometry.
265265

266266
.. seealso:: :py:func:`asWkb`
267267

@@ -270,6 +270,10 @@ Returns a GeoJSON object of the geometry.
270270
.. seealso:: :py:func:`asGml2`
271271

272272
.. seealso:: :py:func:`asGml3`
273+
274+
.. seealso:: :py:func:`asJson`
275+
276+
.. versionadded:: 3.8
273277
%End
274278

275279

‎python/core/auto_generated/geometry/qgsgeometry.sip.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,9 @@ Exports the geometry to a GeoJSON string.
14431443

14441444
QJsonObject asJsonObject( int precision = 17 ) const;
14451445
%Docstring
1446-
Exports the geometry to a GeoJSON object.
1446+
Exports the geometry to a QJsonObject.
1447+
1448+
.. versionadded:: 3.8
14471449
%End
14481450

14491451
QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const /Factory/;

‎python/core/auto_generated/qgsjsonutils.sip.in

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,22 @@ Returns a GeoJSON string representation of a feature.
229229
:return: GeoJSON string
230230

231231
.. seealso:: :py:func:`exportFeatures`
232+
233+
.. seealso:: :py:func:`exportFeatureToJsonObject`
232234
%End
233235

234236
QJsonObject exportFeatureToJsonObject( const QgsFeature &feature,
235237
const QVariantMap &extraProperties = QVariantMap(),
236238
const QVariant &id = QVariant() ) const;
237239
%Docstring
238-
Returns a GeoJSON string representation of a feature.
240+
Returns a QJsonObject representation of a feature.
239241

240242
:param feature: feature to convert
241243
:param extraProperties: map of extra attributes to include in feature's properties
242244
:param id: optional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's
243245
ID is used.
244246

245-
:return: GeoJSON string
247+
:return: QJsonObject
246248

247249
.. seealso:: :py:func:`exportFeatures`
248250
%End
@@ -333,13 +335,15 @@ Exports all attributes from a QgsFeature as a JSON map type.
333335
static QJsonObject exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = 0,
334336
const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
335337
%Docstring
336-
Exports all attributes from a QgsFeature as a JSON map type.
338+
Exports all attributes from a QgsFeature as a QJsonObject.
337339

338340
:param feature: feature to export
339341
:param layer: optional associated vector layer. If specified, this allows
340342
richer export utilising settings like the layer's fields widget configuration.
341343
:param attributeWidgetCaches: optional widget configuration cache. Can be used
342344
to speed up exporting the attributes for multiple features from the same layer.
345+
346+
.. versionadded:: 3.8
343347
%End
344348

345349

‎src/core/geometry/qgsabstractgeometry.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,13 @@ class CORE_EXPORT QgsAbstractGeometry
281281
virtual QString asJson( int precision = 17 ) const = 0;
282282

283283
/**
284-
* Returns a GeoJSON object of the geometry.
284+
* Returns a QJsonObject representation of the geometry.
285285
* \see asWkb()
286286
* \see asWkt()
287287
* \see asGml2()
288288
* \see asGml3()
289+
* \see asJson()
290+
* \since QGIS 3.8
289291
*/
290292
virtual QJsonObject asJsonObject( int precision = 17 ) const = 0;
291293

‎src/core/geometry/qgscurvepolygon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ QJsonObject QgsCurvePolygon::asJsonObject( int precision ) const
449449
}
450450
return
451451
{
452-
{ QLatin1String( "type" ), QLatin1String( "Point" ) },
452+
{ QLatin1String( "type" ), QLatin1String( "Polygon" ) },
453453
{ QLatin1String( "coordinates" ), coordinates }
454454
};
455455
}

‎src/core/geometry/qgsgeometry.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,8 @@ class CORE_EXPORT QgsGeometry
14311431
QString asJson( int precision = 17 ) const;
14321432

14331433
/**
1434-
* Exports the geometry to a GeoJSON object.
1434+
* Exports the geometry to a QJsonObject.
1435+
* \since QGIS 3.8
14351436
*/
14361437
QJsonObject asJsonObject( int precision = 17 ) const;
14371438

‎src/core/geometry/qgsgeometryutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class CORE_EXPORT QgsGeometryUtils
422422
static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;
423423

424424
/**
425-
* Returns a geoJSON coordinates object.
425+
* Returns a QJsonArray coordinates object.
426426
* \note not available in Python bindings
427427
*/
428428
static QJsonArray pointsToJsonObject( const QgsPointSequence &points, int precision ) SIP_SKIP;

‎src/core/geometry/qgsmultipolygon.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ QString QgsMultiPolygon::asJson( int precision ) const
141141

142142
QJsonObject QgsMultiPolygon::asJsonObject( int precision ) const
143143
{
144-
QJsonArray coordinates;
144+
QJsonArray polygons;
145145
for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
146146
{
147+
QJsonArray coordinates;
147148
if ( qgsgeometry_cast<const QgsPolygon *>( geom ) )
148149
{
149150
const QgsPolygon *polygon = static_cast<const QgsPolygon *>( geom );
@@ -162,11 +163,12 @@ QJsonObject QgsMultiPolygon::asJsonObject( int precision ) const
162163
coordinates.append( QgsGeometryUtils::pointsToJsonObject( interiorPts, precision ) );
163164
}
164165
}
166+
polygons.append( coordinates );
165167
}
166168
return
167169
{
168170
{ QLatin1String( "type" ), QLatin1String( "MultiPolygon" ) },
169-
{ QLatin1String( "coordinates" ), coordinates }
171+
{ QLatin1String( "coordinates" ), polygons }
170172
};
171173
}
172174

‎src/core/qgsjsonutils.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "qgsfields.h"
2424

2525
#include <QPointer>
26-
#include <QJsonValue>
26+
#include <QJsonObject>
2727

2828
class QTextCodec;
2929

@@ -194,18 +194,19 @@ class CORE_EXPORT QgsJsonExporter
194194
* ID is used.
195195
* \returns GeoJSON string
196196
* \see exportFeatures()
197+
* \see exportFeatureToJsonObject()
197198
*/
198199
QString exportFeature( const QgsFeature &feature,
199200
const QVariantMap &extraProperties = QVariantMap(),
200201
const QVariant &id = QVariant() ) const;
201202

202203
/**
203-
* Returns a GeoJSON string representation of a feature.
204+
* Returns a QJsonObject representation of a feature.
204205
* \param feature feature to convert
205206
* \param extraProperties map of extra attributes to include in feature's properties
206207
* \param id optional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's
207208
* ID is used.
208-
* \returns GeoJSON string
209+
* \returns QJsonObject
209210
* \see exportFeatures()
210211
*/
211212
QJsonObject exportFeatureToJsonObject( const QgsFeature &feature,
@@ -306,12 +307,13 @@ class CORE_EXPORT QgsJsonUtils
306307
const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
307308

308309
/**
309-
* Exports all attributes from a QgsFeature as a JSON map type.
310+
* Exports all attributes from a QgsFeature as a QJsonObject.
310311
* \param feature feature to export
311312
* \param layer optional associated vector layer. If specified, this allows
312313
* richer export utilising settings like the layer's fields widget configuration.
313314
* \param attributeWidgetCaches optional widget configuration cache. Can be used
314315
* to speed up exporting the attributes for multiple features from the same layer.
316+
* \since QGIS 3.8
315317
*/
316318
static QJsonObject exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
317319
const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );

‎tests/src/core/testqgsjsonutils.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ class TestQgsJsonUtils : public QObject
7575
QCOMPARE( back.at( 0 ).type(), QVariant::Double );
7676
}
7777

78-
void testV2ExportAttributes_data()
78+
void testExportAttributesQJson_data()
7979
{
8080
QTest::addColumn<bool>( "useQJson" );
8181
QTest::newRow( "Use V2 (QJson)" ) << true;
8282
QTest::newRow( "Use old string concat" ) << false;
8383
}
8484

85-
void testV2ExportAttributes()
85+
void testExportAttributesQJson()
8686
{
8787

8888
QFETCH( bool, useQJson );
@@ -109,14 +109,14 @@ class TestQgsJsonUtils : public QObject
109109
}
110110
}
111111

112-
void testV2ExportFeature_data()
112+
void testExportFeatureQJson_data()
113113
{
114114
QTest::addColumn<bool>( "useQJson" );
115115
QTest::newRow( "Use V2 (QJson)" ) << true;
116116
QTest::newRow( "Use old string concat" ) << false;
117117
}
118118

119-
void testV2ExportFeature()
119+
void testExportFeatureQJson()
120120
{
121121

122122
QFETCH( bool, useQJson );
@@ -128,15 +128,15 @@ class TestQgsJsonUtils : public QObject
128128

129129
QgsJsonExporter exporter { &vl };
130130

131-
if ( useQJson ) // average: 0.038 msecs per iteration
131+
if ( useQJson ) // average: 0.041 msecs per iteration
132132
{
133133
QBENCHMARK
134134
{
135135
const auto json { exporter.exportFeatureToJsonObject( feature ) };
136136
QCOMPARE( QJsonDocument( json ).toJson( QJsonDocument::JsonFormat::Compact ),
137137
QStringLiteral( "{\"bbox\":[1.12,1.12,5.45,5.33],\"geometry\":{\"coordinates\":[[[1.12,1.34]"
138138
",[5.45,1.12],[5.34,5.33],[1.56,5.2],[1.12,1.34]],[[2,2],[3,2],[3,3],[2,3],[2,2]]],"
139-
"\"type\":\"Point\"},\"id\":0,\"properties\":{\"flddbl\":2,\"fldint\":1,\"fldtxt\":\"a value\"},\"type\":\"Feature\"}"
139+
"\"type\":\"Polygon\"},\"id\":0,\"properties\":{\"flddbl\":2,\"fldint\":1,\"fldtxt\":\"a value\"},\"type\":\"Feature\"}"
140140
) );
141141
}
142142
}
@@ -152,6 +152,31 @@ class TestQgsJsonUtils : public QObject
152152
}
153153
}
154154
}
155+
156+
void testExportGeomToQJson()
157+
{
158+
const QStringList testWkts
159+
{
160+
{
161+
QStringLiteral( "LINESTRING(-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932)" ),
162+
QStringLiteral( "MULTILINESTRING((-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932))" ),
163+
QStringLiteral( "POINT(-71.064544 42.28787)" ),
164+
QStringLiteral( "MULTIPOINT(-71.064544 42.28787, -71.1776585052917 42.3902909739571)" ),
165+
QStringLiteral( "POLYGON((-71.1776585052917 42.3902909739571,-71.1776820268866 42.3903701743239,"
166+
"-71.1776063012595 42.3903825660754,-71.1775826583081 42.3903033653531,-71.1776585052917 42.3902909739571))" ),
167+
QStringLiteral( "MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2)),((3 3,6 2,6 4,3 3)))" ),
168+
QStringLiteral( "CIRCULARSTRING(220268 150415,220227 150505,220227 150406)" ),
169+
}
170+
};
171+
172+
for ( const auto &w : testWkts )
173+
{
174+
const auto g { QgsGeometry::fromWkt( w ) };
175+
QVERIFY( !g.isNull( ) );
176+
QCOMPARE( QJsonDocument( g.asJsonObject( 3 ) ).toJson( QJsonDocument::JsonFormat::Compact ),
177+
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() ).toJson( QJsonDocument::JsonFormat::Compact ) );
178+
}
179+
}
155180
};
156181

157182
QGSTEST_MAIN( TestQgsJsonUtils )

0 commit comments

Comments
 (0)
Please sign in to comment.