Skip to content

Commit ba81645

Browse files
committedMay 2, 2019
Update tests
1 parent 8378eae commit ba81645

File tree

10 files changed

+27
-237
lines changed

10 files changed

+27
-237
lines changed
 

‎src/core/geometry/qgsgeometryutils.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,22 +1197,6 @@ QString QgsGeometryUtils::pointsToJSON( const QgsPointSequence &points, int prec
11971197
return json;
11981198
}
11991199

1200-
QJsonArray QgsGeometryUtils::pointsToJsonObject( const QgsPointSequence &points, int precision )
1201-
{
1202-
QJsonArray coordinates;
1203-
for ( const QgsPoint &p : points )
1204-
{
1205-
if ( p.is3D() )
1206-
{
1207-
coordinates.append( QJsonArray( { qgsRound( p.x(), precision ), qgsRound( p.y(), precision ), qgsRound( p.z(), precision ) } ) );
1208-
}
1209-
else
1210-
{
1211-
coordinates.append( QJsonArray( { qgsRound( p.x(), precision ), qgsRound( p.y(), precision ) } ) );
1212-
}
1213-
}
1214-
return coordinates;
1215-
}
12161200

12171201
json QgsGeometryUtils::pointsToJson( const QgsPointSequence &points, int precision )
12181202
{

‎src/core/geometry/qgsgeometryutils.h

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

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

431430
/**

‎src/core/qgsjsonutils.cpp

Lines changed: 1 addition & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -71,169 +71,7 @@ QgsCoordinateReferenceSystem QgsJsonExporter::sourceCrs() const
7171
QString QgsJsonExporter::exportFeature( const QgsFeature &feature, const QVariantMap &extraProperties,
7272
const QVariant &id ) const
7373
{
74-
75-
QString s = QStringLiteral( "{\n \"type\":\"Feature\",\n" );
76-
77-
// ID
78-
s += QStringLiteral( " \"id\":%1,\n" ).arg( !id.isValid() ? QString::number( feature.id() ) : QgsJsonUtils::encodeValue( id ) );
79-
80-
QgsGeometry geom = feature.geometry();
81-
if ( !geom.isNull() && mIncludeGeometry )
82-
{
83-
if ( mCrs.isValid() )
84-
{
85-
try
86-
{
87-
QgsGeometry transformed = geom;
88-
if ( transformed.transform( mTransform ) == 0 )
89-
geom = transformed;
90-
}
91-
catch ( QgsCsException &cse )
92-
{
93-
Q_UNUSED( cse );
94-
}
95-
}
96-
QgsRectangle box = geom.boundingBox();
97-
98-
if ( QgsWkbTypes::flatType( geom.wkbType() ) != QgsWkbTypes::Point )
99-
{
100-
s += QStringLiteral( " \"bbox\":[%1, %2, %3, %4],\n" ).arg( qgsDoubleToString( box.xMinimum(), mPrecision ),
101-
qgsDoubleToString( box.yMinimum(), mPrecision ),
102-
qgsDoubleToString( box.xMaximum(), mPrecision ),
103-
qgsDoubleToString( box.yMaximum(), mPrecision ) );
104-
}
105-
s += QLatin1String( " \"geometry\":\n " );
106-
s += geom.asJson( mPrecision );
107-
s += QLatin1String( ",\n" );
108-
}
109-
else
110-
{
111-
s += QLatin1String( " \"geometry\":null,\n" );
112-
}
113-
114-
// build up properties element
115-
QString properties;
116-
int attributeCounter = 0;
117-
if ( mIncludeAttributes || !extraProperties.isEmpty() )
118-
{
119-
//read all attribute values from the feature
120-
121-
if ( mIncludeAttributes )
122-
{
123-
QgsFields fields = mLayer ? mLayer->fields() : feature.fields();
124-
// List of formatters through we want to pass the values
125-
QStringList formattersWhiteList;
126-
formattersWhiteList << QStringLiteral( "KeyValue" )
127-
<< QStringLiteral( "List" )
128-
<< QStringLiteral( "ValueRelation" )
129-
<< QStringLiteral( "ValueMap" );
130-
131-
for ( int i = 0; i < fields.count(); ++i )
132-
{
133-
if ( ( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) || mExcludedAttributeIndexes.contains( i ) )
134-
continue;
135-
136-
if ( attributeCounter > 0 )
137-
properties += QLatin1String( ",\n" );
138-
QVariant val = feature.attributes().at( i );
139-
140-
if ( mLayer )
141-
{
142-
QgsEditorWidgetSetup setup = fields.at( i ).editorWidgetSetup();
143-
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
144-
if ( formattersWhiteList.contains( fieldFormatter->id() ) )
145-
val = fieldFormatter->representValue( mLayer.data(), i, setup.config(), QVariant(), val );
146-
}
147-
148-
QString name = fields.at( i ).name();
149-
if ( mAttributeDisplayName )
150-
{
151-
name = mLayer->attributeDisplayName( i );
152-
}
153-
154-
properties += QStringLiteral( " \"%1\":%2" ).arg( name, QgsJsonUtils::encodeValue( val ) );
155-
156-
++attributeCounter;
157-
}
158-
}
159-
160-
if ( !extraProperties.isEmpty() )
161-
{
162-
QVariantMap::const_iterator it = extraProperties.constBegin();
163-
for ( ; it != extraProperties.constEnd(); ++it )
164-
{
165-
if ( attributeCounter > 0 )
166-
properties += QLatin1String( ",\n" );
167-
168-
properties += QStringLiteral( " \"%1\":%2" ).arg( it.key(), QgsJsonUtils::encodeValue( it.value() ) );
169-
170-
++attributeCounter;
171-
}
172-
}
173-
174-
// related attributes
175-
if ( mLayer && mIncludeRelatedAttributes )
176-
{
177-
QList< QgsRelation > relations = QgsProject::instance()->relationManager()->referencedRelations( mLayer.data() );
178-
const auto constRelations = relations;
179-
for ( const QgsRelation &relation : constRelations )
180-
{
181-
if ( attributeCounter > 0 )
182-
properties += QLatin1String( ",\n" );
183-
184-
QgsFeatureRequest req = relation.getRelatedFeaturesRequest( feature );
185-
req.setFlags( QgsFeatureRequest::NoGeometry );
186-
QgsVectorLayer *childLayer = relation.referencingLayer();
187-
QString relatedFeatureAttributes;
188-
if ( childLayer )
189-
{
190-
QgsFeatureIterator it = childLayer->getFeatures( req );
191-
QVector<QVariant> attributeWidgetCaches;
192-
int fieldIndex = 0;
193-
const QgsFields fields = childLayer->fields();
194-
for ( const QgsField &field : fields )
195-
{
196-
QgsEditorWidgetSetup setup = field.editorWidgetSetup();
197-
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
198-
attributeWidgetCaches.append( fieldFormatter->createCache( childLayer, fieldIndex, setup.config() ) );
199-
fieldIndex++;
200-
}
201-
202-
QgsFeature relatedFet;
203-
int relationFeatures = 0;
204-
while ( it.nextFeature( relatedFet ) )
205-
{
206-
if ( relationFeatures > 0 )
207-
relatedFeatureAttributes += QLatin1String( ",\n" );
208-
209-
relatedFeatureAttributes += QgsJsonUtils::exportAttributes( relatedFet, childLayer, attributeWidgetCaches );
210-
relationFeatures++;
211-
}
212-
}
213-
relatedFeatureAttributes.prepend( '[' ).append( ']' );
214-
215-
properties += QStringLiteral( " \"%1\":%2" ).arg( relation.name(), relatedFeatureAttributes );
216-
attributeCounter++;
217-
}
218-
}
219-
}
220-
221-
bool hasProperties = attributeCounter > 0;
222-
223-
s += QLatin1String( " \"properties\":" );
224-
if ( hasProperties )
225-
{
226-
//read all attribute values from the feature
227-
s += "{\n" + properties + "\n }\n";
228-
}
229-
else
230-
{
231-
s += QLatin1String( "null\n" );
232-
}
233-
234-
s += '}';
235-
236-
return s;
74+
return QString::fromStdString( exportFeatureToJsonObject( feature, extraProperties, id ).dump() );
23775
}
23876

23977
json QgsJsonExporter::exportFeatureToJsonObject( const QgsFeature &feature, const QVariantMap &extraProperties, const QVariant &id ) const

‎src/server/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ INCLUDE_DIRECTORIES(
116116
${CMAKE_BINARY_DIR}/src/python
117117
${CMAKE_BINARY_DIR}/src/analysis
118118
${CMAKE_BINARY_DIR}/src/server
119+
${CMAKE_SOURCE_DIR}/external
119120
)
120121

121122
ADD_LIBRARY(qgis_server SHARED ${QGIS_SERVER_SRCS} ${QGIS_SERVER_MOC_SRCS} ${QGIS_SERVER_HDRS} ${QGIS_SERVER_MOC_HDRS})

‎src/server/services/DummyService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ INCLUDE_DIRECTORIES(
1919
${CMAKE_BINARY_DIR}/src/analysis
2020
${CMAKE_BINARY_DIR}/src/server
2121
${CMAKE_CURRENT_BINARY_DIR}
22+
${CMAKE_SOURCE_DIR}/external
2223
../../../core
2324
../../../core/expression
2425
../../../core/geometry

‎src/server/services/wcs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ INCLUDE_DIRECTORIES(
2727
${CMAKE_BINARY_DIR}/src/analysis
2828
${CMAKE_BINARY_DIR}/src/server
2929
${CMAKE_CURRENT_BINARY_DIR}
30+
${CMAKE_SOURCE_DIR}/external
3031
../../../core
3132
../../../core/dxf
3233
../../../core/expression

‎src/server/services/wfs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ INCLUDE_DIRECTORIES(
3737
${CMAKE_BINARY_DIR}/src/analysis
3838
${CMAKE_BINARY_DIR}/src/server
3939
${CMAKE_CURRENT_BINARY_DIR}
40+
${CMAKE_SOURCE_DIR}/external
4041
../../../core
4142
../../../core/dxf
4243
../../../core/expression

‎src/server/services/wms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ INCLUDE_DIRECTORIES(
6666
${CMAKE_BINARY_DIR}/src/analysis
6767
${CMAKE_BINARY_DIR}/src/server
6868
${CMAKE_CURRENT_BINARY_DIR}
69+
${CMAKE_SOURCE_DIR}/external
6970
)
7071

7172

‎src/server/services/wmts/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ INCLUDE_DIRECTORIES(
3434
${CMAKE_BINARY_DIR}/src/analysis
3535
${CMAKE_BINARY_DIR}/src/server
3636
${CMAKE_CURRENT_BINARY_DIR}
37+
${CMAKE_SOURCE_DIR}/external
3738
../wms
3839
../../../core
3940
../../../core/dxf

‎tests/src/core/testqgsjsonutils.cpp

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ class TestQgsJsonUtils : public QObject
129129
void testExportFeatureJson()
130130
{
131131

132-
QFETCH( enum JsonAlgs, JsonAlgs );
133132

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

139138
QgsJsonExporter exporter { &vl };
140139

141-
if ( JsonAlgs == JsonAlgs::Json )
142-
{
143-
QBENCHMARK
144-
{
145-
const auto j { exporter.exportFeatureToJsonObject( feature ) };
146-
QCOMPARE( QString::fromStdString( j.dump() ), QStringLiteral( "{\"bbox\":[[1.12,1.12,5.45,5.33]],\"geometry\":{\"coordinates\":"
147-
"[[[1.12,1.34],[5.45,1.12],[5.34,5.33],[1.56,5.2],[1.12,1.34]],"
148-
"[[2.0,2.0],[3.0,2.0],[3.0,3.0],[2.0,3.0],[2.0,2.0]]],\"type\":\"Polygon\"}"
149-
",\"id\":0,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
150-
",\"type\":\"Feature\"}"
151-
) );
152-
}
153-
}
154-
else
155-
{
156-
QBENCHMARK
157-
{
158-
const auto json { exporter.exportFeature( feature ) };
159-
QCOMPARE( json, QStringLiteral( "{\n \"type\":\"Feature\",\n \"id\":0,\n \"bbox\":[1.12, 1.12, 5.45, 5.33],\n \"geometry\":\n "
160-
"{\"type\": \"Polygon\", \"coordinates\": [[ [1.12, 1.34], [5.45, 1.12], [5.34, 5.33], [1.56, 5.2], [1.12, 1.34]], "
161-
"[ [2, 2], [3, 2], [3, 3], [2, 3], [2, 2]]] },\n "
162-
"\"properties\":{\n \"fldtxt\":\"a value\",\n \"fldint\":1,\n \"flddbl\":2\n }\n}" ) );
163-
}
164-
}
165-
}
140+
const auto expectedJson { QStringLiteral( "{\"bbox\":[[1.12,1.12,5.45,5.33]],\"geometry\":{\"coordinates\":"
141+
"[[[1.12,1.34],[5.45,1.12],[5.34,5.33],[1.56,5.2],[1.12,1.34]],"
142+
"[[2.0,2.0],[3.0,2.0],[3.0,3.0],[2.0,3.0],[2.0,2.0]]],\"type\":\"Polygon\"}"
143+
",\"id\":0,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
144+
",\"type\":\"Feature\"}" ) };
166145

167-
void testExportGeomToJson_data()
168-
{
169-
QTest::addColumn<JsonAlgs>( "JsonAlgs" );
170-
QTest::newRow( "Use json" ) << JsonAlgs::Json;
171-
QTest::newRow( "Use old string concat" ) << JsonAlgs::String;
146+
const auto j { exporter.exportFeatureToJsonObject( feature ) };
147+
QCOMPARE( QString::fromStdString( j.dump() ), expectedJson );
148+
const auto json { exporter.exportFeature( feature ) };
149+
QCOMPARE( json, expectedJson );
172150
}
173151

152+
174153
void testExportGeomToJson()
175154
{
176155
const QStringList testWkts
@@ -191,33 +170,17 @@ class TestQgsJsonUtils : public QObject
191170
{
192171
const auto g { QgsGeometry::fromWkt( w ) };
193172
QVERIFY( !g.isNull( ) );
194-
QCOMPARE( QJsonDocument::fromJson( QByteArray::fromStdString( g.asJsonObject( 3 ).dump() ) ).toJson( QJsonDocument::JsonFormat::Compact ),
195-
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() ).toJson( QJsonDocument::JsonFormat::Compact ) );
173+
QCOMPARE( QJsonDocument::fromJson( QByteArray::fromStdString( g.asJsonObject( 3 ).dump() ) )
174+
.toJson( QJsonDocument::JsonFormat::Compact ),
175+
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() )
176+
.toJson( QJsonDocument::JsonFormat::Compact ) );
177+
const auto outp { QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() )
178+
.toJson( QJsonDocument::JsonFormat::Compact ) };
179+
qDebug() << QStringLiteral( "{ \"%1\", R\"json(%2)json\" }, " )
180+
.arg( w )
181+
.arg( QString( outp ) );
196182
}
197183

198-
QFETCH( enum JsonAlgs, JsonAlgs );
199-
if ( JsonAlgs == JsonAlgs::Json )
200-
{
201-
QBENCHMARK
202-
{
203-
for ( const auto &w : testWkts )
204-
{
205-
const auto g { QgsGeometry::fromWkt( w ) };
206-
QJsonDocument::fromJson( QByteArray::fromStdString( g.asJsonObject( 3 ).dump() ) ).toJson( QJsonDocument::JsonFormat::Compact );
207-
}
208-
}
209-
}
210-
else
211-
{
212-
QBENCHMARK
213-
{
214-
for ( const auto &w : testWkts )
215-
{
216-
const auto g { QgsGeometry::fromWkt( w ) };
217-
QJsonDocument::fromJson( g.asJson( 3 ).toUtf8() ).toJson( QJsonDocument::JsonFormat::Compact );
218-
}
219-
}
220-
}
221184
}
222185
};
223186

0 commit comments

Comments
 (0)
Please sign in to comment.