Skip to content

Commit ba4346b

Browse files
committed
Make deprecated QtScript library optional
1 parent ca75e8c commit ba4346b

File tree

2 files changed

+130
-4
lines changed

2 files changed

+130
-4
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,16 @@ IF(WITH_CORE)
285285
FIND_PACKAGE(Qt5Concurrent REQUIRED)
286286
FIND_PACKAGE(Qt5PrintSupport REQUIRED)
287287
FIND_PACKAGE(Qt5Positioning)
288+
FIND_PACKAGE(Qt5Script)
289+
IF(Qt5Script_FOUND)
290+
ADD_DEFINITIONS(-DWITH_QTSCRIPT)
291+
ENDIF()
288292
IF (WITH_QTWEBKIT)
289293
FIND_PACKAGE(Qt5WebKit REQUIRED)
290294
FIND_PACKAGE(Qt5WebKitWidgets REQUIRED)
291295
ENDIF(WITH_QTWEBKIT)
292296
FIND_PACKAGE(Qt5Test REQUIRED)
293297
FIND_PACKAGE(Qt5UiTools REQUIRED)
294-
FIND_PACKAGE(Qt5Script REQUIRED)
295298
FIND_PACKAGE(Qt5Sql REQUIRED)
296299
IF (WITH_3D)
297300
FIND_PACKAGE(Qt53DCore REQUIRED)

src/providers/wms/qgswmsprovider.cpp

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@
6464
#include <QEventLoop>
6565
#include <QTextCodec>
6666
#include <QThread>
67+
#ifdef WITH_QTSCRIPT
6768
#include <QScriptEngine>
6869
#include <QScriptValue>
6970
#include <QScriptValueIterator>
71+
#endif
7072
#include <QNetworkDiskCache>
7173
#include <QTimer>
7274

@@ -2972,6 +2974,11 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa
29722974
else if ( jsonPart != -1 )
29732975
{
29742976
QString json = QString::fromUtf8( mIdentifyResultBodies.value( jsonPart ) );
2977+
2978+
QgsFeatureStoreList featureStoreList;
2979+
QgsCoordinateTransform coordinateTransform;
2980+
2981+
#ifdef WITH_QTSCRIPT
29752982
json.prepend( '(' ).append( ')' );
29762983

29772984
QScriptEngine engine;
@@ -2981,9 +2988,6 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa
29812988

29822989
QScriptValue result = engine.evaluate( json );
29832990

2984-
QgsFeatureStoreList featureStoreList;
2985-
QgsCoordinateTransform coordinateTransform;
2986-
29872991
try
29882992
{
29892993
QgsDebugMsg( QString( "result:%1" ).arg( result.toString() ) );
@@ -3097,6 +3101,125 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa
30973101
QgsDebugMsg( QString( "JSON error: %1\nResult: %2" ).arg( err, QString::fromUtf8( mIdentifyResultBodies.value( jsonPart ) ) ) );
30983102
results.insert( results.size(), err ); // string returned for format type "feature" means error
30993103
}
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\nResult: %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
31003223

31013224
results.insert( results.size(), qVariantFromValue( featureStoreList ) );
31023225
}

0 commit comments

Comments
 (0)