/
GeoTiffIIOMetadataEncoder.java
856 lines (695 loc) · 25.9 KB
/
GeoTiffIIOMetadataEncoder.java
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
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2005-2016, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.coverage.grid.io.imageio.geotiff;
import it.geosolutions.imageio.plugins.tiff.BaselineTIFFTagSet;
import it.geosolutions.imageio.plugins.tiff.GeoTIFFTagSet;
import it.geosolutions.imageio.plugins.tiff.TIFFTag;
import it.geosolutions.imageio.plugins.tiff.TIFFTagSet;
import java.awt.geom.AffineTransform;
import java.util.Iterator;
import java.util.Map;
import org.geotools.util.KeySortedList;
import org.jdom2.Element;
/**
* This class is responsible for encoding the geotiff tags into suitable
* metadata for the ImageIO library.
*
* <p>
* Basically it is and encoder/adapter that collects all the different tags,
* order it accordingly to the spec and then organize then into a dom tree ready
* to be used by the ImageIO metadata mechanism.
*
*
*
* @author Simone Giannecchini, GeoSolutions
* @since 2.3
*
*
*
*
* @source $URL$
*/
public class GeoTiffIIOMetadataEncoder {
/** ASCII_SEPARATOR */
public static final String ASCII_SEPARATOR = "|";
public enum TagSet {
BASELINE {
@Override
TIFFTagSet getTagSet() {
return BaselineTIFFTagSet.getInstance();
}
},
GEOTIFF {
@Override
TIFFTagSet getTagSet() {
return GeoTIFFTagSet.getInstance();
}
};
abstract TIFFTagSet getTagSet();
public static TIFFTagSet getDefault() {
return BASELINE.getTagSet();
}
}
private int numModelTiePoints;
private TiePoint[] modelTiePoints;
private PixelScale modelPixelScale;
private double[] modelTransformation;
private int numGeoTiffEntries;
private KeySortedList<Integer, GeoKeyEntry> geoTiffEntries;
private int numGeoTiffDoubleParams;
private double[] geoTiffDoubleParams;
private int numGeoTiffAsciiParams;
private StringBuilder geoTiffAsciiParams;
private double noData;
private boolean isNodataSet = false;
/** TiffTAG Ascii metadata: KeyValue pairs where key is the TAG ID and value is the content */
private Map<String, String> tiffTagsMetadata;
private boolean isMetadataSet;
public GeoTiffIIOMetadataEncoder() {
this(GeoTiffConstants.DEFAULT_GEOTIFF_VERSION,
GeoTiffConstants.DEFAULT_KEY_REVISION_MAJOR,
GeoTiffConstants.DEFAULT_KEY_REVISION_MINOR);
}
public GeoTiffIIOMetadataEncoder(final int geoTIFFVersion,
final int keyRevisionMajor, final int keyRevisionMinor) {
geoTiffEntries = new KeySortedList<Integer, GeoKeyEntry>();
geoTiffDoubleParams = new double[GeoTiffConstants.ARRAY_ELEM_INCREMENT];
geoTiffAsciiParams = new StringBuilder();
modelTiePoints = new TiePoint[GeoTiffConstants.ARRAY_ELEM_INCREMENT];
modelPixelScale = new PixelScale();
modelTransformation = new double[16];
addGeoKeyEntry(geoTIFFVersion, keyRevisionMajor, 1, keyRevisionMinor);
}
public static boolean isTiffUShort(final int value) {
return (value >= GeoTiffConstants.USHORT_MIN)
&& (value <= GeoTiffConstants.USHORT_MAX);
}
public int getGeoTIFFVersion() {
return getGeoKeyEntryAt(0).getKeyID();
}
public void setGeoTIFFVersion(int version) {
getGeoKeyEntryAt(0).setKeyID(version);
}
public int getKeyRevisionMajor() {
return getGeoKeyEntryAt(0).getTiffTagLocation();
}
public int getKeyRevisionMinor() {
return getGeoKeyEntryAt(0).getCount();
}
public void setKeyRevision(int major, int minor) {
getGeoKeyEntryAt(0).setTiffTagLocation(major);
getGeoKeyEntryAt(0).setCount(minor);
}
public double getModelPixelScaleX() {
return modelPixelScale.getScaleX();
}
public double getModelPixelScaleY() {
return modelPixelScale.getScaleY();
}
public double getModelPixelScaleZ() {
return modelPixelScale.getScaleZ();
}
public void setModelPixelScale(double x, double y) {
setModelPixelScale(x, y, 0.0);
}
public void setModelPixelScale(double x, double y, double z) {
if (isModelTransformationSet())
throw new IllegalStateException(
"ModelTransformationTag already set. It is not possible to set the ModelPixelScale.");
modelPixelScale.setScaleX(x);
modelPixelScale.setScaleY(y);
modelPixelScale.setScaleZ(z);
}
public int getNumModelTiePoints() {
return numModelTiePoints;
}
public TiePoint getModelTiePoint() {
return getModelTiePointAt(0);
}
public TiePoint getModelTiePointAt(int index) {
return modelTiePoints[index];
}
public void setModelTiePoint(double i, double j, double x, double y) {
setModelTiePoint(i, j, 0.0, x, y, 0.0);
}
public void setModelTiePoint(double i, double j, double k, double x,
double y, double z) {
if (isModelTransformationSet())
throw new IllegalStateException(
"ModelTransformationTag already set. It is not possible to set the ModelTiePoint.");
if (getNumModelTiePoints() > 0) {
getModelTiePointAt(0).set(i, j, k, x, y, z);
} else {
addModelTiePoint(i, j, k, x, y, z);
}
}
public void addModelTiePoint(double i, double j, double x, double y) {
addModelTiePoint(i, j, 0.0, x, y, 0.0);
}
public void addModelTiePoint(double i, double j, double k, double x,
double y, double z) {
final int numTiePoints = numModelTiePoints;
if (numTiePoints >= (modelTiePoints.length - 1)) {
final TiePoint[] tiePoints = new TiePoint[numTiePoints
+ GeoTiffConstants.ARRAY_ELEM_INCREMENT];
System.arraycopy(modelTiePoints, 0, tiePoints, 0, numTiePoints);
modelTiePoints = tiePoints;
}
modelTiePoints[numTiePoints] = new TiePoint(i, j, k, x, y, z);
numModelTiePoints++;
}
public int getNumGeoKeyEntries() {
return numGeoTiffEntries;
}
public GeoKeyEntry getGeoKeyEntryAt(int index) {
// got to retrieve the eleme at a certain index
final Object it = this.geoTiffEntries.get(index);
if (it != null)
return (GeoKeyEntry) (it);
return null;
}
public GeoKeyEntry getGeoKeyEntry(int keyID) {
GeoKeyEntry retVal = null;
if(geoTiffEntries.count(keyID)<=0){
return null;
}
final Object o = geoTiffEntries.first(keyID);
if (o != null){
retVal = (GeoKeyEntry) o;
}
return retVal;
}
public boolean hasGeoKeyEntry(int keyID) {
return getGeoKeyEntry(keyID) != null;
}
public int getGeoShortParam(int keyID) {
final GeoKeyEntry entry = getNonNullGeoKeyEntry(keyID);
final int tag = entry.getTiffTagLocation();
final int value = entry.getValueOffset();
checkParamTag(tag, 0);
return value;
}
public double getGeoDoubleParam(int keyID) {
final GeoKeyEntry entry = getNonNullGeoKeyEntry(keyID);
final int tag = entry.getTiffTagLocation();
final int offset = entry.getValueOffset();
checkParamTag(tag, getGeoDoubleParamsTag().getNumber());
return geoTiffDoubleParams[offset];
}
public double[] getGeoDoubleParams(int keyID) {
return getGeoDoubleParams(keyID, null);
}
public double[] getGeoDoubleParams(int keyID, double[] values) {
final GeoKeyEntry entry = getNonNullGeoKeyEntry(keyID);
final int tag = entry.getTiffTagLocation();
final int count = entry.getCount();
final int offset = entry.getValueOffset();
checkParamTag(tag, getGeoDoubleParamsTag().getNumber());
if (values == null) {
values = new double[count];
}
System.arraycopy(geoTiffDoubleParams, offset, values, 0, count);
return values;
}
/**
* Gives access to the GeoAscii tiff content for the specified GeoKey metatag.
*
* <p>
* Returns <code>null</code> in case the ascii params is not present.
*
* @param keyID the MetaTag to look for.
*
* @return <code>null</code> in case the ascii params is not present, otherwise the metatag content.
*/
public String getGeoAsciiParam(int keyID) {
final GeoKeyEntry entry = getGeoKeyEntry(keyID);
if(entry==null){
return null;
}
final int tag = entry.getTiffTagLocation();
final int count = entry.getCount();
final int offset = entry.getValueOffset();
checkParamTag(tag, getGeoAsciiParamsTag().getNumber());
return geoTiffAsciiParams.substring(offset, (offset + count));
}
public GeoKeyEntry addGeoShortParam(int keyID, int value) {
return addGeoKeyEntry(keyID, 0, 1, value);
}
public void addGeoDoubleParam(int keyID, double value) {
addGeoDoubleParamsRef(keyID, 1);
addDoubleParam(value);
}
public void addGeoDoubleParams(int keyID, double[] values) {
addGeoDoubleParamsRef(keyID, values.length);
for (int i = 0; i < values.length; i++) {
addDoubleParam(values[i]);
}
}
public void addGeoAscii(int keyID, String value) {
// check whether or not this is the first geoascii element
GeoKeyEntry currentEntry = getGeoKeyEntry(keyID);
if(currentEntry!=null){
// get old value
final String currentGeoAscii=getGeoAsciiParam(keyID);
assert currentGeoAscii!=null;
// store old length
final int currentLength=currentGeoAscii.length();
assert currentEntry.getCount()==currentLength;
// update value
final StringBuilder newValue= new StringBuilder(currentGeoAscii);
if(!currentGeoAscii.endsWith(ASCII_SEPARATOR)){
newValue.append(ASCII_SEPARATOR);
}
newValue.append(value).append(ASCII_SEPARATOR);
// now add to the geoascii metatag
GeoKeyEntry newEntry = updateGeoAsciiParamsRef(keyID, newValue.length(),currentEntry.getValueOffset());
// +1 for the '|' character to be appended
replaceAsciiParam(newValue.toString(), currentEntry.getValueOffset(), currentEntry.getValueOffset()+currentLength);
} else {
// now add to the geoascii metatag
// +1 is required in advance since we need to add the pipe which will be encoded.
addGeoAsciiParamsRef(keyID, value.length() + 1);
// +1 for the '|' character to be appended
appendAsciiParam(value);
}
}
/**
* Updates the provided metatag to the GeoKey directory.
*
* @param keyID the GeoTiff metatag id
* @param tag the TIFF tag to which this points to
* @param count the new count value
* @param offset the new offset value inside the tiff tag or 0 if the value is embeeded
* @return the updated {@link GeoKeyEntry}.
*/
private GeoKeyEntry updateGeoKeyEntry(int keyID, int tag, int count, int offset) {
if (!isTiffUShort(keyID)) {
throw new IllegalArgumentException("keyID is not a TIFF USHORT");
}
if (!isTiffUShort(tag)) {
throw new IllegalArgumentException("tag is not a TIFF USHORT");
}
if (!isTiffUShort(count)) {
throw new IllegalArgumentException("count is not a TIFF USHORT");
}
if (!isTiffUShort(offset)) {
throw new IllegalArgumentException("offset is not a TIFF USHORT");
}
final GeoKeyEntry element = new GeoKeyEntry(keyID, tag,count, offset);
// replacing
final int valuesRemoved=geoTiffEntries.removeAll(keyID); // purge old values for this tag
assert valuesRemoved==1;
geoTiffEntries.add(Integer.valueOf(keyID), element);
return element;
}
/**
* Adds the provided metatag to the GeoKey directory.
*
* @param keyID the GeoTiff metatag id
* @param tag the TIFF tag to which this points to
* @param count the new count value
* @param offset the new offset value inside the tiff tag or 0 if the value is embeeded
* @return the added {@link GeoKeyEntry}.
*/
private GeoKeyEntry addGeoKeyEntry(int keyID, int tag, int count, int offset) {
if (!isTiffUShort(keyID)) {
throw new IllegalArgumentException("keyID is not a TIFF USHORT");
}
if (!isTiffUShort(tag)) {
throw new IllegalArgumentException("tag is not a TIFF USHORT");
}
if (!isTiffUShort(count)) {
throw new IllegalArgumentException("count is not a TIFF USHORT");
}
if (!isTiffUShort(offset)) {
throw new IllegalArgumentException("offset is not a TIFF USHORT");
}
final int numKeyEntries = numGeoTiffEntries;
final GeoKeyEntry element = new GeoKeyEntry(keyID, tag,count, offset);
numGeoTiffEntries++;
geoTiffEntries.add(Integer.valueOf(keyID), element);
getGeoKeyEntryAt(0).setCount(numKeyEntries);
return element;
}
public void assignTo(Element element) {
if (!element.getName().equals(GeoTiffConstants.GEOTIFF_IIO_ROOT_ELEMENT_NAME)) {
throw new IllegalArgumentException("root not found: "
+ GeoTiffConstants.GEOTIFF_IIO_ROOT_ELEMENT_NAME);
}
final Element ifd1 = element.getChild(GeoTiffConstants.GEOTIFF_IFD_TAG);
if (ifd1 == null) {
throw new IllegalArgumentException("Unable to find child "+ GeoTiffConstants.GEOTIFF_IFD_TAG);
}
final Element ifd2 = createIFD();
ifd1.setAttribute(GeoTiffConstants.GEOTIFF_TAGSETS_ATT_NAME, ifd2.getAttributeValue(GeoTiffConstants.GEOTIFF_TAGSETS_ATT_NAME));
final Element[] childElems = (Element[]) ifd2.getChildren().toArray(new Element[0]);
for (int i = 0; i < childElems.length; i++) {
final Element child = childElems[i];
ifd2.removeContent(child);
ifd1.addContent(child);
}
}
public Element createRootTree() {
final Element rootElement = new Element(GeoTiffConstants.GEOTIFF_IIO_ROOT_ELEMENT_NAME);
rootElement.addContent(createIFD());
return rootElement;
}
protected static TIFFTag getGeoKeyDirectoryTag() {
return GeoTIFFTagSet.getInstance().getTag(GeoTIFFTagSet.TAG_GEO_KEY_DIRECTORY);
}
protected static TIFFTag getGeoDoubleParamsTag() {
return GeoTIFFTagSet.getInstance().getTag(GeoTIFFTagSet.TAG_GEO_DOUBLE_PARAMS);
}
protected static TIFFTag getGeoAsciiParamsTag() {
return GeoTIFFTagSet.getInstance().getTag(GeoTIFFTagSet.TAG_GEO_ASCII_PARAMS);
}
protected static TIFFTag getModelPixelScaleTag() {
return GeoTIFFTagSet.getInstance().getTag(GeoTIFFTagSet.TAG_MODEL_PIXEL_SCALE);
}
protected static TIFFTag getModelTiePointTag() {
return GeoTIFFTagSet.getInstance().getTag(GeoTIFFTagSet.TAG_MODEL_TIE_POINT);
}
protected static TIFFTag getModelTransformationTag() {
return GeoTIFFTagSet.getInstance().getTag(GeoTIFFTagSet.TAG_MODEL_TRANSFORMATION);
}
protected static TIFFTag getAsciiTag(String set, int tagID) {
if (set != null && set.length() > 0 ) {
try {
TagSet tagSet = TagSet.valueOf(set);
if (tagSet != null){
return tagSet.getTagSet().getTag(tagID);
}
} catch (Exception e){
}
}
return null;
}
protected static TIFFTag getNoDataTag() {
return GeoTiffConstants.NODATA_TAG;
}
private GeoKeyEntry getNonNullGeoKeyEntry(int keyID) {
final GeoKeyEntry entry = getGeoKeyEntry(keyID);
if (entry == null) {
throw new IllegalArgumentException("Unable to find an entry for the provided geo key " + keyID);
}
return entry;
}
private void checkParamTag(final int tag, final int expectedTag) {
if (tag != expectedTag) {
if (expectedTag == 0) {
throw new IllegalArgumentException("invalid key access, not a GeoTIFF SHORT parameter");
} else if (expectedTag == getGeoDoubleParamsTag().getNumber()) {
throw new IllegalArgumentException("invalid key access, not a GeoTIFF DOUBLE parameter");
} else if (expectedTag == getGeoAsciiParamsTag().getNumber()) {
throw new IllegalArgumentException("invalid key access, not a GeoTIFF ASCII parameter");
} else {
throw new IllegalStateException();
}
}
}
private void addDoubleParam(double param) {
final int numDoubleParams = numGeoTiffDoubleParams;
if (numDoubleParams >= (geoTiffDoubleParams.length - 1)) {
final double[] doubleParams = new double[numDoubleParams+ GeoTiffConstants.ARRAY_ELEM_INCREMENT];
System.arraycopy(geoTiffDoubleParams, 0, doubleParams, 0,numDoubleParams);
geoTiffDoubleParams = doubleParams;
}
geoTiffDoubleParams[numDoubleParams] = param;
numGeoTiffDoubleParams++;
}
/**
* Replaces part of the ascii params with the provided elements
*
* @param str the {@link String} for the replacement.
*/
private void replaceAsciiParam(String str, int start, int end) {
geoTiffAsciiParams.replace(start, end, str);
}
/**
* Appends a new ascii params at the current list of elements, incrementing the size and appending a new separator
* "|"
*
* @param param the {@link String} to be appended.
*/
private void appendAsciiParam(String param) {
geoTiffAsciiParams.append(param);
geoTiffAsciiParams.append('|');
numGeoTiffAsciiParams++;
}
private GeoKeyEntry addGeoDoubleParamsRef(int keyID, int count) {
return addGeoKeyEntry(keyID, getGeoDoubleParamsTag().getNumber(), count,getCurrentGeoDoublesOffset());
}
private GeoKeyEntry addGeoAsciiParamsRef(int keyID, int length) {
return addGeoKeyEntry(keyID, getGeoAsciiParamsTag().getNumber(), length,getCurrentGeoAsciisOffset());
}
private GeoKeyEntry updateGeoAsciiParamsRef(int keyID, int length,int offset) {
return updateGeoKeyEntry(keyID, getGeoAsciiParamsTag().getNumber(), length, offset);
}
private int getCurrentGeoDoublesOffset() {
return numGeoTiffDoubleParams;
}
private int getCurrentGeoAsciisOffset() {
return geoTiffAsciiParams.length();
}
private Element createIFD() {
Element ifd = new Element(GeoTiffConstants.GEOTIFF_IFD_TAG);
ifd.setAttribute(GeoTiffConstants.GEOTIFF_TAGSETS_ATT_NAME,BaselineTIFFTagSet.class.getName() + ","+ GeoTIFFTagSet.class.getName());
if (modelPixelScale.isSet()) {
ifd.addContent(createModelPixelScaleElement());
}
if (isModelTiePointsSet()) {
ifd.addContent(createModelTiePointsElement());
} else if (isModelTransformationSet()) {
ifd.addContent(createModelTransformationElement());
}
if (getNumGeoKeyEntries() > 1) {
ifd.addContent(createGeoKeyDirectoryElement());
}
if (numGeoTiffDoubleParams > 0) {
ifd.addContent(createGeoDoubleParamsElement());
}
if (numGeoTiffAsciiParams > 0) {
ifd.addContent(createGeoAsciiParamsElement());
}
if (isNodataSet) {
ifd.addContent(createNoDataElement());
}
if (isMetadataSet){
createMetadataElement(ifd);
}
return ifd;
}
private boolean isModelTiePointsSet() {
return numModelTiePoints > 0;
}
private boolean isModelTransformationSet() {
for (int i = 0; i < modelTransformation.length; i++) {
if (modelTransformation[i] != 0.0) {
return true;
}
}
return false;
}
public void setModelTransformation(final AffineTransform rasterToModel) {
if (modelPixelScale!=null&&modelPixelScale.isSet())
throw new IllegalStateException("ModelPixelScaleTag already set. It is not possible to set the ModelTransformation.");
if (isModelTiePointsSet())
throw new IllegalStateException("ModelTiePointsTag already set. It is not possible to set the ModelTransformation.");
// //
//
// See pag 28 of the spec for an explanation
//
// //
// a
modelTransformation[0] = rasterToModel.getScaleX();
// b
modelTransformation[1] = rasterToModel.getShearX();
// c
modelTransformation[2] = 0;
// d
modelTransformation[3] = rasterToModel.getTranslateX();
// e
modelTransformation[4] = rasterToModel.getShearY();
// f
modelTransformation[5] = rasterToModel.getScaleY();
// g
modelTransformation[6] = 0;
// h
modelTransformation[7] = rasterToModel.getTranslateY();
// i
modelTransformation[8] = 0;
// j
modelTransformation[9] = 0;
// k
modelTransformation[10] = 0;
// l
modelTransformation[11] = 0;
// m
modelTransformation[12] = 0;
// n
modelTransformation[13] = 0;
// o
modelTransformation[14] = 0;
// p
modelTransformation[15] = 1;
}
private Element createGeoKeyDirectoryElement() {
Element field = createFieldElement(getGeoKeyDirectoryTag());
Element data = new Element(GeoTiffConstants.GEOTIFF_SHORTS_TAG);
field.addContent(data);
int[] values;
//GeoKey directory root tag
values = getGeoKeyEntryAt(0).getValues();
data.addContent(createShortElement(values[0]));
data.addContent(createShortElement(values[1]));
data.addContent(createShortElement(values[3]));
data.addContent(createShortElement(values[2]));
//GeoKeys
for (int i = 1; i < numGeoTiffEntries; i++) {
values = getGeoKeyEntryAt(i).getValues();
int lenght = values.length;
for (int j = 0; j < lenght; j++) {
Element GeoKeyRecord = createShortElement(values[j]);
data.addContent(GeoKeyRecord);
}
}
return field;
}
private Element createGeoDoubleParamsElement() {
Element field = createFieldElement(getGeoDoubleParamsTag());
Element data = new Element(GeoTiffConstants.GEOTIFF_DOUBLES_TAG);
field.addContent(data);
for (int i = 0; i < numGeoTiffDoubleParams; i++) {
Element param = createDoubleElement(geoTiffDoubleParams[i]);
data.addContent(param);
}
return field;
}
private Element createGeoAsciiParamsElement() {
Element field = createFieldElement(getGeoAsciiParamsTag());
Element data = new Element(GeoTiffConstants.GEOTIFF_ASCIIS_TAG);
field.addContent(data);
data.addContent(createAsciiElement(geoTiffAsciiParams.toString()));
return field;
}
private Element createModelPixelScaleElement() {
Element field = createFieldElement(getModelPixelScaleTag());
Element data = new Element(GeoTiffConstants.GEOTIFF_DOUBLES_TAG);
field.addContent(data);
addDoubleElements(data, modelPixelScale.getValues());
return field;
}
private Element createModelTransformationElement() {
Element field = createFieldElement(getModelTransformationTag());
Element data = new Element(GeoTiffConstants.GEOTIFF_DOUBLES_TAG);
field.addContent(data);
addDoubleElements(data, modelTransformation);
return field;
}
private Element createModelTiePointsElement() {
Element field = createFieldElement(getModelTiePointTag());
Element data = new Element(GeoTiffConstants.GEOTIFF_DOUBLES_TAG);
field.addContent(data);
for (int i = 0; i < numModelTiePoints; i++) {
addDoubleElements(data, modelTiePoints[i].getData());
}
return field;
}
private Element createNoDataElement() {
Element field = createFieldElement(getNoDataTag());
Element data = new Element(GeoTiffConstants.GEOTIFF_ASCIIS_TAG);
field.addContent(data);
data.addContent(createAsciiElement(Double.toString(noData)));
return field;
}
private void createMetadataElement(Element ifd) {
if (ifd != null && tiffTagsMetadata != null && !tiffTagsMetadata.isEmpty()){
Iterator<String> keys = tiffTagsMetadata.keySet().iterator();
while (keys.hasNext()){
String key = keys.next();
String[] setIdPair = key.split(":");
String set = TagSet.BASELINE.toString();
if (setIdPair.length > 1){
set = setIdPair[0].toUpperCase();
}
String keyName = setIdPair[setIdPair.length - 1];
if (GeoTiffConstants.isNumeric(keyName)){
final String value = tiffTagsMetadata.get(key);
final TIFFTag tag = getAsciiTag(set, Integer.valueOf(keyName));
if (tag != null){
Element field = createFieldElement(tag);
Element data = new Element(GeoTiffConstants.GEOTIFF_ASCIIS_TAG);
field.addContent(data);
data.addContent(createAsciiElement(value));
ifd.addContent(field);
}
}
}
}
}
private Element createFieldElement(final TIFFTag tag) {
Element field = new Element(GeoTiffConstants.GEOTIFF_FIELD_TAG);
field.setAttribute(GeoTiffConstants.NUMBER_ATTRIBUTE, String.valueOf(tag.getNumber()));
field.setAttribute(GeoTiffConstants.NAME_ATTRIBUTE, tag.getName());
return field;
}
private Element createShortElement(final int value) {
Element GeoKeyRecord = new Element(GeoTiffConstants.GEOTIFF_SHORT_TAG);
GeoKeyRecord.setAttribute(GeoTiffConstants.VALUE_ATTRIBUTE, String.valueOf(value));
return GeoKeyRecord;
}
private Element createDoubleElement(final double value) {
Element param = new Element(GeoTiffConstants.GEOTIFF_DOUBLE_TAG);
param.setAttribute(GeoTiffConstants.VALUE_ATTRIBUTE, String.valueOf(value));
return param;
}
private Element createAsciiElement(final String value) {
Element param = new Element(GeoTiffConstants.GEOTIFF_ASCII_TAG);
param.setAttribute(GeoTiffConstants.VALUE_ATTRIBUTE, String.valueOf(value));
return param;
}
private void addDoubleElements(Element data, final double[] values) {
final int length = values.length;
for (int j = 0; j < length; j++) {
Element GeoKeyRecord = createDoubleElement(values[j]);
data.addContent(GeoKeyRecord);
}
}
public double getNoData() {
return noData;
}
public void setNoData(double noData) {
this.noData = noData;
isNodataSet = true;
}
/**
* Allows to setup metadata by leveraging on Ascii TIFF Tags.
*
* @param name
* is the Ascii TIFF Tag identifier. It can be a String representing:
* 1) a simple Integer (referring to a tag ID) (in that case it will refer to the BaselineTIFFTagSet
* 2) OR an identifier in the form: TIFFTagSet:TIFFTagID. As an
* instance: "BaselineTIFFTagSet:305" in order to add the Copyright info.
* @param value
* is the value to be assigned to that metadata.
* @see GeoTiffIIOMetadataEncoder.TagSet
*/
public void setTiffTagsMetadata(Map<String, String> metadata) {
this.tiffTagsMetadata = metadata;
isMetadataSet = true;
}
}