use of org.geotools.coverage.grid.InvalidGridGeometryException in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method colRowFromCoordinate.
/**
* Utility method to get col and row of a coordinate from a {@link GridGeometry2D}.
*
* @param coordinate the coordinate to transform.
* @param gridGeometry the gridgeometry to use.
* @param point if not <code>null</code>, the row col values are put inside the supplied point's x and y.
* @return the array with [col, row] or <code>null</code> if something went wrong.
*/
public static int[] colRowFromCoordinate(Coordinate coordinate, GridGeometry2D gridGeometry, Point point) {
try {
DirectPosition pos = new DirectPosition2D(coordinate.x, coordinate.y);
GridCoordinates2D worldToGrid = gridGeometry.worldToGrid(pos);
if (point != null) {
point.x = worldToGrid.x;
point.y = worldToGrid.y;
}
return new int[] { worldToGrid.x, worldToGrid.y };
} catch (InvalidGridGeometryException e) {
e.printStackTrace();
} catch (TransformException e) {
e.printStackTrace();
}
point.x = Integer.MAX_VALUE;
point.y = Integer.MAX_VALUE;
return null;
}
use of org.geotools.coverage.grid.InvalidGridGeometryException in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method coordinateFromColRow.
/**
* Utility method to get the coordinate of a col and row from a {@link GridGeometry2D}.
*
* @param col the col to transform.
* @param row the row to transform.
* @param gridGeometry the gridgeometry to use.
* @return the coordinate or <code>null</code> if something went wrong.
*/
public static Coordinate coordinateFromColRow(int col, int row, GridGeometry2D gridGeometry) {
try {
GridCoordinates2D pos = new GridCoordinates2D(col, row);
DirectPosition gridToWorld = gridGeometry.gridToWorld(pos);
double[] coord = gridToWorld.getCoordinate();
return new Coordinate(coord[0], coord[1]);
} catch (InvalidGridGeometryException e) {
e.printStackTrace();
} catch (TransformException e) {
e.printStackTrace();
}
return null;
}
use of org.geotools.coverage.grid.InvalidGridGeometryException in project hortonmachine by TheHortonMachine.
the class RasterInfoLayer method draw.
@Override
public void draw(Graphics2D g2d, MapContent mapContent, MapViewport viewport) {
try {
if (raster != null) {
ReferencedEnvelope screenBounds = viewport.getBounds();
AffineTransform worldToScreen = viewport.getWorldToScreen();
double w = screenBounds.getMinX();
double s = screenBounds.getMinY();
double e = screenBounds.getMaxX();
double n = screenBounds.getMaxY();
double half = cellSize / 2;
GridCoordinates2D llPix = gg.worldToGrid(new DirectPosition2D(w, s));
GridCoordinates2D urPix = gg.worldToGrid(new DirectPosition2D(e, n));
int fromC = (int) Math.floor(llPix.getX());
int toC = (int) Math.ceil(urPix.getX()) + 1;
int fromR = (int) Math.floor(urPix.getY());
int toR = (int) Math.ceil(llPix.getY()) + 1;
int cellCount = (toC - fromC) * (toR - fromR);
if (cellCount > 1E4) {
// / too many cells
return;
}
DecimalFormat f = new DecimalFormat(DEFAULT_NUMFORMAT);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
for (int r = fromR; r < toR; r++) {
if (r < 0 || r >= rows)
continue;
for (int c = fromC; c < toC; c++) {
if (c < 0 || c >= cols)
continue;
Coordinate cellCenterCoord = CoverageUtilities.coordinateFromColRow(c, r, gg);
Point2D p1 = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x - half, cellCenterCoord.y - half), null);
Point2D p2 = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x - half, cellCenterCoord.y + half), null);
Point2D p3 = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x + half, cellCenterCoord.y + half), null);
Point2D p4 = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x + half, cellCenterCoord.y - half), null);
if (showCells) {
GeneralPath path = new GeneralPath();
path.moveTo(p1.getX(), p1.getY());
path.lineTo(p2.getX(), p2.getY());
path.lineTo(p3.getX(), p3.getY());
path.lineTo(p4.getX(), p4.getY());
g2d.setColor(Color.black);
g2d.setStroke(new BasicStroke(1));
g2d.draw(path);
}
double value = CoverageUtilities.getValue(raster, c, r);
if (HMConstants.isNovalue(value)) {
continue;
}
if (showtext) {
int cellW = (int) (p3.getX() - p2.getX());
int cellH = (int) (p1.getY() - p2.getY());
int fontSize = cellH / 5;
if (fontSize > 14) {
fontSize = 14;
}
Font font = g2d.getFont();
Font newFont = new Font("default", Font.BOLD, fontSize);
g2d.setFont(newFont);
g2d.setColor(Color.BLACK);
FontMetrics fontMetrics = g2d.getFontMetrics();
CoverageUtilities.getValue(raster, c, r);
String text1 = "col:" + c;
String text2 = "row:" + r;
String text3 = "value:" + f.format(value);
if (!drawStrings(g2d, p1, p2, cellW, cellH, fontMetrics, text1, text2, text3)) {
text1 = "c:" + c;
text2 = "r:" + r;
text3 = "v:" + f.format(value);
if (!drawStrings(g2d, p1, p2, cellW, cellH, fontMetrics, text1, text2, text3)) {
text1 = "" + c;
text2 = "" + r;
text3 = "" + f.format(value);
if (!drawStrings(g2d, p1, p2, cellW, cellH, fontMetrics, text1, text2, text3)) {
drawStrings(g2d, p1, p2, cellW, cellH, fontMetrics, text1, text2, null);
}
}
}
}
}
}
}
} catch (InvalidGridGeometryException | TransformException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
use of org.geotools.coverage.grid.InvalidGridGeometryException in project hortonmachine by TheHortonMachine.
the class HM method drawInfo.
private static void drawInfo(Graphics2D g2d, AffineTransform worldToScreen, ReferencedEnvelope envelope, GridCoverage2D raster, String dtm) throws Exception {
try {
String DEFAULT_NUMFORMAT = "0.#";
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(raster);
GridGeometry2D gg = raster.getGridGeometry();
double xres = regionMap.getXres();
double yres = regionMap.getYres();
double w = envelope.getMinX();
double s = envelope.getMinY();
double e = envelope.getMaxX();
double n = envelope.getMaxY();
double halfX = regionMap.getXres() / 2;
double halfY = regionMap.getYres() / 2;
int cols = regionMap.getCols();
int rows = regionMap.getRows();
GridCoordinates2D llPix = gg.worldToGrid(new DirectPosition2D(w, s));
GridCoordinates2D urPix = gg.worldToGrid(new DirectPosition2D(e, n));
int fromC = (int) Math.floor(llPix.getX());
int toC = (int) Math.ceil(urPix.getX()) + 1;
int fromR = (int) Math.floor(urPix.getY());
int toR = (int) Math.ceil(llPix.getY()) + 1;
boolean doDtmInfo = false;
RandomIter dtmIter = null;
GridCoverage2D dtmGc;
if (dtm != null) {
doDtmInfo = true;
dtmGc = OmsRasterReader.readRaster(dtm);
dtmIter = CoverageUtilities.getRandomIterator(dtmGc);
}
int[] centerColRow = CoverageUtilities.colRowFromCoordinate(envelope.centre(), gg, null);
DecimalFormat f = new DecimalFormat(DEFAULT_NUMFORMAT);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
for (int r = fromR; r < toR; r++) {
if (r < 0 || r >= rows)
continue;
for (int c = fromC; c < toC; c++) {
if (c < 0 || c >= cols)
continue;
boolean isCenter = centerColRow[0] == c && centerColRow[1] == r;
Coordinate cellCenterCoord = CoverageUtilities.coordinateFromColRow(c, r, gg);
Point2D ll = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x - halfX, cellCenterCoord.y - halfY), null);
Point2D ul = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x - halfX, cellCenterCoord.y + halfY), null);
Point2D ur = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x + halfX, cellCenterCoord.y + halfY), null);
Point2D lr = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x + halfX, cellCenterCoord.y - halfY), null);
GeneralPath path = new GeneralPath();
path.moveTo(ll.getX(), ll.getY());
path.lineTo(ul.getX(), ul.getY());
path.lineTo(ur.getX(), ur.getY());
path.lineTo(lr.getX(), lr.getY());
double value = CoverageUtilities.getValue(raster, c, r);
String valueFormatted = f.format(value);
if (isCenter) {
g2d.setColor(Color.red);
g2d.setStroke(new BasicStroke(3));
} else if (HMConstants.isNovalue(value)) {
g2d.setColor(Color.gray);
g2d.setStroke(new BasicStroke(1));
valueFormatted = "nv";
} else {
g2d.setColor(Color.black);
g2d.setStroke(new BasicStroke(1));
}
g2d.draw(path);
// draw dtm info
if (doDtmInfo && !HMConstants.isNovalue(value)) {
GridNode node = new GridNode(dtmIter, cols, rows, xres, yres, c, r, null);
int dot = 8;
Point2D pCenter = worldToScreen.transform(new Point2D.Double(cellCenterCoord.x, cellCenterCoord.y), null);
if (node.isPit()) {
g2d.setColor(Color.red);
g2d.fillOval((int) pCenter.getX() - dot / 2, (int) pCenter.getY() - dot / 2, dot, dot);
} else {
g2d.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(3));
int d = (int) ((ur.getX() - ll.getX()) * 0.2);
int flow = node.getFlow();
if (!HMConstants.isNovalue(flow)) {
GeneralPath fPath = null;
Direction dir = Direction.forFlow(flow);
switch(dir) {
case E:
g2d.fillOval((int) ll.getX() + d - dot / 2, (int) pCenter.getY() - dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(ll.getX() + d, pCenter.getY());
fPath.lineTo(ur.getX() - d, pCenter.getY());
g2d.draw(fPath);
break;
case W:
g2d.fillOval((int) ur.getX() - d - dot / 2, (int) pCenter.getY() - dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(ur.getX() - d, pCenter.getY());
fPath.lineTo(ll.getX() + d, pCenter.getY());
g2d.draw(fPath);
break;
case S:
g2d.fillOval((int) pCenter.getX() - dot / 2, (int) ur.getY() + d + dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(pCenter.getX(), ur.getY() + d);
fPath.lineTo(pCenter.getX(), ll.getY() - d);
g2d.draw(fPath);
break;
case N:
g2d.fillOval((int) pCenter.getX() - dot / 2, (int) ll.getY() - d - dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(pCenter.getX(), ll.getY() - d);
fPath.lineTo(pCenter.getX(), ur.getY() + d);
g2d.draw(fPath);
break;
case SE:
g2d.fillOval((int) ul.getX() + d - dot / 2, (int) ul.getY() + d - dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(ul.getX() + d, ul.getY() + d);
fPath.lineTo(lr.getX() - d, lr.getY() - d);
g2d.draw(fPath);
break;
case WS:
g2d.fillOval((int) ur.getX() - d - dot / 2, (int) ur.getY() + d - dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(ur.getX() - d, ur.getY() + d);
fPath.lineTo(ll.getX() + d, ll.getY() - d);
g2d.draw(fPath);
break;
case NW:
g2d.fillOval((int) lr.getX() - d - dot / 2, (int) lr.getY() - d - dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(lr.getX() - d, lr.getY() - d);
fPath.lineTo(ul.getX() + d, ul.getY() + d);
g2d.draw(fPath);
break;
case EN:
g2d.fillOval((int) ll.getX() + d - dot / 2, (int) ll.getY() - d - dot / 2, dot, dot);
fPath = new GeneralPath();
fPath.moveTo(ll.getX() + d, ll.getY() - d);
fPath.lineTo(ur.getX() - d, ur.getY() + d);
g2d.draw(fPath);
break;
default:
break;
}
}
}
}
// draw string info
int cellW = (int) (ur.getX() - ul.getX());
int cellH = (int) (ll.getY() - ul.getY());
int fontSize = cellH / 5;
if (fontSize > 14) {
fontSize = 14;
}
Font newFont = new Font("default", Font.BOLD, fontSize);
g2d.setFont(newFont);
g2d.setColor(Color.BLACK);
FontMetrics fontMetrics = g2d.getFontMetrics();
String text1 = "col:" + c;
String text2 = "row:" + r;
String text3 = "value:" + valueFormatted;
if (!drawStrings(g2d, ll, ul, cellW, cellH, fontMetrics, text1, text2, text3)) {
text1 = "c:" + c;
text2 = "r:" + r;
text3 = "v:" + valueFormatted;
if (!drawStrings(g2d, ll, ul, cellW, cellH, fontMetrics, text1, text2, text3)) {
text1 = "" + c;
text2 = "" + r;
text3 = "" + valueFormatted;
if (!drawStrings(g2d, ll, ul, cellW, cellH, fontMetrics, text1, text2, text3)) {
drawStrings(g2d, ll, ul, cellW, cellH, fontMetrics, text1, text2, null);
}
}
}
}
}
} catch (InvalidGridGeometryException | TransformException e1) {
e1.printStackTrace();
}
}
use of org.geotools.coverage.grid.InvalidGridGeometryException in project hortonmachine by TheHortonMachine.
the class OmsScanLineRasterizer method rasterizepolygon.
private void rasterizepolygon(final GridGeometry2D gridGeometry) throws InvalidGridGeometryException, TransformException {
int size = inVector.size();
pm.beginTask("Rasterizing features...", size);
FeatureIterator<SimpleFeature> featureIterator = inVector.features();
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(getDefaultThreadsNum());
while (featureIterator.hasNext()) {
final SimpleFeature feature = featureIterator.next();
// extract the value to put into the raster.
double tmpValue = -1.0;
if (pValue == null) {
tmpValue = ((Number) feature.getAttribute(fCat)).doubleValue();
} else {
tmpValue = pValue;
}
final double value = tmpValue;
final double delta = xRes / 4.0;
Runnable runner = new Runnable() {
public void run() {
try {
Geometry geometry = (Geometry) feature.getDefaultGeometry();
int numGeometries = geometry.getNumGeometries();
for (int i = 0; i < numGeometries; i++) {
final Geometry geometryN = geometry.getGeometryN(i);
// PreparedGeometryFactory.prepare(geometryN);
for (int r = 0; r < height; r++) {
// do scan line to fill the polygon
double[] westPos = gridGeometry.gridToWorld(new GridCoordinates2D(0, r)).getCoordinate();
double[] eastPos = gridGeometry.gridToWorld(new GridCoordinates2D(width - 1, r)).getCoordinate();
Coordinate west = new Coordinate(westPos[0], westPos[1]);
Coordinate east = new Coordinate(eastPos[0], eastPos[1]);
LineString line = gf.createLineString(new Coordinate[] { west, east });
if (geometryN.intersects(line)) {
Geometry internalLines = geometryN.intersection(line);
int lineNums = internalLines.getNumGeometries();
for (int l = 0; l < lineNums; l++) {
Coordinate[] coords = internalLines.getGeometryN(l).getCoordinates();
if (coords.length == 2) {
for (int j = 0; j < coords.length; j = j + 2) {
Coordinate startC = new Coordinate(coords[j].x + delta, coords[j].y);
Coordinate endC = new Coordinate(coords[j + 1].x - delta, coords[j + 1].y);
DirectPosition2D startDP;
DirectPosition2D endDP;
if (startC.x < endC.x) {
startDP = new DirectPosition2D(startC.x, startC.x);
endDP = new DirectPosition2D(endC.x, endC.x);
} else {
startDP = new DirectPosition2D(endC.x, endC.x);
endDP = new DirectPosition2D(startC.x, startC.x);
}
GridCoordinates2D startGridCoord = gridGeometry.worldToGrid(startDP);
GridCoordinates2D endGridCoord = gridGeometry.worldToGrid(endDP);
/*
* the part in between has to be filled
*/
for (int k = startGridCoord.x; k <= endGridCoord.x; k++) {
if (inIter != null && fCat == null) {
double v = inIter.getSampleDouble(k, r, 0);
outWR.setSample(k, r, 0, v);
} else {
outWR.setSample(k, r, 0, value);
}
}
}
} else {
if (coords.length == 1) {
pm.errorMessage(MessageFormat.format("Found a cusp in: {0}/{1}", coords[0].x, coords[0].y));
} else {
throw new ModelsIOException(MessageFormat.format("Found intersection with more than 2 points in: {0}/{1}", coords[0].x, coords[0].y), this);
}
}
}
}
}
}
pm.worked(1);
} catch (Exception e) {
pm.errorMessage(e.getLocalizedMessage());
e.printStackTrace();
}
}
};
fixedThreadPool.execute(runner);
}
try {
fixedThreadPool.shutdown();
fixedThreadPool.awaitTermination(30, TimeUnit.DAYS);
fixedThreadPool.shutdownNow();
} catch (InterruptedException e) {
e.printStackTrace();
}
pm.done();
featureIterator.close();
}
Aggregations