151 QDomElement propsElem = pft->
dom().firstChildElement( u
"qgis"_s ).toElement().firstChildElement( u
"properties"_s );
152 if ( !propsElem.isNull() )
154 const QDomNodeList srsNodes = propsElem.elementsByTagName( u
"SpatialRefSys"_s );
156 QDomElement projElem;
157 if ( srsNodes.count() > 0 )
159 srsElem = srsNodes.at( 0 ).toElement();
160 const QDomNodeList projNodes = srsElem.elementsByTagName( u
"ProjectionsEnabled"_s );
161 if ( projNodes.count() == 0 )
163 projElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
164 projElem.setAttribute( u
"type"_s, u
"int"_s );
165 const QDomText projText = pft->
dom().createTextNode( u
"0"_s );
166 projElem.appendChild( projText );
167 srsElem.appendChild( projElem );
172 srsElem = pft->
dom().createElement( u
"SpatialRefSys"_s );
173 projElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
174 projElem.setAttribute( u
"type"_s, u
"int"_s );
175 const QDomText projText = pft->
dom().createTextNode( u
"0"_s );
176 projElem.appendChild( projText );
177 srsElem.appendChild( projElem );
178 propsElem.appendChild( srsElem );
184 const QDomNodeList canvasNodes = pft->
dom().elementsByTagName( u
"mapcanvas"_s );
185 if ( canvasNodes.count() > 0 )
187 const QDomElement canvasElem = canvasNodes.at( 0 ).toElement();
188 const QDomNodeList canvasSrsNodes = canvasElem.elementsByTagName( u
"spatialrefsys"_s );
189 if ( canvasSrsNodes.count() > 0 )
191 const QDomElement canvasSrsElem = canvasSrsNodes.at( 0 ).toElement();
196 const QDomNodeList proj4Nodes = canvasSrsElem.elementsByTagName( u
"proj4"_s );
197 if ( proj4Nodes.count() > 0 )
199 const QDomElement proj4Node = proj4Nodes.at( 0 ).toElement();
200 proj = proj4Node.text();
202 const QDomNodeList authidNodes = canvasSrsElem.elementsByTagName( u
"authid"_s );
203 if ( authidNodes.count() > 0 )
205 const QDomElement authidNode = authidNodes.at( 0 ).toElement();
206 authid = authidNode.text();
208 const QDomNodeList srsidNodes = canvasSrsElem.elementsByTagName( u
"srsid"_s );
209 if ( srsidNodes.count() > 0 )
211 const QDomElement srsidNode = srsidNodes.at( 0 ).toElement();
212 srsid = srsidNode.text();
216 const QDomNodeList oldProjectProj4Nodes = srsElem.elementsByTagName( u
"ProjectCRSProj4String"_s );
217 for (
int i = oldProjectProj4Nodes.count(); i >= 0; --i )
219 srsElem.removeChild( oldProjectProj4Nodes.at( i ) );
221 const QDomNodeList oldProjectCrsNodes = srsElem.elementsByTagName( u
"ProjectCrs"_s );
222 for (
int i = oldProjectCrsNodes.count(); i >= 0; --i )
224 srsElem.removeChild( oldProjectCrsNodes.at( i ) );
226 const QDomNodeList oldProjectCrsIdNodes = srsElem.elementsByTagName( u
"ProjectCRSID"_s );
227 for (
int i = oldProjectCrsIdNodes.count(); i >= 0; --i )
229 srsElem.removeChild( oldProjectCrsIdNodes.at( i ) );
231 const QDomNodeList projectionsEnabledNodes = srsElem.elementsByTagName( u
"ProjectionsEnabled"_s );
232 for (
int i = projectionsEnabledNodes.count(); i >= 0; --i )
234 srsElem.removeChild( projectionsEnabledNodes.at( i ) );
237 QDomElement proj4Elem = pft->
dom().createElement( u
"ProjectCRSProj4String"_s );
238 proj4Elem.setAttribute( u
"type"_s, u
"QString"_s );
239 const QDomText proj4Text = pft->
dom().createTextNode( proj );
240 proj4Elem.appendChild( proj4Text );
241 QDomElement projectCrsElem = pft->
dom().createElement( u
"ProjectCrs"_s );
242 projectCrsElem.setAttribute( u
"type"_s, u
"QString"_s );
243 const QDomText projectCrsText = pft->
dom().createTextNode( authid );
244 projectCrsElem.appendChild( projectCrsText );
245 QDomElement projectCrsIdElem = pft->
dom().createElement( u
"ProjectCRSID"_s );
246 projectCrsIdElem.setAttribute( u
"type"_s, u
"int"_s );
247 const QDomText srsidText = pft->
dom().createTextNode( srsid );
248 projectCrsIdElem.appendChild( srsidText );
249 QDomElement projectionsEnabledElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
250 projectionsEnabledElem.setAttribute( u
"type"_s, u
"int"_s );
251 const QDomText projectionsEnabledText = pft->
dom().createTextNode( u
"1"_s );
252 projectionsEnabledElem.appendChild( projectionsEnabledText );
253 srsElem.appendChild( proj4Elem );
254 srsElem.appendChild( projectCrsElem );
255 srsElem.appendChild( projectCrsIdElem );
256 srsElem.appendChild( projectionsEnabledElem );
258 const QDomNodeList srsNodes = propsElem.elementsByTagName( u
"SpatialRefSys"_s );
259 for (
int i = srsNodes.count(); i >= 0; --i )
261 propsElem.removeChild( srsNodes.at( i ) );
263 propsElem.appendChild( srsElem );
269 const QDomNodeList mapLayers = pft->
dom().elementsByTagName( u
"maplayer"_s );
271 for (
int mapLayerIndex = 0; mapLayerIndex < mapLayers.count(); ++mapLayerIndex )
273 QDomElement layerElem = mapLayers.at( mapLayerIndex ).toElement();
276 QDomElement fieldConfigurationElement = pft->
dom().createElement( u
"fieldConfiguration"_s );
277 layerElem.appendChild( fieldConfigurationElement );
279 const QDomNodeList editTypeNodes = layerElem.namedItem( u
"edittypes"_s ).childNodes();
280 QDomElement constraintExpressionsElem = pft->
dom().createElement( u
"constraintExpressions"_s );
281 layerElem.appendChild( constraintExpressionsElem );
283 for (
int i = 0; i < editTypeNodes.size(); ++i )
285 const QDomNode editTypeNode = editTypeNodes.at( i );
286 const QDomElement editTypeElement = editTypeNode.toElement();
288 QDomElement fieldElement = pft->
dom().createElement( u
"field"_s );
289 fieldConfigurationElement.appendChild( fieldElement );
291 const QString name = editTypeElement.attribute( u
"name"_s );
292 fieldElement.setAttribute( u
"name"_s, name );
293 QDomElement constraintExpressionElem = pft->
dom().createElement( u
"constraint"_s );
294 constraintExpressionElem.setAttribute( u
"field"_s, name );
295 constraintExpressionsElem.appendChild( constraintExpressionElem );
297 QDomElement editWidgetElement = pft->
dom().createElement( u
"editWidget"_s );
298 fieldElement.appendChild( editWidgetElement );
300 const QString ewv2Type = editTypeElement.attribute( u
"widgetv2type"_s );
301 editWidgetElement.setAttribute( u
"type"_s, ewv2Type );
303 const QDomElement ewv2CfgElem = editTypeElement.namedItem( u
"widgetv2config"_s ).toElement();
305 if ( !ewv2CfgElem.isNull() )
307 QDomElement editWidgetConfigElement = pft->
dom().createElement( u
"config"_s );
308 editWidgetElement.appendChild( editWidgetConfigElement );
310 QVariantMap editWidgetConfiguration;
312 const QDomNamedNodeMap configAttrs = ewv2CfgElem.attributes();
313 for (
int configIndex = 0; configIndex < configAttrs.count(); ++configIndex )
315 const QDomAttr configAttr = configAttrs.item( configIndex ).toAttr();
316 if ( configAttr.name() ==
"fieldEditable"_L1 )
318 editWidgetConfigElement.setAttribute( u
"fieldEditable"_s, configAttr.value() );
320 else if ( configAttr.name() ==
"labelOnTop"_L1 )
322 editWidgetConfigElement.setAttribute( u
"labelOnTop"_s, configAttr.value() );
324 else if ( configAttr.name() ==
"notNull"_L1 )
326 editWidgetConfigElement.setAttribute( u
"notNull"_s, configAttr.value() );
328 else if ( configAttr.name() ==
"constraint"_L1 )
330 constraintExpressionElem.setAttribute( u
"exp"_s, configAttr.value() );
332 else if ( configAttr.name() ==
"constraintDescription"_L1 )
334 constraintExpressionElem.setAttribute( u
"desc"_s, configAttr.value() );
338 editWidgetConfiguration.insert( configAttr.name(), configAttr.value() );
342 if ( ewv2Type ==
"ValueMap"_L1 )
344 const QDomNodeList configElements = ewv2CfgElem.childNodes();
346 for (
int configIndex = 0; configIndex < configElements.count(); ++configIndex )
348 const QDomElement configElem = configElements.at( configIndex ).toElement();
349 map.insert( configElem.attribute( u
"key"_s ), configElem.attribute( u
"value"_s ) );
351 editWidgetConfiguration.insert( u
"map"_s, map );
353 else if ( ewv2Type ==
"Photo"_L1 )
355 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
357 editWidgetConfiguration.insert( u
"DocumentViewer"_s, 1 );
358 editWidgetConfiguration.insert( u
"DocumentViewerHeight"_s, editWidgetConfiguration.value( u
"Height"_s ) );
359 editWidgetConfiguration.insert( u
"DocumentViewerWidth"_s, editWidgetConfiguration.value( u
"Width"_s ) );
360 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
362 else if ( ewv2Type ==
"FileName"_L1 )
364 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
366 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
368 else if ( ewv2Type ==
"WebView"_L1 )
370 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
372 editWidgetConfiguration.insert( u
"DocumentViewerHeight"_s, editWidgetConfiguration.value( u
"Height"_s ) );
373 editWidgetConfiguration.insert( u
"DocumentViewerWidth"_s, editWidgetConfiguration.value( u
"Width"_s ) );
374 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
387 const QDomNode noDataNode = rasterPropertiesElem.namedItem( u
"mNoDataValue"_s );
388 const QDomElement noDataElement = noDataNode.toElement();
389 if ( !noDataElement.text().isEmpty() )
392 QDomElement noDataElem = doc.createElement( u
"noData"_s );
394 QDomElement noDataRangeList = doc.createElement( u
"noDataRangeList"_s );
395 noDataRangeList.setAttribute( u
"bandNo"_s, 1 );
397 QDomElement noDataRange = doc.createElement( u
"noDataRange"_s );
398 noDataRange.setAttribute( u
"min"_s, noDataElement.text() );
399 noDataRange.setAttribute( u
"max"_s, noDataElement.text() );
400 noDataRangeList.appendChild( noDataRange );
402 noDataElem.appendChild( noDataRangeList );
404 parentNode.appendChild( noDataElem );
407 QDomElement rasterRendererElem = doc.createElement( u
"rasterrenderer"_s );
411 rasterRendererElem.setAttribute( u
"invertColor"_s, u
"0"_s );
412 const QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( u
"mInvertColor"_s );
413 if ( !invertColorElem.isNull() )
415 if ( invertColorElem.text() ==
"true"_L1 )
417 rasterRendererElem.setAttribute( u
"invertColor"_s, u
"1"_s );
422 rasterRendererElem.setAttribute( u
"opacity"_s, u
"1"_s );
423 const QDomElement transparencyElem = parentNode.firstChildElement( u
"transparencyLevelInt"_s );
424 if ( !transparencyElem.isNull() )
426 const double transparency = transparencyElem.text().toInt();
427 rasterRendererElem.setAttribute( u
"opacity"_s, QString::number( transparency / 255.0 ) );
431 rasterRendererElem.setAttribute( u
"alphaBand"_s, -1 );
434 const int grayBand =
rasterBandNumber( rasterPropertiesElem, u
"mGrayBandName"_s, rlayer );
437 QString drawingStyle = rasterPropertiesElem.firstChildElement( u
"mDrawingStyle"_s ).text();
444 if ( drawingStyle ==
"PalettedColor"_L1 )
446 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
447 const QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
449 for (
int i = 0; i < colorRampEntryList.size(); ++i )
451 const QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
452 const QString strValue = colorRampEntryElem.attribute( u
"value"_s );
453 const double value = strValue.toDouble();
454 if ( value < 0 || value > 10000 || !
qgsDoubleNear( value,
static_cast< int >( value ) ) )
456 QgsDebugMsgLevel( u
"forcing SingleBandPseudoColor value = %1"_s.arg( value ), 2 );
457 drawingStyle = u
"SingleBandPseudoColor"_s;
463 if ( drawingStyle ==
"SingleBandGray"_L1 )
465 rasterRendererElem.setAttribute( u
"type"_s, u
"singlebandgray"_s );
466 rasterRendererElem.setAttribute( u
"grayBand"_s, grayBand );
469 else if ( drawingStyle ==
"SingleBandPseudoColor"_L1 )
471 rasterRendererElem.setAttribute( u
"type"_s, u
"singlebandpseudocolor"_s );
472 rasterRendererElem.setAttribute( u
"band"_s, grayBand );
473 QDomElement newRasterShaderElem = doc.createElement( u
"rastershader"_s );
474 QDomElement newColorRampShaderElem = doc.createElement( u
"colorrampshader"_s );
475 newRasterShaderElem.appendChild( newColorRampShaderElem );
476 rasterRendererElem.appendChild( newRasterShaderElem );
479 const QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( u
"mColorShadingAlgorithm"_s ).text();
480 if ( colorShadingAlgorithm ==
"PseudoColorShader"_L1 || colorShadingAlgorithm ==
"FreakOutShader"_L1 )
482 newColorRampShaderElem.setAttribute( u
"colorRampType"_s, u
"INTERPOLATED"_s );
488 const double breakSize = ( maxValue - minValue ) / 3;
490 QStringList colorList;
491 if ( colorShadingAlgorithm ==
"FreakOutShader"_L1 )
493 colorList << u
"#ff00ff"_s << u
"#00ffff"_s << u
"#ff0000"_s << u
"#00ff00"_s;
497 colorList << u
"#0000ff"_s << u
"#00ffff"_s << u
"#ffff00"_s << u
"#ff0000"_s;
499 QStringList::const_iterator colorIt = colorList.constBegin();
500 double boundValue = minValue;
501 for ( ; colorIt != colorList.constEnd(); ++colorIt )
503 QDomElement newItemElem = doc.createElement( u
"item"_s );
504 newItemElem.setAttribute( u
"value"_s, QString::number( boundValue ) );
505 newItemElem.setAttribute( u
"label"_s, QString::number( boundValue ) );
506 newItemElem.setAttribute( u
"color"_s, *colorIt );
507 newColorRampShaderElem.appendChild( newItemElem );
508 boundValue += breakSize;
511 else if ( colorShadingAlgorithm ==
"ColorRampShader"_L1 )
513 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
514 const QString type = customColorRampElem.firstChildElement( u
"colorRampType"_s ).text();
515 newColorRampShaderElem.setAttribute( u
"colorRampType"_s, type );
516 const QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
518 QString value, label;
520 int red, green, blue;
521 QDomElement currentItemElem;
522 for (
int i = 0; i < colorNodeList.size(); ++i )
524 currentItemElem = colorNodeList.at( i ).toElement();
525 value = currentItemElem.attribute( u
"value"_s );
526 label = currentItemElem.attribute( u
"label"_s );
527 red = currentItemElem.attribute( u
"red"_s ).toInt();
528 green = currentItemElem.attribute( u
"green"_s ).toInt();
529 blue = currentItemElem.attribute( u
"blue"_s ).toInt();
530 newColor = QColor( red, green, blue );
531 QDomElement newItemElem = doc.createElement( u
"item"_s );
532 newItemElem.setAttribute( u
"value"_s, value );
533 newItemElem.setAttribute( u
"label"_s, label );
534 newItemElem.setAttribute( u
"color"_s, newColor.name() );
535 newColorRampShaderElem.appendChild( newItemElem );
539 else if ( drawingStyle ==
"PalettedColor"_L1 )
541 rasterRendererElem.setAttribute( u
"type"_s, u
"paletted"_s );
542 rasterRendererElem.setAttribute( u
"band"_s, grayBand );
543 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
544 const QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
545 QDomElement newColorPaletteElem = doc.createElement( u
"colorPalette"_s );
551 QDomElement colorRampEntryElem;
552 for (
int i = 0; i < colorRampEntryList.size(); ++i )
554 colorRampEntryElem = colorRampEntryList.at( i ).toElement();
555 QDomElement newPaletteElem = doc.createElement( u
"paletteEntry"_s );
556 value =
static_cast< int >( colorRampEntryElem.attribute( u
"value"_s ).toDouble() );
557 newPaletteElem.setAttribute( u
"value"_s, value );
558 red = colorRampEntryElem.attribute( u
"red"_s ).toInt();
559 green = colorRampEntryElem.attribute( u
"green"_s ).toInt();
560 blue = colorRampEntryElem.attribute( u
"blue"_s ).toInt();
561 newPaletteElem.setAttribute( u
"color"_s, QColor( red, green, blue ).name() );
562 const QString label = colorRampEntryElem.attribute( u
"label"_s );
563 if ( !label.isEmpty() )
565 newPaletteElem.setAttribute( u
"label"_s, label );
567 newColorPaletteElem.appendChild( newPaletteElem );
569 rasterRendererElem.appendChild( newColorPaletteElem );
571 else if ( drawingStyle ==
"MultiBandColor"_L1 )
573 rasterRendererElem.setAttribute( u
"type"_s, u
"multibandcolor"_s );
576 const int redBand =
rasterBandNumber( rasterPropertiesElem, u
"mRedBandName"_s, rlayer );
577 const int greenBand =
rasterBandNumber( rasterPropertiesElem, u
"mGreenBandName"_s, rlayer );
578 const int blueBand =
rasterBandNumber( rasterPropertiesElem, u
"mBlueBandName"_s, rlayer );
579 rasterRendererElem.setAttribute( u
"redBand"_s, redBand );
580 rasterRendererElem.setAttribute( u
"greenBand"_s, greenBand );
581 rasterRendererElem.setAttribute( u
"blueBand"_s, blueBand );
591 if ( !parentNode.isNull() )
593 parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
630 if ( rasterproperties.isNull() || rendererElem.isNull() )
635 double minimumValue = 0;
636 double maximumValue = 0;
637 const QDomElement contrastMinMaxElem = rasterproperties.firstChildElement( u
"contrastEnhancementMinMaxValues"_s );
638 if ( contrastMinMaxElem.isNull() )
643 const QDomElement contrastEnhancementAlgorithmElem = rasterproperties.firstChildElement( u
"mContrastEnhancementAlgorithm"_s );
644 if ( contrastEnhancementAlgorithmElem.isNull() )
650 int algorithmEnum = 0;
651 const QString algorithmString = contrastEnhancementAlgorithmElem.
text();
652 if ( algorithmString ==
"StretchToMinimumMaximum"_L1 )
656 else if ( algorithmString ==
"StretchAndClipToMinimumMaximum"_L1 )
660 else if ( algorithmString ==
"ClipToMinimumMaximum"_L1 )
664 else if ( algorithmString ==
"UserDefinedEnhancement"_L1 )
669 const QDomNodeList minMaxEntryList = contrastMinMaxElem.elementsByTagName( u
"minMaxEntry"_s );
670 QStringList enhancementNameList;
671 if ( minMaxEntryList.size() == 1 )
673 enhancementNameList << u
"contrastEnhancement"_s;
675 if ( minMaxEntryList.size() == 3 )
677 enhancementNameList << u
"redContrastEnhancement"_s << u
"greenContrastEnhancement"_s << u
"blueContrastEnhancement"_s;
679 if ( minMaxEntryList.size() > enhancementNameList.size() )
684 QDomElement minMaxEntryElem;
685 for (
int i = 0; i < minMaxEntryList.size(); ++i )
687 minMaxEntryElem = minMaxEntryList.at( i ).toElement();
688 const QDomElement minElem = minMaxEntryElem.firstChildElement( u
"min"_s );
689 if ( minElem.isNull() )
693 minimumValue = minElem.text().toDouble();
695 const QDomElement maxElem = minMaxEntryElem.firstChildElement( u
"max"_s );
696 if ( maxElem.isNull() )
700 maximumValue = maxElem.text().toDouble();
702 QDomElement newContrastEnhancementElem = doc.createElement( enhancementNameList.at( i ) );
703 QDomElement newMinValElem = doc.createElement( u
"minValue"_s );
704 const QDomText minText = doc.createTextNode( QString::number( minimumValue ) );
705 newMinValElem.appendChild( minText );
706 newContrastEnhancementElem.appendChild( newMinValElem );
707 QDomElement newMaxValElem = doc.createElement( u
"maxValue"_s );
708 const QDomText maxText = doc.createTextNode( QString::number( maximumValue ) );
709 newMaxValElem.appendChild( maxText );
710 newContrastEnhancementElem.appendChild( newMaxValElem );
712 QDomElement newAlgorithmElem = doc.createElement( u
"algorithm"_s );
713 const QDomText newAlgorithmText = doc.createTextNode( QString::number( algorithmEnum ) );
714 newAlgorithmElem.appendChild( newAlgorithmText );
715 newContrastEnhancementElem.appendChild( newAlgorithmElem );
717 rendererElem.appendChild( newContrastEnhancementElem );