| 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 |
import java.util.Enumeration; |
|---|
| 34 |
import java.util.Hashtable; |
|---|
| 35 |
import java.util.Vector; |
|---|
| 36 |
|
|---|
| 37 |
import org.gdal.gdal.Band; |
|---|
| 38 |
|
|---|
| 39 |
import org.gdal.gdal.Dataset; |
|---|
| 40 |
import org.gdal.gdal.Driver; |
|---|
| 41 |
import org.gdal.gdal.GCP; |
|---|
| 42 |
import org.gdal.gdal.gdal; |
|---|
| 43 |
import org.gdal.gdalconst.gdalconstConstants; |
|---|
| 44 |
import org.gdal.osr.CoordinateTransformation; |
|---|
| 45 |
import org.gdal.osr.SpatialReference; |
|---|
| 46 |
|
|---|
| 47 |
public class gdalinfo { |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
public static void Usage() |
|---|
| 54 |
|
|---|
| 55 |
{ |
|---|
| 56 |
System.out |
|---|
| 57 |
.println("Usage: gdalinfo [--help-general] [-mm] [-nogcp] [-nomd] " |
|---|
| 58 |
+ "datasetname"); |
|---|
| 59 |
System.exit(1); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
public static void main(String[] args) { |
|---|
| 67 |
{ |
|---|
| 68 |
Dataset hDataset; |
|---|
| 69 |
Band hBand; |
|---|
| 70 |
int i, iBand; |
|---|
| 71 |
double[] adfGeoTransform = new double[6]; |
|---|
| 72 |
Driver hDriver; |
|---|
| 73 |
Hashtable papszMetadata; |
|---|
| 74 |
boolean bComputeMinMax = false, bSample = false; |
|---|
| 75 |
boolean bShowGCPs = true, bShowMetadata = true; |
|---|
| 76 |
boolean bStats = false; |
|---|
| 77 |
String pszFilename = null; |
|---|
| 78 |
|
|---|
| 79 |
gdal.AllRegister(); |
|---|
| 80 |
|
|---|
| 81 |
if (args.length < 1) { |
|---|
| 82 |
Usage(); |
|---|
| 83 |
System.exit(0); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
for (i = 0; i < args.length; i++) { |
|---|
| 90 |
if (args[i].equals("-mm")) |
|---|
| 91 |
bComputeMinMax = true; |
|---|
| 92 |
else if (args[i].equals("-stats")) |
|---|
| 93 |
bStats = true; |
|---|
| 94 |
else if (args[i].equals("-sample")) |
|---|
| 95 |
bSample = true; |
|---|
| 96 |
else if (args[i].equals("-nogcp")) |
|---|
| 97 |
bShowGCPs = false; |
|---|
| 98 |
else if (args[i].equals("-nomd")) |
|---|
| 99 |
bShowMetadata = false; |
|---|
| 100 |
else if (args[i].startsWith("-")) |
|---|
| 101 |
Usage(); |
|---|
| 102 |
else if (pszFilename == null) |
|---|
| 103 |
pszFilename = args[i]; |
|---|
| 104 |
else |
|---|
| 105 |
Usage(); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
if (pszFilename == null) |
|---|
| 109 |
Usage(); |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
hDataset = gdal.Open(pszFilename, gdalconstConstants.GA_ReadOnly); |
|---|
| 115 |
|
|---|
| 116 |
if (hDataset == null) { |
|---|
| 117 |
System.err |
|---|
| 118 |
.println("GDALOpen failed - " + gdal.GetLastErrorNo()); |
|---|
| 119 |
System.err.println(gdal.GetLastErrorMsg()); |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
System.exit(1); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
hDriver = hDataset.GetDriver(); |
|---|
| 134 |
System.out.println("Driver: " + hDriver.getShortName() + "/" |
|---|
| 135 |
+ hDriver.getLongName()); |
|---|
| 136 |
|
|---|
| 137 |
System.out.println("Size is " + hDataset.getRasterXSize() + ", " |
|---|
| 138 |
+ hDataset.getRasterYSize()); |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
if (hDataset.GetProjectionRef() != null) { |
|---|
| 144 |
SpatialReference hSRS; |
|---|
| 145 |
String pszProjection; |
|---|
| 146 |
|
|---|
| 147 |
pszProjection = hDataset.GetProjectionRef(); |
|---|
| 148 |
|
|---|
| 149 |
hSRS = new SpatialReference(pszProjection); |
|---|
| 150 |
if (hSRS != null) { |
|---|
| 151 |
String[] pszPrettyWkt = new String[1]; |
|---|
| 152 |
|
|---|
| 153 |
hSRS.ExportToPrettyWkt(pszPrettyWkt, 0); |
|---|
| 154 |
System.out.println("Coordinate System is:"); |
|---|
| 155 |
System.out.println(pszPrettyWkt[0]); |
|---|
| 156 |
|
|---|
| 157 |
} else |
|---|
| 158 |
System.out.println("Coordinate System is `" |
|---|
| 159 |
+ hDataset.GetProjectionRef() + "'"); |
|---|
| 160 |
|
|---|
| 161 |
hSRS.delete(); |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
hDataset.GetGeoTransform(adfGeoTransform); |
|---|
| 168 |
{ |
|---|
| 169 |
if (adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0) { |
|---|
| 170 |
System.out.println("Origin = (" + adfGeoTransform[0] + "," |
|---|
| 171 |
+ adfGeoTransform[3] + ")"); |
|---|
| 172 |
|
|---|
| 173 |
System.out.println("Pixel Size = (" + adfGeoTransform[1] |
|---|
| 174 |
+ "," + adfGeoTransform[5] + ")"); |
|---|
| 175 |
} else |
|---|
| 176 |
System.out.println("GeoTransform ="); |
|---|
| 177 |
System.out.println(" " + adfGeoTransform[0] + ", " |
|---|
| 178 |
+ adfGeoTransform[1] + ", " + adfGeoTransform[2]); |
|---|
| 179 |
System.out.println(" " + adfGeoTransform[3] + ", " |
|---|
| 180 |
+ adfGeoTransform[4] + ", " + adfGeoTransform[5]); |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
if (bShowGCPs && hDataset.GetGCPCount() > 0) { |
|---|
| 187 |
System.out.println("GCP Projection = " |
|---|
| 188 |
+ hDataset.GetGCPProjection()); |
|---|
| 189 |
|
|---|
| 190 |
int count = 0; |
|---|
| 191 |
Vector GCPs = new Vector(); |
|---|
| 192 |
hDataset.GetGCPs(GCPs); |
|---|
| 193 |
|
|---|
| 194 |
Enumeration e = GCPs.elements(); |
|---|
| 195 |
while (e.hasMoreElements()) { |
|---|
| 196 |
GCP gcp = (GCP) e.nextElement(); |
|---|
| 197 |
System.out.println("GCP[" + (count++) + "]: Id=" |
|---|
| 198 |
+ gcp.getId() + ", Info=" + gcp.getInfo()); |
|---|
| 199 |
System.out.println(" (" + gcp.getGCPPixel() + "," |
|---|
| 200 |
+ gcp.getGCPLine() + ") (" + gcp.getGCPX() + "," |
|---|
| 201 |
+ gcp.getGCPY() + "," + gcp.getGCPZ() + ")"); |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
papszMetadata = hDataset.GetMetadata_Dict(""); |
|---|
| 210 |
if (bShowMetadata && papszMetadata.size() > 0) { |
|---|
| 211 |
Enumeration keys = papszMetadata.keys(); |
|---|
| 212 |
System.out.println("Metadata:"); |
|---|
| 213 |
while (keys.hasMoreElements()) { |
|---|
| 214 |
String key = (String) keys.nextElement(); |
|---|
| 215 |
System.out.println(" " + key + "=" |
|---|
| 216 |
+ papszMetadata.get(key)); |
|---|
| 217 |
} |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
papszMetadata = hDataset.GetMetadata_Dict("SUBDATASETS"); |
|---|
| 224 |
if (papszMetadata.size() > 0) { |
|---|
| 225 |
System.out.println("Subdatasets:"); |
|---|
| 226 |
Enumeration keys = papszMetadata.keys(); |
|---|
| 227 |
while (keys.hasMoreElements()) { |
|---|
| 228 |
String key = (String) keys.nextElement(); |
|---|
| 229 |
System.out.println(" " + key + "=" |
|---|
| 230 |
+ papszMetadata.get(key)); |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
System.out.println("Corner Coordinates:\n"); |
|---|
| 238 |
GDALInfoReportCorner(hDataset, "Upper Left", 0.0, 0.0); |
|---|
| 239 |
GDALInfoReportCorner(hDataset, "Lower Left", 0.0, hDataset |
|---|
| 240 |
.getRasterYSize()); |
|---|
| 241 |
GDALInfoReportCorner(hDataset, "Upper Right", hDataset |
|---|
| 242 |
.getRasterXSize(), 0.0); |
|---|
| 243 |
GDALInfoReportCorner(hDataset, "Lower Right", hDataset |
|---|
| 244 |
.getRasterXSize(), hDataset.getRasterYSize()); |
|---|
| 245 |
GDALInfoReportCorner(hDataset, "Center", |
|---|
| 246 |
hDataset.getRasterXSize() / 2.0, |
|---|
| 247 |
hDataset.getRasterYSize() / 2.0); |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
for (iBand = 0; iBand < hDataset.getRasterCount(); iBand++) { |
|---|
| 253 |
Double[] pass1 = new Double[1], pass2 = new Double[1]; |
|---|
| 254 |
double[] adfCMinMax = new double[2]; |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
hBand = hDataset.GetRasterBand(iBand + 1); |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
System.out.println("Band " |
|---|
| 269 |
+ (iBand+1) |
|---|
| 270 |
+ " Type=" |
|---|
| 271 |
+ gdal.GetDataTypeName(hBand.getDataType()) |
|---|
| 272 |
+ ", ColorInterp=" |
|---|
| 273 |
+ gdal.GetColorInterpretationName(hBand |
|---|
| 274 |
.GetRasterColorInterpretation())); |
|---|
| 275 |
|
|---|
| 276 |
String hBandDesc = hBand.GetDescription(); |
|---|
| 277 |
if (hBandDesc != null && hBandDesc.length() > 0) |
|---|
| 278 |
System.out.println(" Description = " + hBandDesc); |
|---|
| 279 |
|
|---|
| 280 |
hBand.GetMinimum(pass1); |
|---|
| 281 |
hBand.GetMaximum(pass2); |
|---|
| 282 |
if(pass1[0] != null || pass2[0] != null || bComputeMinMax) { |
|---|
| 283 |
System.out.println(" Min=" + pass1[0] + " Max=" |
|---|
| 284 |
+ pass2[0]); |
|---|
| 285 |
} |
|---|
| 286 |
if (bComputeMinMax) { |
|---|
| 287 |
hBand.ComputeRasterMinMax(adfCMinMax, 0); |
|---|
| 288 |
System.out.println(" Computed Min/Max=" + adfCMinMax[0] |
|---|
| 289 |
+ "," + adfCMinMax[1]); |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
hBand.GetNoDataValue(pass1); |
|---|
| 301 |
if(pass1[0] != null) |
|---|
| 302 |
{ |
|---|
| 303 |
System.out.println(" NoData Value=" + pass1[0]); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
if (hBand.GetOverviewCount() > 0) { |
|---|
| 307 |
int iOverview; |
|---|
| 308 |
|
|---|
| 309 |
System.out.println(" Overviews: "); |
|---|
| 310 |
for (iOverview = 0; iOverview < hBand.GetOverviewCount(); iOverview++) { |
|---|
| 311 |
Band hOverview; |
|---|
| 312 |
|
|---|
| 313 |
if (iOverview != 0) |
|---|
| 314 |
System.out.print(", "); |
|---|
| 315 |
|
|---|
| 316 |
hOverview = hBand.GetOverview(iOverview); |
|---|
| 317 |
System.out.print(hOverview.getXSize() + "x" |
|---|
| 318 |
+ hOverview.getYSize()); |
|---|
| 319 |
} |
|---|
| 320 |
System.out.println(""); |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
hBand.GetScale(pass1); |
|---|
| 344 |
if(pass1[0] != null) { |
|---|
| 345 |
System.out.print(" Offset: " + pass1[0]); |
|---|
| 346 |
} |
|---|
| 347 |
hBand.GetOffset(pass1); |
|---|
| 348 |
if(pass1[0] != null) { |
|---|
| 349 |
System.out.println(", Scale:" + pass1[0]); |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
papszMetadata = hBand.GetMetadata_Dict(""); |
|---|
| 353 |
if( bShowMetadata && papszMetadata.size() > 0 ) { |
|---|
| 354 |
Enumeration keys = papszMetadata.keys(); |
|---|
| 355 |
System.out.println("Metadata:"); |
|---|
| 356 |
while (keys.hasMoreElements()) { |
|---|
| 357 |
String key = (String) keys.nextElement(); |
|---|
| 358 |
System.out.println(" " + key + "=" |
|---|
| 359 |
+ papszMetadata.get(key)); |
|---|
| 360 |
} |
|---|
| 361 |
} |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
hDataset.delete(); |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
System.exit(0); |
|---|
| 387 |
} |
|---|
| 388 |
} |
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
static boolean GDALInfoReportCorner(Dataset hDataset, String corner_name, |
|---|
| 395 |
double x, double y) |
|---|
| 396 |
|
|---|
| 397 |
{ |
|---|
| 398 |
double dfGeoX, dfGeoY; |
|---|
| 399 |
String pszProjection; |
|---|
| 400 |
double[] adfGeoTransform = new double[6]; |
|---|
| 401 |
CoordinateTransformation hTransform = null; |
|---|
| 402 |
|
|---|
| 403 |
System.out.print(corner_name + " "); |
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
hDataset.GetGeoTransform(adfGeoTransform); |
|---|
| 409 |
{ |
|---|
| 410 |
pszProjection = hDataset.GetProjectionRef(); |
|---|
| 411 |
|
|---|
| 412 |
dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x |
|---|
| 413 |
+ adfGeoTransform[2] * y; |
|---|
| 414 |
dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x |
|---|
| 415 |
+ adfGeoTransform[5] * y; |
|---|
| 416 |
} |
|---|
| 417 |
|
|---|
| 418 |
if (adfGeoTransform[0] == 0 && adfGeoTransform[1] == 0 |
|---|
| 419 |
&& adfGeoTransform[2] == 0 && adfGeoTransform[3] == 0 |
|---|
| 420 |
&& adfGeoTransform[4] == 0 && adfGeoTransform[5] == 0) { |
|---|
| 421 |
System.out.println("(" + x + "," + y + ")"); |
|---|
| 422 |
return false; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
System.out.print("(" + dfGeoX + "," + dfGeoY + ") "); |
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
if (pszProjection != null && pszProjection.length() > 0) { |
|---|
| 434 |
SpatialReference hProj, hLatLong = null; |
|---|
| 435 |
|
|---|
| 436 |
hProj = new SpatialReference(pszProjection); |
|---|
| 437 |
if (hProj != null) |
|---|
| 438 |
hLatLong = hProj.CloneGeogCS(); |
|---|
| 439 |
|
|---|
| 440 |
if (hLatLong != null) { |
|---|
| 441 |
|
|---|
| 442 |
hTransform = new CoordinateTransformation(hProj, hLatLong); |
|---|
| 443 |
|
|---|
| 444 |
hLatLong.delete(); |
|---|
| 445 |
} |
|---|
| 446 |
|
|---|
| 447 |
if (hProj != null) |
|---|
| 448 |
hProj.delete(); |
|---|
| 449 |
} |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
if (hTransform != null) { |
|---|
| 455 |
double[] transPoint = new double[3]; |
|---|
| 456 |
hTransform.TransformPoint(transPoint, dfGeoX, dfGeoY, 0); |
|---|
| 457 |
System.out.print("(" + gdal.DecToDMS(transPoint[0], "Long", 2)); |
|---|
| 458 |
System.out |
|---|
| 459 |
.print("," + gdal.DecToDMS(transPoint[1], "Lat", 2) + ")"); |
|---|
| 460 |
} |
|---|
| 461 |
|
|---|
| 462 |
if (hTransform != null) |
|---|
| 463 |
hTransform.delete(); |
|---|
| 464 |
|
|---|
| 465 |
System.out.println(""); |
|---|
| 466 |
|
|---|
| 467 |
return true; |
|---|
| 468 |
} |
|---|
| 469 |
} |
|---|