public class GeoTIFFTest {
private final static String url = "jdbc:postgresql://localhost/bag";
private static GridCoverage2D grid;
private static Raster gridData;
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);
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();
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);
}
}
}
}