149 QDomElement propsElem = pft->
dom().firstChildElement( u
"qgis"_s ).toElement().firstChildElement( u
"properties"_s );
150 if ( !propsElem.isNull() )
152 const QDomNodeList srsNodes = propsElem.elementsByTagName( u
"SpatialRefSys"_s );
154 QDomElement projElem;
155 if ( srsNodes.count() > 0 )
157 srsElem = srsNodes.at( 0 ).toElement();
158 const QDomNodeList projNodes = srsElem.elementsByTagName( u
"ProjectionsEnabled"_s );
159 if ( projNodes.count() == 0 )
161 projElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
162 projElem.setAttribute( u
"type"_s, u
"int"_s );
163 const QDomText projText = pft->
dom().createTextNode( u
"0"_s );
164 projElem.appendChild( projText );
165 srsElem.appendChild( projElem );
170 srsElem = pft->
dom().createElement( u
"SpatialRefSys"_s );
171 projElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
172 projElem.setAttribute( u
"type"_s, u
"int"_s );
173 const QDomText projText = pft->
dom().createTextNode( u
"0"_s );
174 projElem.appendChild( projText );
175 srsElem.appendChild( projElem );
176 propsElem.appendChild( srsElem );
182 const QDomNodeList canvasNodes = pft->
dom().elementsByTagName( u
"mapcanvas"_s );
183 if ( canvasNodes.count() > 0 )
185 const QDomElement canvasElem = canvasNodes.at( 0 ).toElement();
186 const QDomNodeList canvasSrsNodes = canvasElem.elementsByTagName( u
"spatialrefsys"_s );
187 if ( canvasSrsNodes.count() > 0 )
189 const QDomElement canvasSrsElem = canvasSrsNodes.at( 0 ).toElement();
194 const QDomNodeList proj4Nodes = canvasSrsElem.elementsByTagName( u
"proj4"_s );
195 if ( proj4Nodes.count() > 0 )
197 const QDomElement proj4Node = proj4Nodes.at( 0 ).toElement();
198 proj = proj4Node.text();
200 const QDomNodeList authidNodes = canvasSrsElem.elementsByTagName( u
"authid"_s );
201 if ( authidNodes.count() > 0 )
203 const QDomElement authidNode = authidNodes.at( 0 ).toElement();
204 authid = authidNode.text();
206 const QDomNodeList srsidNodes = canvasSrsElem.elementsByTagName( u
"srsid"_s );
207 if ( srsidNodes.count() > 0 )
209 const QDomElement srsidNode = srsidNodes.at( 0 ).toElement();
210 srsid = srsidNode.text();
214 const QDomNodeList oldProjectProj4Nodes = srsElem.elementsByTagName( u
"ProjectCRSProj4String"_s );
215 for (
int i = oldProjectProj4Nodes.count(); i >= 0; --i )
217 srsElem.removeChild( oldProjectProj4Nodes.at( i ) );
219 const QDomNodeList oldProjectCrsNodes = srsElem.elementsByTagName( u
"ProjectCrs"_s );
220 for (
int i = oldProjectCrsNodes.count(); i >= 0; --i )
222 srsElem.removeChild( oldProjectCrsNodes.at( i ) );
224 const QDomNodeList oldProjectCrsIdNodes = srsElem.elementsByTagName( u
"ProjectCRSID"_s );
225 for (
int i = oldProjectCrsIdNodes.count(); i >= 0; --i )
227 srsElem.removeChild( oldProjectCrsIdNodes.at( i ) );
229 const QDomNodeList projectionsEnabledNodes = srsElem.elementsByTagName( u
"ProjectionsEnabled"_s );
230 for (
int i = projectionsEnabledNodes.count(); i >= 0; --i )
232 srsElem.removeChild( projectionsEnabledNodes.at( i ) );
235 QDomElement proj4Elem = pft->
dom().createElement( u
"ProjectCRSProj4String"_s );
236 proj4Elem.setAttribute( u
"type"_s, u
"QString"_s );
237 const QDomText proj4Text = pft->
dom().createTextNode( proj );
238 proj4Elem.appendChild( proj4Text );
239 QDomElement projectCrsElem = pft->
dom().createElement( u
"ProjectCrs"_s );
240 projectCrsElem.setAttribute( u
"type"_s, u
"QString"_s );
241 const QDomText projectCrsText = pft->
dom().createTextNode( authid );
242 projectCrsElem.appendChild( projectCrsText );
243 QDomElement projectCrsIdElem = pft->
dom().createElement( u
"ProjectCRSID"_s );
244 projectCrsIdElem.setAttribute( u
"type"_s, u
"int"_s );
245 const QDomText srsidText = pft->
dom().createTextNode( srsid );
246 projectCrsIdElem.appendChild( srsidText );
247 QDomElement projectionsEnabledElem = pft->
dom().createElement( u
"ProjectionsEnabled"_s );
248 projectionsEnabledElem.setAttribute( u
"type"_s, u
"int"_s );
249 const QDomText projectionsEnabledText = pft->
dom().createTextNode( u
"1"_s );
250 projectionsEnabledElem.appendChild( projectionsEnabledText );
251 srsElem.appendChild( proj4Elem );
252 srsElem.appendChild( projectCrsElem );
253 srsElem.appendChild( projectCrsIdElem );
254 srsElem.appendChild( projectionsEnabledElem );
256 const QDomNodeList srsNodes = propsElem.elementsByTagName( u
"SpatialRefSys"_s );
257 for (
int i = srsNodes.count(); i >= 0; --i )
259 propsElem.removeChild( srsNodes.at( i ) );
261 propsElem.appendChild( srsElem );
267 const QDomNodeList mapLayers = pft->
dom().elementsByTagName( u
"maplayer"_s );
269 for (
int mapLayerIndex = 0; mapLayerIndex < mapLayers.count(); ++mapLayerIndex )
271 QDomElement layerElem = mapLayers.at( mapLayerIndex ).toElement();
274 QDomElement fieldConfigurationElement = pft->
dom().createElement( u
"fieldConfiguration"_s );
275 layerElem.appendChild( fieldConfigurationElement );
277 const QDomNodeList editTypeNodes = layerElem.namedItem( u
"edittypes"_s ).childNodes();
278 QDomElement constraintExpressionsElem = pft->
dom().createElement( u
"constraintExpressions"_s );
279 layerElem.appendChild( constraintExpressionsElem );
281 for (
int i = 0; i < editTypeNodes.size(); ++i )
283 const QDomNode editTypeNode = editTypeNodes.at( i );
284 const QDomElement editTypeElement = editTypeNode.toElement();
286 QDomElement fieldElement = pft->
dom().createElement( u
"field"_s );
287 fieldConfigurationElement.appendChild( fieldElement );
289 const QString name = editTypeElement.attribute( u
"name"_s );
290 fieldElement.setAttribute( u
"name"_s, name );
291 QDomElement constraintExpressionElem = pft->
dom().createElement( u
"constraint"_s );
292 constraintExpressionElem.setAttribute( u
"field"_s, name );
293 constraintExpressionsElem.appendChild( constraintExpressionElem );
295 QDomElement editWidgetElement = pft->
dom().createElement( u
"editWidget"_s );
296 fieldElement.appendChild( editWidgetElement );
298 const QString ewv2Type = editTypeElement.attribute( u
"widgetv2type"_s );
299 editWidgetElement.setAttribute( u
"type"_s, ewv2Type );
301 const QDomElement ewv2CfgElem = editTypeElement.namedItem( u
"widgetv2config"_s ).toElement();
303 if ( !ewv2CfgElem.isNull() )
305 QDomElement editWidgetConfigElement = pft->
dom().createElement( u
"config"_s );
306 editWidgetElement.appendChild( editWidgetConfigElement );
308 QVariantMap editWidgetConfiguration;
310 const QDomNamedNodeMap configAttrs = ewv2CfgElem.attributes();
311 for (
int configIndex = 0; configIndex < configAttrs.count(); ++configIndex )
313 const QDomAttr configAttr = configAttrs.item( configIndex ).toAttr();
314 if ( configAttr.name() ==
"fieldEditable"_L1 )
316 editWidgetConfigElement.setAttribute( u
"fieldEditable"_s, configAttr.value() );
318 else if ( configAttr.name() ==
"labelOnTop"_L1 )
320 editWidgetConfigElement.setAttribute( u
"labelOnTop"_s, configAttr.value() );
322 else if ( configAttr.name() ==
"notNull"_L1 )
324 editWidgetConfigElement.setAttribute( u
"notNull"_s, configAttr.value() );
326 else if ( configAttr.name() ==
"constraint"_L1 )
328 constraintExpressionElem.setAttribute( u
"exp"_s, configAttr.value() );
330 else if ( configAttr.name() ==
"constraintDescription"_L1 )
332 constraintExpressionElem.setAttribute( u
"desc"_s, configAttr.value() );
336 editWidgetConfiguration.insert( configAttr.name(), configAttr.value() );
340 if ( ewv2Type ==
"ValueMap"_L1 )
342 const QDomNodeList configElements = ewv2CfgElem.childNodes();
344 for (
int configIndex = 0; configIndex < configElements.count(); ++configIndex )
346 const QDomElement configElem = configElements.at( configIndex ).toElement();
347 map.insert( configElem.attribute( u
"key"_s ), configElem.attribute( u
"value"_s ) );
349 editWidgetConfiguration.insert( u
"map"_s, map );
351 else if ( ewv2Type ==
"Photo"_L1 )
353 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
355 editWidgetConfiguration.insert( u
"DocumentViewer"_s, 1 );
356 editWidgetConfiguration.insert( u
"DocumentViewerHeight"_s, editWidgetConfiguration.value( u
"Height"_s ) );
357 editWidgetConfiguration.insert( u
"DocumentViewerWidth"_s, editWidgetConfiguration.value( u
"Width"_s ) );
358 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
360 else if ( ewv2Type ==
"FileName"_L1 )
362 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
364 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
366 else if ( ewv2Type ==
"WebView"_L1 )
368 editWidgetElement.setAttribute( u
"type"_s, u
"ExternalResource"_s );
370 editWidgetConfiguration.insert( u
"DocumentViewerHeight"_s, editWidgetConfiguration.value( u
"Height"_s ) );
371 editWidgetConfiguration.insert( u
"DocumentViewerWidth"_s, editWidgetConfiguration.value( u
"Width"_s ) );
372 editWidgetConfiguration.insert( u
"RelativeStorage"_s, 1 );
386 const QDomNode noDataNode = rasterPropertiesElem.namedItem( u
"mNoDataValue"_s );
387 const QDomElement noDataElement = noDataNode.toElement();
388 if ( !noDataElement.text().isEmpty() )
391 QDomElement noDataElem = doc.createElement( u
"noData"_s );
393 QDomElement noDataRangeList = doc.createElement( u
"noDataRangeList"_s );
394 noDataRangeList.setAttribute( u
"bandNo"_s, 1 );
396 QDomElement noDataRange = doc.createElement( u
"noDataRange"_s );
397 noDataRange.setAttribute( u
"min"_s, noDataElement.text() );
398 noDataRange.setAttribute( u
"max"_s, noDataElement.text() );
399 noDataRangeList.appendChild( noDataRange );
401 noDataElem.appendChild( noDataRangeList );
403 parentNode.appendChild( noDataElem );
406 QDomElement rasterRendererElem = doc.createElement( u
"rasterrenderer"_s );
410 rasterRendererElem.setAttribute( u
"invertColor"_s, u
"0"_s );
411 const QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( u
"mInvertColor"_s );
412 if ( !invertColorElem.isNull() )
414 if ( invertColorElem.text() ==
"true"_L1 )
416 rasterRendererElem.setAttribute( u
"invertColor"_s, u
"1"_s );
421 rasterRendererElem.setAttribute( u
"opacity"_s, u
"1"_s );
422 const QDomElement transparencyElem = parentNode.firstChildElement( u
"transparencyLevelInt"_s );
423 if ( !transparencyElem.isNull() )
425 const double transparency = transparencyElem.text().toInt();
426 rasterRendererElem.setAttribute( u
"opacity"_s, QString::number( transparency / 255.0 ) );
430 rasterRendererElem.setAttribute( u
"alphaBand"_s, -1 );
433 const int grayBand =
rasterBandNumber( rasterPropertiesElem, u
"mGrayBandName"_s, rlayer );
436 QString drawingStyle = rasterPropertiesElem.firstChildElement( u
"mDrawingStyle"_s ).text();
443 if ( drawingStyle ==
"PalettedColor"_L1 )
445 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
446 const QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
448 for (
int i = 0; i < colorRampEntryList.size(); ++i )
450 const QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
451 const QString strValue = colorRampEntryElem.attribute( u
"value"_s );
452 const double value = strValue.toDouble();
453 if ( value < 0 || value > 10000 || !
qgsDoubleNear( value,
static_cast< int >( value ) ) )
455 QgsDebugMsgLevel( u
"forcing SingleBandPseudoColor value = %1"_s.arg( value ), 2 );
456 drawingStyle = u
"SingleBandPseudoColor"_s;
462 if ( drawingStyle ==
"SingleBandGray"_L1 )
464 rasterRendererElem.setAttribute( u
"type"_s, u
"singlebandgray"_s );
465 rasterRendererElem.setAttribute( u
"grayBand"_s, grayBand );
468 else if ( drawingStyle ==
"SingleBandPseudoColor"_L1 )
470 rasterRendererElem.setAttribute( u
"type"_s, u
"singlebandpseudocolor"_s );
471 rasterRendererElem.setAttribute( u
"band"_s, grayBand );
472 QDomElement newRasterShaderElem = doc.createElement( u
"rastershader"_s );
473 QDomElement newColorRampShaderElem = doc.createElement( u
"colorrampshader"_s );
474 newRasterShaderElem.appendChild( newColorRampShaderElem );
475 rasterRendererElem.appendChild( newRasterShaderElem );
478 const QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( u
"mColorShadingAlgorithm"_s ).text();
479 if ( colorShadingAlgorithm ==
"PseudoColorShader"_L1 || colorShadingAlgorithm ==
"FreakOutShader"_L1 )
481 newColorRampShaderElem.setAttribute( u
"colorRampType"_s, u
"INTERPOLATED"_s );
487 const double breakSize = ( maxValue - minValue ) / 3;
489 QStringList colorList;
490 if ( colorShadingAlgorithm ==
"FreakOutShader"_L1 )
492 colorList << u
"#ff00ff"_s << u
"#00ffff"_s << u
"#ff0000"_s << u
"#00ff00"_s;
496 colorList << u
"#0000ff"_s << u
"#00ffff"_s << u
"#ffff00"_s << u
"#ff0000"_s;
498 QStringList::const_iterator colorIt = colorList.constBegin();
499 double boundValue = minValue;
500 for ( ; colorIt != colorList.constEnd(); ++colorIt )
502 QDomElement newItemElem = doc.createElement( u
"item"_s );
503 newItemElem.setAttribute( u
"value"_s, QString::number( boundValue ) );
504 newItemElem.setAttribute( u
"label"_s, QString::number( boundValue ) );
505 newItemElem.setAttribute( u
"color"_s, *colorIt );
506 newColorRampShaderElem.appendChild( newItemElem );
507 boundValue += breakSize;
510 else if ( colorShadingAlgorithm ==
"ColorRampShader"_L1 )
512 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
513 const QString type = customColorRampElem.firstChildElement( u
"colorRampType"_s ).text();
514 newColorRampShaderElem.setAttribute( u
"colorRampType"_s, type );
515 const QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
517 QString value, label;
519 int red, green, blue;
520 QDomElement currentItemElem;
521 for (
int i = 0; i < colorNodeList.size(); ++i )
523 currentItemElem = colorNodeList.at( i ).toElement();
524 value = currentItemElem.attribute( u
"value"_s );
525 label = currentItemElem.attribute( u
"label"_s );
526 red = currentItemElem.attribute( u
"red"_s ).toInt();
527 green = currentItemElem.attribute( u
"green"_s ).toInt();
528 blue = currentItemElem.attribute( u
"blue"_s ).toInt();
529 newColor = QColor( red, green, blue );
530 QDomElement newItemElem = doc.createElement( u
"item"_s );
531 newItemElem.setAttribute( u
"value"_s, value );
532 newItemElem.setAttribute( u
"label"_s, label );
533 newItemElem.setAttribute( u
"color"_s, newColor.name() );
534 newColorRampShaderElem.appendChild( newItemElem );
538 else if ( drawingStyle ==
"PalettedColor"_L1 )
540 rasterRendererElem.setAttribute( u
"type"_s, u
"paletted"_s );
541 rasterRendererElem.setAttribute( u
"band"_s, grayBand );
542 const QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( u
"customColorRamp"_s );
543 const QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( u
"colorRampEntry"_s );
544 QDomElement newColorPaletteElem = doc.createElement( u
"colorPalette"_s );
550 QDomElement colorRampEntryElem;
551 for (
int i = 0; i < colorRampEntryList.size(); ++i )
553 colorRampEntryElem = colorRampEntryList.at( i ).toElement();
554 QDomElement newPaletteElem = doc.createElement( u
"paletteEntry"_s );
555 value =
static_cast< int >( colorRampEntryElem.attribute( u
"value"_s ).toDouble() );
556 newPaletteElem.setAttribute( u
"value"_s, value );
557 red = colorRampEntryElem.attribute( u
"red"_s ).toInt();
558 green = colorRampEntryElem.attribute( u
"green"_s ).toInt();
559 blue = colorRampEntryElem.attribute( u
"blue"_s ).toInt();
560 newPaletteElem.setAttribute( u
"color"_s, QColor( red, green, blue ).name() );
561 const QString label = colorRampEntryElem.attribute( u
"label"_s );
562 if ( !label.isEmpty() )
564 newPaletteElem.setAttribute( u
"label"_s, label );
566 newColorPaletteElem.appendChild( newPaletteElem );
568 rasterRendererElem.appendChild( newColorPaletteElem );
570 else if ( drawingStyle ==
"MultiBandColor"_L1 )
572 rasterRendererElem.setAttribute( u
"type"_s, u
"multibandcolor"_s );
575 const int redBand =
rasterBandNumber( rasterPropertiesElem, u
"mRedBandName"_s, rlayer );
576 const int greenBand =
rasterBandNumber( rasterPropertiesElem, u
"mGreenBandName"_s, rlayer );
577 const int blueBand =
rasterBandNumber( rasterPropertiesElem, u
"mBlueBandName"_s, rlayer );
578 rasterRendererElem.setAttribute( u
"redBand"_s, redBand );
579 rasterRendererElem.setAttribute( u
"greenBand"_s, greenBand );
580 rasterRendererElem.setAttribute( u
"blueBand"_s, blueBand );
590 if ( !parentNode.isNull() )
592 parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
629 if ( rasterproperties.isNull() || rendererElem.isNull() )
634 double minimumValue = 0;
635 double maximumValue = 0;
636 const QDomElement contrastMinMaxElem = rasterproperties.firstChildElement( u
"contrastEnhancementMinMaxValues"_s );
637 if ( contrastMinMaxElem.isNull() )
642 const QDomElement contrastEnhancementAlgorithmElem = rasterproperties.firstChildElement( u
"mContrastEnhancementAlgorithm"_s );
643 if ( contrastEnhancementAlgorithmElem.isNull() )
649 int algorithmEnum = 0;
650 const QString algorithmString = contrastEnhancementAlgorithmElem.
text();
651 if ( algorithmString ==
"StretchToMinimumMaximum"_L1 )
655 else if ( algorithmString ==
"StretchAndClipToMinimumMaximum"_L1 )
659 else if ( algorithmString ==
"ClipToMinimumMaximum"_L1 )
663 else if ( algorithmString ==
"UserDefinedEnhancement"_L1 )
668 const QDomNodeList minMaxEntryList = contrastMinMaxElem.elementsByTagName( u
"minMaxEntry"_s );
669 QStringList enhancementNameList;
670 if ( minMaxEntryList.size() == 1 )
672 enhancementNameList << u
"contrastEnhancement"_s;
674 if ( minMaxEntryList.size() == 3 )
676 enhancementNameList << u
"redContrastEnhancement"_s << u
"greenContrastEnhancement"_s << u
"blueContrastEnhancement"_s;
678 if ( minMaxEntryList.size() > enhancementNameList.size() )
683 QDomElement minMaxEntryElem;
684 for (
int i = 0; i < minMaxEntryList.size(); ++i )
686 minMaxEntryElem = minMaxEntryList.at( i ).toElement();
687 const QDomElement minElem = minMaxEntryElem.firstChildElement( u
"min"_s );
688 if ( minElem.isNull() )
692 minimumValue = minElem.text().toDouble();
694 const QDomElement maxElem = minMaxEntryElem.firstChildElement( u
"max"_s );
695 if ( maxElem.isNull() )
699 maximumValue = maxElem.text().toDouble();
701 QDomElement newContrastEnhancementElem = doc.createElement( enhancementNameList.at( i ) );
702 QDomElement newMinValElem = doc.createElement( u
"minValue"_s );
703 const QDomText minText = doc.createTextNode( QString::number( minimumValue ) );
704 newMinValElem.appendChild( minText );
705 newContrastEnhancementElem.appendChild( newMinValElem );
706 QDomElement newMaxValElem = doc.createElement( u
"maxValue"_s );
707 const QDomText maxText = doc.createTextNode( QString::number( maximumValue ) );
708 newMaxValElem.appendChild( maxText );
709 newContrastEnhancementElem.appendChild( newMaxValElem );
711 QDomElement newAlgorithmElem = doc.createElement( u
"algorithm"_s );
712 const QDomText newAlgorithmText = doc.createTextNode( QString::number( algorithmEnum ) );
713 newAlgorithmElem.appendChild( newAlgorithmText );
714 newContrastEnhancementElem.appendChild( newAlgorithmElem );
716 rendererElem.appendChild( newContrastEnhancementElem );