Skip to content

Commit

Permalink
Added tests and doxy
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Apr 16, 2019
1 parent 75537a4 commit 64fdbe3
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 21 deletions.
Expand Up @@ -261,7 +261,7 @@ Returns a GeoJSON representation of the geometry.

virtual QJsonObject asJsonObject( int precision = 17 ) const = 0;
%Docstring
Returns a GeoJSON object of the geometry.
Returns a QJsonObject representation of the geometry.

.. seealso:: :py:func:`asWkb`

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

.. seealso:: :py:func:`asGml3`

.. seealso:: :py:func:`asJson`

.. versionadded:: 3.8
%End


Expand Down
4 changes: 3 additions & 1 deletion python/core/auto_generated/geometry/qgsgeometry.sip.in
Expand Up @@ -1443,7 +1443,9 @@ Exports the geometry to a GeoJSON string.

QJsonObject asJsonObject( int precision = 17 ) const;
%Docstring
Exports the geometry to a GeoJSON object.
Exports the geometry to a QJsonObject.

.. versionadded:: 3.8
%End

QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const /Factory/;
Expand Down
10 changes: 7 additions & 3 deletions python/core/auto_generated/qgsjsonutils.sip.in
Expand Up @@ -229,20 +229,22 @@ Returns a GeoJSON string representation of a feature.
:return: GeoJSON string

.. seealso:: :py:func:`exportFeatures`

.. seealso:: :py:func:`exportFeatureToJsonObject`
%End

QJsonObject exportFeatureToJsonObject( const QgsFeature &feature,
const QVariantMap &extraProperties = QVariantMap(),
const QVariant &id = QVariant() ) const;
%Docstring
Returns a GeoJSON string representation of a feature.
Returns a QJsonObject representation of a feature.

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

:return: GeoJSON string
:return: QJsonObject

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

:param feature: feature to export
:param layer: optional associated vector layer. If specified, this allows
richer export utilising settings like the layer's fields widget configuration.
:param attributeWidgetCaches: optional widget configuration cache. Can be used
to speed up exporting the attributes for multiple features from the same layer.

.. versionadded:: 3.8
%End


Expand Down
4 changes: 3 additions & 1 deletion src/core/geometry/qgsabstractgeometry.h
Expand Up @@ -281,11 +281,13 @@ class CORE_EXPORT QgsAbstractGeometry
virtual QString asJson( int precision = 17 ) const = 0;

/**
* Returns a GeoJSON object of the geometry.
* Returns a QJsonObject representation of the geometry.
* \see asWkb()
* \see asWkt()
* \see asGml2()
* \see asGml3()
* \see asJson()
* \since QGIS 3.8
*/
virtual QJsonObject asJsonObject( int precision = 17 ) const = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscurvepolygon.cpp
Expand Up @@ -449,7 +449,7 @@ QJsonObject QgsCurvePolygon::asJsonObject( int precision ) const
}
return
{
{ QLatin1String( "type" ), QLatin1String( "Point" ) },
{ QLatin1String( "type" ), QLatin1String( "Polygon" ) },
{ QLatin1String( "coordinates" ), coordinates }
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/geometry/qgsgeometry.h
Expand Up @@ -1431,7 +1431,8 @@ class CORE_EXPORT QgsGeometry
QString asJson( int precision = 17 ) const;

/**
* Exports the geometry to a GeoJSON object.
* Exports the geometry to a QJsonObject.
* \since QGIS 3.8
*/
QJsonObject asJsonObject( int precision = 17 ) const;

Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgsgeometryutils.h
Expand Up @@ -422,7 +422,7 @@ class CORE_EXPORT QgsGeometryUtils
static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;

/**
* Returns a geoJSON coordinates object.
* Returns a QJsonArray coordinates object.
* \note not available in Python bindings
*/
static QJsonArray pointsToJsonObject( const QgsPointSequence &points, int precision ) SIP_SKIP;
Expand Down
6 changes: 4 additions & 2 deletions src/core/geometry/qgsmultipolygon.cpp
Expand Up @@ -141,9 +141,10 @@ QString QgsMultiPolygon::asJson( int precision ) const

QJsonObject QgsMultiPolygon::asJsonObject( int precision ) const
{
QJsonArray coordinates;
QJsonArray polygons;
for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
{
QJsonArray coordinates;
if ( qgsgeometry_cast<const QgsPolygon *>( geom ) )
{
const QgsPolygon *polygon = static_cast<const QgsPolygon *>( geom );
Expand All @@ -162,11 +163,12 @@ QJsonObject QgsMultiPolygon::asJsonObject( int precision ) const
coordinates.append( QgsGeometryUtils::pointsToJsonObject( interiorPts, precision ) );
}
}
polygons.append( coordinates );
}
return
{
{ QLatin1String( "type" ), QLatin1String( "MultiPolygon" ) },
{ QLatin1String( "coordinates" ), coordinates }
{ QLatin1String( "coordinates" ), polygons }
};
}

Expand Down
10 changes: 6 additions & 4 deletions src/core/qgsjsonutils.h
Expand Up @@ -23,7 +23,7 @@
#include "qgsfields.h"

#include <QPointer>
#include <QJsonValue>
#include <QJsonObject>

class QTextCodec;

Expand Down Expand Up @@ -194,18 +194,19 @@ class CORE_EXPORT QgsJsonExporter
* ID is used.
* \returns GeoJSON string
* \see exportFeatures()
* \see exportFeatureToJsonObject()
*/
QString exportFeature( const QgsFeature &feature,
const QVariantMap &extraProperties = QVariantMap(),
const QVariant &id = QVariant() ) const;

/**
* Returns a GeoJSON string representation of a feature.
* Returns a QJsonObject representation of a feature.
* \param feature feature to convert
* \param extraProperties map of extra attributes to include in feature's properties
* \param id optional ID to use as GeoJSON feature's ID instead of input feature's ID. If omitted, feature's
* ID is used.
* \returns GeoJSON string
* \returns QJsonObject
* \see exportFeatures()
*/
QJsonObject exportFeatureToJsonObject( const QgsFeature &feature,
Expand Down Expand Up @@ -306,12 +307,13 @@ class CORE_EXPORT QgsJsonUtils
const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );

/**
* Exports all attributes from a QgsFeature as a JSON map type.
* Exports all attributes from a QgsFeature as a QJsonObject.
* \param feature feature to export
* \param layer optional associated vector layer. If specified, this allows
* richer export utilising settings like the layer's fields widget configuration.
* \param attributeWidgetCaches optional widget configuration cache. Can be used
* to speed up exporting the attributes for multiple features from the same layer.
* \since QGIS 3.8
*/
static QJsonObject exportAttributesToJsonObject( const QgsFeature &feature, QgsVectorLayer *layer = nullptr,
const QVector<QVariant> &attributeWidgetCaches = QVector<QVariant>() );
Expand Down
37 changes: 31 additions & 6 deletions tests/src/core/testqgsjsonutils.cpp
Expand Up @@ -75,14 +75,14 @@ class TestQgsJsonUtils : public QObject
QCOMPARE( back.at( 0 ).type(), QVariant::Double );
}

void testV2ExportAttributes_data()
void testExportAttributesQJson_data()
{
QTest::addColumn<bool>( "useQJson" );
QTest::newRow( "Use V2 (QJson)" ) << true;
QTest::newRow( "Use old string concat" ) << false;
}

void testV2ExportAttributes()
void testExportAttributesQJson()
{

QFETCH( bool, useQJson );
Expand All @@ -109,14 +109,14 @@ class TestQgsJsonUtils : public QObject
}
}

void testV2ExportFeature_data()
void testExportFeatureQJson_data()
{
QTest::addColumn<bool>( "useQJson" );
QTest::newRow( "Use V2 (QJson)" ) << true;
QTest::newRow( "Use old string concat" ) << false;
}

void testV2ExportFeature()
void testExportFeatureQJson()
{

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

QgsJsonExporter exporter { &vl };

if ( useQJson ) // average: 0.038 msecs per iteration
if ( useQJson ) // average: 0.041 msecs per iteration
{
QBENCHMARK
{
const auto json { exporter.exportFeatureToJsonObject( feature ) };
QCOMPARE( QJsonDocument( json ).toJson( QJsonDocument::JsonFormat::Compact ),
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,2],[3,2],[3,3],[2,3],[2,2]]],"
"\"type\":\"Point\"},\"id\":0,\"properties\":{\"flddbl\":2,\"fldint\":1,\"fldtxt\":\"a value\"},\"type\":\"Feature\"}"
"\"type\":\"Polygon\"},\"id\":0,\"properties\":{\"flddbl\":2,\"fldint\":1,\"fldtxt\":\"a value\"},\"type\":\"Feature\"}"
) );
}
}
Expand All @@ -152,6 +152,31 @@ class TestQgsJsonUtils : public QObject
}
}
}

void testExportGeomToQJson()
{
const QStringList testWkts
{
{
QStringLiteral( "LINESTRING(-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932)" ),
QStringLiteral( "MULTILINESTRING((-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932))" ),
QStringLiteral( "POINT(-71.064544 42.28787)" ),
QStringLiteral( "MULTIPOINT(-71.064544 42.28787, -71.1776585052917 42.3902909739571)" ),
QStringLiteral( "POLYGON((-71.1776585052917 42.3902909739571,-71.1776820268866 42.3903701743239,"
"-71.1776063012595 42.3903825660754,-71.1775826583081 42.3903033653531,-71.1776585052917 42.3902909739571))" ),
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)))" ),
QStringLiteral( "CIRCULARSTRING(220268 150415,220227 150505,220227 150406)" ),
}
};

for ( const auto &w : testWkts )
{
const auto g { QgsGeometry::fromWkt( w ) };
QVERIFY( !g.isNull( ) );
QCOMPARE( QJsonDocument( g.asJsonObject( 3 ) ).toJson( QJsonDocument::JsonFormat::Compact ),
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() ).toJson( QJsonDocument::JsonFormat::Compact ) );
}
}
};

QGSTEST_MAIN( TestQgsJsonUtils )
Expand Down

0 comments on commit 64fdbe3

Please sign in to comment.