/
qgsprojectfiletransform.cpp
931 lines (821 loc) · 34.3 KB
/
qgsprojectfiletransform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/***************************************************************************
qgsprojectfiletransform.cpp - description
-------------------
begin : Sun 15 dec 2007
copyright : (C) 2007 by Magnus Homann
email : magnus at homann.se
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsprojectfiletransform.h"
#include "qgsprojectversion.h"
#include "qgslogger.h"
#include "qgsrasterlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include <QTextStream>
#include <QDomDocument>
#include <QPrinter> //to find out screen resolution
#include <cstdlib>
#include "qgsproject.h"
#include "qgsprojectproperty.h"
typedef QgsProjectVersion PFV;
QgsProjectFileTransform::transform QgsProjectFileTransform::transformers[] =
{
{PFV( 0, 8, 0 ), PFV( 0, 8, 1 ), &QgsProjectFileTransform::transformNull},
{PFV( 0, 8, 1 ), PFV( 0, 9, 0 ), &QgsProjectFileTransform::transform081to090},
{PFV( 0, 9, 0 ), PFV( 0, 9, 1 ), &QgsProjectFileTransform::transformNull},
{PFV( 0, 9, 1 ), PFV( 0, 10, 0 ), &QgsProjectFileTransform::transform091to0100},
// Following line is a hack that takes us straight from 0.9.2 to 0.11.0
// due to an unknown bug in migrating 0.9.2 files which we didnt pursue (TS & GS)
{PFV( 0, 9, 2 ), PFV( 0, 11, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 0, 10, 0 ), PFV( 0, 11, 0 ), &QgsProjectFileTransform::transform0100to0110},
{PFV( 0, 11, 0 ), PFV( 1, 0, 0 ), &QgsProjectFileTransform::transform0110to1000},
{PFV( 1, 0, 0 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 1, 0, 2 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 1, 1, 0 ), PFV( 1, 2, 0 ), &QgsProjectFileTransform::transform1100to1200},
{PFV( 1, 2, 0 ), PFV( 1, 3, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 1, 3, 0 ), PFV( 1, 4, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 1, 4, 0 ), PFV( 1, 5, 0 ), &QgsProjectFileTransform::transform1400to1500},
{PFV( 1, 5, 0 ), PFV( 1, 6, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 1, 6, 0 ), PFV( 1, 7, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 1, 7, 0 ), PFV( 1, 8, 0 ), &QgsProjectFileTransform::transformNull},
{PFV( 1, 8, 0 ), PFV( 1, 9, 0 ), &QgsProjectFileTransform::transform1800to1900}
};
bool QgsProjectFileTransform::updateRevision( QgsProjectVersion newVersion )
{
Q_UNUSED( newVersion );
bool returnValue = false;
if ( ! mDom.isNull() )
{
for ( std::size_t i = 0; i < sizeof( transformers ) / sizeof( transform ); i++ )
{
if ( transformers[i].from == mCurrentVersion )
{
// Run the transformer, and update the revision in every case
( this->*( transformers[i].transformFunc ) )();
mCurrentVersion = transformers[i].to;
returnValue = true;
}
}
}
return returnValue;
}
void QgsProjectFileTransform::dump()
{
QgsDebugMsg( QString( "Current project file version is %1.%2.%3" )
.arg( mCurrentVersion.majorVersion() )
.arg( mCurrentVersion.minorVersion() )
.arg( mCurrentVersion.subVersion() ) );
#ifdef QGISDEBUG
// Using QgsDebugMsg() didn't print the entire mDom...
std::cout << mDom.toString( 2 ).toLatin1().constData(); // OK
#endif
}
/*
* Transformers below!
*/
void QgsProjectFileTransform::transform081to090()
{
QgsDebugMsg( "Entering..." );
if ( ! mDom.isNull() )
{
// Start with inserting a mapcanvas element and populate it
QDomElement mapCanvas; // A null element.
// there should only be one <qgis>
QDomNode qgis = mDom.firstChildElement( "qgis" );
if ( ! qgis.isNull() )
{
QgsDebugMsg( "Populating new mapcanvas" );
// Create a mapcanvas
mapCanvas = mDom.createElement( "mapcanvas" );
// Append mapcanvas to parent 'qgis'.
qgis.appendChild( mapCanvas );
// Re-parent units
mapCanvas.appendChild( qgis.namedItem( "units" ) );
// Re-parent extent
mapCanvas.appendChild( qgis.namedItem( "extent" ) );
// See if we can find if projection is on.
QDomElement properties = qgis.firstChildElement( "properties" );
QDomElement spatial = properties.firstChildElement( "SpatialRefSys" );
QDomElement hasCrsTransformEnabled = spatial.firstChildElement( "ProjectionsEnabled" );
// Type is 'int', and '1' if on.
// Create an element
QDomElement projection = mDom.createElement( "projections" );
QgsDebugMsg( QString( "Projection flag: " ) + hasCrsTransformEnabled.text() );
// Set flag from ProjectionsEnabled
projection.appendChild( mDom.createTextNode( hasCrsTransformEnabled.text() ) );
// Set new element as child of <mapcanvas>
mapCanvas.appendChild( projection );
}
// Transforming coordinate-transforms
// Create a list of all map layers
QDomNodeList mapLayers = mDom.elementsByTagName( "maplayer" );
bool doneDestination = false;
for ( int i = 0; i < mapLayers.count(); i++ )
{
QDomNode mapLayer = mapLayers.item( i );
// Find the coordinatetransform
QDomNode coordinateTransform = mapLayer.namedItem( "coordinatetransform" );
// Find the sourcesrs
QDomNode sourceCrs = coordinateTransform.namedItem( "sourcesrs" );
// Rename to srs
sourceCrs.toElement().setTagName( "srs" );
// Re-parent to maplayer
mapLayer.appendChild( sourceCrs );
// Re-move coordinatetransform
// Take the destination CRS of the first layer and use for mapcanvas projection
if ( ! doneDestination )
{
// Use destination CRS from the last layer
QDomNode destinationCRS = coordinateTransform.namedItem( "destinationsrs" );
// Re-parent the destination CRS to the mapcanvas
// If mapcanvas wasn't set, nothing will happen.
mapCanvas.appendChild( destinationCRS );
// Only do this once
doneDestination = true;
}
mapLayer.removeChild( coordinateTransform );
//QDomNode id = mapLayer.namedItem("id");
//QgsDebugMsg(QString("Found maplayer ") + id.toElement().text());
}
// Set the flag 'visible' to match the status of 'checked'
QDomNodeList legendLayerFiles = mDom.elementsByTagName( "legendlayerfile" );
QgsDebugMsg( QString( "Legend layer file entries: " ) + QString::number( legendLayerFiles.count() ) );
for ( int i = 0; i < mapLayers.count(); i++ )
{
// Get one maplayer element from list
QDomElement mapLayer = mapLayers.item( i ).toElement();
// Find it's id.
QString id = mapLayer.firstChildElement( "id" ).text();
QgsDebugMsg( QString( "Handling layer " + id ) );
// Now, look it up in legend
for ( int j = 0; j < legendLayerFiles.count(); j++ )
{
QDomElement legendLayerFile = legendLayerFiles.item( j ).toElement();
if ( id == legendLayerFile.attribute( "layerid" ) )
{
// Found a the legend layer that matches the maplayer
QgsDebugMsg( "Found matching id" );
// Set visible flag from maplayer to legendlayer
legendLayerFile.setAttribute( "visible", mapLayer.attribute( "visible" ) );
// Set overview flag from maplayer to legendlayer
legendLayerFile.setAttribute( "isInOverview", mapLayer.attribute( "showInOverviewFlag" ) );
}
}
}
}
return;
}
void QgsProjectFileTransform::transform091to0100()
{
QgsDebugMsg( "entering" );
if ( ! mDom.isNull() )
{
// Insert transforms here!
QDomNodeList rasterPropertyList = mDom.elementsByTagName( "rasterproperties" );
QgsDebugMsg( QString( "Raster properties file entries: " ) + QString::number( rasterPropertyList.count() ) );
for ( int i = 0; i < rasterPropertyList.count(); i++ )
{
// Get one rasterproperty element from list, and rename the sub-properties.
QDomNode rasterProperty = rasterPropertyList.item( i );
// rasterProperty.namedItem("").toElement().setTagName("");
rasterProperty.namedItem( "stdDevsToPlotDouble" ).toElement().setTagName( "mStandardDeviations" );
rasterProperty.namedItem( "invertHistogramFlag" ).toElement().setTagName( "mInvertPixelsFlag" );
rasterProperty.namedItem( "showDebugOverLayFlag" ).toElement().setTagName( "mDebugOverLayFlag" );
rasterProperty.namedItem( "redBandNameQString" ).toElement().setTagName( "mRedBandName" );
rasterProperty.namedItem( "blueBandNameQString" ).toElement().setTagName( "mBlueBandName" );
rasterProperty.namedItem( "greenBandNameQString" ).toElement().setTagName( "mGreenBandName" );
rasterProperty.namedItem( "grayBandNameQString" ).toElement().setTagName( "mGrayBandName" );
}
// Changing symbol size for hard: symbols
QDomNodeList symbolPropertyList = mDom.elementsByTagName( "symbol" );
for ( int i = 0; i < symbolPropertyList.count(); i++ )
{
// Get the <poinmtsymbol> to check for 'hard:' for each <symbol>
QDomNode symbolProperty = symbolPropertyList.item( i );
QDomElement pointSymbol = symbolProperty.firstChildElement( "pointsymbol" );
if ( pointSymbol.text().startsWith( "hard:" ) )
{
// Get pointsize and line width
int lineWidth = symbolProperty.firstChildElement( "outlinewidth" ).text().toInt();
int pointSize = symbolProperty.firstChildElement( "pointsize" ).text().toInt();
// Just a precaution, checking for 0
if ( pointSize != 0 )
{
// int r = (s-2*lw)/2-1 --> 2r = (s-2*lw)-2 --> 2r+2 = s-2*lw
// --> 2r+2+2*lw = s
// where '2r' is the old size.
pointSize = pointSize + 2 + 2 * lineWidth;
QgsDebugMsg( QString( "Setting point size to %1" ).arg( pointSize ) );
QDomElement newPointSizeProperty = mDom.createElement( "pointsize" );
QDomText newPointSizeTxt = mDom.createTextNode( QString::number( pointSize ) );
newPointSizeProperty.appendChild( newPointSizeTxt );
symbolProperty.replaceChild( newPointSizeProperty, pointSymbol );
}
}
}
}
return;
}
void QgsProjectFileTransform::transform0100to0110()
{
if ( ! mDom.isNull() )
{
//Change 'outlinewidth' in QgsSymbol
QPrinter myPrinter( QPrinter::ScreenResolution );
int screenDpi = myPrinter.resolution();
double widthScaleFactor = 25.4 / screenDpi;
QDomNodeList outlineWidthList = mDom.elementsByTagName( "outlinewidth" );
for ( int i = 0; i < outlineWidthList.size(); ++i )
{
//calculate new width
QDomElement currentOutlineElem = outlineWidthList.at( i ).toElement();
double outlineWidth = currentOutlineElem.text().toDouble();
outlineWidth *= widthScaleFactor;
//replace old text node
QDomNode outlineTextNode = currentOutlineElem.firstChild();
QDomText newOutlineText = mDom.createTextNode( QString::number( outlineWidth ) );
currentOutlineElem.replaceChild( newOutlineText, outlineTextNode );
}
//Change 'pointsize' in QgsSymbol
QDomNodeList pointSizeList = mDom.elementsByTagName( "pointsize" );
for ( int i = 0; i < pointSizeList.size(); ++i )
{
//calculate new size
QDomElement currentPointSizeElem = pointSizeList.at( i ).toElement();
double pointSize = currentPointSizeElem.text().toDouble();
pointSize *= widthScaleFactor;
//replace old text node
QDomNode pointSizeTextNode = currentPointSizeElem.firstChild();
QDomText newPointSizeText = mDom.createTextNode( QString::number(( int )pointSize ) );
currentPointSizeElem.replaceChild( newPointSizeText, pointSizeTextNode );
}
}
}
void QgsProjectFileTransform::transform0110to1000()
{
if ( ! mDom.isNull() )
{
QDomNodeList layerList = mDom.elementsByTagName( "maplayer" );
for ( int i = 0; i < layerList.size(); ++i )
{
QDomElement layerElem = layerList.at( i ).toElement();
QString typeString = layerElem.attribute( "type" );
if ( typeString != "vector" )
{
continue;
}
//datasource
QDomNode dataSourceNode = layerElem.namedItem( "datasource" );
if ( dataSourceNode.isNull() )
{
return;
}
QString dataSource = dataSourceNode.toElement().text();
//provider key
QDomNode providerNode = layerElem.namedItem( "provider" );
if ( providerNode.isNull() )
{
return;
}
QString providerKey = providerNode.toElement().text();
//create the layer to get the provider for int->fieldName conversion
QgsVectorLayer* theLayer = new QgsVectorLayer( dataSource, "", providerKey, false );
if ( !theLayer->isValid() )
{
delete theLayer;
return;
}
QgsVectorDataProvider* theProvider = theLayer->dataProvider();
if ( !theProvider )
{
return;
}
QgsFields theFields = theProvider->fields();
//read classificationfield
QDomNodeList classificationFieldList = layerElem.elementsByTagName( "classificationfield" );
for ( int j = 0; j < classificationFieldList.size(); ++j )
{
QDomElement classificationFieldElem = classificationFieldList.at( j ).toElement();
int fieldNumber = classificationFieldElem.text().toInt();
if ( fieldNumber >= 0 && fieldNumber < theFields.count() )
{
QDomText fieldName = mDom.createTextNode( theFields[fieldNumber].name() );
QDomNode nameNode = classificationFieldElem.firstChild();
classificationFieldElem.replaceChild( fieldName, nameNode );
}
}
}
}
}
void QgsProjectFileTransform::transform1100to1200()
{
QgsDebugMsg( "Entering..." );
if ( mDom.isNull() )
return;
QDomNode qgis = mDom.firstChildElement( "qgis" );
if ( qgis.isNull() )
return;
QDomElement properties = qgis.firstChildElement( "properties" );
if ( properties.isNull() )
return;
QDomElement digitizing = properties.firstChildElement( "Digitizing" );
if ( digitizing.isNull() )
return;
QDomElement tolList = digitizing.firstChildElement( "LayerSnappingToleranceList" );
if ( tolList.isNull() )
return;
QDomElement tolUnitList = digitizing.firstChildElement( "LayerSnappingToleranceUnitList" );
if ( !tolUnitList.isNull() )
return;
QStringList units;
for ( int i = 0; i < tolList.childNodes().count(); i++ )
units << "0";
QgsPropertyValue value( units );
value.writeXML( "LayerSnappingToleranceUnitList", digitizing, mDom );
}
void QgsProjectFileTransform::transform1400to1500()
{
//Adapt the XML description of the composer legend model to version 1.5
if ( mDom.isNull() )
{
return;
}
//Add layer id to <VectorClassificationItem>
QDomNodeList layerItemList = mDom.elementsByTagName( "LayerItem" );
QDomElement currentLayerItemElem;
QString currentLayerId;
for ( int i = 0; i < layerItemList.size(); ++i )
{
currentLayerItemElem = layerItemList.at( i ).toElement();
if ( currentLayerItemElem.isNull() )
{
continue;
}
currentLayerId = currentLayerItemElem.attribute( "layerId" );
QDomNodeList vectorClassificationList = currentLayerItemElem.elementsByTagName( "VectorClassificationItem" );
QDomElement currentClassificationElem;
for ( int j = 0; j < vectorClassificationList.size(); ++j )
{
currentClassificationElem = vectorClassificationList.at( j ).toElement();
if ( !currentClassificationElem.isNull() )
{
currentClassificationElem.setAttribute( "layerId", currentLayerId );
}
}
//replace the text items with VectorClassification or RasterClassification items
QDomNodeList textItemList = currentLayerItemElem.elementsByTagName( "TextItem" );
QDomElement currentTextItem;
for ( int j = 0; j < textItemList.size(); ++j )
{
currentTextItem = textItemList.at( j ).toElement();
if ( currentTextItem.isNull() )
{
continue;
}
QDomElement classificationElement;
if ( vectorClassificationList.size() > 0 ) //we guess it is a vector layer
{
classificationElement = mDom.createElement( "VectorClassificationItem" );
}
else
{
classificationElement = mDom.createElement( "RasterClassificationItem" );
}
classificationElement.setAttribute( "layerId", currentLayerId );
classificationElement.setAttribute( "text", currentTextItem.attribute( "text" ) );
currentLayerItemElem.replaceChild( classificationElement, currentTextItem );
}
}
}
void QgsProjectFileTransform::transform1800to1900()
{
if ( mDom.isNull() )
{
return;
}
QDomNodeList layerItemList = mDom.elementsByTagName( "rasterproperties" );
for ( int i = 0; i < layerItemList.size(); ++i )
{
QDomElement rasterPropertiesElem = layerItemList.at( i ).toElement();
QDomNode layerNode = rasterPropertiesElem.parentNode();
QDomElement dataSourceElem = layerNode.firstChildElement( "datasource" );
QDomElement layerNameElem = layerNode.firstChildElement( "layername" );
QgsRasterLayer rasterLayer;
// TODO: We have to use more data from project file to read the layer it correctly,
// OTOH, we should not read it until it was converted
rasterLayer.readXML( layerNode );
convertRasterProperties( mDom, layerNode, rasterPropertiesElem, &rasterLayer );
}
//composer: replace mGridAnnotationPosition with mLeftGridAnnotationPosition & co.
// and mGridAnnotationDirection with mLeftGridAnnotationDirection & co.
QDomNodeList composerMapList = mDom.elementsByTagName( "ComposerMap" );
for ( int i = 0; i < composerMapList.size(); ++i )
{
QDomNodeList gridList = composerMapList.at( i ).toElement().elementsByTagName( "Grid" );
for ( int j = 0; j < gridList.size(); ++j )
{
QDomNodeList annotationList = gridList.at( j ).toElement().elementsByTagName( "Annotation" );
for ( int k = 0; k < annotationList.size(); ++k )
{
QDomElement annotationElem = annotationList.at( k ).toElement();
//position
if ( annotationElem.hasAttribute( "position" ) )
{
int pos = annotationElem.attribute( "position" ).toInt();
annotationElem.setAttribute( "leftPosition", pos );
annotationElem.setAttribute( "rightPosition", pos );
annotationElem.setAttribute( "topPosition", pos );
annotationElem.setAttribute( "bottomPosition", pos );
annotationElem.removeAttribute( "position" );
}
//direction
if ( annotationElem.hasAttribute( "direction" ) )
{
int dir = annotationElem.attribute( "direction" ).toInt();
if ( dir == 2 )
{
annotationElem.setAttribute( "leftDirection", 0 );
annotationElem.setAttribute( "rightDirection", 0 );
annotationElem.setAttribute( "topDirection", 1 );
annotationElem.setAttribute( "bottomDirection", 1 );
}
else if ( dir == 3 )
{
annotationElem.setAttribute( "leftDirection", 1 );
annotationElem.setAttribute( "rightDirection", 1 );
annotationElem.setAttribute( "topDirection", 0 );
annotationElem.setAttribute( "bottomDirection", 0 );
}
else
{
annotationElem.setAttribute( "leftDirection", dir );
annotationElem.setAttribute( "rightDirection", dir );
annotationElem.setAttribute( "topDirection", dir );
annotationElem.setAttribute( "bottomDirection", dir );
}
annotationElem.removeAttribute( "direction" );
}
}
}
}
//Composer: move all items under Composition element
QDomNodeList composerList = mDom.elementsByTagName( "Composer" );
for ( int i = 0; i < composerList.size(); ++i )
{
QDomElement composerElem = composerList.at( i ).toElement();
//find <QgsComposition element
QDomElement compositionElem = composerElem.firstChildElement( "Composition" );
if ( compositionElem.isNull() )
{
continue;
}
QDomNodeList composerChildren = composerElem.childNodes();
if ( composerChildren.size() < 1 )
{
continue;
}
for ( int j = composerChildren.size() - 1; j >= 0; --j )
{
QDomElement childElem = composerChildren.at( j ).toElement();
if ( childElem.tagName() == "Composition" )
{
continue;
}
composerElem.removeChild( childElem );
compositionElem.appendChild( childElem );
}
}
// SimpleFill symbol layer v2: avoid double transparency
// replacing alpha value of symbol layer's color with 255 (the
// transparency value is already stored as symbol transparency).
QDomNodeList rendererList = mDom.elementsByTagName( "renderer-v2" );
for ( int i = 0; i < rendererList.size(); ++i )
{
QDomNodeList layerList = rendererList.at( i ).toElement().elementsByTagName( "layer" );
for ( int j = 0; j < layerList.size(); ++j )
{
QDomElement layerElem = layerList.at( j ).toElement();
if ( layerElem.attribute( "class" ) == "SimpleFill" )
{
QDomNodeList propList = layerElem.elementsByTagName( "prop" );
for ( int k = 0; k < propList.size(); ++k )
{
QDomElement propElem = propList.at( k ).toElement();
if ( propElem.attribute( "k" ) == "color" || propElem.attribute( "k" ) == "color_border" )
{
propElem.setAttribute( "v", propElem.attribute( "v" ).section( ",", 0, 2 ) + ",255" );
}
}
}
}
}
QgsDebugMsg( mDom.toString() );
}
void QgsProjectFileTransform::convertRasterProperties( QDomDocument& doc, QDomNode& parentNode,
QDomElement& rasterPropertiesElem, QgsRasterLayer* rlayer )
{
//no data
//TODO: We would need to set no data on all bands, but we don't know number of bands here
QDomNode noDataNode = rasterPropertiesElem.namedItem( "mNoDataValue" );
QDomElement noDataElement = noDataNode.toElement();
if ( !noDataElement.text().isEmpty() )
{
QgsDebugMsg( "mNoDataValue = " + noDataElement.text() );
QDomElement noDataElem = doc.createElement( "noData" );
QDomElement noDataRangeList = doc.createElement( "noDataRangeList" );
noDataRangeList.setAttribute( "bandNo", 1 );
QDomElement noDataRange = doc.createElement( "noDataRange" );
noDataRange.setAttribute( "min", noDataElement.text() );
noDataRange.setAttribute( "max", noDataElement.text() );
noDataRangeList.appendChild( noDataRange );
noDataElem.appendChild( noDataRangeList );
parentNode.appendChild( noDataElem );
}
QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
//convert general properties
//invert color
rasterRendererElem.setAttribute( "invertColor", "0" );
QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( "mInvertColor" );
if ( !invertColorElem.isNull() )
{
if ( invertColorElem.text() == "true" )
{
rasterRendererElem.setAttribute( "invertColor", "1" );
}
}
//opacity
rasterRendererElem.setAttribute( "opacity", "1" );
QDomElement transparencyElem = parentNode.firstChildElement( "transparencyLevelInt" );
if ( !transparencyElem.isNull() )
{
double transparency = transparencyElem.text().toInt();
rasterRendererElem.setAttribute( "opacity", QString::number( transparency / 255.0 ) );
}
//alphaBand was not saved until now (bug)
rasterRendererElem.setAttribute( "alphaBand", -1 );
//gray band is used for several renderers
int grayBand = rasterBandNumber( rasterPropertiesElem, "mGrayBandName", rlayer );
//convert renderer specific properties
QString drawingStyle = rasterPropertiesElem.firstChildElement( "mDrawingStyle" ).text();
// While PalettedColor should normaly contain only integer values, usually
// color palette 0-255, it may happen (Tim, issue #7023) that it contains
// colormap classification with double values and text labels
// (which should normaly only appear in SingleBandPseudoColor drawingStyle)
// => we have to check first the values and change drawingStyle if necessary
if ( drawingStyle == "PalettedColor" )
{
QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" );
QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( "colorRampEntry" );
for ( int i = 0; i < colorRampEntryList.size(); ++i )
{
QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
QString strValue = colorRampEntryElem.attribute( "value" );
double value = strValue.toDouble();
if ( value < 0 || value > 10000 || value != ( int )value )
{
QgsDebugMsg( QString( "forcing SingleBandPseudoColor value = %1" ).arg( value ) );
drawingStyle = "SingleBandPseudoColor";
break;
}
}
}
if ( drawingStyle == "SingleBandGray" )
{
rasterRendererElem.setAttribute( "type", "singlebandgray" );
rasterRendererElem.setAttribute( "grayBand", grayBand );
transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
}
else if ( drawingStyle == "SingleBandPseudoColor" )
{
rasterRendererElem.setAttribute( "type", "singlebandpseudocolor" );
rasterRendererElem.setAttribute( "band", grayBand );
QDomElement newRasterShaderElem = doc.createElement( "rastershader" );
QDomElement newColorRampShaderElem = doc.createElement( "colorrampshader" );
newRasterShaderElem.appendChild( newColorRampShaderElem );
rasterRendererElem.appendChild( newRasterShaderElem );
//switch depending on mColorShadingAlgorithm
QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( "mColorShadingAlgorithm" ).text();
if ( colorShadingAlgorithm == "PseudoColorShader" || colorShadingAlgorithm == "FreakOutShader" )
{
newColorRampShaderElem.setAttribute( "colorRampType", "INTERPOLATED" );
//get minmax from rasterlayer
QgsRasterBandStats rasterBandStats = rlayer->dataProvider()->bandStatistics( grayBand );
double minValue = rasterBandStats.minimumValue;
double maxValue = rasterBandStats.maximumValue;
double breakSize = ( maxValue - minValue ) / 3;
QStringList colorList;
if ( colorShadingAlgorithm == "FreakOutShader" )
{
colorList << "#ff00ff" << "#00ffff" << "#ff0000" << "#00ff00";
}
else //pseudocolor
{
colorList << "#0000ff" << "#00ffff" << "#ffff00" << "#ff0000";
}
QStringList::const_iterator colorIt = colorList.constBegin();
double boundValue = minValue;
for ( ; colorIt != colorList.constEnd(); ++colorIt )
{
QDomElement newItemElem = doc.createElement( "item" );
newItemElem.setAttribute( "value", QString::number( boundValue ) );
newItemElem.setAttribute( "label", QString::number( boundValue ) );
newItemElem.setAttribute( "color", *colorIt );
newColorRampShaderElem.appendChild( newItemElem );
boundValue += breakSize;
}
}
else if ( colorShadingAlgorithm == "ColorRampShader" )
{
QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" );
QString type = customColorRampElem.firstChildElement( "colorRampType" ).text();
newColorRampShaderElem.setAttribute( "colorRampType", type );
QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( "colorRampEntry" );
QString value, label;
QColor newColor;
int red, green, blue;
QDomElement currentItemElem;
for ( int i = 0; i < colorNodeList.size(); ++i )
{
currentItemElem = colorNodeList.at( i ).toElement();
value = currentItemElem.attribute( "value" );
label = currentItemElem.attribute( "label" );
red = currentItemElem.attribute( "red" ).toInt();
green = currentItemElem.attribute( "green" ).toInt();
blue = currentItemElem.attribute( "blue" ).toInt();
newColor = QColor( red, green, blue );
QDomElement newItemElem = doc.createElement( "item" );
newItemElem.setAttribute( "value", value );
newItemElem.setAttribute( "label", label );
newItemElem.setAttribute( "color", newColor.name() );
newColorRampShaderElem.appendChild( newItemElem );
}
}
}
else if ( drawingStyle == "PalettedColor" )
{
rasterRendererElem.setAttribute( "type", "paletted" );
rasterRendererElem.setAttribute( "band", grayBand );
QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" );
QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( "colorRampEntry" );
QDomElement newColorPaletteElem = doc.createElement( "colorPalette" );
int red = 0;
int green = 0;
int blue = 0;
int value = 0;
QDomElement colorRampEntryElem;
for ( int i = 0; i < colorRampEntryList.size(); ++i )
{
colorRampEntryElem = colorRampEntryList.at( i ).toElement();
QDomElement newPaletteElem = doc.createElement( "paletteEntry" );
value = ( int )( colorRampEntryElem.attribute( "value" ).toDouble() );
newPaletteElem.setAttribute( "value", value );
red = colorRampEntryElem.attribute( "red" ).toInt();
green = colorRampEntryElem.attribute( "green" ).toInt();
blue = colorRampEntryElem.attribute( "blue" ).toInt();
newPaletteElem.setAttribute( "color", QColor( red, green, blue ).name() );
newColorPaletteElem.appendChild( newPaletteElem );
}
rasterRendererElem.appendChild( newColorPaletteElem );
}
else if ( drawingStyle == "MultiBandColor" )
{
rasterRendererElem.setAttribute( "type", "multibandcolor" );
//red band, green band, blue band
int redBand = rasterBandNumber( rasterPropertiesElem, "mRedBandName", rlayer );
int greenBand = rasterBandNumber( rasterPropertiesElem, "mGreenBandName", rlayer );
int blueBand = rasterBandNumber( rasterPropertiesElem, "mBlueBandName", rlayer );
rasterRendererElem.setAttribute( "redBand", redBand );
rasterRendererElem.setAttribute( "greenBand", greenBand );
rasterRendererElem.setAttribute( "blueBand", blueBand );
transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
}
else
{
return;
}
//replace rasterproperties element with rasterrenderer element
if ( !parentNode.isNull() )
{
parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
}
}
int QgsProjectFileTransform::rasterBandNumber( const QDomElement& rasterPropertiesElem, const QString bandName,
QgsRasterLayer* rlayer )
{
if ( !rlayer )
{
return -1;
}
int band = -1;
QDomElement rasterBandElem = rasterPropertiesElem.firstChildElement( bandName );
if ( !rasterBandElem.isNull() )
{
for ( int i = 1; i <= rlayer->bandCount(); i++ )
{
if ( rlayer->bandName( i ) == rasterBandElem.text() )
{
band = i;
break;
}
}
}
return band;
}
void QgsProjectFileTransform::transformContrastEnhancement( QDomDocument& doc, const QDomElement& rasterproperties, QDomElement& rendererElem )
{
if ( rasterproperties.isNull() || rendererElem.isNull() )
{
return;
}
double minimumValue = 0;
double maximumValue = 0;
QDomElement contrastMinMaxElem = rasterproperties.firstChildElement( "contrastEnhancementMinMaxValues" );
if ( contrastMinMaxElem.isNull() )
{
return;
}
QDomElement contrastEnhancementAlgorithmElem = rasterproperties.firstChildElement( "mContrastEnhancementAlgorithm" );
if ( contrastEnhancementAlgorithmElem.isNull() )
{
return;
}
//convert enhancement name to enumeration
int algorithmEnum = 0;
QString algorithmString = contrastEnhancementAlgorithmElem.text();
if ( algorithmString == "StretchToMinimumMaximum" )
{
algorithmEnum = 1;
}
else if ( algorithmString == "StretchAndClipToMinimumMaximum" )
{
algorithmEnum = 2;
}
else if ( algorithmString == "ClipToMinimumMaximum" )
{
algorithmEnum = 3;
}
else if ( algorithmString == "UserDefinedEnhancement" )
{
algorithmEnum = 4;
}
QDomNodeList minMaxEntryList = contrastMinMaxElem.elementsByTagName( "minMaxEntry" );
QStringList enhancementNameList;
if ( minMaxEntryList.size() == 1 )
{
enhancementNameList << "contrastEnhancement";
}
if ( minMaxEntryList.size() == 3 )
{
enhancementNameList << "redContrastEnhancement" << "greenContrastEnhancement" << "blueContrastEnhancement";
}
if ( minMaxEntryList.size() > enhancementNameList.size() )
{
return;
}
QDomElement minMaxEntryElem;
for ( int i = 0; i < minMaxEntryList.size(); ++i )
{
minMaxEntryElem = minMaxEntryList.at( i ).toElement();
QDomElement minElem = minMaxEntryElem.firstChildElement( "min" );
if ( minElem.isNull() )
{
return;
}
minimumValue = minElem.text().toDouble();
QDomElement maxElem = minMaxEntryElem.firstChildElement( "max" );
if ( maxElem.isNull() )
{
return;
}
maximumValue = maxElem.text().toDouble();
QDomElement newContrastEnhancementElem = doc.createElement( enhancementNameList.at( i ) );
QDomElement newMinValElem = doc.createElement( "minValue" );
QDomText minText = doc.createTextNode( QString::number( minimumValue ) );
newMinValElem.appendChild( minText );
newContrastEnhancementElem.appendChild( newMinValElem );
QDomElement newMaxValElem = doc.createElement( "maxValue" );
QDomText maxText = doc.createTextNode( QString::number( maximumValue ) );
newMaxValElem.appendChild( maxText );
newContrastEnhancementElem.appendChild( newMaxValElem );
QDomElement newAlgorithmElem = doc.createElement( "algorithm" );
QDomText newAlgorithmText = doc.createTextNode( QString::number( algorithmEnum ) );
newAlgorithmElem.appendChild( newAlgorithmText );
newContrastEnhancementElem.appendChild( newAlgorithmElem );
rendererElem.appendChild( newContrastEnhancementElem );
}
}
void QgsProjectFileTransform::transformRasterTransparency( QDomDocument& doc, const QDomElement& orig, QDomElement& rendererElem )
{
//soon...
Q_UNUSED( doc );
Q_UNUSED( orig );
Q_UNUSED( rendererElem );
}