Created
January 9, 2018 13:48
-
-
Save ianturton/e54c41169ef32f48b1ac9b93c580e165 to your computer and use it in GitHub Desktop.
A basic and inefficient program to replace a polygon's area in a raster with a new value using GeoTools.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ianturton.cookbook.raster; | |
import java.awt.image.WritableRaster; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.media.jai.RasterFactory; | |
import org.geotools.coverage.CoverageFactoryFinder; | |
import org.geotools.coverage.grid.GridCoordinates2D; | |
import org.geotools.coverage.grid.GridCoverage2D; | |
import org.geotools.coverage.grid.GridCoverageFactory; | |
import org.geotools.coverage.grid.InvalidGridGeometryException; | |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; | |
import org.geotools.coverage.grid.io.AbstractGridFormat; | |
import org.geotools.coverage.grid.io.GridFormatFinder; | |
import org.geotools.factory.Hints; | |
import org.geotools.gce.geotiff.GeoTiffFormat; | |
import org.geotools.geometry.Envelope2D; | |
import org.opengis.coverage.grid.GridCoverageWriter; | |
import org.opengis.coverage.grid.GridEnvelope; | |
import org.opengis.geometry.DirectPosition; | |
import org.opengis.referencing.operation.TransformException; | |
import com.vividsolutions.jts.geom.Coordinate; | |
import com.vividsolutions.jts.geom.GeometryFactory; | |
import com.vividsolutions.jts.geom.Polygon; | |
public class UpdateByPolygon { | |
private GridCoverage2D coverage; | |
private GridCoverage2D output; | |
static final GeometryFactory gf = new GeometryFactory(); | |
public void loadRaster(File in) { | |
AbstractGridFormat format = GridFormatFinder.findFormat(in); | |
Hints hints = null; | |
if (format instanceof GeoTiffFormat) { | |
hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE); | |
} | |
AbstractGridCoverage2DReader reader = format.getReader(in,hints); | |
coverage = null; | |
try { | |
coverage = reader.read(null); | |
} catch (IOException giveUp) { | |
throw new RuntimeException(giveUp); | |
} | |
reader.dispose(); | |
} | |
public void writeRaster(File out) { | |
try { | |
AbstractGridFormat format = new GeoTiffFormat(); | |
Hints hints = null; | |
if (format instanceof GeoTiffFormat) { | |
hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE); | |
} | |
GridCoverageWriter writer = format.getWriter(out,hints); | |
try { | |
writer.write(output, null); | |
writer.dispose(); | |
} catch (IllegalArgumentException | IOException e) { | |
e.printStackTrace(); | |
} | |
} catch (InvalidGridGeometryException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Set pixels inside the polygon to value | |
* | |
* @param poly | |
* @param value | |
* @throws TransformException | |
*/ | |
public void intersect(Polygon poly, double[] value) throws TransformException { | |
GridEnvelope gridRange = coverage.getGridGeometry().getGridRange(); | |
WritableRaster writableRaster = RasterFactory.createBandedRaster(java.awt.image.DataBuffer.TYPE_DOUBLE, | |
gridRange.getSpan(0), gridRange.getSpan(1), coverage.getNumSampleDimensions(), null); | |
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE); | |
GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(hints); | |
Envelope2D envelope = coverage.getEnvelope2D(); | |
output = factory.create("Result", writableRaster, envelope); | |
double[] dest = new double[3]; | |
GridEnvelope gridRange2D = coverage.getGridGeometry().getGridRange(); | |
for (int i = gridRange2D.getLow(0); i < gridRange2D.getHigh(0); i++) { | |
for (int j = gridRange2D.getLow(1); j < gridRange2D.getHigh(1); j++) { | |
GridCoordinates2D coord = new GridCoordinates2D(i, j); | |
DirectPosition p = coverage.getGridGeometry().gridToWorld(coord); | |
if (poly.contains(gf.createPoint(new Coordinate(p.getOrdinate(0), p.getOrdinate(1))))) { | |
writableRaster.setDataElements(i, j, value); | |
} else { | |
coverage.evaluate(coord, dest); | |
writableRaster.setDataElements(i, j, dest); | |
} | |
} | |
} | |
} | |
/** | |
* @return the output | |
*/ | |
public GridCoverage2D getOutput() { | |
return output; | |
} | |
public static void main(String[] args) throws TransformException { | |
UpdateByPolygon updater = new UpdateByPolygon(); | |
updater.loadRaster(new File("world2.tif")); | |
Coordinate[] coordinates = new Coordinate[] {new Coordinate(0,0),new Coordinate(0,10),new Coordinate(10,10), new Coordinate(10,0),new Coordinate(0,0)}; | |
updater.intersect(gf.createPolygon(gf.createLinearRing(coordinates)), new double[] {0,0,0}); | |
updater.writeRaster(new File("world3.tif")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment