geotools读取tiff的高程数据
时间: 2023-08-08 15:08:55 浏览: 211
GeoTiff读取程序
3星 · 编辑精心推荐
使用 GeoTools 读取 tiff 的高程数据可以按照以下步骤进行:
1. 导入 GeoTools 的相关包:
```java
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.coverage.grid.GridCoverageReader;
import org.geotools.coverage.grid.GridEnvelope2D;
import org.geotools.coverage.grid.InvalidGridGeometryException;
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
import org.geotools.coverage.grid.io.GridFormatNotFoundException;
import org.geotools.coverage.grid.io.GridFormatRegistry;
import org.geotools.gce.geotiff.GeoTiffFormat;
import org.geotools.geometry.Envelope2D;
import org.opengis.coverage.grid.GridCoverage;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValue;
import org.opengis.parameter.ParameterValueGroup;
```
2. 创建一个 GeoTiffFormat 对象:
```java
GeoTiffFormat geotiff = new GeoTiffFormat();
```
3. 通过 GeoTiffFormat 对象获取 GridCoverageReader 对象:
```java
AbstractGridCoverage2DReader reader = null;
try {
reader = (AbstractGridCoverage2DReader) geotiff.getReader(file);
} catch (IOException e) {
e.printStackTrace();
}
```
其中,`file` 为 tiff 文件的路径。
4. 通过 GridCoverageReader 对象读取 tiff 文件的高程数据:
```java
ParameterValue<AbstractGridCoverage2DReader> input = AbstractGridFormatReader.INPUT_FORMAT_READER
.createValue(reader);
final ParameterValue<String> name = AbstractGridFormatReader.INPUT_COVERAGE_NAME.createValue();
name.setValue(reader.getCoverageNames()[0]);
final GeneralParameterValue[] params = new GeneralParameterValue[2];
params[0] = input;
params[1] = name;
GridCoverage2D coverage = null;
try {
coverage = reader.read(params);
} catch (IllegalArgumentException | IOException | InvalidGridGeometryException e) {
e.printStackTrace();
}
```
5. 获取高程数据的范围:
```java
GridEnvelope2D gridRange2D = coverage.getGridGeometry().getGridRange2D();
int rows = gridRange2D.getSpan(1);
int cols = gridRange2D.getSpan(0);
```
6. 获取高程数据:
```java
double[] heights = new double[rows*cols];
int idx = 0;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
heights[idx++] = coverage.evaluate(new GridCoordinates2D(x, y)).doubleValue();
}
}
```
其中,`heights` 数组中存储的即为高程数据。
阅读全文