-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDEM.java
1019 lines (954 loc) · 28 KB
/
DEM.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
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
DEM -- A Geographic Information System for Line-Of-Sight Radio Communications.
Copyright (C) 1998, 1999 Jeffrey B. Otterson
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.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
For more information, to submit bugs, software changes, etc., please contact
Jeff Otterson / N1KDO
3543 Tritt Springs Way
Marietta, GA 30062
otterson@mindspring.com
*/
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
/**
* class to represent a logical Digital Elevation Model.
*/
public class DEM
{
private static final int DEM_BUFFER_SIZE = 1024;
/**
* constant to select the South West corner of the DEM.
*/
public static final int SW_CORNER = 0;
/**
* constant to select the North West corner of the DEM.
*/
public static final int NW_CORNER = 1;
/**
* constant to select the North East corner of the DEM.
*/
public static final int NE_CORNER = 2;
/**
* constant to select the South East corner of the DEM.
*/
public static final int SE_CORNER = 3;
String fileName;
String name;
byte units;
GeoCoordinate corners[];
short minimumElevation;
short maximumElevation;
byte xResolution;
byte yResolution;
byte zResolution;
short rows;
short columns;
short elevations[][];
DEMmain demMain;
StatusBar statusBar;
/**
* Construct a new, empty DEM object.
*/
public DEM()
{
this("", null);
} /* DEM constructor */
/**
* Construct a new, empty DEM object, referencing the specified parent.
* @param fileName the DEM's fileName.
* @param demMain the DEMmain object that owns this DEM.
*/
public DEM(String fileName, DEMmain demMain)
{
this.fileName = fileName;
this.demMain = demMain;
statusBar = demMain.statusBar;
} /* DEM constructor */
/**
* get the DEM's file name.
* @return a string with the DEM's file name.
*/
public String getFileName()
{
return fileName;
} /* getFileName() */
/**
* get the units of this DEM.
* @return the units code.
*/
public byte getUnits()
{
return units;
} /* getRows() */
/**
* get the number of rows in this DEM.
* @return the number of rows.
*/
public short getRows()
{
return rows;
} /* getRows() */
/**
* get the number of columns in this DEM.
* @return the number of columns.
*/
public short getColumns()
{
return columns;
} /* getColumns() */
/**
* get the minimum elevation of this DEM.
* @return the minimum elevation.
*/
public short getMinimumElevation()
{
return minimumElevation;
} /* getMinimumElevation */
/**
* get the maximum elevation of this DEM.
* @return the minimum elevation.
*/
public short getMaximumElevation()
{
return maximumElevation;
} /* getMaximumElevation */
/**
* return the X resolution of the DEM in arc-seconds.
* @return the x-resolution of the DEM in arc-seconds.
*/
public byte getXResolution()
{
return xResolution;
} /* getXResolution() */
/**
* return the Y resolution of the DEM in arc-seconds.
* @return the y-resolution of the DEM in arc-seconds.
*/
public byte getYResolution()
{
return yResolution;
} /* getYResolution() */
/**
* return the GeoCoordinate for the specified corner.
* @param cornerNum an integer representing the corner: SW_CORNER,
* NW_CORNER, NE_CORNER, SE_CORNER.
* @return a GeoCoordinate for the specified corner, or null.
*/
public GeoCoordinate getCorner(int cornerNum)
{
if ((cornerNum < SW_CORNER) || (cornerNum > SE_CORNER))
return null;
else
return corners[cornerNum];
} /* getCorner() */
/**
* return the GeoCoordinate for the center of the DEM.
* WARNING: doesn't deal with DEMs that cross either 0-degree line.
* @return a GeoCoordinate for the center of the DEM.
*/
public GeoCoordinate getCenter()
{
GeoCoordinate nwCorner = getCorner(NW_CORNER);
GeoCoordinate seCorner = getCorner(SE_CORNER);
int lat = (nwCorner.getLatitude() + seCorner.getLatitude()) / 2;
int lon = (nwCorner.getLongitude() + seCorner.getLongitude()) / 2;
return new GeoCoordinate(lat, lon);
} /* getCenter() */
/**
* determine if the x,y location is valid on the DEM.
* @param x the longitudinal offset.
* @param y the latitudinal offset.
* @return true if the location is on the DEM.
*/
public boolean isValidLocation(int column, int row)
{
return ((column >= 0) && (column < columns) && (row >= 0) && (row < rows));
} /* validPoint */
/**
* get the elevation of a particular point in this DEM.
* @param column the column number of the elevation.
* @param row the row number of the elevation.
* @return the elevation at that point or 0.
*/
public short getElevation(int column, int row)
{
if ((row < 0) || (row >= rows) || (column < 0) || (column >= columns))
{
new ErrorBox(demMain,
"Error!",
"DEM.getElevation: column="+column+" row="+row+" columns="+columns+" rows="+rows);
return 0;
}
else
{
return elevations[column][row];
}
} /* getElevation() */
/**
* get the elevation of a particular point in this DEM.
* @param gc the location to get the elevation for.
* @return the elevation at that point or 0.
*/
public short getElevation(GeoCoordinate gc)
{
int row = (gc.getLatitude() - corners[SW_CORNER].getLatitude()) / yResolution;
int column = (gc.getLongitude() - corners[SW_CORNER].getLongitude()) / xResolution ;
if ((row < 0) || (row >= rows) || (column < 0) || (column >= columns))
{
new ErrorBox(demMain,
"Error!",
"DEM.getElevation: column="+column+" row="+row+" columns="+columns+" rows="+rows);
return 0;
}
else
{
return elevations[column][row];
}
} /* getElevation() */
/**
* write the DEM data to a file in a binary compressed format.
* @return true for successful save.
*/
public boolean write()
{
return write(fileName);
} /* write() */
/**
* write the DEM data to a specified file in a binary compressed format.
* @param demFileName the name of the file to write.
* @return true for successful save.
*/
public boolean write(String demFileName)
{
if (statusBar != null)
{
statusBar.setMessage("Saving "+demFileName);
} /* if statusBar != null */
try
{
FileOutputStream fout = new FileOutputStream(demFileName);
DeflaterOutputStream dout = new DeflaterOutputStream(fout);
DataOutputStream out = new DataOutputStream(dout);
int i, j;
out.writeUTF(name);
out.writeByte(units);
for(i = 0; i < 4; i++)
{
out.writeInt(corners[i].getLatitude());
out.writeInt(corners[i].getLongitude());
} /* for i */
out.writeShort(minimumElevation);
out.writeShort(maximumElevation);
out.writeByte(xResolution);
out.writeByte(yResolution);
out.writeByte(zResolution);
out.writeShort(rows);
out.writeShort(columns);
int pctDone;
for (i=0; i < columns; i++)
{
if (statusBar != null)
{
pctDone = i * 100 / columns;
statusBar.updateProgress(pctDone);
} /* if statusBar != null */
for (j = 0; j < rows; j++)
{
out.writeShort(elevations[i][j]);
} /* for j */
} /* for i */
out.close();
dout.close();
fout.close();
} /* try */
catch (IOException e)
{
new ErrorBox(demMain,
"Error saving " + demFileName,
e.toString());
return false;
}
catch (SecurityException e)
{
new ErrorBox(demMain,
"Error saving " + demFileName,
e.toString());
return false;
}
if (statusBar != null)
{
statusBar.setMessage("Saved "+demFileName);
statusBar.updateProgress(0);
} /* if statusBar != null */
return true;
} /* write() */
/**
* read the DEM data from a file.
* @param demFileName the name of the file to read.
* @return true for successful read.
*/
public boolean read(String demFileName)
{
fileName = demFileName;
return read();
} /* read() */
/**
* read the DEM data from a disk file.
* @return true for successful load.
*/
public boolean read()
{
if (statusBar != null)
{
statusBar.setMessage("Loading "+fileName);
} /* if statusBar != null */
try
{
FileInputStream fin = new FileInputStream(fileName);
InflaterInputStream iin = new InflaterInputStream(fin);
DataInputStream in = new DataInputStream(iin);
int i, j, lat, lon;
name = in.readUTF();
units = in.readByte();
corners = new GeoCoordinate[8];
for(i = 0; i < 4; i++)
{
lat = in.readInt();
lon = in.readInt();
corners[i] = new GeoCoordinate(lat, lon);
} /* for i */
minimumElevation = in.readShort();
maximumElevation = in.readShort();
xResolution = in.readByte();
yResolution = in.readByte();
zResolution = in.readByte();
rows = in.readShort();
columns = in.readShort();
elevations = new short[columns][rows];
int pctDone;
short elevation;
for (i = 0; i < columns; i++)
{
if (statusBar != null)
{
pctDone = i * 100 / columns;
statusBar.updateProgress(pctDone);
} /* if statusBar != null */
for (j = 0; j < rows; j++)
{
elevation = in.readShort();
elevations[i][j] = elevation;
} /* for j */
} /* for i */
in.close();
iin.close();
fin.close();
} /* try */
catch (FileNotFoundException e)
{
new ErrorBox(demMain,
"Error reading " + fileName,
e.toString());
return false;
}
catch (IOException e)
{
new ErrorBox(demMain,
"Error reading " + fileName,
e.toString());
return false;
}
catch (SecurityException e)
{
new ErrorBox(demMain,
"Error reading " + fileName,
e.toString());
return false;
}
if (statusBar != null)
{
statusBar.setMessage("Loaded " + fileName);
statusBar.updateProgress(0);
} /* if statusBar != null */
return true;
} /* read() */
/**
* extract DEM data for a given region from a file.
* @param swCoordinate the coordinates fo the SW corner of the new DEM.
* @param neCoordinate the coordinates fo the NE corner of the new DEM.
* @return true for successful load.
*/
public boolean read(GeoCoordinate swCoordinate,
GeoCoordinate neCoordinate)
{
if (statusBar != null)
{
statusBar.setMessage("Loading "+fileName);
} /* if statusBar != null */
try
{
FileInputStream fin = new FileInputStream(fileName);
InflaterInputStream iin = new InflaterInputStream(fin);
DataInputStream in = new DataInputStream(iin);
int i, j, lat, lon;
name = in.readUTF();
units = in.readByte();
corners = new GeoCoordinate[8];
for(i = 0; i < 4; i++)
{
lat = in.readInt();
lon = in.readInt();
corners[i] = new GeoCoordinate(lat, lon);
} /* for i */
minimumElevation = in.readShort();
maximumElevation = in.readShort();
xResolution = in.readByte();
yResolution = in.readByte();
zResolution = in.readByte();
rows = in.readShort();
columns = in.readShort();
int x1, x2, y1, y2;
int swLat, swLon, neLat, neLon;
lat = corners[SW_CORNER].getLatitude();
lon = corners[SW_CORNER].getLongitude();
swLat = swCoordinate.getLatitude();
swLon = swCoordinate.getLongitude();
neLat = neCoordinate.getLatitude();
neLon = neCoordinate.getLongitude();
x1 = (swLon - lon) / xResolution;
x2 = (neLon - lon) / xResolution;
y1 = (swLat - lat) / yResolution;
y2 = (neLat - lat) / yResolution;
if ((x1 < 0) ||
(x1 > columns) ||
(x2 < 0) ||
(x2 > columns) ||
(y1 < 0) ||
(y1 > rows) ||
(y2 < 0) ||
(y2 > rows))
{
new ErrorBox(demMain,
"Error extracting from " + fileName,
"Coordinates are not on the DEM:\n"+
"SW: " + corners[SW_CORNER].toString() + "\n" +
"NE: " + corners[NE_CORNER].toString());
return false;
} /* if x1... */
int x, y;
short newColumns = (short) (x2 - x1 + 1);
short newRows = (short) (y2 - y1 + 1);
elevations = new short[newColumns][newRows];
int pctDone;
int minElev = 0;
int maxElev = 0;
short elevation;
x = 0;
for (i = 0; i < columns; i++)
{
y = 0;
if (statusBar != null)
{
pctDone = i * 100 / columns;
statusBar.updateProgress(pctDone);
} /* if statusBar != null */
for (j = 0; j < rows; j++)
{
elevation = in.readShort();
if ((i >= x1) &&
(i <= x2) &&
(j >= y1) &&
(j <= y2))
{
if ((x == 0) &&
(y == 0))
{ /* first point */
minElev = elevation;
maxElev = elevation;
} /* if x == 0 && y == 0 */
else
{ /* not first point */
minElev = Math.min(elevation, minElev);
maxElev = Math.max(elevation, maxElev);
} /* if x == 0 && y == 0 */
elevations[x][y++] = elevation;
} /* if i >= x1 */
} /* for j */
if ((i >= x1) &&
(i <= x2))
x++;
} /* for i */
columns = newColumns;
rows = newRows;
minimumElevation = (short) minElev;
maximumElevation = (short) maxElev;
corners[SW_CORNER] = new GeoCoordinate(swLat, swLon);
corners[NW_CORNER] = new GeoCoordinate(neLat, swLon);
corners[NE_CORNER] = new GeoCoordinate(neLat, neLon);
corners[SE_CORNER] = new GeoCoordinate(swLat, neLon);
in.close();
iin.close();
fin.close();
} /* try */
catch (FileNotFoundException e)
{
new ErrorBox(demMain,
"Error extracting from " + fileName,
e.toString());
return false;
}
catch (IOException e)
{
new ErrorBox(demMain,
"Error extracting from " + fileName,
e.toString());
return false;
}
catch (SecurityException e)
{
new ErrorBox(demMain,
"Error extracting from " + fileName,
e.toString());
return false;
}
if (statusBar != null)
{
statusBar.setMessage("Loaded " + fileName);
statusBar.updateProgress(0);
} /* if statusBar != null */
return true;
} /* read() */
/**
* read DEM data from a USGS format DEM 1 degree (1:250000) file.
* @param demFileName the name of the file to read.
* @return true for successful read.
*/
public boolean readUSGS(String demFileName)
{
fileName = demFileName;
return read();
} /* readUSGS() */
/**
* read DEM data from a USGS format DEM 1 degree (1:250000) file.
* @return true for successful read.
*/
public boolean readUSGS()
{
if (statusBar != null)
statusBar.setMessage("Loading (USGS) "+fileName);
try
{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName),2048);
byte buffer[] = new byte[1024];
int offset;
int index;
int increment;
int lat,lon;
/* read record type A (header) */
in.read(buffer, 0, 1024);
name = new String (buffer, 0, 40);
units = (byte) Util.safeStringToInt(new String(buffer,534,6));
offset = 546;
increment = 24;
index = 0;
corners = new GeoCoordinate[8];
for (index = 0; index < 4; index++)
{
lon = convertScientificToInt(new String(buffer, offset, increment));
offset += increment;
lat = convertScientificToInt(new String(buffer, offset, increment));
offset += increment;
corners[index] = new GeoCoordinate(lat, lon);
} /* for index */
minimumElevation = (short) convertScientificToInt(new String(buffer, 738, 24));
maximumElevation = (short) convertScientificToInt(new String(buffer, 762, 24));
xResolution = (byte) convertScientificToInt(new String(buffer, 816, 12));
yResolution = (byte) convertScientificToInt(new String(buffer, 828, 12));
zResolution = (byte) convertScientificToInt(new String(buffer, 840, 12));
int testRows = Util.safeStringToInt(new String(buffer, 852, 6));
if (testRows != 1)
{
new ErrorBox(demMain,
"Error reading (USGS) " + fileName,
"Unsupported DEM file feature!");
return false;
} /* if rows != 1 */
columns = (short) Util.safeStringToInt(new String(buffer, 858, 6));
rows = columns; // particularly rude hack for 1 degreee DEMs only.
/* read record type B (data "profiles") */
elevations = new short[columns][rows];
int i, j;
int rowNum;
boolean firstBlock = true;
int bytesRead;
int rowsThisBlock;
int pctDone;
/* read the profiles */
for (i=0; i < columns; i++)
{
if (statusBar != null)
{
pctDone = i * 100 / columns;
statusBar.updateProgress(pctDone);
} /* if statusBar != null */
rowNum = 0;
while (rowNum < rows)
{
bytesRead = in.read(buffer, 0, 1024);
if (rowNum == 0)
{ /* first block */
offset = 144;
rowsThisBlock = 146;
} /* if rowNum == 0 */
else
{ /* subsequent block */
offset = 0;
rowsThisBlock = 170;
} /* if rowNum == 0 */
for (j=0; j < rowsThisBlock && rowNum < rows; j++)
{
elevations[i][rowNum++] = (short) Util.safeStringToInt(new String(buffer, offset, 6));
offset += 6;
} /* for j */
} /* while */
} /* for i */
in.close();
} /* try */
catch (FileNotFoundException e)
{
new ErrorBox(demMain,
"Error reading (USGS) from " + fileName,
e.toString());
return false;
}
catch (IOException e)
{
new ErrorBox(demMain,
"Error reading (USGS) from " + fileName,
e.toString());
return false;
}
catch (SecurityException e)
{
new ErrorBox(demMain,
"Error reading (USGS) from " + fileName,
e.toString());
return false;
}
if (statusBar != null)
{
statusBar.setMessage("Loaded (USGS) "+fileName);
statusBar.updateProgress(0);
} /* if statusBar != null */
return true;
} /* readUSGS() */
/**
* get the integer value of a number in scientific notation.
* this version does this the hard way, with many floating point
* math operations, and a fair amount of casting and promotion.
* @param s the string representation of the number.
* @return the number converted to an integer or 0 if not convertable.
*/
public static int floatConvertScientificToInt(String s)
{
int o = s.indexOf('D');
if (o == -1)
{
return 0;
} /* if o == -1 */
else
{
float value = Util.safeStringToFloat(s.substring(0,o));
int power = Util.safeStringToInt(s.substring(o+2));
value = (float) (value * Math.pow(10.0, power));
return (int) value;
} /* if o == -1 */
} /* floatConvertScientificToInt() */
/**
* get the integer value of a number in scientific notation.
* this version should be faster, the number is only converted once, and
* no floating point is used, which eliminates promotion, casting, etc.
* @param s the string representation of the number.
* @return the number converted to an integer or 0 if not convertable.
*/
public static int convertScientificToInt(String s)
{
int o = s.indexOf('D');
if (o == -1)
o = s.indexOf('E');
if (o == -1)
return 0;
String mant = s.substring(0,o);
int dp = mant.indexOf('.');
if (dp == -1)
return 0;
mant = mant.substring(0,dp) + mant.substring(dp+1);
int power = Util.safeStringToInt(s.substring(o+2));
mant = mant.substring(0, dp+power);
return(Util.safeStringToInt(mant));
} /* convertScientificToInt() */
/**
* merge two adjacent DEMs into a new DEM.
* @param file1 the name of the western DEM file.
* @param file2 the name of the eastern DEM file.
* @param resultFile the name of the resulting DEM file.
* @param demMain the DEMmain that owns the parent frame and status bar.
* @return true for success.
*/
public static boolean merge(String file1,
String file2,
String resultFile,
DEMmain demMain)
{
StatusBar statusBar = demMain.statusBar;
if (statusBar != null)
{
statusBar.setMessage("Merging DEMs...");
} /* if statusBar != null */
try
{
String name1, name2;
byte units1, units2;
GeoCoordinate corners1[], corners2[];
short minElev1, maxElev1, minElev2, maxElev2;
short minimumElevation, maximumElevation;
int i, j, lat, lon;
byte xRes1, yRes1, zRes1, xRes2, yRes2, zRes2;
int rows1, columns1, rows2, columns2;
int rows, columns;
boolean snFlag = false;
FileInputStream fin1 = new FileInputStream(file1);
InflaterInputStream iin1 = new InflaterInputStream(fin1);
DataInputStream in1 = new DataInputStream(iin1);
FileInputStream fin2 = new FileInputStream(file2);
InflaterInputStream iin2 = new InflaterInputStream(fin2);
DataInputStream in2 = new DataInputStream(iin2);
name1 = in1.readUTF();
units1 = in1.readByte();
corners1 = new GeoCoordinate[4];
for(i = 0; i < 4; i++)
{
lat = in1.readInt();
lon = in1.readInt();
corners1[i] = new GeoCoordinate(lat, lon);
} /* for i */
minElev1 = in1.readShort();
maxElev1 = in1.readShort();
xRes1 = in1.readByte();
yRes1 = in1.readByte();
zRes1 = in1.readByte();
rows1 = in1.readShort();
columns1 = in1.readShort();
name2 = in2.readUTF();
units2 = in2.readByte();
corners2 = new GeoCoordinate[4];
for(i = 0; i < 4; i++)
{
lat = in2.readInt();
lon = in2.readInt();
corners2[i] = new GeoCoordinate(lat, lon);
} /* for i */
minElev2 = in2.readShort();
maxElev2 = in2.readShort();
xRes2 = in2.readByte();
yRes2 = in2.readByte();
zRes2 = in2.readByte();
rows2 = in2.readShort();
columns2 = in2.readShort();
if ((xRes1 != xRes2) ||
(yRes1 != yRes2) ||
(zRes1 != zRes2) ||
(units1 != units2))
{
new ErrorBox(demMain,
"Error merging!",
"Files are not compatable (resolution or units):\n" +
file1 + "\n" +
file2);
return false;
} /* if xRes1 != xRes2... */
if (!corners1[SE_CORNER].equals(corners2[SW_CORNER]) ||
!corners1[NE_CORNER].equals(corners2[NW_CORNER]))
{ /* not west-east */
if (corners1[NW_CORNER].equals(corners2[SW_CORNER]) &&
corners1[NE_CORNER].equals(corners2[SE_CORNER]))
{ /* it's south-north */
snFlag = true;
} /* if corners1[NW_CORNER]... */
else
{ /* not south-north */
new ErrorBox(demMain,
"Error merging!",
"Files are not adjacent:\n" +
file1 + "\n" +
file2);
return false;
} /* if corners1[NW_CORNER]... */
} /* if !corners1[...].equals(corners2[...]) */
minimumElevation = (short) Math.min(minElev1, minElev2);
maximumElevation = (short) Math.max(maxElev1, maxElev2);
if (snFlag)
{
if (columns1 != columns2)
{
statusBar.setMessage("DEM geometry mismatch");
return false;
} /* if columns1 != columns2 */
rows = rows1 + rows2 - 1;
columns = columns1;
} /* if snFlag */
else
{
if (rows1 != rows2)
{
statusBar.setMessage("DEM geometry mismatch");
return false;
} /* if columns1 != columns2 */
rows = rows1;
columns = columns1 + columns2 - 1;
} /* if snFlag */
FileOutputStream fout = new FileOutputStream(resultFile);
DeflaterOutputStream dout = new DeflaterOutputStream(fout);
DataOutputStream out = new DataOutputStream(dout);
out.writeUTF("merged DEM");
out.writeByte(units1);
if (snFlag)
{ /* south-north */
out.writeInt(corners1[SW_CORNER].getLatitude());
out.writeInt(corners1[SW_CORNER].getLongitude());
out.writeInt(corners2[NW_CORNER].getLatitude());
out.writeInt(corners2[NW_CORNER].getLongitude());
out.writeInt(corners2[NE_CORNER].getLatitude());
out.writeInt(corners2[NE_CORNER].getLongitude());
out.writeInt(corners1[SE_CORNER].getLatitude());
out.writeInt(corners1[SE_CORNER].getLongitude());
} /* if snFlag */
else
{ /* west-east */
out.writeInt(corners1[SW_CORNER].getLatitude());
out.writeInt(corners1[SW_CORNER].getLongitude());
out.writeInt(corners1[NW_CORNER].getLatitude());
out.writeInt(corners1[NW_CORNER].getLongitude());
out.writeInt(corners2[NE_CORNER].getLatitude());
out.writeInt(corners2[NE_CORNER].getLongitude());
out.writeInt(corners2[SE_CORNER].getLatitude());
out.writeInt(corners2[SE_CORNER].getLongitude());
} /* if snFlag */
out.writeShort(minimumElevation);
out.writeShort(maximumElevation);
out.writeByte(xRes1);
out.writeByte(yRes1);
out.writeByte(zRes1);
out.writeShort(rows);
out.writeShort(columns);
int pctDone;
short elevation;
if (snFlag)
{ /* south-north merge */
for (i = 0; i < columns; i++)
{
if (statusBar != null)
{
pctDone = i * 100 / columns;
statusBar.updateProgress(pctDone);
} /* if statusBar != null */
for (j = 0; j < rows1; j ++)
{
elevation = in1.readShort();
out.writeShort(elevation);
} /* for j */
for (j = 0; j < rows2; j ++)
{
elevation = in2.readShort();
if (j != 0)
out.writeShort(elevation);
} /* for j */
} /* for i */
} /* if snFlag */
else
{ /* west-east merge */
int endColumn = columns1 - 1;
for (i = 0; i < endColumn ; i++)
{
if (statusBar != null)
{
pctDone = i * 100 / columns;
statusBar.updateProgress(pctDone);
} /* if statusBar != null */
for (j = 0; j < rows; j++)
{
elevation = in1.readShort();
out.writeShort(elevation);
} /* for j */
} /* for i */
endColumn = columns2;
for (i = 0; i < endColumn ; i++)
{
if (statusBar != null)
{
pctDone = (i + columns1 - 1) * 100 / columns;
statusBar.updateProgress(pctDone);
} /* if statusBar != null */
for (j = 0; j < rows; j++)
{
elevation = in2.readShort();
out.writeShort(elevation);
} /* for j */
} /* for i */
} /* if (!) snFlag */
in1.close();
iin1.close();
fin1.close();
in1.close();
iin1.close();
fin1.close();
out.close();
dout.close();
fout.close();
} /* try */
catch (FileNotFoundException e)
{
new ErrorBox(demMain,
"Error merging!",
e.toString());
return false;
}
catch (IOException e)
{
new ErrorBox(demMain,
"Error merging!",