一、pom依赖
注:依赖较多,可根据实际情况选择添加
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-process</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-process-raster</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-coverage</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-render</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-metadata</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geotiff</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>17.4</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>17.4</version>
</dependency>
二、参考代码
import java.io.File;
import java.io.IOException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.io.GridCoverage2DReader;
import org.geotools.gce.geotiff.GeoTiffFormat;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.opengis.geometry.Envelope;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
/**
* @Author:wfr
* @Date:2024/4/2 15:52
* @Describe:
*/
public class TifGeometryReader {
public static void main(String[] args) {
File tifFile = new File("your tifFile path.tif");
try {
// 使用 GeoTiffFormat 获取 GeoTiff 格式的 GridCoverage2DReader
GeoTiffFormat format = new GeoTiffFormat();
GridCoverage2DReader reader = format.getReader(tifFile);
// 读取 GridCoverage2D 对象
GridCoverage2D coverage = reader.read(null);
// 获取几何信息
Envelope envelope = coverage.getEnvelope();
CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
// 创建几何工厂
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
final int dimension = envelope.getDimension();
System.out.println(dimension);
double maxX = envelope.getMaximum(0);
double minX = envelope.getMinimum(0);
double maxY =envelope.getMaximum(1);
double minY = envelope.getMinimum(1);
for (int i = 0; i < dimension; i++) {
double maximum = envelope.getMaximum(i);
final double minimum = envelope.getMinimum(i);
System.out.println("maximum=="+maximum);
System.out.println("minimum=="+minimum);
}
// 创建多边形的点数组
// 创建坐标数组
Coordinate[] coordinates = {
new Coordinate(minX, minY),
new Coordinate(maxX, minY),
new Coordinate(maxX, maxY),
new Coordinate(minX, maxY),
new Coordinate(minX, minY)
};
// 创建 Polygon 几何对象
Polygon polygon = geometryFactory.createPolygon(coordinates);
// 打印几何对象
System.out.println("Geometry: " + polygon);
// 关闭读取器
reader.dispose();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、输出结果
输出结果可以看到是一个面,实际就是获取tif四至范围。