Skip to content
Permalink
Browse files

Update tests

  • Loading branch information
elpaso committed May 2, 2019
1 parent 8378eae commit ba816459e1ecafafbd31fc0a23f48aff0746c20f
@@ -1197,22 +1197,6 @@ QString QgsGeometryUtils::pointsToJSON( const QgsPointSequence &points, int prec
return json;
}

QJsonArray QgsGeometryUtils::pointsToJsonObject( const QgsPointSequence &points, int precision )
{
QJsonArray coordinates;
for ( const QgsPoint &p : points )
{
if ( p.is3D() )
{
coordinates.append( QJsonArray( { qgsRound( p.x(), precision ), qgsRound( p.y(), precision ), qgsRound( p.z(), precision ) } ) );
}
else
{
coordinates.append( QJsonArray( { qgsRound( p.x(), precision ), qgsRound( p.y(), precision ) } ) );
}
}
return coordinates;
}

json QgsGeometryUtils::pointsToJson( const QgsPointSequence &points, int precision )
{
@@ -422,10 +422,9 @@ class CORE_EXPORT QgsGeometryUtils
static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;

/**
* Returns a QJsonArray coordinates object.
* Returns coordinates as json object.
* \note not available in Python bindings
*/
static QJsonArray pointsToJsonObject( const QgsPointSequence &points, int precision ) SIP_SKIP;
static json pointsToJson( const QgsPointSequence &points, int precision ) SIP_SKIP;

/**
@@ -71,169 +71,7 @@ QgsCoordinateReferenceSystem QgsJsonExporter::sourceCrs() const
QString QgsJsonExporter::exportFeature( const QgsFeature &feature, const QVariantMap &extraProperties,
const QVariant &id ) const
{

QString s = QStringLiteral( "{\n \"type\":\"Feature\",\n" );

// ID
s += QStringLiteral( " \"id\":%1,\n" ).arg( !id.isValid() ? QString::number( feature.id() ) : QgsJsonUtils::encodeValue( id ) );

QgsGeometry geom = feature.geometry();
if ( !geom.isNull() && mIncludeGeometry )
{
if ( mCrs.isValid() )
{
try
{
QgsGeometry transformed = geom;
if ( transformed.transform( mTransform ) == 0 )
geom = transformed;
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
}
}
QgsRectangle box = geom.boundingBox();

if ( QgsWkbTypes::flatType( geom.wkbType() ) != QgsWkbTypes::Point )
{
s += QStringLiteral( " \"bbox\":[%1, %2, %3, %4],\n" ).arg( qgsDoubleToString( box.xMinimum(), mPrecision ),
qgsDoubleToString( box.yMinimum(), mPrecision ),
qgsDoubleToString( box.xMaximum(), mPrecision ),
qgsDoubleToString( box.yMaximum(), mPrecision ) );
}
s += QLatin1String( " \"geometry\":\n " );
s += geom.asJson( mPrecision );
s += QLatin1String( ",\n" );
}
else
{
s += QLatin1String( " \"geometry\":null,\n" );
}

// build up properties element
QString properties;
int attributeCounter = 0;
if ( mIncludeAttributes || !extraProperties.isEmpty() )
{
//read all attribute values from the feature

if ( mIncludeAttributes )
{
QgsFields fields = mLayer ? mLayer->fields() : feature.fields();
// List of formatters through we want to pass the values
QStringList formattersWhiteList;
formattersWhiteList << QStringLiteral( "KeyValue" )
<< QStringLiteral( "List" )
<< QStringLiteral( "ValueRelation" )
<< QStringLiteral( "ValueMap" );

for ( int i = 0; i < fields.count(); ++i )
{
if ( ( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) || mExcludedAttributeIndexes.contains( i ) )
continue;

if ( attributeCounter > 0 )
properties += QLatin1String( ",\n" );
QVariant val = feature.attributes().at( i );

if ( mLayer )
{
QgsEditorWidgetSetup setup = fields.at( i ).editorWidgetSetup();
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
if ( formattersWhiteList.contains( fieldFormatter->id() ) )
val = fieldFormatter->representValue( mLayer.data(), i, setup.config(), QVariant(), val );
}

QString name = fields.at( i ).name();
if ( mAttributeDisplayName )
{
name = mLayer->attributeDisplayName( i );
}

properties += QStringLiteral( " \"%1\":%2" ).arg( name, QgsJsonUtils::encodeValue( val ) );

++attributeCounter;
}
}

if ( !extraProperties.isEmpty() )
{
QVariantMap::const_iterator it = extraProperties.constBegin();
for ( ; it != extraProperties.constEnd(); ++it )
{
if ( attributeCounter > 0 )
properties += QLatin1String( ",\n" );

properties += QStringLiteral( " \"%1\":%2" ).arg( it.key(), QgsJsonUtils::encodeValue( it.value() ) );

++attributeCounter;
}
}

// related attributes
if ( mLayer && mIncludeRelatedAttributes )
{
QList< QgsRelation > relations = QgsProject::instance()->relationManager()->referencedRelations( mLayer.data() );
const auto constRelations = relations;
for ( const QgsRelation &relation : constRelations )
{
if ( attributeCounter > 0 )
properties += QLatin1String( ",\n" );

QgsFeatureRequest req = relation.getRelatedFeaturesRequest( feature );
req.setFlags( QgsFeatureRequest::NoGeometry );
QgsVectorLayer *childLayer = relation.referencingLayer();
QString relatedFeatureAttributes;
if ( childLayer )
{
QgsFeatureIterator it = childLayer->getFeatures( req );
QVector<QVariant> attributeWidgetCaches;
int fieldIndex = 0;
const QgsFields fields = childLayer->fields();
for ( const QgsField &field : fields )
{
QgsEditorWidgetSetup setup = field.editorWidgetSetup();
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
attributeWidgetCaches.append( fieldFormatter->createCache( childLayer, fieldIndex, setup.config() ) );
fieldIndex++;
}

QgsFeature relatedFet;
int relationFeatures = 0;
while ( it.nextFeature( relatedFet ) )
{
if ( relationFeatures > 0 )
relatedFeatureAttributes += QLatin1String( ",\n" );

relatedFeatureAttributes += QgsJsonUtils::exportAttributes( relatedFet, childLayer, attributeWidgetCaches );
relationFeatures++;
}
}
relatedFeatureAttributes.prepend( '[' ).append( ']' );

properties += QStringLiteral( " \"%1\":%2" ).arg( relation.name(), relatedFeatureAttributes );
attributeCounter++;
}
}
}

bool hasProperties = attributeCounter > 0;

s += QLatin1String( " \"properties\":" );
if ( hasProperties )
{
//read all attribute values from the feature
s += "{\n" + properties + "\n }\n";
}
else
{
s += QLatin1String( "null\n" );
}

s += '}';

return s;
return QString::fromStdString( exportFeatureToJsonObject( feature, extraProperties, id ).dump() );
}

json QgsJsonExporter::exportFeatureToJsonObject( const QgsFeature &feature, const QVariantMap &extraProperties, const QVariant &id ) const
@@ -116,6 +116,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/src/python
${CMAKE_BINARY_DIR}/src/analysis
${CMAKE_BINARY_DIR}/src/server
${CMAKE_SOURCE_DIR}/external
)

ADD_LIBRARY(qgis_server SHARED ${QGIS_SERVER_SRCS} ${QGIS_SERVER_MOC_SRCS} ${QGIS_SERVER_HDRS} ${QGIS_SERVER_MOC_HDRS})
@@ -19,6 +19,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/src/analysis
${CMAKE_BINARY_DIR}/src/server
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/external
../../../core
../../../core/expression
../../../core/geometry
@@ -27,6 +27,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/src/analysis
${CMAKE_BINARY_DIR}/src/server
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/external
../../../core
../../../core/dxf
../../../core/expression
@@ -37,6 +37,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/src/analysis
${CMAKE_BINARY_DIR}/src/server
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/external
../../../core
../../../core/dxf
../../../core/expression
@@ -66,6 +66,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/src/analysis
${CMAKE_BINARY_DIR}/src/server
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/external
)


@@ -34,6 +34,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_BINARY_DIR}/src/analysis
${CMAKE_BINARY_DIR}/src/server
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/external
../wms
../../../core
../../../core/dxf
@@ -129,7 +129,6 @@ class TestQgsJsonUtils : public QObject
void testExportFeatureJson()
{

QFETCH( enum JsonAlgs, JsonAlgs );

QgsVectorLayer vl { QStringLiteral( "Polygon?field=fldtxt:string&field=fldint:integer&field=flddbl:double" ), QStringLiteral( "mem" ), QStringLiteral( "memory" ) };
QgsFeature feature { vl.fields() };
@@ -138,39 +137,19 @@ class TestQgsJsonUtils : public QObject

QgsJsonExporter exporter { &vl };

if ( JsonAlgs == JsonAlgs::Json )
{
QBENCHMARK
{
const auto j { exporter.exportFeatureToJsonObject( feature ) };
QCOMPARE( QString::fromStdString( j.dump() ), 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\"}"
",\"type\":\"Feature\"}"
) );
}
}
else
{
QBENCHMARK
{
const auto json { exporter.exportFeature( feature ) };
QCOMPARE( json, QStringLiteral( "{\n \"type\":\"Feature\",\n \"id\":0,\n \"bbox\":[1.12, 1.12, 5.45, 5.33],\n \"geometry\":\n "
"{\"type\": \"Polygon\", \"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]]] },\n "
"\"properties\":{\n \"fldtxt\":\"a value\",\n \"fldint\":1,\n \"flddbl\":2\n }\n}" ) );
}
}
}
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\"}"
",\"type\":\"Feature\"}" ) };

void testExportGeomToJson_data()
{
QTest::addColumn<JsonAlgs>( "JsonAlgs" );
QTest::newRow( "Use json" ) << JsonAlgs::Json;
QTest::newRow( "Use old string concat" ) << JsonAlgs::String;
const auto j { exporter.exportFeatureToJsonObject( feature ) };
QCOMPARE( QString::fromStdString( j.dump() ), expectedJson );
const auto json { exporter.exportFeature( feature ) };
QCOMPARE( json, expectedJson );
}


void testExportGeomToJson()
{
const QStringList testWkts
@@ -191,33 +170,17 @@ class TestQgsJsonUtils : public QObject
{
const auto g { QgsGeometry::fromWkt( w ) };
QVERIFY( !g.isNull( ) );
QCOMPARE( QJsonDocument::fromJson( QByteArray::fromStdString( g.asJsonObject( 3 ).dump() ) ).toJson( QJsonDocument::JsonFormat::Compact ),
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() ).toJson( QJsonDocument::JsonFormat::Compact ) );
QCOMPARE( QJsonDocument::fromJson( QByteArray::fromStdString( g.asJsonObject( 3 ).dump() ) )
.toJson( QJsonDocument::JsonFormat::Compact ),
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() )
.toJson( QJsonDocument::JsonFormat::Compact ) );
const auto outp { QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() )
.toJson( QJsonDocument::JsonFormat::Compact ) };
qDebug() << QStringLiteral( "{ \"%1\", R\"json(%2)json\" }, " )
.arg( w )
.arg( QString( outp ) );
}

QFETCH( enum JsonAlgs, JsonAlgs );
if ( JsonAlgs == JsonAlgs::Json )
{
QBENCHMARK
{
for ( const auto &w : testWkts )
{
const auto g { QgsGeometry::fromWkt( w ) };
QJsonDocument::fromJson( QByteArray::fromStdString( g.asJsonObject( 3 ).dump() ) ).toJson( QJsonDocument::JsonFormat::Compact );
}
}
}
else
{
QBENCHMARK
{
for ( const auto &w : testWkts )
{
const auto g { QgsGeometry::fromWkt( w ) };
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() ).toJson( QJsonDocument::JsonFormat::Compact );
}
}
}
}
};

0 comments on commit ba81645

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