Skip to content
Permalink
Browse files

Added tests and doxy

  • Loading branch information
elpaso committed Apr 16, 2019
1 parent 75537a4 commit 64fdbe3d76cde223fadfc6f382080f730a2d710e
@@ -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`

@@ -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


@@ -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/;
@@ -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
@@ -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


@@ -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;

@@ -449,7 +449,7 @@ QJsonObject QgsCurvePolygon::asJsonObject( int precision ) const
}
return
{
{ QLatin1String( "type" ), QLatin1String( "Point" ) },
{ QLatin1String( "type" ), QLatin1String( "Polygon" ) },
{ QLatin1String( "coordinates" ), coordinates }
};
}
@@ -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;

@@ -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;
@@ -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 );
@@ -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 }
};
}

@@ -23,7 +23,7 @@
#include "qgsfields.h"

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

class QTextCodec;

@@ -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,
@@ -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>() );
@@ -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 );
@@ -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 );
@@ -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\"}"
) );
}
}
@@ -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 )

0 comments on commit 64fdbe3

Please sign in to comment.
You can’t perform that action at this time.