1 |
mojays |
2 |
package appl.util; |
2 |
|
|
|
3 |
|
|
import java.awt.image.DataBuffer; |
4 |
|
|
import java.io.File; |
5 |
|
|
import java.io.FileInputStream; |
6 |
|
|
import java.io.FileNotFoundException; |
7 |
|
|
import java.io.IOException; |
8 |
|
|
import java.io.InputStreamReader; |
9 |
|
|
|
10 |
|
|
import schmitzm.data.WritableGrid; |
11 |
|
|
import schmitzm.geotools.io.GeoImportUtil; |
12 |
|
|
|
13 |
mojays |
52 |
import gtmig.org.geotools.gce.arcgrid.ArcGridRaster; |
14 |
mojays |
2 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
15 |
|
|
|
16 |
|
|
/** |
17 |
|
|
* This Class provides some simple mostly independent functions |
18 |
|
|
* related to Geo-Rasters and {@link WritableGrid WritableGrids}. |
19 |
|
|
* |
20 |
|
|
* @author Dominik Appl |
21 |
|
|
* |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
public class RasterUtil { |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* Extracts metadata out of a ArcGridASCII File without reading the |
28 |
|
|
* whole Grid (only the Header is parsed). |
29 |
|
|
* |
30 |
|
|
* @param src the ArcInfoASCII file |
31 |
|
|
* @return the meta data |
32 |
|
|
*/ |
33 |
|
|
public static RasterMetaData getRasterMetaData_from_ArcGridASCII_File( |
34 |
|
|
File src) { |
35 |
|
|
|
36 |
|
|
try { |
37 |
|
|
ArcGridRaster raster = new ArcGridRaster(new InputStreamReader( |
38 |
|
|
new FileInputStream(src)), false); |
39 |
|
|
raster.parseHeader(); |
40 |
|
|
CoordinateReferenceSystem crs = GeoImportUtil.determineProjection(src); |
41 |
|
|
return new RasterMetaData(DataBuffer.TYPE_FLOAT, raster.getNCols(), |
42 |
|
|
raster.getNRows(), 0, 0, raster.getXlCorner(), raster |
43 |
|
|
.getYlCorner(), raster.getCellSize(),crs); |
44 |
|
|
|
45 |
|
|
} catch (FileNotFoundException e) { |
46 |
|
|
// TODO Auto-generated catch block |
47 |
|
|
e.printStackTrace(); |
48 |
|
|
} catch (IOException e) { |
49 |
|
|
// TODO Auto-generated catch block |
50 |
|
|
e.printStackTrace(); |
51 |
|
|
} |
52 |
|
|
return new RasterMetaData(DataBuffer.TYPE_UNDEFINED, 0, 0, 0, 0, 0, 0, |
53 |
|
|
0,null); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
/** |
57 |
|
|
* Runs through every grid cell and checks if the two Grids are equal (based |
58 |
|
|
* on the {@link RasterMetaData} of the first Grid. If the difference is |
59 |
|
|
* smaller than 0.0001 only conversion Warning is given. Output to console |
60 |
|
|
* may be enabled. Writes out warning to sysout, if metaData is not equal, |
61 |
|
|
* but continues checking |
62 |
|
|
* |
63 |
|
|
* @param a |
64 |
|
|
* 1st grid |
65 |
|
|
* @param b |
66 |
|
|
* 2nd grid |
67 |
|
|
* @param outputToConsole |
68 |
|
|
* if true the results and some infos are written on System.out |
69 |
|
|
* @return true, if the grids match |
70 |
|
|
*/ |
71 |
|
|
public static boolean checkEqual(WritableGrid a, WritableGrid b, |
72 |
|
|
boolean outputToConsole) { |
73 |
|
|
|
74 |
|
|
if (a == null && b == null) |
75 |
|
|
return true; |
76 |
|
|
if (a == null || b == null) |
77 |
|
|
return false; |
78 |
|
|
|
79 |
|
|
//for errors which are probably caused by rounding |
80 |
|
|
float diffTolerance = 1.0e-5f; |
81 |
|
|
float maxdiff = 0; // maximum difference found |
82 |
|
|
int maxdiffX = 0, maxdiffY = 0; // coordinates of maxdiff |
83 |
|
|
|
84 |
|
|
RasterMetaData metaData = new RasterMetaData(a); |
85 |
|
|
if (!metaData.equals(new RasterMetaData(b))) { |
86 |
|
|
if (outputToConsole) |
87 |
|
|
System.out |
88 |
|
|
.println("Warning! Metadata of the two Grids is not equal!"); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
// run through the whole grid |
92 |
|
|
float aValue, bValue; |
93 |
|
|
float currentdiff; |
94 |
|
|
int counter = 0; |
95 |
|
|
for (int y = metaData.getMinY(); y < metaData.getHeight() |
96 |
|
|
- metaData.getMinY(); y++) |
97 |
|
|
for (int x = metaData.getMinX(); x < metaData.getWidth() |
98 |
|
|
- metaData.getMinY(); x++) { |
99 |
|
|
counter++; |
100 |
|
|
aValue = a.getRasterSampleAsFloat(x, y); |
101 |
|
|
bValue = b.getRasterSampleAsFloat(x, y); |
102 |
|
|
//check equal |
103 |
|
|
if (aValue == bValue) |
104 |
|
|
continue; |
105 |
|
|
if (Float.isNaN(aValue) && Float.isNaN(bValue)) |
106 |
|
|
continue; |
107 |
|
|
|
108 |
|
|
//check for light differences which might indicate conversionErrors |
109 |
|
|
currentdiff = Math.abs(aValue - bValue); |
110 |
|
|
if (currentdiff < diffTolerance) { |
111 |
|
|
if (currentdiff > maxdiff) { |
112 |
|
|
maxdiff = currentdiff; |
113 |
|
|
maxdiffX = x; |
114 |
|
|
maxdiffY = y; |
115 |
|
|
} |
116 |
|
|
continue; |
117 |
|
|
} |
118 |
|
|
// else not equal: |
119 |
|
|
if (outputToConsole) |
120 |
|
|
System.out |
121 |
|
|
.println("Warning: Two Grids were not equal at pos (" |
122 |
|
|
+ x |
123 |
|
|
+ "," |
124 |
|
|
+ y |
125 |
|
|
+ ") --- first value " |
126 |
|
|
+ a.getRasterSampleAsFloat(x, y) |
127 |
|
|
+ " and second value " |
128 |
|
|
+ b.getRasterSampleAsFloat(x, y)); |
129 |
|
|
return false; |
130 |
|
|
} |
131 |
|
|
if (outputToConsole && !(maxdiff == 0)) |
132 |
|
|
System.out |
133 |
|
|
.println("The Gridvalues differ slightly. The maximum Difference is: " |
134 |
|
|
+ maxdiff |
135 |
|
|
+ " at (" |
136 |
|
|
+ maxdiffX |
137 |
|
|
+ "," |
138 |
|
|
+ maxdiffY |
139 |
|
|
+ "). Finished grid compare after " |
140 |
|
|
+ counter |
141 |
|
|
+ " checks"); |
142 |
|
|
else if (outputToConsole) |
143 |
|
|
System.out |
144 |
|
|
.println("Grids found equal after " + counter + " checks"); |
145 |
|
|
return true; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
/** |
150 |
|
|
* Copies all values from a {@link WritableGrid} into another |
151 |
|
|
* {@link WritableGrid}. Of course the grids must have the same dimensions. |
152 |
|
|
* |
153 |
|
|
* @param src |
154 |
|
|
* @param target |
155 |
|
|
* @throws UnsupportedOperationException |
156 |
|
|
* if the dimensions of the grids does not match or one of the |
157 |
|
|
* grids is null |
158 |
|
|
*/ |
159 |
|
|
public static void copyInto(WritableGrid src, WritableGrid target){ |
160 |
|
|
if(src==null || target==null) |
161 |
|
|
throw new UnsupportedOperationException("Error: One of the grids was null"); |
162 |
|
|
//check if width and height are matching: |
163 |
|
|
if((src.getHeight()!=target.getHeight()) || (src.getWidth()!=target.getWidth())) |
164 |
|
|
throw new UnsupportedOperationException("The bounds of the two rasters don't not match! Copy failed!"); |
165 |
|
|
for(int y=0; y < src.getHeight();y++) |
166 |
|
|
for(int x=0; x < src.getWidth(); x++) |
167 |
|
|
target.setRasterSample(src.getRasterSample(x,y), x,y); |
168 |
|
|
|
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
/** |
172 |
|
|
* Prints the grid to the console (useful for testing). |
173 |
|
|
* |
174 |
|
|
* @param grid the grid to be printed |
175 |
|
|
* @param spacing the space to be reserved for one cell-value |
176 |
|
|
* @param precision the decimal places to be printed out |
177 |
|
|
* @param name the name will be shown in the heading of the output |
178 |
|
|
*/ |
179 |
|
|
public static void printGrid(WritableGrid grid, int spacing, int precision, String name){ |
180 |
|
|
//generate format String |
181 |
|
|
System.out.println("****************************** " + name + " ******************************"); |
182 |
|
|
int width =grid.getWidth(); |
183 |
|
|
int noOfCells = grid.getWidth() * grid.getHeight(); |
184 |
|
|
Object[] cellarray = new Object[noOfCells]; |
185 |
|
|
StringBuffer buffer = new StringBuffer(noOfCells * 9); |
186 |
|
|
String singleCellFormatString = "%" + spacing + "." + precision + "f "; |
187 |
|
|
buffer.append("%n "); |
188 |
|
|
for(int y=0; y < grid.getHeight();y++){ |
189 |
|
|
for(int x=0; x < grid.getWidth(); x++){ |
190 |
|
|
buffer.append(singleCellFormatString); |
191 |
|
|
cellarray[y*width+x]=grid.getRasterSampleAsFloat(x,y); |
192 |
|
|
} |
193 |
|
|
buffer.append("%n "); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
System.out.printf(buffer.toString(), cellarray); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* Prints the grid to the console (useful for testing). |
201 |
|
|
* Spacing will be 5 and precision will be 2. |
202 |
|
|
* |
203 |
|
|
* @param grid the grid to be printed |
204 |
|
|
* @param name the name will be shown in the heading of the output |
205 |
|
|
*/ |
206 |
|
|
public static void printGrid(WritableGrid grid, String name){ |
207 |
|
|
printGrid(grid,5,2,name); |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
/** |
211 |
|
|
* Prints the grid to the console (useful for testing). |
212 |
|
|
* Spacing will be 5 and precision will be 2. |
213 |
|
|
* |
214 |
|
|
* @param grid the grid to be printed |
215 |
|
|
*/ |
216 |
|
|
public static void printGrid(WritableGrid grid){ |
217 |
|
|
printGrid(grid," "); |
218 |
|
|
} |
219 |
|
|
} |