152 QDomElement propsElem = pft->
dom().firstChildElement( u
"qgis"_s ).toElement().firstChildElement( u
"properties"_s );
153 if ( !propsElem.isNull() )
155 const QDomNodeList srsNodes = propsElem.elementsByTagName( u
"SpatialRefSys"_s );
157 QDomElement projElem;
158 if ( srsNodes.count() > 0 )
160 srsElem = srsNodes.at( 0 ).toElement();
161 const QDomNodeList projNodes = srsElem.elementsByTagName( u
"ProjectionsEnabled"_s );
162 if ( projNodes.count() == 0 )
164 projElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
165 projElem.setAttribute( u
"type"_s, u
"int"_s );
166 const QDomText projText = pft->
dom().createTextNode( u
"0"_s );
167 projElem.appendChild( projText );
168 srsElem.appendChild( projElem );
173 srsElem = pft->
dom().createElement( u
"SpatialRefSys"_s );
174 projElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
175 projElem.setAttribute( u
"type"_s, u
"int"_s );
176 const QDomText projText = pft->
dom().createTextNode( u
"0"_s );
177 projElem.appendChild( projText );
178 srsElem.appendChild( projElem );
179 propsElem.appendChild( srsElem );
185 const QDomNodeList canvasNodes = pft->
dom().elementsByTagName( u
"mapcanvas"_s );
186 if ( canvasNodes.count() > 0 )
188 const QDomElement canvasElem = canvasNodes.at( 0 ).toElement();
189 const QDomNodeList canvasSrsNodes = canvasElem.elementsByTagName( u
"spatialrefsys"_s );
190 if ( canvasSrsNodes.count() > 0 )
192 const QDomElement canvasSrsElem = canvasSrsNodes.at( 0 ).toElement();
197 const QDomNodeList proj4Nodes = canvasSrsElem.elementsByTagName( u
"proj4"_s );
198 if ( proj4Nodes.count() > 0 )
200 const QDomElement proj4Node = proj4Nodes.at( 0 ).toElement();
201 proj = proj4Node.text();
203 const QDomNodeList authidNodes = canvasSrsElem.elementsByTagName( u
"authid"_s );
204 if ( authidNodes.count() > 0 )
206 const QDomElement authidNode = authidNodes.at( 0 ).toElement();
207 authid = authidNode.text();
209 const QDomNodeList srsidNodes = canvasSrsElem.elementsByTagName( u
"srsid"_s );
210 if ( srsidNodes.count() > 0 )
212 const QDomElement srsidNode = srsidNodes.at( 0 ).toElement();
213 srsid = srsidNode.text();
217 const QDomNodeList oldProjectProj4Nodes = srsElem.elementsByTagName( u
"ProjectCRSProj4String"_s );
218 for (
int i = oldProjectProj4Nodes.count(); i >= 0; --i )
220 srsElem.removeChild( oldProjectProj4Nodes.at( i ) );
222 const QDomNodeList oldProjectCrsNodes = srsElem.elementsByTagName( u
"ProjectCrs"_s );
223 for (
int i = oldProjectCrsNodes.count(); i >= 0; --i )
225 srsElem.removeChild( oldProjectCrsNodes.at( i ) );
227 const QDomNodeList oldProjectCrsIdNodes = srsElem.elementsByTagName( u
"ProjectCRSID"_s );
228 for (
int i = oldProjectCrsIdNodes.count(); i >= 0; --i )
230 srsElem.removeChild( oldProjectCrsIdNodes.at( i ) );
232 const QDomNodeList projectionsEnabledNodes = srsElem.elementsByTagName( u
"ProjectionsEnabled"_s );
233 for (
int i = projectionsEnabledNodes.count(); i >= 0; --i )
235 srsElem.removeChild( projectionsEnabledNodes.at( i ) );
238 QDomElement proj4Elem = pft->
dom().createElement( u
"ProjectCRSProj4String"_s );
239 proj4Elem.setAttribute( u
"type"_s, u
"QString"_s );
240 const QDomText proj4Text = pft->
dom().createTextNode( proj );
241 proj4Elem.appendChild( proj4Text );
242 QDomElement projectCrsElem = pft->
dom().createElement( u
"ProjectCrs"_s );
243 projectCrsElem.setAttribute( u
"type"_s, u
"QString"_s );
244 const QDomText projectCrsText = pft->
dom().createTextNode( authid );
245 projectCrsElem.appendChild( projectCrsText );
246 QDomElement projectCrsIdElem = pft->
dom().createElement( u
"ProjectCRSID"_s );
247 projectCrsIdElem.setAttribute( u
"type"_s, u
"int"_s );
248 const QDomText srsidText = pft->
dom().createTextNode( srsid );
249 projectCrsIdElem.appendChild( srsidText );
250 QDomElement projectionsEnabledElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
251 projectionsEnabledElem.setAttribute( u
"type"_s, u
"int"_s );
252 const QDomText projectionsEnabledText = pft->
dom().createTextNode( u
"1"_s );
253 projectionsEnabledElem.appendChild( projectionsEnabledText );
254 srsElem.appendChild( proj4Elem );
255 srsElem.appendChild( projectCrsElem );
256 srsElem.appendChild( projectCrsIdElem );
257 srsElem.appendChild( projectionsEnabledElem );
259 const QDomNodeList srsNodes = propsElem.elementsByTagName( u
"SpatialRefSys"_s );
260 for (
int i = srsNodes.count(); i >= 0; --i )
262 propsElem.removeChild( srsNodes.at( i ) );
264 propsElem.appendChild( srsElem );
270 const QDomNodeList mapLayers = pft->
dom().elementsByTagName( u
"maplayer"_s );
272 for (
int mapLayerIndex = 0; mapLayerIndex < mapLayers.count(); ++mapLayerIndex )
274 QDomElement layerElem = mapLayers.at( mapLayerIndex ).toElement();
277 QDomElement fieldConfigurationElement = pft->
dom().createElement( u
"fieldConfiguration"_s );
278 layerElem.appendChild( fieldConfigurationElement );
280 const QDomNodeList editTypeNodes = layerElem.namedItem( u
"edittypes"_s ).childNodes();
281 QDomElement constraintExpressionsElem = pft->
dom().createElement( u
"constraintExpressions"_s );
282 layerElem.appendChild( constraintExpressionsElem );
284 for (
int i = 0; i < editTypeNodes.size(); ++i )
286 const QDomNode editTypeNode = editTypeNodes.at( i );
287 const QDomElement editTypeElement = editTypeNode.toElement();
289 QDomElement fieldElement = pft->
dom().createElement( u
"field"_s );
290 fieldConfigurationElement.appendChild( fieldElement );
292 const QString name = editTypeElement.attribute( u
"name"_s );
293 fieldElement.setAttribute( u
"name"_s, name );
294 QDomElement constraintExpressionElem = pft->
dom().createElement( u
"constraint"_s );
295 constraintExpressionElem.setAttribute( u
"field"_s, name );
296 constraintExpressionsElem.appendChild( constraintExpressionElem );
298 QDomElement editWidgetElement = pft->
dom().createElement( u
"editWidget"_s );
299 fieldElement.appendChild( editWidgetElement );
301 const QString ewv2Type = editTypeElement.attribute( u
"widgetv2type"_s );
302 editWidgetElement.setAttribute( u
"type"_s, ewv2Type );
304 const QDomElement ewv2CfgElem = editTypeElement.namedItem( u
"widgetv2config"_s ).toElement();
306 if ( !ewv2CfgElem.isNull() )
308 QDomElement editWidgetConfigElement = pft->
dom().createElement( u
"config"_s );
309 editWidgetElement.appendChild( editWidgetConfigElement );
311 QVariantMap editWidgetConfiguration;
313 const QDomNamedNodeMap configAttrs = ewv2CfgElem.attributes();
314 for (
int configIndex = 0; configIndex < configAttrs.count(); ++configIndex )
316 const QDomAttr configAttr = configAttrs.item( configIndex ).toAttr();
317 if ( configAttr.name() ==
"fieldEditable"_L1 )
319 editWidgetConfigElement.setAttribute( u
"fieldEditable"_s, configAttr.value() );
321 else if ( configAttr.name() ==
"labelOnTop"_L1 )
323 editWidgetConfigElement.setAttribute( u
"labelOnTop"_s, configAttr.value() );
325 else if ( configAttr.name() ==
"notNull"_L1 )
327 editWidgetConfigElement.setAttribute( u
"notNull"_s, configAttr.value() );
329 else if ( configAttr.name() ==
"constraint"_L1 )
331 constraintExpressionElem.setAttribute( u
"exp"_s, configAttr.value() );
333 else if ( configAttr.name() ==
"constraintDescription"_L1 )
335 constraintExpressionElem.setAttribute( u
"desc"_s, configAttr.value() );
339 editWidgetConfiguration.insert( configAttr.name(), configAttr.value() );
343 if ( ewv2Type ==
"ValueMap"_L1 )
345 const QDomNodeList configElements = ewv2CfgElem.childNodes();
347 for (
int configIndex = 0; configIndex < configElements.count(); ++configIndex )
349 const QDomElement configElem = configElements.at( configIndex ).toElement();
350 map.insert( configElem.attribute( u
"key"_s ), configElem.attribute( u
"value"_s ) );
352 editWidgetConfiguration.insert( u
"map"_s, map );
354 else if ( ewv2Type ==
"Photo"_L1 )
356 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
358 editWidgetConfiguration.insert( u
"DocumentViewer"_s, 1 );
359 editWidgetConfiguration.insert( u
"DocumentViewerHeight"_s, editWidgetConfiguration.value( u
"Height"_s ) );
360 editWidgetConfiguration.insert( u
"DocumentViewerWidth"_s, editWidgetConfiguration.value( u
"Width"_s ) );
361 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
363 else if ( ewv2Type ==
"FileName"_L1 )
365 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
367 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
369 else if ( ewv2Type ==
"WebView"_L1 )
371 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
373 editWidgetConfiguration.insert( u
"DocumentViewerHeight"_s, editWidgetConfiguration.value( u
"Height"_s ) );
374 editWidgetConfiguration.insert( u
"DocumentViewerWidth"_s, editWidgetConfiguration.value( u
"Width"_s ) );
375 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
389 const QDomNode noDataNode = rasterPropertiesElem.namedItem( u
"mNoDataValue"_s );
390 const QDomElement noDataElement = noDataNode.toElement();
391 if ( !noDataElement.text().isEmpty() )
394 QDomElement noDataElem = doc.createElement( u
"noData"_s );
396 QDomElement noDataRangeList = doc.createElement( u
"noDataRangeList"_s );
397 noDataRangeList.setAttribute( u
"bandNo"_s, 1 );
399 QDomElement noDataRange = doc.createElement( u
"noDataRange"_s );
400 noDataRange.setAttribute( u
"min"_s, noDataElement.text() );
401 noDataRange.setAttribute( u
"max"_s, noDataElement.text() );
402 noDataRangeList.appendChild( noDataRange );
404 noDataElem.appendChild( noDataRangeList );
406 parentNode.appendChild( noDataElem );
409 QDomElement rasterRendererElem = doc.createElement( u
"rasterrenderer"_s );
413 rasterRendererElem.setAttribute( u
"invertColor"_s, u
"0"_s );
414 const QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( u
"mInvertColor"_s );
415 if ( !invertColorElem.isNull() )
417 if ( invertColorElem.text() ==
"true"_L1 )
419 rasterRendererElem.setAttribute( u
"invertColor"_s, u
"1"_s );
424 rasterRendererElem.setAttribute( u
"opacity"_s, u
"1"_s );
425 const QDomElement transparencyElem = parentNode.firstChildElement( u
"transparencyLevelInt"_s );
426 if ( !transparencyElem.isNull() )
428 const double transparency = transparencyElem.text().toInt();
429 rasterRendererElem.setAttribute( u
"opacity"_s, QString::number( transparency / 255.0 ) );
433 rasterRendererElem.setAttribute( u
"alphaBand"_s, -1 );
436 const int grayBand =
rasterBandNumber( rasterPropertiesElem, u
"mGrayBandName"_s, rlayer );
439 QString drawingStyle = rasterPropertiesElem.firstChildElement( u
"mDrawingStyle"_s ).text();
446 if ( drawingStyle ==
"PalettedColor"_L1 )
448 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
449 const QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
451 for (
int i = 0; i < colorRampEntryList.size(); ++i )
453 const QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
454 const QString strValue = colorRampEntryElem.attribute( u
"value"_s );
455 const double value = strValue.toDouble();
456 if ( value < 0 || value > 10000 || !
qgsDoubleNear( value,
static_cast< int >( value ) ) )
458 QgsDebugMsgLevel( u
"forcing SingleBandPseudoColor value = %1"_s.arg( value ), 2 );
459 drawingStyle = u
"SingleBandPseudoColor"_s;
465 if ( drawingStyle ==
"SingleBandGray"_L1 )
467 rasterRendererElem.setAttribute( u
"type"_s, u
"singlebandgray"_s );
468 rasterRendererElem.setAttribute( u
"grayBand"_s, grayBand );
471 else if ( drawingStyle ==
"SingleBandPseudoColor"_L1 )
473 rasterRendererElem.setAttribute( u
"type"_s, u
"singlebandpseudocolor"_s );
474 rasterRendererElem.setAttribute( u
"band"_s, grayBand );
475 QDomElement newRasterShaderElem = doc.createElement( u
"rastershader"_s );
476 QDomElement newColorRampShaderElem = doc.createElement( u
"colorrampshader"_s );
477 newRasterShaderElem.appendChild( newColorRampShaderElem );
478 rasterRendererElem.appendChild( newRasterShaderElem );
481 const QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( u
"mColorShadingAlgorithm"_s ).text();
482 if ( colorShadingAlgorithm ==
"PseudoColorShader"_L1 || colorShadingAlgorithm ==
"FreakOutShader"_L1 )
484 newColorRampShaderElem.setAttribute( u
"colorRampType"_s, u
"INTERPOLATED"_s );
490 const double breakSize = ( maxValue - minValue ) / 3;
492 QStringList colorList;
493 if ( colorShadingAlgorithm ==
"FreakOutShader"_L1 )
495 colorList << u
"#ff00ff"_s << u
"#00ffff"_s << u
"#ff0000"_s << u
"#00ff00"_s;
499 colorList << u
"#0000ff"_s << u
"#00ffff"_s << u
"#ffff00"_s << u
"#ff0000"_s;
501 QStringList::const_iterator colorIt = colorList.constBegin();
502 double boundValue = minValue;
503 for ( ; colorIt != colorList.constEnd(); ++colorIt )
505 QDomElement newItemElem = doc.createElement( u
"item"_s );
506 newItemElem.setAttribute( u
"value"_s, QString::number( boundValue ) );
507 newItemElem.setAttribute( u
"label"_s, QString::number( boundValue ) );
508 newItemElem.setAttribute( u
"color"_s, *colorIt );
509 newColorRampShaderElem.appendChild( newItemElem );
510 boundValue += breakSize;
513 else if ( colorShadingAlgorithm ==
"ColorRampShader"_L1 )
515 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
516 const QString type = customColorRampElem.firstChildElement( u
"colorRampType"_s ).text();
517 newColorRampShaderElem.setAttribute( u
"colorRampType"_s, type );
518 const QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
520 QString value, label;
522 int red, green, blue;
523 QDomElement currentItemElem;
524 for (
int i = 0; i < colorNodeList.size(); ++i )
526 currentItemElem = colorNodeList.at( i ).toElement();
527 value = currentItemElem.attribute( u
"value"_s );
528 label = currentItemElem.attribute( u
"label"_s );
529 red = currentItemElem.attribute( u
"red"_s ).toInt();
530 green = currentItemElem.attribute( u
"green"_s ).toInt();
531 blue = currentItemElem.attribute( u
"blue"_s ).toInt();
532 newColor = QColor( red, green, blue );
533 QDomElement newItemElem = doc.createElement( u
"item"_s );
534 newItemElem.setAttribute( u
"value"_s, value );
535 newItemElem.setAttribute( u
"label"_s, label );
536 newItemElem.setAttribute( u
"color"_s, newColor.name() );
537 newColorRampShaderElem.appendChild( newItemElem );
541 else if ( drawingStyle ==
"PalettedColor"_L1 )
543 rasterRendererElem.setAttribute( u
"type"_s, u
"paletted"_s );
544 rasterRendererElem.setAttribute( u
"band"_s, grayBand );
545 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
546 const QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
547 QDomElement newColorPaletteElem = doc.createElement( u
"colorPalette"_s );
553 QDomElement colorRampEntryElem;
554 for (
int i = 0; i < colorRampEntryList.size(); ++i )
556 colorRampEntryElem = colorRampEntryList.at( i ).toElement();
557 QDomElement newPaletteElem = doc.createElement( u
"paletteEntry"_s );
558 value =
static_cast< int >( colorRampEntryElem.attribute( u
"value"_s ).toDouble() );
559 newPaletteElem.setAttribute( u
"value"_s, value );
560 red = colorRampEntryElem.attribute( u
"red"_s ).toInt();
561 green = colorRampEntryElem.attribute( u
"green"_s ).toInt();
562 blue = colorRampEntryElem.attribute( u
"blue"_s ).toInt();
563 newPaletteElem.setAttribute( u
"color"_s, QColor( red, green, blue ).name() );
564 const QString label = colorRampEntryElem.attribute( u
"label"_s );
565 if ( !label.isEmpty() )
567 newPaletteElem.setAttribute( u
"label"_s, label );
569 newColorPaletteElem.appendChild( newPaletteElem );
571 rasterRendererElem.appendChild( newColorPaletteElem );
573 else if ( drawingStyle ==
"MultiBandColor"_L1 )
575 rasterRendererElem.setAttribute( u
"type"_s, u
"multibandcolor"_s );
578 const int redBand =
rasterBandNumber( rasterPropertiesElem, u
"mRedBandName"_s, rlayer );
579 const int greenBand =
rasterBandNumber( rasterPropertiesElem, u
"mGreenBandName"_s, rlayer );
580 const int blueBand =
rasterBandNumber( rasterPropertiesElem, u
"mBlueBandName"_s, rlayer );
581 rasterRendererElem.setAttribute( u
"redBand"_s, redBand );
582 rasterRendererElem.setAttribute( u
"greenBand"_s, greenBand );
583 rasterRendererElem.setAttribute( u
"blueBand"_s, blueBand );
593 if ( !parentNode.isNull() )
595 parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
632 if ( rasterproperties.isNull() || rendererElem.isNull() )
637 double minimumValue = 0;
638 double maximumValue = 0;
639 const QDomElement contrastMinMaxElem = rasterproperties.firstChildElement( u
"contrastEnhancementMinMaxValues"_s );
640 if ( contrastMinMaxElem.isNull() )
645 const QDomElement contrastEnhancementAlgorithmElem = rasterproperties.firstChildElement( u
"mContrastEnhancementAlgorithm"_s );
646 if ( contrastEnhancementAlgorithmElem.isNull() )
652 int algorithmEnum = 0;
653 const QString algorithmString = contrastEnhancementAlgorithmElem.
text();
654 if ( algorithmString ==
"StretchToMinimumMaximum"_L1 )
658 else if ( algorithmString ==
"StretchAndClipToMinimumMaximum"_L1 )
662 else if ( algorithmString ==
"ClipToMinimumMaximum"_L1 )
666 else if ( algorithmString ==
"UserDefinedEnhancement"_L1 )
671 const QDomNodeList minMaxEntryList = contrastMinMaxElem.elementsByTagName( u
"minMaxEntry"_s );
672 QStringList enhancementNameList;
673 if ( minMaxEntryList.size() == 1 )
675 enhancementNameList << u
"contrastEnhancement"_s;
677 if ( minMaxEntryList.size() == 3 )
679 enhancementNameList << u
"redContrastEnhancement"_s << u
"greenContrastEnhancement"_s << u
"blueContrastEnhancement"_s;
681 if ( minMaxEntryList.size() > enhancementNameList.size() )
686 QDomElement minMaxEntryElem;
687 for (
int i = 0; i < minMaxEntryList.size(); ++i )
689 minMaxEntryElem = minMaxEntryList.at( i ).toElement();
690 const QDomElement minElem = minMaxEntryElem.firstChildElement( u
"min"_s );
691 if ( minElem.isNull() )
695 minimumValue = minElem.text().toDouble();
697 const QDomElement maxElem = minMaxEntryElem.firstChildElement( u
"max"_s );
698 if ( maxElem.isNull() )
702 maximumValue = maxElem.text().toDouble();
704 QDomElement newContrastEnhancementElem = doc.createElement( enhancementNameList.at( i ) );
705 QDomElement newMinValElem = doc.createElement( u
"minValue"_s );
706 const QDomText minText = doc.createTextNode( QString::number( minimumValue ) );
707 newMinValElem.appendChild( minText );
708 newContrastEnhancementElem.appendChild( newMinValElem );
709 QDomElement newMaxValElem = doc.createElement( u
"maxValue"_s );
710 const QDomText maxText = doc.createTextNode( QString::number( maximumValue ) );
711 newMaxValElem.appendChild( maxText );
712 newContrastEnhancementElem.appendChild( newMaxValElem );
714 QDomElement newAlgorithmElem = doc.createElement( u
"algorithm"_s );
715 const QDomText newAlgorithmText = doc.createTextNode( QString::number( algorithmEnum ) );
716 newAlgorithmElem.appendChild( newAlgorithmText );
717 newContrastEnhancementElem.appendChild( newAlgorithmElem );
719 rendererElem.appendChild( newContrastEnhancementElem );