From ba4346b5c58dce0a17edc9c02bb6a4b0f175d918 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Thu, 7 Dec 2017 00:24:18 +0100 Subject: [PATCH] Make deprecated QtScript library optional --- CMakeLists.txt | 5 +- src/providers/wms/qgswmsprovider.cpp | 129 ++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b76c4f701c4..f8591b7bb94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,13 +285,16 @@ IF(WITH_CORE) FIND_PACKAGE(Qt5Concurrent REQUIRED) FIND_PACKAGE(Qt5PrintSupport REQUIRED) FIND_PACKAGE(Qt5Positioning) + FIND_PACKAGE(Qt5Script) + IF(Qt5Script_FOUND) + ADD_DEFINITIONS(-DWITH_QTSCRIPT) + ENDIF() IF (WITH_QTWEBKIT) FIND_PACKAGE(Qt5WebKit REQUIRED) FIND_PACKAGE(Qt5WebKitWidgets REQUIRED) ENDIF(WITH_QTWEBKIT) FIND_PACKAGE(Qt5Test REQUIRED) FIND_PACKAGE(Qt5UiTools REQUIRED) - FIND_PACKAGE(Qt5Script REQUIRED) FIND_PACKAGE(Qt5Sql REQUIRED) IF (WITH_3D) FIND_PACKAGE(Qt53DCore REQUIRED) diff --git a/src/providers/wms/qgswmsprovider.cpp b/src/providers/wms/qgswmsprovider.cpp index 613a52e90cb..9d1df73226b 100644 --- a/src/providers/wms/qgswmsprovider.cpp +++ b/src/providers/wms/qgswmsprovider.cpp @@ -64,9 +64,11 @@ #include #include #include +#ifdef WITH_QTSCRIPT #include #include #include +#endif #include #include @@ -2972,6 +2974,11 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa else if ( jsonPart != -1 ) { QString json = QString::fromUtf8( mIdentifyResultBodies.value( jsonPart ) ); + + QgsFeatureStoreList featureStoreList; + QgsCoordinateTransform coordinateTransform; + +#ifdef WITH_QTSCRIPT json.prepend( '(' ).append( ')' ); QScriptEngine engine; @@ -2981,9 +2988,6 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa QScriptValue result = engine.evaluate( json ); - QgsFeatureStoreList featureStoreList; - QgsCoordinateTransform coordinateTransform; - try { QgsDebugMsg( QString( "result:%1" ).arg( result.toString() ) ); @@ -3097,6 +3101,125 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPointXY &point, QgsRa QgsDebugMsg( QString( "JSON error: %1\nResult: %2" ).arg( err, QString::fromUtf8( mIdentifyResultBodies.value( jsonPart ) ) ) ); results.insert( results.size(), err ); // string returned for format type "feature" means error } +#else + try + { + QJsonDocument doc = QJsonDocument::fromJson( json.toUtf8() ); + if ( doc.isNull() ) + throw QStringLiteral( "Doc expected" ); + if ( !doc.isObject() ) + throw QStringLiteral( "Object expected" ); + + QJsonObject result = doc.object(); + if ( result.value( QLatin1String( "type" ) ).toString() != QLatin1String( "FeatureCollection" ) ) + throw QStringLiteral( "Type FeatureCollection expected: %1" ).arg( result.value( QLatin1String( "type" ) ).toString() ); + + if ( result.value( QLatin1String( "crs" ) ).isObject() ) + { + QString crsType = result.value( QLatin1String( "crs" ) ).toObject().value( QLatin1String( "type" ) ).toString(); + QString crsText; + if ( crsType == QLatin1String( "name" ) ) + crsText = result.value( QStringLiteral( "crs" ) ).toObject().value( QLatin1String( "properties" ) ).toObject().value( QLatin1String( "name" ) ).toString(); + else if ( crsType == QLatin1String( "EPSG" ) ) + crsText = QStringLiteral( "%1:%2" ).arg( crsType, result.value( QLatin1String( "crs" ) ).toObject().value( QLatin1String( "properties" ) ).toObject().value( QStringLiteral( "code" ) ).toString() ); + else + { + QgsDebugMsg( QStringLiteral( "crs not supported:%1" ).arg( result.value( QLatin1String( "crs" ) ).toString() ) ); + } + + QgsCoordinateReferenceSystem featuresCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( crsText ); + + if ( !featuresCrs.isValid() ) + throw QStringLiteral( "CRS %1 invalid" ).arg( crsText ); + + if ( featuresCrs.isValid() && featuresCrs != crs() ) + { + coordinateTransform = QgsCoordinateTransform( featuresCrs, crs() ); + } + } + + const QJsonValue fc = result.value( QLatin1String( "features" ) ); + if ( !fc.isArray() ) + throw QStringLiteral( "FeatureCollection array expected" ); + + const QJsonArray features = fc.toArray(); + + int i = -1; + for ( const QJsonValue &fv : features ) + { + ++i; + const QJsonObject f = fv.toObject(); + const QJsonValue props = f.value( QLatin1String( "properties" ) ); + if ( !props.isObject() ) + { + QgsDebugMsg( "no properties found" ); + continue; + } + + QgsFields fields; + + const QJsonObject properties = props.toObject(); + auto fieldIterator = properties.constBegin(); + + for ( ; fieldIterator != properties.constEnd(); ++fieldIterator ) + { + fields.append( QgsField( fieldIterator.key(), QVariant::String ) ); + } + + QgsFeature feature( fields ); + + if ( f.value( QLatin1String( "geometry" ) ).isObject() ) + { + QJsonDocument serializer( f.value( QLatin1String( "geometry" ) ).toObject() ); + QString geom = serializer.toJson( QJsonDocument::JsonFormat::Compact ); + + gdal::ogr_geometry_unique_ptr ogrGeom( OGR_G_CreateGeometryFromJson( geom.toUtf8() ) ); + if ( ogrGeom ) + { + int wkbSize = OGR_G_WkbSize( ogrGeom.get() ); + unsigned char *wkb = new unsigned char[ wkbSize ]; + OGR_G_ExportToWkb( ogrGeom.get(), ( OGRwkbByteOrder ) QgsApplication::endian(), wkb ); + + QgsGeometry g; + g.fromWkb( wkb, wkbSize ); + feature.setGeometry( g ); + + if ( coordinateTransform.isValid() && feature.hasGeometry() ) + { + QgsGeometry transformed = feature.geometry(); + transformed.transform( coordinateTransform ); + feature.setGeometry( transformed ); + } + } + } + + int j = 0; + fieldIterator = properties.constBegin(); + for ( ; fieldIterator != properties.constEnd(); ++fieldIterator ) + { + feature.setAttribute( j++, fieldIterator.value().toString() ); + } + + QgsFeatureStore featureStore( fields, crs() ); + + QVariantMap params; + params.insert( QStringLiteral( "sublayer" ), layerList[count] ); + params.insert( QStringLiteral( "featureType" ), QStringLiteral( "%1_%2" ).arg( count ).arg( i ) ); + params.insert( QStringLiteral( "getFeatureInfoUrl" ), requestUrl.toString() ); + featureStore.setParams( params ); + + feature.setValid( true ); + featureStore.addFeature( feature ); + + featureStoreList.append( featureStore ); + } + } + catch ( const QString &err ) + { + QgsDebugMsg( QString( "JSON error: %1\nResult: %2" ).arg( err, QString::fromUtf8( mIdentifyResultBodies.value( jsonPart ) ) ) ); + results.insert( results.size(), err ); // string returned for format type "feature" means error + } +#endif results.insert( results.size(), qVariantFromValue( featureStoreList ) ); }