QGIS API Documentation  2.99.0-Master (23ddace)
qgsprojectfiletransform.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprojectfiletransform.cpp - description
3  -------------------
4  begin : Sun 15 dec 2007
5  copyright : (C) 2007 by Magnus Homann
6  email : magnus at homann.se
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
20 #include "qgsprojectversion.h"
21 #include "qgslogger.h"
22 #include "qgsrasterlayer.h"
23 #include "qgsreadwritecontext.h"
24 #include "qgsvectordataprovider.h"
25 #include "qgsvectorlayer.h"
26 #include <QTextStream>
27 #include <QDomDocument>
28 #include <QPrinter> //to find out screen resolution
29 #include <cstdlib>
30 #include "qgspathresolver.h"
31 #include "qgsproject.h"
32 #include "qgsprojectproperty.h"
33 #include "qgsrasterbandstats.h"
34 #include "qgsrasterdataprovider.h"
35 #include "qgsxmlutils.h"
36 
38 
39 
40 QgsProjectFileTransform::TransformItem QgsProjectFileTransform::sTransformers[] =
41 {
42  {PFV( 0, 8, 0 ), PFV( 0, 8, 1 ), &QgsProjectFileTransform::transformNull},
43  {PFV( 0, 8, 1 ), PFV( 0, 9, 0 ), &QgsProjectFileTransform::transform081to090},
44  {PFV( 0, 9, 0 ), PFV( 0, 9, 1 ), &QgsProjectFileTransform::transformNull},
45  {PFV( 0, 9, 1 ), PFV( 0, 10, 0 ), &QgsProjectFileTransform::transform091to0100},
46  // Following line is a hack that takes us straight from 0.9.2 to 0.11.0
47  // due to an unknown bug in migrating 0.9.2 files which we didn't pursue (TS & GS)
48  {PFV( 0, 9, 2 ), PFV( 0, 11, 0 ), &QgsProjectFileTransform::transformNull},
49  {PFV( 0, 10, 0 ), PFV( 0, 11, 0 ), &QgsProjectFileTransform::transform0100to0110},
50  {PFV( 0, 11, 0 ), PFV( 1, 0, 0 ), &QgsProjectFileTransform::transform0110to1000},
51  {PFV( 1, 0, 0 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
52  {PFV( 1, 0, 2 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
53  {PFV( 1, 1, 0 ), PFV( 1, 2, 0 ), &QgsProjectFileTransform::transform1100to1200},
54  {PFV( 1, 2, 0 ), PFV( 1, 3, 0 ), &QgsProjectFileTransform::transformNull},
55  {PFV( 1, 3, 0 ), PFV( 1, 4, 0 ), &QgsProjectFileTransform::transformNull},
56  {PFV( 1, 4, 0 ), PFV( 1, 5, 0 ), &QgsProjectFileTransform::transform1400to1500},
57  {PFV( 1, 5, 0 ), PFV( 1, 6, 0 ), &QgsProjectFileTransform::transformNull},
58  {PFV( 1, 6, 0 ), PFV( 1, 7, 0 ), &QgsProjectFileTransform::transformNull},
59  {PFV( 1, 7, 0 ), PFV( 1, 8, 0 ), &QgsProjectFileTransform::transformNull},
60  {PFV( 1, 8, 0 ), PFV( 1, 9, 0 ), &QgsProjectFileTransform::transform1800to1900},
61  {PFV( 1, 9, 0 ), PFV( 2, 0, 0 ), &QgsProjectFileTransform::transformNull},
62  {PFV( 2, 0, 0 ), PFV( 2, 1, 0 ), &QgsProjectFileTransform::transformNull},
63  {PFV( 2, 1, 0 ), PFV( 2, 2, 0 ), &QgsProjectFileTransform::transformNull},
64  {PFV( 2, 2, 0 ), PFV( 2, 3, 0 ), &QgsProjectFileTransform::transform2200to2300},
65  // A transformer with a NULL from version means that it should be run when upgrading
66  // from any version and will take care that it's not going to cause trouble if it's
67  // run several times on the same file.
68  {PFV(), PFV( 2, 99, 0 ), &QgsProjectFileTransform::transform2990},
69 };
70 
72 {
73  Q_UNUSED( newVersion );
74  bool returnValue = false;
75 
76  if ( !mDom.isNull() )
77  {
78  for ( std::size_t i = 0; i < sizeof( sTransformers ) / sizeof( TransformItem ); i++ )
79  {
80  const TransformItem &transformer = sTransformers[i];
81  if ( transformer.from == mCurrentVersion || transformer.from.isNull() )
82  {
83  // Run the transformer, and update the revision in every case
84  ( this->*( transformer.transformFunc ) )();
85  mCurrentVersion = transformer.to;
86  returnValue = true;
87  }
88  }
89  }
90  return returnValue;
91 }
92 
94 {
95  QgsDebugMsg( QString( "Current project file version is %1.%2.%3" )
96  .arg( mCurrentVersion.majorVersion() )
97  .arg( mCurrentVersion.minorVersion() )
98  .arg( mCurrentVersion.subVersion() ) );
99 #ifdef QGISDEBUG
100  // Using QgsDebugMsg() didn't print the entire mDom...
101  std::cout << mDom.toString( 2 ).toLatin1().constData(); // OK
102 #endif
103 }
104 
105 /*
106  * Transformers below!
107  */
108 
109 void QgsProjectFileTransform::transform081to090()
110 {
111  QgsDebugMsg( "Entering..." );
112  if ( ! mDom.isNull() )
113  {
114  // Start with inserting a mapcanvas element and populate it
115 
116  QDomElement mapCanvas; // A null element.
117 
118  // there should only be one <qgis>
119  QDomNode qgis = mDom.firstChildElement( QStringLiteral( "qgis" ) );
120  if ( ! qgis.isNull() )
121  {
122  QgsDebugMsg( "Populating new mapcanvas" );
123 
124  // Create a mapcanvas
125  mapCanvas = mDom.createElement( QStringLiteral( "mapcanvas" ) );
126  // Append mapcanvas to parent 'qgis'.
127  qgis.appendChild( mapCanvas );
128  // Re-parent units
129  mapCanvas.appendChild( qgis.namedItem( QStringLiteral( "units" ) ) );
130  // Re-parent extent
131  mapCanvas.appendChild( qgis.namedItem( QStringLiteral( "extent" ) ) );
132 
133  // See if we can find if projection is on.
134 
135  QDomElement properties = qgis.firstChildElement( QStringLiteral( "properties" ) );
136  QDomElement spatial = properties.firstChildElement( QStringLiteral( "SpatialRefSys" ) );
137  QDomElement hasCrsTransformEnabled = spatial.firstChildElement( QStringLiteral( "ProjectionsEnabled" ) );
138  // Type is 'int', and '1' if on.
139  // Create an element
140  QDomElement projection = mDom.createElement( QStringLiteral( "projections" ) );
141  QgsDebugMsg( QString( "Projection flag: " ) + hasCrsTransformEnabled.text() );
142  // Set flag from ProjectionsEnabled
143  projection.appendChild( mDom.createTextNode( hasCrsTransformEnabled.text() ) );
144  // Set new element as child of <mapcanvas>
145  mapCanvas.appendChild( projection );
146 
147  }
148 
149 
150  // Transforming coordinate-transforms
151  // Create a list of all map layers
152  QDomNodeList mapLayers = mDom.elementsByTagName( QStringLiteral( "maplayer" ) );
153  bool doneDestination = false;
154  for ( int i = 0; i < mapLayers.count(); i++ )
155  {
156  QDomNode mapLayer = mapLayers.item( i );
157  // Find the coordinatetransform
158  QDomNode coordinateTransform = mapLayer.namedItem( QStringLiteral( "coordinatetransform" ) );
159  // Find the sourcesrs
160  QDomNode sourceCrs = coordinateTransform.namedItem( QStringLiteral( "sourcesrs" ) );
161  // Rename to srs
162  sourceCrs.toElement().setTagName( QStringLiteral( "srs" ) );
163  // Re-parent to maplayer
164  mapLayer.appendChild( sourceCrs );
165  // Re-move coordinatetransform
166  // Take the destination CRS of the first layer and use for mapcanvas projection
167  if ( ! doneDestination )
168  {
169  // Use destination CRS from the last layer
170  QDomNode destinationCRS = coordinateTransform.namedItem( QStringLiteral( "destinationsrs" ) );
171  // Re-parent the destination CRS to the mapcanvas
172  // If mapcanvas wasn't set, nothing will happen.
173  mapCanvas.appendChild( destinationCRS );
174  // Only do this once
175  doneDestination = true;
176  }
177  mapLayer.removeChild( coordinateTransform );
178  //QDomNode id = mapLayer.namedItem("id");
179  //QgsDebugMsg(QString("Found maplayer ") + id.toElement().text());
180 
181  }
182 
183  // Set the flag 'visible' to match the status of 'checked'
184  QDomNodeList legendLayerFiles = mDom.elementsByTagName( QStringLiteral( "legendlayerfile" ) );
185  QgsDebugMsg( QString( "Legend layer file entries: " ) + QString::number( legendLayerFiles.count() ) );
186  for ( int i = 0; i < mapLayers.count(); i++ )
187  {
188  // Get one maplayer element from list
189  QDomElement mapLayer = mapLayers.item( i ).toElement();
190  // Find it's id.
191  QString id = mapLayer.firstChildElement( QStringLiteral( "id" ) ).text();
192  QgsDebugMsg( QString( "Handling layer " + id ) );
193  // Now, look it up in legend
194  for ( int j = 0; j < legendLayerFiles.count(); j++ )
195  {
196  QDomElement legendLayerFile = legendLayerFiles.item( j ).toElement();
197  if ( id == legendLayerFile.attribute( QStringLiteral( "layerid" ) ) )
198  {
199  // Found a the legend layer that matches the maplayer
200  QgsDebugMsg( "Found matching id" );
201 
202  // Set visible flag from maplayer to legendlayer
203  legendLayerFile.setAttribute( QStringLiteral( "visible" ), mapLayer.attribute( QStringLiteral( "visible" ) ) );
204 
205  // Set overview flag from maplayer to legendlayer
206  legendLayerFile.setAttribute( QStringLiteral( "isInOverview" ), mapLayer.attribute( QStringLiteral( "showInOverviewFlag" ) ) );
207  }
208  }
209  }
210  }
211 }
212 
213 void QgsProjectFileTransform::transform091to0100()
214 {
215  if ( ! mDom.isNull() )
216  {
217  // Insert transforms here!
218  QDomNodeList rasterPropertyList = mDom.elementsByTagName( QStringLiteral( "rasterproperties" ) );
219  QgsDebugMsg( QString( "Raster properties file entries: " ) + QString::number( rasterPropertyList.count() ) );
220  for ( int i = 0; i < rasterPropertyList.count(); i++ )
221  {
222  // Get one rasterproperty element from list, and rename the sub-properties.
223  QDomNode rasterProperty = rasterPropertyList.item( i );
224  // rasterProperty.namedItem("").toElement().setTagName("");
225 
226  rasterProperty.namedItem( QStringLiteral( "stdDevsToPlotDouble" ) ).toElement().setTagName( QStringLiteral( "mStandardDeviations" ) );
227 
228  rasterProperty.namedItem( QStringLiteral( "invertHistogramFlag" ) ).toElement().setTagName( QStringLiteral( "mInvertPixelsFlag" ) );
229  rasterProperty.namedItem( QStringLiteral( "showDebugOverLayFlag" ) ).toElement().setTagName( QStringLiteral( "mDebugOverLayFlag" ) );
230 
231  rasterProperty.namedItem( QStringLiteral( "redBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mRedBandName" ) );
232  rasterProperty.namedItem( QStringLiteral( "blueBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mBlueBandName" ) );
233  rasterProperty.namedItem( QStringLiteral( "greenBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mGreenBandName" ) );
234  rasterProperty.namedItem( QStringLiteral( "grayBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mGrayBandName" ) );
235  }
236 
237  // Changing symbol size for hard: symbols
238  QDomNodeList symbolPropertyList = mDom.elementsByTagName( QStringLiteral( "symbol" ) );
239  for ( int i = 0; i < symbolPropertyList.count(); i++ )
240  {
241  // Get the <poinmtsymbol> to check for 'hard:' for each <symbol>
242  QDomNode symbolProperty = symbolPropertyList.item( i );
243 
244  QDomElement pointSymbol = symbolProperty.firstChildElement( QStringLiteral( "pointsymbol" ) );
245  if ( pointSymbol.text().startsWith( QLatin1String( "hard:" ) ) )
246  {
247  // Get pointsize and line width
248  int lineWidth = symbolProperty.firstChildElement( QStringLiteral( "outlinewidth" ) ).text().toInt();
249  int pointSize = symbolProperty.firstChildElement( QStringLiteral( "pointsize" ) ).text().toInt();
250  // Just a precaution, checking for 0
251  if ( pointSize != 0 )
252  {
253  // int r = (s-2*lw)/2-1 --> 2r = (s-2*lw)-2 --> 2r+2 = s-2*lw
254  // --> 2r+2+2*lw = s
255  // where '2r' is the old size.
256  pointSize = pointSize + 2 + 2 * lineWidth;
257  QgsDebugMsg( QString( "Setting point size to %1" ).arg( pointSize ) );
258  QDomElement newPointSizeProperty = mDom.createElement( QStringLiteral( "pointsize" ) );
259  QDomText newPointSizeTxt = mDom.createTextNode( QString::number( pointSize ) );
260  newPointSizeProperty.appendChild( newPointSizeTxt );
261  symbolProperty.replaceChild( newPointSizeProperty, pointSymbol );
262  }
263  }
264  }
265 
266  }
267 }
268 
269 void QgsProjectFileTransform::transform0100to0110()
270 {
271  if ( ! mDom.isNull() )
272  {
273 #ifndef QT_NO_PRINTER
274  //Change 'outlinewidth' in QgsSymbol
275  QPrinter myPrinter( QPrinter::ScreenResolution );
276  int screenDpi = myPrinter.resolution();
277  double widthScaleFactor = 25.4 / screenDpi;
278 
279  QDomNodeList outlineWidthList = mDom.elementsByTagName( QStringLiteral( "outlinewidth" ) );
280  for ( int i = 0; i < outlineWidthList.size(); ++i )
281  {
282  //calculate new width
283  QDomElement currentOutlineElem = outlineWidthList.at( i ).toElement();
284  double outlineWidth = currentOutlineElem.text().toDouble();
285  outlineWidth *= widthScaleFactor;
286 
287  //replace old text node
288  QDomNode outlineTextNode = currentOutlineElem.firstChild();
289  QDomText newOutlineText = mDom.createTextNode( QString::number( outlineWidth ) );
290  currentOutlineElem.replaceChild( newOutlineText, outlineTextNode );
291 
292  }
293 
294  //Change 'pointsize' in QgsSymbol
295  QDomNodeList pointSizeList = mDom.elementsByTagName( QStringLiteral( "pointsize" ) );
296  for ( int i = 0; i < pointSizeList.size(); ++i )
297  {
298  //calculate new size
299  QDomElement currentPointSizeElem = pointSizeList.at( i ).toElement();
300  double pointSize = currentPointSizeElem.text().toDouble();
301  pointSize *= widthScaleFactor;
302 
303  //replace old text node
304  QDomNode pointSizeTextNode = currentPointSizeElem.firstChild();
305  QDomText newPointSizeText = mDom.createTextNode( QString::number( static_cast< int >( pointSize ) ) );
306  currentPointSizeElem.replaceChild( newPointSizeText, pointSizeTextNode );
307  }
308 #endif
309  }
310 }
311 
312 void QgsProjectFileTransform::transform0110to1000()
313 {
314  if ( ! mDom.isNull() )
315  {
316  QDomNodeList layerList = mDom.elementsByTagName( QStringLiteral( "maplayer" ) );
317  for ( int i = 0; i < layerList.size(); ++i )
318  {
319  QDomElement layerElem = layerList.at( i ).toElement();
320  QString typeString = layerElem.attribute( QStringLiteral( "type" ) );
321  if ( typeString != QLatin1String( "vector" ) )
322  {
323  continue;
324  }
325 
326  //datasource
327  QDomNode dataSourceNode = layerElem.namedItem( QStringLiteral( "datasource" ) );
328  if ( dataSourceNode.isNull() )
329  {
330  return;
331  }
332  QString dataSource = dataSourceNode.toElement().text();
333 
334  //provider key
335  QDomNode providerNode = layerElem.namedItem( QStringLiteral( "provider" ) );
336  if ( providerNode.isNull() )
337  {
338  return;
339  }
340  QString providerKey = providerNode.toElement().text();
341 
342  //create the layer to get the provider for int->fieldName conversion
343  QgsVectorLayer *layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, false );
344  if ( !layer->isValid() )
345  {
346  delete layer;
347  return;
348  }
349 
350  QgsVectorDataProvider *provider = layer->dataProvider();
351  if ( !provider )
352  {
353  return;
354  }
355  QgsFields fields = provider->fields();
356 
357  //read classificationfield
358  QDomNodeList classificationFieldList = layerElem.elementsByTagName( QStringLiteral( "classificationfield" ) );
359  for ( int j = 0; j < classificationFieldList.size(); ++j )
360  {
361  QDomElement classificationFieldElem = classificationFieldList.at( j ).toElement();
362  int fieldNumber = classificationFieldElem.text().toInt();
363  if ( fieldNumber >= 0 && fieldNumber < fields.count() )
364  {
365  QDomText fieldName = mDom.createTextNode( fields.at( fieldNumber ).name() );
366  QDomNode nameNode = classificationFieldElem.firstChild();
367  classificationFieldElem.replaceChild( fieldName, nameNode );
368  }
369  }
370 
371  }
372  }
373 }
374 
375 void QgsProjectFileTransform::transform1100to1200()
376 {
377  QgsDebugMsg( "Entering..." );
378  if ( mDom.isNull() )
379  return;
380 
381  QDomNode qgis = mDom.firstChildElement( QStringLiteral( "qgis" ) );
382  if ( qgis.isNull() )
383  return;
384 
385  QDomElement properties = qgis.firstChildElement( QStringLiteral( "properties" ) );
386  if ( properties.isNull() )
387  return;
388 
389  QDomElement digitizing = properties.firstChildElement( QStringLiteral( "Digitizing" ) );
390  if ( digitizing.isNull() )
391  return;
392 
393  QDomElement tolList = digitizing.firstChildElement( QStringLiteral( "LayerSnappingToleranceList" ) );
394  if ( tolList.isNull() )
395  return;
396 
397  QDomElement tolUnitList = digitizing.firstChildElement( QStringLiteral( "LayerSnappingToleranceUnitList" ) );
398  if ( !tolUnitList.isNull() )
399  return;
400 
401  QStringList units;
402  for ( int i = 0; i < tolList.childNodes().count(); i++ )
403  units << QStringLiteral( "0" );
404 
405  QgsProjectPropertyValue value( units );
406  value.writeXml( QStringLiteral( "LayerSnappingToleranceUnitList" ), digitizing, mDom );
407 }
408 
409 void QgsProjectFileTransform::transform1400to1500()
410 {
411  //Adapt the XML description of the composer legend model to version 1.5
412  if ( mDom.isNull() )
413  {
414  return;
415  }
416  //Add layer id to <VectorClassificationItem>
417  QDomNodeList layerItemList = mDom.elementsByTagName( QStringLiteral( "LayerItem" ) );
418  QDomElement currentLayerItemElem;
419  QString currentLayerId;
420 
421  for ( int i = 0; i < layerItemList.size(); ++i )
422  {
423  currentLayerItemElem = layerItemList.at( i ).toElement();
424  if ( currentLayerItemElem.isNull() )
425  {
426  continue;
427  }
428  currentLayerId = currentLayerItemElem.attribute( QStringLiteral( "layerId" ) );
429 
430  QDomNodeList vectorClassificationList = currentLayerItemElem.elementsByTagName( QStringLiteral( "VectorClassificationItem" ) );
431  QDomElement currentClassificationElem;
432  for ( int j = 0; j < vectorClassificationList.size(); ++j )
433  {
434  currentClassificationElem = vectorClassificationList.at( j ).toElement();
435  if ( !currentClassificationElem.isNull() )
436  {
437  currentClassificationElem.setAttribute( QStringLiteral( "layerId" ), currentLayerId );
438  }
439  }
440 
441  //replace the text items with VectorClassification or RasterClassification items
442  QDomNodeList textItemList = currentLayerItemElem.elementsByTagName( QStringLiteral( "TextItem" ) );
443  QDomElement currentTextItem;
444 
445  for ( int j = 0; j < textItemList.size(); ++j )
446  {
447  currentTextItem = textItemList.at( j ).toElement();
448  if ( currentTextItem.isNull() )
449  {
450  continue;
451  }
452 
453  QDomElement classificationElement;
454  if ( !vectorClassificationList.isEmpty() ) //we guess it is a vector layer
455  {
456  classificationElement = mDom.createElement( QStringLiteral( "VectorClassificationItem" ) );
457  }
458  else
459  {
460  classificationElement = mDom.createElement( QStringLiteral( "RasterClassificationItem" ) );
461  }
462 
463  classificationElement.setAttribute( QStringLiteral( "layerId" ), currentLayerId );
464  classificationElement.setAttribute( QStringLiteral( "text" ), currentTextItem.attribute( QStringLiteral( "text" ) ) );
465  currentLayerItemElem.replaceChild( classificationElement, currentTextItem );
466  }
467  }
468 }
469 
470 void QgsProjectFileTransform::transform1800to1900()
471 {
472  if ( mDom.isNull() )
473  {
474  return;
475  }
476 
477  QgsReadWriteContext context;
478  context.setPathResolver( QgsProject::instance()->pathResolver() );
479 
480  QDomNodeList layerItemList = mDom.elementsByTagName( QStringLiteral( "rasterproperties" ) );
481  for ( int i = 0; i < layerItemList.size(); ++i )
482  {
483  QDomElement rasterPropertiesElem = layerItemList.at( i ).toElement();
484  QDomNode layerNode = rasterPropertiesElem.parentNode();
485  QDomElement dataSourceElem = layerNode.firstChildElement( QStringLiteral( "datasource" ) );
486  QDomElement layerNameElem = layerNode.firstChildElement( QStringLiteral( "layername" ) );
487  QgsRasterLayer rasterLayer;
488  // TODO: We have to use more data from project file to read the layer it correctly,
489  // OTOH, we should not read it until it was converted
490  rasterLayer.readLayerXml( layerNode.toElement(), context );
491  convertRasterProperties( mDom, layerNode, rasterPropertiesElem, &rasterLayer );
492  }
493 
494  //composer: replace mGridAnnotationPosition with mLeftGridAnnotationPosition & co.
495  // and mGridAnnotationDirection with mLeftGridAnnotationDirection & co.
496  QDomNodeList composerMapList = mDom.elementsByTagName( QStringLiteral( "ComposerMap" ) );
497  for ( int i = 0; i < composerMapList.size(); ++i )
498  {
499  QDomNodeList gridList = composerMapList.at( i ).toElement().elementsByTagName( QStringLiteral( "Grid" ) );
500  for ( int j = 0; j < gridList.size(); ++j )
501  {
502  QDomNodeList annotationList = gridList.at( j ).toElement().elementsByTagName( QStringLiteral( "Annotation" ) );
503  for ( int k = 0; k < annotationList.size(); ++k )
504  {
505  QDomElement annotationElem = annotationList.at( k ).toElement();
506 
507  //position
508  if ( annotationElem.hasAttribute( QStringLiteral( "position" ) ) )
509  {
510  int pos = annotationElem.attribute( QStringLiteral( "position" ) ).toInt();
511  annotationElem.setAttribute( QStringLiteral( "leftPosition" ), pos );
512  annotationElem.setAttribute( QStringLiteral( "rightPosition" ), pos );
513  annotationElem.setAttribute( QStringLiteral( "topPosition" ), pos );
514  annotationElem.setAttribute( QStringLiteral( "bottomPosition" ), pos );
515  annotationElem.removeAttribute( QStringLiteral( "position" ) );
516  }
517 
518  //direction
519  if ( annotationElem.hasAttribute( QStringLiteral( "direction" ) ) )
520  {
521  int dir = annotationElem.attribute( QStringLiteral( "direction" ) ).toInt();
522  if ( dir == 2 )
523  {
524  annotationElem.setAttribute( QStringLiteral( "leftDirection" ), 0 );
525  annotationElem.setAttribute( QStringLiteral( "rightDirection" ), 0 );
526  annotationElem.setAttribute( QStringLiteral( "topDirection" ), 1 );
527  annotationElem.setAttribute( QStringLiteral( "bottomDirection" ), 1 );
528  }
529  else if ( dir == 3 )
530  {
531  annotationElem.setAttribute( QStringLiteral( "leftDirection" ), 1 );
532  annotationElem.setAttribute( QStringLiteral( "rightDirection" ), 1 );
533  annotationElem.setAttribute( QStringLiteral( "topDirection" ), 0 );
534  annotationElem.setAttribute( QStringLiteral( "bottomDirection" ), 0 );
535  }
536  else
537  {
538  annotationElem.setAttribute( QStringLiteral( "leftDirection" ), dir );
539  annotationElem.setAttribute( QStringLiteral( "rightDirection" ), dir );
540  annotationElem.setAttribute( QStringLiteral( "topDirection" ), dir );
541  annotationElem.setAttribute( QStringLiteral( "bottomDirection" ), dir );
542  }
543  annotationElem.removeAttribute( QStringLiteral( "direction" ) );
544  }
545  }
546  }
547  }
548 
549  //Composer: move all items under Composition element
550  QDomNodeList composerList = mDom.elementsByTagName( QStringLiteral( "Composer" ) );
551  for ( int i = 0; i < composerList.size(); ++i )
552  {
553  QDomElement composerElem = composerList.at( i ).toElement();
554 
555  //find <QgsComposition element
556  QDomElement compositionElem = composerElem.firstChildElement( QStringLiteral( "Composition" ) );
557  if ( compositionElem.isNull() )
558  {
559  continue;
560  }
561 
562  QDomNodeList composerChildren = composerElem.childNodes();
563 
564  if ( composerChildren.size() < 1 )
565  {
566  continue;
567  }
568 
569  for ( int j = composerChildren.size() - 1; j >= 0; --j )
570  {
571  QDomElement childElem = composerChildren.at( j ).toElement();
572  if ( childElem.tagName() == QLatin1String( "Composition" ) )
573  {
574  continue;
575  }
576 
577  composerElem.removeChild( childElem );
578  compositionElem.appendChild( childElem );
579 
580  }
581  }
582 
583  // SimpleFill symbol layer v2: avoid double transparency
584  // replacing alpha value of symbol layer's color with 255 (the
585  // transparency value is already stored as symbol transparency).
586  QDomNodeList rendererList = mDom.elementsByTagName( QStringLiteral( "renderer-v2" ) );
587  for ( int i = 0; i < rendererList.size(); ++i )
588  {
589  QDomNodeList layerList = rendererList.at( i ).toElement().elementsByTagName( QStringLiteral( "layer" ) );
590  for ( int j = 0; j < layerList.size(); ++j )
591  {
592  QDomElement layerElem = layerList.at( j ).toElement();
593  if ( layerElem.attribute( QStringLiteral( "class" ) ) == QLatin1String( "SimpleFill" ) )
594  {
595  QDomNodeList propList = layerElem.elementsByTagName( QStringLiteral( "prop" ) );
596  for ( int k = 0; k < propList.size(); ++k )
597  {
598  QDomElement propElem = propList.at( k ).toElement();
599  if ( propElem.attribute( QStringLiteral( "k" ) ) == QLatin1String( "color" ) || propElem.attribute( QStringLiteral( "k" ) ) == QLatin1String( "color_border" ) )
600  {
601  propElem.setAttribute( QStringLiteral( "v" ), propElem.attribute( QStringLiteral( "v" ) ).section( ',', 0, 2 ) + ",255" );
602  }
603  }
604  }
605  }
606  }
607 
608  QgsDebugMsg( mDom.toString() );
609 }
610 
611 void QgsProjectFileTransform::transform2200to2300()
612 {
613  //composer: set placement for all picture items to middle, to mimic <=2.2 behavior
614  QDomNodeList composerPictureList = mDom.elementsByTagName( QStringLiteral( "ComposerPicture" ) );
615  for ( int i = 0; i < composerPictureList.size(); ++i )
616  {
617  QDomElement picture = composerPictureList.at( i ).toElement();
618  picture.setAttribute( QStringLiteral( "anchorPoint" ), QString::number( 4 ) );
619  }
620 }
621 
622 void QgsProjectFileTransform::transform2990()
623 {
624  // transform OTF off to "no projection" for project
625  QDomElement propsElem = mDom.firstChildElement( QStringLiteral( "qgis" ) ).toElement().firstChildElement( QStringLiteral( "properties" ) );
626  QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
627  QDomElement srsElem;
628  QDomElement projElem;
629  if ( srsNodes.count() > 0 )
630  {
631  srsElem = srsNodes.at( 0 ).toElement();
632  QDomNodeList projNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectionsEnabled" ) );
633  if ( projNodes.count() == 0 )
634  {
635  projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
636  projElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
637  QDomText projText = mDom.createTextNode( QStringLiteral( "0" ) );
638  projElem.appendChild( projText );
639  srsElem.appendChild( projElem );
640  }
641  }
642  else
643  {
644  srsElem = mDom.createElement( QStringLiteral( "SpatialRefSys" ) );
645  projElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
646  projElem.setAttribute( "type", "int" );
647  QDomText projText = mDom.createTextNode( QStringLiteral( "0" ) );
648  projElem.appendChild( projText );
649  srsElem.appendChild( projElem );
650  propsElem.appendChild( srsElem );
651  }
652  // transform map canvas CRS to project CRS - this is because project CRS was inconsistently used
653  // prior to 3.0. In >= 3.0 main canvas CRS is forced to match project CRS, so we need to make
654  // sure we can read the project CRS correctly
655  QDomNodeList canvasNodes = mDom.elementsByTagName( QStringLiteral( "mapcanvas" ) );
656  if ( canvasNodes.count() > 0 )
657  {
658  QDomElement canvasElem = canvasNodes.at( 0 ).toElement();
659  QDomNodeList projectionsNodes = canvasElem.elementsByTagName( QStringLiteral( "projections" ) );
660  bool hasOtf = false;
661  if ( projectionsNodes.count() > 0 )
662  {
663  QDomElement projectionsElem = projectionsNodes.at( 0 ).toElement();
664  hasOtf = projectionsElem.text().toInt();
665  }
666 
667  QDomNodeList canvasSrsNodes = canvasElem.elementsByTagName( QStringLiteral( "spatialrefsys" ) );
668  if ( canvasSrsNodes.count() > 0 )
669  {
670  QDomElement canvasSrsElem = canvasSrsNodes.at( 0 ).toElement();
671  QString proj;
672  QString authid;
673  QString srsid;
674 
675  QDomNodeList proj4Nodes = canvasSrsElem.elementsByTagName( QStringLiteral( "proj4" ) );
676  if ( proj4Nodes.count() > 0 )
677  {
678  QDomElement proj4Node = proj4Nodes.at( 0 ).toElement();
679  proj = proj4Node.text();
680  }
681  QDomNodeList authidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( "authid" ) );
682  if ( authidNodes.count() > 0 )
683  {
684  QDomElement authidNode = authidNodes.at( 0 ).toElement();
685  authid = authidNode.text();
686  }
687  QDomNodeList srsidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( "srsid" ) );
688  if ( srsidNodes.count() > 0 )
689  {
690  QDomElement srsidNode = srsidNodes.at( 0 ).toElement();
691  srsid = srsidNode.text();
692  }
693 
694  // clear existing project CRS nodes
695  QDomNodeList oldProjectProj4Nodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCRSProj4String" ) );
696  for ( int i = oldProjectProj4Nodes.count(); i >= 0; --i )
697  {
698  srsElem.removeChild( oldProjectProj4Nodes.at( i ) );
699  }
700  QDomNodeList oldProjectCrsNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCrs" ) );
701  for ( int i = oldProjectCrsNodes.count(); i >= 0; --i )
702  {
703  srsElem.removeChild( oldProjectCrsNodes.at( i ) );
704  }
705  QDomNodeList oldProjectCrsIdNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectCRSID" ) );
706  for ( int i = oldProjectCrsIdNodes.count(); i >= 0; --i )
707  {
708  srsElem.removeChild( oldProjectCrsIdNodes.at( i ) );
709  }
710  QDomNodeList projectionsEnabledNodes = srsElem.elementsByTagName( QStringLiteral( "ProjectionsEnabled" ) );
711  for ( int i = projectionsEnabledNodes.count(); i >= 0; --i )
712  {
713  srsElem.removeChild( projectionsEnabledNodes.at( i ) );
714  }
715 
716  QDomElement proj4Elem = mDom.createElement( QStringLiteral( "ProjectCRSProj4String" ) );
717  proj4Elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QString" ) );
718  QDomText proj4Text = mDom.createTextNode( proj );
719  proj4Elem.appendChild( proj4Text );
720  QDomElement projectCrsElem = mDom.createElement( QStringLiteral( "ProjectCrs" ) );
721  projectCrsElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QString" ) );
722  QDomText projectCrsText = mDom.createTextNode( authid );
723  projectCrsElem.appendChild( projectCrsText );
724  QDomElement projectCrsIdElem = mDom.createElement( QStringLiteral( "ProjectCRSID" ) );
725  projectCrsIdElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
726  QDomText srsidText = mDom.createTextNode( srsid );
727  projectCrsIdElem.appendChild( srsidText );
728  QDomElement projectionsEnabledElem = mDom.createElement( QStringLiteral( "ProjectionsEnabled" ) );
729  projectionsEnabledElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "int" ) );
730  QDomText projectionsEnabledText = mDom.createTextNode( hasOtf ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
731  projectionsEnabledElem.appendChild( projectionsEnabledText );
732  srsElem.appendChild( proj4Elem );
733  srsElem.appendChild( projectCrsElem );
734  srsElem.appendChild( projectCrsIdElem );
735  srsElem.appendChild( projectionsEnabledElem );
736 
737  QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( "SpatialRefSys" ) );
738  for ( int i = srsNodes.count(); i >= 0; --i )
739  {
740  propsElem.removeChild( srsNodes.at( i ) );
741  }
742  propsElem.appendChild( srsElem );
743  }
744  }
745 
746 
747  QDomNodeList mapLayers = mDom.elementsByTagName( QStringLiteral( "maplayer" ) );
748 
749  for ( int mapLayerIndex = 0; mapLayerIndex < mapLayers.count(); ++mapLayerIndex )
750  {
751  QDomElement layerElem = mapLayers.at( mapLayerIndex ).toElement();
752 
753  // The newly added fieldConfiguration element
754  QDomElement fieldConfigurationElement = mDom.createElement( QStringLiteral( "fieldConfiguration" ) );
755  layerElem.appendChild( fieldConfigurationElement );
756 
757  QDomNodeList editTypeNodes = layerElem.namedItem( QStringLiteral( "edittypes" ) ).childNodes();
758  QDomElement constraintExpressionsElem = mDom.createElement( QStringLiteral( "constraintExpressions" ) );
759  layerElem.appendChild( constraintExpressionsElem );
760 
761  for ( int i = 0; i < editTypeNodes.size(); ++i )
762  {
763  QDomNode editTypeNode = editTypeNodes.at( i );
764  QDomElement editTypeElement = editTypeNode.toElement();
765 
766  QDomElement fieldElement = mDom.createElement( QStringLiteral( "field" ) );
767  fieldConfigurationElement.appendChild( fieldElement );
768 
769  QString name = editTypeElement.attribute( QStringLiteral( "name" ) );
770  fieldElement.setAttribute( QStringLiteral( "name" ), name );
771  QDomElement constraintExpressionElem = mDom.createElement( QStringLiteral( "constraint" ) );
772  constraintExpressionElem.setAttribute( "field", name );
773  constraintExpressionsElem.appendChild( constraintExpressionElem );
774 
775  QDomElement editWidgetElement = mDom.createElement( QStringLiteral( "editWidget" ) );
776  fieldElement.appendChild( editWidgetElement );
777 
778  QString ewv2Type = editTypeElement.attribute( QStringLiteral( "widgetv2type" ) );
779  editWidgetElement.setAttribute( "type", ewv2Type );
780 
781  QDomElement ewv2CfgElem = editTypeElement.namedItem( QStringLiteral( "widgetv2config" ) ).toElement();
782 
783  if ( !ewv2CfgElem.isNull() )
784  {
785  QDomElement editWidgetConfigElement = mDom.createElement( QStringLiteral( "config" ) );
786  editWidgetElement.appendChild( editWidgetConfigElement );
787 
788  QVariantMap editWidgetConfiguration;
789 
790  QDomNamedNodeMap configAttrs = ewv2CfgElem.attributes();
791  for ( int configIndex = 0; configIndex < configAttrs.count(); ++configIndex )
792  {
793  QDomAttr configAttr = configAttrs.item( configIndex ).toAttr();
794  if ( configAttr.name() == QStringLiteral( "fieldEditable" ) )
795  {
796  editWidgetConfigElement.setAttribute( QStringLiteral( "fieldEditable" ), configAttr.value() );
797  }
798  else if ( configAttr.name() == QStringLiteral( "labelOnTop" ) )
799  {
800  editWidgetConfigElement.setAttribute( QStringLiteral( "labelOnTop" ), configAttr.value() );
801  }
802  else if ( configAttr.name() == QStringLiteral( "notNull" ) )
803  {
804  editWidgetConfigElement.setAttribute( QStringLiteral( "notNull" ), configAttr.value() );
805  }
806  else if ( configAttr.name() == QStringLiteral( "constraint" ) )
807  {
808  constraintExpressionElem.setAttribute( "exp", configAttr.value() );
809  }
810  else if ( configAttr.name() == QStringLiteral( "constraintDescription" ) )
811  {
812  constraintExpressionElem.setAttribute( "desc", configAttr.value() );
813  }
814  else
815  {
816  editWidgetConfiguration.insert( configAttr.name(), configAttr.value() );
817  }
818  }
819 
820  if ( ewv2Type == QStringLiteral( "ValueMap" ) )
821  {
822  QDomNodeList configElements = ewv2CfgElem.childNodes();
823  QVariantMap map;
824  for ( int configIndex = 0; configIndex < configElements.count(); ++configIndex )
825  {
826  QDomElement configElem = configElements.at( configIndex ).toElement();
827  map.insert( configElem.attribute( QStringLiteral( "key" ) ), configElem.attribute( QStringLiteral( "value" ) ) );
828  }
829  editWidgetConfiguration.insert( QStringLiteral( "map" ), map );
830  }
831  else if ( ewv2Type == QStringLiteral( "Photo" ) )
832  {
833  editWidgetElement.setAttribute( "type", QStringLiteral( "ExternalResource" ) );
834 
835  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewer" ), 1 );
836  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerHeight" ), editWidgetConfiguration.value( QStringLiteral( "Height" ) ) );
837  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerWidth" ), editWidgetConfiguration.value( QStringLiteral( "Width" ) ) );
838  editWidgetConfiguration.insert( QStringLiteral( "RelativeStorage" ), 1 );
839  }
840  else if ( ewv2Type == QStringLiteral( "FileName" ) )
841  {
842  editWidgetElement.setAttribute( "type", QStringLiteral( "ExternalResource" ) );
843 
844  editWidgetConfiguration.insert( QStringLiteral( "RelativeStorage" ), 1 );
845  }
846  else if ( ewv2Type == QStringLiteral( "WebView" ) )
847  {
848  editWidgetElement.setAttribute( "type", QStringLiteral( "ExternalResource" ) );
849 
850  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerHeight" ), editWidgetConfiguration.value( QStringLiteral( "Height" ) ) );
851  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerWidth" ), editWidgetConfiguration.value( QStringLiteral( "Width" ) ) );
852  editWidgetConfiguration.insert( QStringLiteral( "RelativeStorage" ), 1 );
853  }
854 
855  editWidgetConfigElement.appendChild( QgsXmlUtils::writeVariant( editWidgetConfiguration, mDom ) );
856  }
857  }
858  }
859 }
860 
861 void QgsProjectFileTransform::convertRasterProperties( QDomDocument &doc, QDomNode &parentNode,
862  QDomElement &rasterPropertiesElem, QgsRasterLayer *rlayer )
863 {
864  //no data
865  //TODO: We would need to set no data on all bands, but we don't know number of bands here
866  QDomNode noDataNode = rasterPropertiesElem.namedItem( QStringLiteral( "mNoDataValue" ) );
867  QDomElement noDataElement = noDataNode.toElement();
868  if ( !noDataElement.text().isEmpty() )
869  {
870  QgsDebugMsg( "mNoDataValue = " + noDataElement.text() );
871  QDomElement noDataElem = doc.createElement( QStringLiteral( "noData" ) );
872 
873  QDomElement noDataRangeList = doc.createElement( QStringLiteral( "noDataRangeList" ) );
874  noDataRangeList.setAttribute( QStringLiteral( "bandNo" ), 1 );
875 
876  QDomElement noDataRange = doc.createElement( QStringLiteral( "noDataRange" ) );
877  noDataRange.setAttribute( QStringLiteral( "min" ), noDataElement.text() );
878  noDataRange.setAttribute( QStringLiteral( "max" ), noDataElement.text() );
879  noDataRangeList.appendChild( noDataRange );
880 
881  noDataElem.appendChild( noDataRangeList );
882 
883  parentNode.appendChild( noDataElem );
884  }
885 
886  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
887  //convert general properties
888 
889  //invert color
890  rasterRendererElem.setAttribute( QStringLiteral( "invertColor" ), QStringLiteral( "0" ) );
891  QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "mInvertColor" ) );
892  if ( !invertColorElem.isNull() )
893  {
894  if ( invertColorElem.text() == QLatin1String( "true" ) )
895  {
896  rasterRendererElem.setAttribute( QStringLiteral( "invertColor" ), QStringLiteral( "1" ) );
897  }
898  }
899 
900  //opacity
901  rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QStringLiteral( "1" ) );
902  QDomElement transparencyElem = parentNode.firstChildElement( QStringLiteral( "transparencyLevelInt" ) );
903  if ( !transparencyElem.isNull() )
904  {
905  double transparency = transparencyElem.text().toInt();
906  rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QString::number( transparency / 255.0 ) );
907  }
908 
909  //alphaBand was not saved until now (bug)
910  rasterRendererElem.setAttribute( QStringLiteral( "alphaBand" ), -1 );
911 
912  //gray band is used for several renderers
913  int grayBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mGrayBandName" ), rlayer );
914 
915  //convert renderer specific properties
916  QString drawingStyle = rasterPropertiesElem.firstChildElement( QStringLiteral( "mDrawingStyle" ) ).text();
917 
918  // While PalettedColor should normally contain only integer values, usually
919  // color palette 0-255, it may happen (Tim, issue #7023) that it contains
920  // colormap classification with double values and text labels
921  // (which should normally only appear in SingleBandPseudoColor drawingStyle)
922  // => we have to check first the values and change drawingStyle if necessary
923  if ( drawingStyle == QLatin1String( "PalettedColor" ) )
924  {
925  QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "customColorRamp" ) );
926  QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( QStringLiteral( "colorRampEntry" ) );
927 
928  for ( int i = 0; i < colorRampEntryList.size(); ++i )
929  {
930  QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
931  QString strValue = colorRampEntryElem.attribute( QStringLiteral( "value" ) );
932  double value = strValue.toDouble();
933  if ( value < 0 || value > 10000 || !qgsDoubleNear( value, static_cast< int >( value ) ) )
934  {
935  QgsDebugMsg( QString( "forcing SingleBandPseudoColor value = %1" ).arg( value ) );
936  drawingStyle = QStringLiteral( "SingleBandPseudoColor" );
937  break;
938  }
939  }
940  }
941 
942  if ( drawingStyle == QLatin1String( "SingleBandGray" ) )
943  {
944  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "singlebandgray" ) );
945  rasterRendererElem.setAttribute( QStringLiteral( "grayBand" ), grayBand );
946  transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
947  }
948  else if ( drawingStyle == QLatin1String( "SingleBandPseudoColor" ) )
949  {
950  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "singlebandpseudocolor" ) );
951  rasterRendererElem.setAttribute( QStringLiteral( "band" ), grayBand );
952  QDomElement newRasterShaderElem = doc.createElement( QStringLiteral( "rastershader" ) );
953  QDomElement newColorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
954  newRasterShaderElem.appendChild( newColorRampShaderElem );
955  rasterRendererElem.appendChild( newRasterShaderElem );
956 
957  //switch depending on mColorShadingAlgorithm
958  QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( QStringLiteral( "mColorShadingAlgorithm" ) ).text();
959  if ( colorShadingAlgorithm == QLatin1String( "PseudoColorShader" ) || colorShadingAlgorithm == QLatin1String( "FreakOutShader" ) )
960  {
961  newColorRampShaderElem.setAttribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) );
962 
963  //get minmax from rasterlayer
964  QgsRasterBandStats rasterBandStats = rlayer->dataProvider()->bandStatistics( grayBand );
965  double minValue = rasterBandStats.minimumValue;
966  double maxValue = rasterBandStats.maximumValue;
967  double breakSize = ( maxValue - minValue ) / 3;
968 
969  QStringList colorList;
970  if ( colorShadingAlgorithm == QLatin1String( "FreakOutShader" ) )
971  {
972  colorList << QStringLiteral( "#ff00ff" ) << QStringLiteral( "#00ffff" ) << QStringLiteral( "#ff0000" ) << QStringLiteral( "#00ff00" );
973  }
974  else //pseudocolor
975  {
976  colorList << QStringLiteral( "#0000ff" ) << QStringLiteral( "#00ffff" ) << QStringLiteral( "#ffff00" ) << QStringLiteral( "#ff0000" );
977  }
978  QStringList::const_iterator colorIt = colorList.constBegin();
979  double boundValue = minValue;
980  for ( ; colorIt != colorList.constEnd(); ++colorIt )
981  {
982  QDomElement newItemElem = doc.createElement( QStringLiteral( "item" ) );
983  newItemElem.setAttribute( QStringLiteral( "value" ), QString::number( boundValue ) );
984  newItemElem.setAttribute( QStringLiteral( "label" ), QString::number( boundValue ) );
985  newItemElem.setAttribute( QStringLiteral( "color" ), *colorIt );
986  newColorRampShaderElem.appendChild( newItemElem );
987  boundValue += breakSize;
988  }
989  }
990  else if ( colorShadingAlgorithm == QLatin1String( "ColorRampShader" ) )
991  {
992  QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "customColorRamp" ) );
993  QString type = customColorRampElem.firstChildElement( QStringLiteral( "colorRampType" ) ).text();
994  newColorRampShaderElem.setAttribute( QStringLiteral( "colorRampType" ), type );
995  QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( QStringLiteral( "colorRampEntry" ) );
996 
997  QString value, label;
998  QColor newColor;
999  int red, green, blue;
1000  QDomElement currentItemElem;
1001  for ( int i = 0; i < colorNodeList.size(); ++i )
1002  {
1003  currentItemElem = colorNodeList.at( i ).toElement();
1004  value = currentItemElem.attribute( QStringLiteral( "value" ) );
1005  label = currentItemElem.attribute( QStringLiteral( "label" ) );
1006  red = currentItemElem.attribute( QStringLiteral( "red" ) ).toInt();
1007  green = currentItemElem.attribute( QStringLiteral( "green" ) ).toInt();
1008  blue = currentItemElem.attribute( QStringLiteral( "blue" ) ).toInt();
1009  newColor = QColor( red, green, blue );
1010  QDomElement newItemElem = doc.createElement( QStringLiteral( "item" ) );
1011  newItemElem.setAttribute( QStringLiteral( "value" ), value );
1012  newItemElem.setAttribute( QStringLiteral( "label" ), label );
1013  newItemElem.setAttribute( QStringLiteral( "color" ), newColor.name() );
1014  newColorRampShaderElem.appendChild( newItemElem );
1015  }
1016  }
1017  }
1018  else if ( drawingStyle == QLatin1String( "PalettedColor" ) )
1019  {
1020  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "paletted" ) );
1021  rasterRendererElem.setAttribute( QStringLiteral( "band" ), grayBand );
1022  QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "customColorRamp" ) );
1023  QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( QStringLiteral( "colorRampEntry" ) );
1024  QDomElement newColorPaletteElem = doc.createElement( QStringLiteral( "colorPalette" ) );
1025 
1026  int red = 0;
1027  int green = 0;
1028  int blue = 0;
1029  int value = 0;
1030  QDomElement colorRampEntryElem;
1031  for ( int i = 0; i < colorRampEntryList.size(); ++i )
1032  {
1033  colorRampEntryElem = colorRampEntryList.at( i ).toElement();
1034  QDomElement newPaletteElem = doc.createElement( QStringLiteral( "paletteEntry" ) );
1035  value = static_cast< int >( colorRampEntryElem.attribute( QStringLiteral( "value" ) ).toDouble() );
1036  newPaletteElem.setAttribute( QStringLiteral( "value" ), value );
1037  red = colorRampEntryElem.attribute( QStringLiteral( "red" ) ).toInt();
1038  green = colorRampEntryElem.attribute( QStringLiteral( "green" ) ).toInt();
1039  blue = colorRampEntryElem.attribute( QStringLiteral( "blue" ) ).toInt();
1040  newPaletteElem.setAttribute( QStringLiteral( "color" ), QColor( red, green, blue ).name() );
1041  QString label = colorRampEntryElem.attribute( QStringLiteral( "label" ) );
1042  if ( !label.isEmpty() )
1043  {
1044  newPaletteElem.setAttribute( QStringLiteral( "label" ), label );
1045  }
1046  newColorPaletteElem.appendChild( newPaletteElem );
1047  }
1048  rasterRendererElem.appendChild( newColorPaletteElem );
1049  }
1050  else if ( drawingStyle == QLatin1String( "MultiBandColor" ) )
1051  {
1052  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "multibandcolor" ) );
1053 
1054  //red band, green band, blue band
1055  int redBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mRedBandName" ), rlayer );
1056  int greenBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mGreenBandName" ), rlayer );
1057  int blueBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mBlueBandName" ), rlayer );
1058  rasterRendererElem.setAttribute( QStringLiteral( "redBand" ), redBand );
1059  rasterRendererElem.setAttribute( QStringLiteral( "greenBand" ), greenBand );
1060  rasterRendererElem.setAttribute( QStringLiteral( "blueBand" ), blueBand );
1061 
1062  transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
1063  }
1064  else
1065  {
1066  return;
1067  }
1068 
1069  //replace rasterproperties element with rasterrenderer element
1070  if ( !parentNode.isNull() )
1071  {
1072  parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
1073  }
1074 }
1075 
1076 int QgsProjectFileTransform::rasterBandNumber( const QDomElement &rasterPropertiesElem, const QString &bandName,
1077  QgsRasterLayer *rlayer )
1078 {
1079  if ( !rlayer )
1080  {
1081  return -1;
1082  }
1083 
1084  int band = -1;
1085  QDomElement rasterBandElem = rasterPropertiesElem.firstChildElement( bandName );
1086  if ( !rasterBandElem.isNull() )
1087  {
1088  QRegExp re( "(\\d+)" );
1089 
1090  if ( re.indexIn( rasterBandElem.text() ) >= 0 )
1091  {
1092  return re.cap( 1 ).toInt();
1093  }
1094  }
1095  return band;
1096 }
1097 
1098 void QgsProjectFileTransform::transformContrastEnhancement( QDomDocument &doc, const QDomElement &rasterproperties, QDomElement &rendererElem )
1099 {
1100  if ( rasterproperties.isNull() || rendererElem.isNull() )
1101  {
1102  return;
1103  }
1104 
1105  double minimumValue = 0;
1106  double maximumValue = 0;
1107  QDomElement contrastMinMaxElem = rasterproperties.firstChildElement( QStringLiteral( "contrastEnhancementMinMaxValues" ) );
1108  if ( contrastMinMaxElem.isNull() )
1109  {
1110  return;
1111  }
1112 
1113  QDomElement contrastEnhancementAlgorithmElem = rasterproperties.firstChildElement( QStringLiteral( "mContrastEnhancementAlgorithm" ) );
1114  if ( contrastEnhancementAlgorithmElem.isNull() )
1115  {
1116  return;
1117  }
1118 
1119  //convert enhancement name to enumeration
1120  int algorithmEnum = 0;
1121  QString algorithmString = contrastEnhancementAlgorithmElem.text();
1122  if ( algorithmString == QLatin1String( "StretchToMinimumMaximum" ) )
1123  {
1124  algorithmEnum = 1;
1125  }
1126  else if ( algorithmString == QLatin1String( "StretchAndClipToMinimumMaximum" ) )
1127  {
1128  algorithmEnum = 2;
1129  }
1130  else if ( algorithmString == QLatin1String( "ClipToMinimumMaximum" ) )
1131  {
1132  algorithmEnum = 3;
1133  }
1134  else if ( algorithmString == QLatin1String( "UserDefinedEnhancement" ) )
1135  {
1136  algorithmEnum = 4;
1137  }
1138 
1139  QDomNodeList minMaxEntryList = contrastMinMaxElem.elementsByTagName( QStringLiteral( "minMaxEntry" ) );
1140  QStringList enhancementNameList;
1141  if ( minMaxEntryList.size() == 1 )
1142  {
1143  enhancementNameList << QStringLiteral( "contrastEnhancement" );
1144  }
1145  if ( minMaxEntryList.size() == 3 )
1146  {
1147  enhancementNameList << QStringLiteral( "redContrastEnhancement" ) << QStringLiteral( "greenContrastEnhancement" ) << QStringLiteral( "blueContrastEnhancement" );
1148  }
1149  if ( minMaxEntryList.size() > enhancementNameList.size() )
1150  {
1151  return;
1152  }
1153 
1154  QDomElement minMaxEntryElem;
1155  for ( int i = 0; i < minMaxEntryList.size(); ++i )
1156  {
1157  minMaxEntryElem = minMaxEntryList.at( i ).toElement();
1158  QDomElement minElem = minMaxEntryElem.firstChildElement( QStringLiteral( "min" ) );
1159  if ( minElem.isNull() )
1160  {
1161  return;
1162  }
1163  minimumValue = minElem.text().toDouble();
1164 
1165  QDomElement maxElem = minMaxEntryElem.firstChildElement( QStringLiteral( "max" ) );
1166  if ( maxElem.isNull() )
1167  {
1168  return;
1169  }
1170  maximumValue = maxElem.text().toDouble();
1171 
1172  QDomElement newContrastEnhancementElem = doc.createElement( enhancementNameList.at( i ) );
1173  QDomElement newMinValElem = doc.createElement( QStringLiteral( "minValue" ) );
1174  QDomText minText = doc.createTextNode( QString::number( minimumValue ) );
1175  newMinValElem.appendChild( minText );
1176  newContrastEnhancementElem.appendChild( newMinValElem );
1177  QDomElement newMaxValElem = doc.createElement( QStringLiteral( "maxValue" ) );
1178  QDomText maxText = doc.createTextNode( QString::number( maximumValue ) );
1179  newMaxValElem.appendChild( maxText );
1180  newContrastEnhancementElem.appendChild( newMaxValElem );
1181 
1182  QDomElement newAlgorithmElem = doc.createElement( QStringLiteral( "algorithm" ) );
1183  QDomText newAlgorithmText = doc.createTextNode( QString::number( algorithmEnum ) );
1184  newAlgorithmElem.appendChild( newAlgorithmText );
1185  newContrastEnhancementElem.appendChild( newAlgorithmElem );
1186 
1187  rendererElem.appendChild( newContrastEnhancementElem );
1188  }
1189 }
1190 
1191 void QgsProjectFileTransform::transformRasterTransparency( QDomDocument &doc, const QDomElement &orig, QDomElement &rendererElem )
1192 {
1193  //soon...
1194  Q_UNUSED( doc );
1195  Q_UNUSED( orig );
1196  Q_UNUSED( rendererElem );
1197 }
1198 
static void convertRasterProperties(QDomDocument &doc, QDomNode &parentNode, QDomElement &rasterPropertiesElem, QgsRasterLayer *rlayer)
QgsProjectVersion PFV
The class is used as a container of context for various read/write operations on other objects...
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
void dump()
Prints the contents via QgsDebugMsg()
QString name
Definition: qgsfield.h:54
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
bool updateRevision(const QgsProjectVersion &version)
double maximumValue
The maximum cell value in the raster band.
Container of fields for a vector layer.
Definition: qgsfields.h:41
bool isValid() const
Return the status of the layer.
virtual QgsFields fields() const override=0
Returns the fields associated with this data provider.
bool isNull() const
Returns true if this is a NULL project version.
int count() const
Return number of items.
Definition: qgsfields.cpp:115
Project property value node, contains a QgsProjectPropertyKey&#39;s value.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:203
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:135
QgsRasterDataProvider * dataProvider() override
The RasterBandStats struct is a container for statistics about a single raster band.
A class to describe the version of a project.
bool readLayerXml(const QDomElement &layerElement, const QgsReadWriteContext &context)
Sets state from Dom document.
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Get band statistics.
bool writeXml(const QString &nodeName, QDomElement &element, QDomDocument &document) override
Writes the property hierarchy to a specified DOM element.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:377
QgsVectorDataProvider * dataProvider() override
Returns the layer&#39;s data provider.
double minimumValue
The minimum cell value in the raster band.
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.