GeoTools 读取 Geotiff 文件

GeoTools 读取 Geotiff 文件

maven 相关配置

<dependencies>
4<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geotiff</artifactId>
4</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
</dependency>
<!-- ... -->
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
</repositories>

参考代码

Reading each pixel of each band of multiband GeoTiff with GeoTools

public class Test {
public static void test(java.io.File file) throws Exception {
ParameterValue<OverviewPolicy> policy = AbstractGridFormat.OVERVIEW_POLICY.createValue();
policy.setValue(OverviewPolicy.IGNORE);
//this will basically read 4 tiles worth of data at once from the disk...
ParameterValue<String> gridsize = AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue();
//Setting read type: use JAI ImageRead (true) or ImageReaders read methods (false)
ParameterValue<Boolean> useJaiRead = AbstractGridFormat.USE_JAI_IMAGEREAD.createValue();
useJaiRead.setValue(true);
GridCoverage2DReader reader = new GeoTiffReader(file);
GridEnvelope dimensions = reader.getOriginalGridRange();
GridCoordinates maxDimensions = dimensions.getHigh();
int w = maxDimensions.getCoordinateValue(0)+1;
int h = maxDimensions.getCoordinateValue(1)+1;
int numBands = reader.getGridCoverageCount();
GridCoverage2D coverage = reader.read(
new GeneralParameterValue[]{policy, gridsize, useJaiRead}
);
GridGeometry2D geometry = coverage.getGridGeometry();
for (int i=0; i<w; i++) {
for (int j=0; j<h; j++) {
org.geotools.geometry.Envelope2D pixelEnvelop =
geometry.gridToWorld(new GridEnvelope2D(i, j, 1, 1));
double lat = pixelEnvelop.getCenterY();
double lon = pixelEnvelop.getCenterX();
double[] vals = new double[numBands];
coverage.evaluate(new GridCoordinates2D(i, j), vals);
//Do something!
}
}
}
}

Access Information from GeoTIFF using Java

public class GeoTIFFTest {
private final static String url = "jdbc:postgresql://localhost/bag";
private static GridCoverage2D grid;
private static Raster gridData; // java.awt.image.Raster;
public static void main(String[] args) throws Exception {
initTif();
loadData();
}
private static void initTif() throws Exception {
File tiffFile = new File("/Volumes/Iomega_HDD/mac/data/r44hn1.tif");
GeoTiffReader reader = new GeoTiffReader(tiffFile);
grid =reader.read(null);
gridData = grid.getRenderedImage().getData();
}
private static double getValue(double x, double y) throws Exception {
GridGeometry2D gg = grid.getGridGeometry();
DirectPosition2D posWorld = new DirectPosition2D(x,y);
GridCoordinates2D posGrid = gg.worldToGrid(posWorld);
// envelope is the size in the target projection
double[] pixel=new double[1];
double[] data = gridData.getPixel(posGrid.x, posGrid.y, pixel);
return data[0];
}
private static void loadData() throws Exception {
Connection conn = DriverManager.getConnection(url);
QueryRunner runner = new QueryRunner(); // org.apache.commons.dbutils.QueryRunner;
final Map<Long, Double> map = new HashMap<Long, Double>();
ResultSetHandler handler = new ResultSetHandler() {
@Override
public Object handle(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
String point = resultSet.getString("point");
double x = Double.parseDouble(point.substring(
point.indexOf('(') + 1,
point.indexOf(' ')
));
double y = Double.parseDouble(point.substring(
point.indexOf(' ') + 1,
point.indexOf(')')
));
try {
double hoogte = getValue(x, y);
map.put(resultSet.getLong("gid"),hoogte);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
};
runner.query(conn, "SELECT gid, ST_AsText(ST_Centroid(geovlak)) as point \n" +
"FROM bag8mrt2014.pand\n" +
"WHERE geovlak && ST_MakeEnvelope(130153, 408769,132896, 410774, 28992) ORDER by gid ;", handler);
int count = 0;
for (Long key : map.keySet()) {
System.out.println("Inserting for key = " + key + " value: " + map.get(key));
int col = runner.update(conn, "UPDATE bag8mrt2014.pand SET hoogte= ? where gid = ?",
map.get(key), key);
count++;
if (count%100 == 0) {
System.out.println("count = " + count);
}
}
}
}

http://docs.geotools.org/latest/userguide/tutorial/raster/image.html

文章目录
  1. 1. GeoTools 读取 Geotiff 文件
    1. 1.1. maven 相关配置
    2. 1.2. 参考代码
|