Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create converter for ArcGis FeatureServer type classBreaks #58058

Merged
merged 24 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e6104b3
Create converter for ArcGis FeatureServer type classBreaks
merydian May 29, 2024
29549c6
Use "" instead of <> for imports
merydian Jul 11, 2024
9d4e789
Make esriMode from classificationMethod be available
merydian Jul 11, 2024
2e1f0fd
Make esriMode mutable
merydian Jul 11, 2024
983c5bd
Replace QString with QLatin1String in mode setting
merydian Jul 11, 2024
57623de
Add else to if statements in mode setting
merydian Jul 11, 2024
616bf93
Throw error when ESRI classification mode is not currently supported
merydian Jul 11, 2024
97c4d49
Iterate stops directly
merydian Jul 11, 2024
a750d06
Convert to float instead of double
merydian Jul 11, 2024
e42d395
Convert to double instead of float
merydian Jul 11, 2024
e5b7d71
Convert to double instead of float
merydian Jul 11, 2024
137369d
Remove nullptr from lastLabel variable
merydian Jul 11, 2024
3abf376
Set lastValue to minValue instead of 0
merydian Jul 11, 2024
4ea6bd7
Convert to double instead of float
merydian Jul 16, 2024
3e139ed
Search for classificationMethod in authoringInfo for esriMode
merydian Jul 16, 2024
27125ba
Fix test json to fix graduatedRenderer test
merydian Jul 16, 2024
02edfbc
Test ranges and their min/max values
merydian Jul 16, 2024
e67693d
Run astyle_all.sh
merydian Jul 16, 2024
05c8b4c
Apply suggestions from code review
nyalldawson Jul 17, 2024
0164a2e
Update src/core/providers/arcgis/qgsarcgisrestutils.cpp
nyalldawson Jul 17, 2024
a508f18
Update src/core/providers/arcgis/qgsarcgisrestutils.cpp
nyalldawson Jul 17, 2024
39f0643
Update tests/src/python/test_provider_afs.py
nyalldawson Jul 17, 2024
aea2575
Fix potential leak
nyalldawson Jul 17, 2024
78b40c2
Don't store invalid transparency values
nyalldawson Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 121 additions & 2 deletions src/core/providers/arcgis/qgsarcgisrestutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@

#include <QRegularExpression>
#include <QUrl>
#include <qgsapplication.h>
#include <qgsclassificationcustom.h>
#include <qgsclassificationequalinterval.h>
#include <qgsclassificationfixedinterval.h>
#include <qgsclassificationjenks.h>
#include <qgsclassificationquantile.h>
#include <qgsclassificationstandarddeviation.h>
#include <qgsgraduatedsymbolrenderer.h>
merydian marked this conversation as resolved.
Show resolved Hide resolved

QMetaType::Type QgsArcGisRestUtils::convertFieldType( const QString &esriFieldType )
{
Expand Down Expand Up @@ -945,8 +953,119 @@ QgsFeatureRenderer *QgsArcGisRestUtils::convertRenderer( const QVariantMap &rend
}
else if ( type == QLatin1String( "classBreaks" ) )
{
// currently unsupported
return nullptr;
const QString attrName = rendererData.value( QStringLiteral( "field" ) ).toString();
QgsGraduatedSymbolRenderer* graduatedRenderer = new QgsGraduatedSymbolRenderer(attrName);

const QVariantList classBreakInfos = rendererData.value( QStringLiteral( "classBreakInfos" ) ).toList();
const QVariantMap authoringInfo = rendererData.value( QStringLiteral( "authoringInfo" ) ).toMap();
QVariantMap symbolData;

const QString esriMode = rendererData.value( QStringLiteral( "authoringInfo" ) ).toString();
merydian marked this conversation as resolved.
Show resolved Hide resolved
if (esriMode.isEmpty()){
const QString esriMode = rendererData.value( QStringLiteral( "classificationMethod" ) ).toString();
merydian marked this conversation as resolved.
Show resolved Hide resolved
}


if (esriMode == QString("esriClassifyDefinedInterval")){
merydian marked this conversation as resolved.
Show resolved Hide resolved
QgsClassificationFixedInterval* method = new QgsClassificationFixedInterval();
graduatedRenderer->setClassificationMethod(method);
}
if (esriMode == QString("esriClassifyEqualInterval")){
merydian marked this conversation as resolved.
Show resolved Hide resolved
QgsClassificationEqualInterval* method = new QgsClassificationEqualInterval();
graduatedRenderer->setClassificationMethod(method);
}
if (esriMode == QString("esriClassifyGeometricalInterval")){
QgsClassificationCustom* method = new QgsClassificationCustom();
graduatedRenderer->setClassificationMethod(method);
}
if (esriMode == QString("esriClassifyManual")){
QgsClassificationCustom* method = new QgsClassificationCustom();
graduatedRenderer->setClassificationMethod(method);
}
if (esriMode == QString("esriClassifyNaturalBreaks")){
QgsClassificationJenks* method = new QgsClassificationJenks();
graduatedRenderer->setClassificationMethod(method);
}
if (esriMode == QString("esriClassifyQuantile")){
QgsClassificationQuantile* method = new QgsClassificationQuantile();
graduatedRenderer->setClassificationMethod(method);
}
if (esriMode == QString("esriClassifyStandardDeviation")){
QgsClassificationStandardDeviation* method = new QgsClassificationStandardDeviation();
graduatedRenderer->setClassificationMethod(method);
}
merydian marked this conversation as resolved.
Show resolved Hide resolved


if ( !classBreakInfos.isEmpty() )
{
symbolData = classBreakInfos.at( 0 ).toMap().value( QStringLiteral( "symbol" ) ).toMap();
}
std::unique_ptr< QgsSymbol > symbol( QgsArcGisRestUtils::convertSymbol( symbolData ) );
double transparency = rendererData.value( QStringLiteral( "transparency" ) ).toFloat();
merydian marked this conversation as resolved.
Show resolved Hide resolved

double opacity = (100.0 - transparency) / 100.0;

if ( !symbol )
return nullptr;
else {
symbol->setOpacity(opacity);
graduatedRenderer->setSourceSymbol( symbol.release() );
}

const QVariantList visualVariablesData = rendererData.value( QStringLiteral( "visualVariables" ) ).toList();
double lastValue = 0;
merydian marked this conversation as resolved.
Show resolved Hide resolved
for ( const QVariant& visualVariable : visualVariablesData )
{
const QVariantList stops = visualVariable.toMap().value( QStringLiteral( "stops" ) ).toList();
QString lastLabel = nullptr;
merydian marked this conversation as resolved.
Show resolved Hide resolved

for (int i = 0; i < stops.size(); ++i)
{
const QVariant& stop = stops.at(i);
merydian marked this conversation as resolved.
Show resolved Hide resolved
const QVariantMap stopData = stop.toMap();
const QString label = stopData.value( QStringLiteral( "label" ) ).toString();
const double breakpoint = stopData.value( QStringLiteral( "value" ) ).toFloat();
merydian marked this conversation as resolved.
Show resolved Hide resolved
std::unique_ptr< QgsSymbol > symbolForStop( graduatedRenderer->sourceSymbol()->clone() );

if ( visualVariable.toMap().value( QStringLiteral( "type" ) ).toString() == QStringLiteral( "colorInfo" ) )
{
// handle color change stops:
QColor fillColor = convertColor( stopData.value( QStringLiteral( "color" ) ) );
symbolForStop->setColor( fillColor );

QgsRendererRange range;

range.setLowerValue( lastValue );
range.setUpperValue( breakpoint );
range.setLabel( label );
range.setSymbol( symbolForStop.release() );

lastValue = breakpoint;
graduatedRenderer->addClass( range );
}
}
}
lastValue = 0;
for ( const QVariant& classBreakInfo : classBreakInfos )
{
const QVariantMap symbolData = classBreakInfo.toMap().value( QStringLiteral( "symbol" ) ).toMap();
std::unique_ptr< QgsSymbol > symbol( QgsArcGisRestUtils::convertSymbol( symbolData ) );
double classMaxValue = classBreakInfo.toMap().value( QStringLiteral( "classMaxValue" ) ).toFloat();
merydian marked this conversation as resolved.
Show resolved Hide resolved
const QString label = classBreakInfo.toMap().value( QStringLiteral( "label" ) ).toString();

QgsRendererRange range;

range.setLowerValue( lastValue );
range.setUpperValue( classMaxValue );
range.setLabel( label );
range.setSymbol( symbol.release() );

lastValue = classMaxValue;
graduatedRenderer->addClass( range );
}


return graduatedRenderer;
}
else if ( type == QLatin1String( "heatmap" ) )
{
Expand Down
2 changes: 2 additions & 0 deletions src/providers/arcgisrest/qgsafsprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri, const ProviderOptions &optio
// renderer
mRendererDataMap = layerData.value( QStringLiteral( "drawingInfo" ) ).toMap().value( QStringLiteral( "renderer" ) ).toMap();
mLabelingDataList = layerData.value( QStringLiteral( "drawingInfo" ) ).toMap().value( QStringLiteral( "labelingInfo" ) ).toList();
const double transparency = layerData.value(QStringLiteral("drawingInfo")).toMap().value(QStringLiteral("transparency")).toFloat();
merydian marked this conversation as resolved.
Show resolved Hide resolved
mRendererDataMap.insert(QStringLiteral("transparency"), QVariant(transparency));
nyalldawson marked this conversation as resolved.
Show resolved Hide resolved

mValid = true;
}
Expand Down
178 changes: 176 additions & 2 deletions tests/src/python/test_provider_afs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
QgsVectorDataProviderTemporalCapabilities,
QgsVectorLayer,
QgsWkbTypes,
QgsGraduatedSymbolRenderer,
QgsSymbol,
)
import unittest
from qgis.testing import start_app, QgisTestCase
Expand Down Expand Up @@ -1122,8 +1124,8 @@ def testFieldAlias(self):
self.assertEqual(vl.fields().at(1).name(), 'second')
self.assertFalse(vl.fields().at(1).alias())

def testRenderer(self):
""" Test that renderer is correctly acquired from provider """
def testCategorizedRenderer(self):
""" Test that the categorized renderer is correctly acquired from provider """

endpoint = self.basetestpath + '/renderer_fake_qgis_http_endpoint'
with open(sanitize(endpoint, '?f=json'), 'wb') as f:
Expand Down Expand Up @@ -1223,6 +1225,178 @@ def testRenderer(self):
self.assertEqual(vl.renderer().categories()[0].value(), 'US')
self.assertEqual(vl.renderer().categories()[1].value(), 'Canada')

def testGraduatedRenderer(self):
""" Test that the graduated renderer is correctly acquired from provider """

endpoint = self.basetestpath + '/renderer_fake_qgis_http_endpoint'
nyalldawson marked this conversation as resolved.
Show resolved Hide resolved
with open(sanitize(endpoint, '?f=json'), 'wb') as f:
f.write(b"""{
"currentVersion": 11.2,
"id": 0,
"name": "Test graduated renderer",
"type": "Feature Layer",
"useStandardizedQueries": true,
"geometryType": "esriGeometryPolygon",
"minScale": 0,
"maxScale": 1155581,
"extent": {
"xmin": -17771274.9623,
"ymin": 2175061.919500001,
"xmax": -7521909.497300002,
"ymax": 9988155.384400003,
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
}
},
"drawingInfo": {
"renderer": {
"visualVariables": [
{
"type": "colorInfo",
"field": "SUM",
"valueExpression": null,
"stops": [
{
"value": 10151,
"color": [
255,
196,
174,
255
],
"label": "< 10,151"
},
{
"value": 632613.25,
"color": [
249,
129,
108,
255
],
"label": null
},
{
"value": 1255075.5,
"color": [
236,
82,
68,
255
],
"label": "1,255,075"
},
{
"value": 1877537.75,
"color": [
194,
61,
51,
255
],
"label": null
},
{
"value": 2500000,
"color": [
123,
66,
56,
255
],
"label": "> 2,500,000"
}
]
},
{
"type": "sizeInfo",
"target": "outline",
"expression": "view.scale",
"valueExpression": "$view.scale",
"stops": [
{
"size": 1.5,
"value": 3468153
},
{
"size": 0.75,
"value": 10837979
},
{
"size": 0.375,
"value": 43351915
},
{
"size": 0,
"value": 86703831
}
]
}
],
"authoringInfo": {
"visualVariables": [
{
"type": "colorInfo",
"minSliderValue": 10151,
"maxSliderValue": 15185477,
"theme": "high-to-low"
}
]
},
"type": "classBreaks",
"field": "SUM",
"minValue": -9007199254740991,
"classBreakInfos": [
{
"symbol": {
"color": [
170,
170,
170,
255
],
"outline": {
"color": [
194,
194,
194,
64
],
"width": 0.75,
"type": "esriSLS",
"style": "esriSLSSolid"
},
"type": "esriSFS",
"style": "esriSFSSolid"
},
"classMaxValue": 9007199254740991
}
]
},
"transparency": 20
},
"allowGeometryUpdates": true
}""")

with open(sanitize(endpoint, '/query?f=json_where=1=1&returnIdsOnly=true'), 'wb') as f:
f.write(b"""
{
"objectIdFieldName": "OBJECTID",
"objectIds": [
1
]
}
""")

# Create test layer
vl = QgsVectorLayer("url='http://" + endpoint + "' crs='epsg:3857'", 'test', 'arcgisfeatureserver')
self.assertTrue(vl.isValid())
self.assertIsNotNone(vl.dataProvider().createRenderer())
self.assertIsInstance(vl.renderer(), QgsGraduatedSymbolRenderer)
self.assertIsInstance(vl.renderer().sourceSymbol(), QgsSymbol)
merydian marked this conversation as resolved.
Show resolved Hide resolved


def testBboxRestriction(self):
"""
Test limiting provider to features within a preset bounding box
Expand Down
Loading