64
64
#include < QEventLoop>
65
65
#include < QTextCodec>
66
66
#include < QThread>
67
+ #ifdef WITH_QTSCRIPT
67
68
#include < QScriptEngine>
68
69
#include < QScriptValue>
69
70
#include < QScriptValueIterator>
71
+ #endif
70
72
#include < QNetworkDiskCache>
71
73
#include < QTimer>
72
74
@@ -2972,6 +2974,11 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa
2972
2974
else if ( jsonPart != -1 )
2973
2975
{
2974
2976
QString json = QString::fromUtf8 ( mIdentifyResultBodies .value ( jsonPart ) );
2977
+
2978
+ QgsFeatureStoreList featureStoreList;
2979
+ QgsCoordinateTransform coordinateTransform;
2980
+
2981
+ #ifdef WITH_QTSCRIPT
2975
2982
json.prepend ( ' (' ).append ( ' )' );
2976
2983
2977
2984
QScriptEngine engine;
@@ -2981,9 +2988,6 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa
2981
2988
2982
2989
QScriptValue result = engine.evaluate ( json );
2983
2990
2984
- QgsFeatureStoreList featureStoreList;
2985
- QgsCoordinateTransform coordinateTransform;
2986
-
2987
2991
try
2988
2992
{
2989
2993
QgsDebugMsg ( QString ( " result:%1" ).arg ( result.toString () ) );
@@ -3097,6 +3101,125 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa
3097
3101
QgsDebugMsg ( QString ( " JSON error: %1\n Result: %2" ).arg ( err, QString::fromUtf8 ( mIdentifyResultBodies .value ( jsonPart ) ) ) );
3098
3102
results.insert ( results.size (), err ); // string returned for format type "feature" means error
3099
3103
}
3104
+ #else
3105
+ try
3106
+ {
3107
+ QJsonDocument doc = QJsonDocument::fromJson ( json.toUtf8 () );
3108
+ if ( doc.isNull () )
3109
+ throw QStringLiteral ( " Doc expected" );
3110
+ if ( !doc.isObject () )
3111
+ throw QStringLiteral ( " Object expected" );
3112
+
3113
+ QJsonObject result = doc.object ();
3114
+ if ( result.value ( QLatin1String ( " type" ) ).toString () != QLatin1String ( " FeatureCollection" ) )
3115
+ throw QStringLiteral ( " Type FeatureCollection expected: %1" ).arg ( result.value ( QLatin1String ( " type" ) ).toString () );
3116
+
3117
+ if ( result.value ( QLatin1String ( " crs" ) ).isObject () )
3118
+ {
3119
+ QString crsType = result.value ( QLatin1String ( " crs" ) ).toObject ().value ( QLatin1String ( " type" ) ).toString ();
3120
+ QString crsText;
3121
+ if ( crsType == QLatin1String ( " name" ) )
3122
+ crsText = result.value ( QStringLiteral ( " crs" ) ).toObject ().value ( QLatin1String ( " properties" ) ).toObject ().value ( QLatin1String ( " name" ) ).toString ();
3123
+ else if ( crsType == QLatin1String ( " EPSG" ) )
3124
+ crsText = QStringLiteral ( " %1:%2" ).arg ( crsType, result.value ( QLatin1String ( " crs" ) ).toObject ().value ( QLatin1String ( " properties" ) ).toObject ().value ( QStringLiteral ( " code" ) ).toString () );
3125
+ else
3126
+ {
3127
+ QgsDebugMsg ( QStringLiteral ( " crs not supported:%1" ).arg ( result.value ( QLatin1String ( " crs" ) ).toString () ) );
3128
+ }
3129
+
3130
+ QgsCoordinateReferenceSystem featuresCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs ( crsText );
3131
+
3132
+ if ( !featuresCrs.isValid () )
3133
+ throw QStringLiteral ( " CRS %1 invalid" ).arg ( crsText );
3134
+
3135
+ if ( featuresCrs.isValid () && featuresCrs != crs () )
3136
+ {
3137
+ coordinateTransform = QgsCoordinateTransform ( featuresCrs, crs () );
3138
+ }
3139
+ }
3140
+
3141
+ const QJsonValue fc = result.value ( QLatin1String ( " features" ) );
3142
+ if ( !fc.isArray () )
3143
+ throw QStringLiteral ( " FeatureCollection array expected" );
3144
+
3145
+ const QJsonArray features = fc.toArray ();
3146
+
3147
+ int i = -1 ;
3148
+ for ( const QJsonValue &fv : features )
3149
+ {
3150
+ ++i;
3151
+ const QJsonObject f = fv.toObject ();
3152
+ const QJsonValue props = f.value ( QLatin1String ( " properties" ) );
3153
+ if ( !props.isObject () )
3154
+ {
3155
+ QgsDebugMsg ( " no properties found" );
3156
+ continue ;
3157
+ }
3158
+
3159
+ QgsFields fields;
3160
+
3161
+ const QJsonObject properties = props.toObject ();
3162
+ auto fieldIterator = properties.constBegin ();
3163
+
3164
+ for ( ; fieldIterator != properties.constEnd (); ++fieldIterator )
3165
+ {
3166
+ fields.append ( QgsField ( fieldIterator.key (), QVariant::String ) );
3167
+ }
3168
+
3169
+ QgsFeature feature ( fields );
3170
+
3171
+ if ( f.value ( QLatin1String ( " geometry" ) ).isObject () )
3172
+ {
3173
+ QJsonDocument serializer ( f.value ( QLatin1String ( " geometry" ) ).toObject () );
3174
+ QString geom = serializer.toJson ( QJsonDocument::JsonFormat::Compact );
3175
+
3176
+ gdal::ogr_geometry_unique_ptr ogrGeom ( OGR_G_CreateGeometryFromJson ( geom.toUtf8 () ) );
3177
+ if ( ogrGeom )
3178
+ {
3179
+ int wkbSize = OGR_G_WkbSize ( ogrGeom.get () );
3180
+ unsigned char *wkb = new unsigned char [ wkbSize ];
3181
+ OGR_G_ExportToWkb ( ogrGeom.get (), ( OGRwkbByteOrder ) QgsApplication::endian (), wkb );
3182
+
3183
+ QgsGeometry g;
3184
+ g.fromWkb ( wkb, wkbSize );
3185
+ feature.setGeometry ( g );
3186
+
3187
+ if ( coordinateTransform.isValid () && feature.hasGeometry () )
3188
+ {
3189
+ QgsGeometry transformed = feature.geometry ();
3190
+ transformed.transform ( coordinateTransform );
3191
+ feature.setGeometry ( transformed );
3192
+ }
3193
+ }
3194
+ }
3195
+
3196
+ int j = 0 ;
3197
+ fieldIterator = properties.constBegin ();
3198
+ for ( ; fieldIterator != properties.constEnd (); ++fieldIterator )
3199
+ {
3200
+ feature.setAttribute ( j++, fieldIterator.value ().toString () );
3201
+ }
3202
+
3203
+ QgsFeatureStore featureStore ( fields, crs () );
3204
+
3205
+ QVariantMap params;
3206
+ params.insert ( QStringLiteral ( " sublayer" ), layerList[count] );
3207
+ params.insert ( QStringLiteral ( " featureType" ), QStringLiteral ( " %1_%2" ).arg ( count ).arg ( i ) );
3208
+ params.insert ( QStringLiteral ( " getFeatureInfoUrl" ), requestUrl.toString () );
3209
+ featureStore.setParams ( params );
3210
+
3211
+ feature.setValid ( true );
3212
+ featureStore.addFeature ( feature );
3213
+
3214
+ featureStoreList.append ( featureStore );
3215
+ }
3216
+ }
3217
+ catch ( const QString &err )
3218
+ {
3219
+ QgsDebugMsg ( QString ( " JSON error: %1\n Result: %2" ).arg ( err, QString::fromUtf8 ( mIdentifyResultBodies .value ( jsonPart ) ) ) );
3220
+ results.insert ( results.size (), err ); // string returned for format type "feature" means error
3221
+ }
3222
+ #endif
3100
3223
3101
3224
results.insert ( results.size (), qVariantFromValue ( featureStoreList ) );
3102
3225
}
0 commit comments