Skip to content

Commit

Permalink
Make deprecated QtScript library optional
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 6, 2017
1 parent ca75e8c commit ba4346b
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Expand Up @@ -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)
Expand Down
129 changes: 126 additions & 3 deletions src/providers/wms/qgswmsprovider.cpp
Expand Up @@ -64,9 +64,11 @@
#include <QEventLoop>
#include <QTextCodec>
#include <QThread>
#ifdef WITH_QTSCRIPT
#include <QScriptEngine>
#include <QScriptValue>
#include <QScriptValueIterator>
#endif
#include <QNetworkDiskCache>
#include <QTimer>

Expand Down Expand Up @@ -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;
Expand All @@ -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() ) );
Expand Down Expand Up @@ -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 ) );
}
Expand Down

0 comments on commit ba4346b

Please sign in to comment.