Search in sources :

Example 1 with Direction

use of org.hortonmachine.gears.libs.modules.Direction in project hortonmachine by TheHortonMachine.

the class OmsSlope method calculateSlope.

/**
 * Calculates the slope of a given flowdirection value in currentCol and currentRow.
 *
 * @param node the current {@link GridNode}.
 * @param flowValue the value of the flowdirection.
 * @return
 */
public static double calculateSlope(GridNode node, double flowValue) {
    double value = doubleNovalue;
    if (!isNovalue(flowValue)) {
        int flowDir = (int) flowValue;
        if (flowDir != 10) {
            Direction direction = Direction.forFlow(flowDir);
            double distance = direction.getDistance(node.xRes, node.yRes);
            double currentElevation = node.elevation;
            double nextElevation = node.getElevationAt(direction);
            value = (currentElevation - nextElevation) / distance;
        }
    }
    return value;
}
Also used : Direction(org.hortonmachine.gears.libs.modules.Direction)

Example 2 with Direction

use of org.hortonmachine.gears.libs.modules.Direction in project hortonmachine by TheHortonMachine.

the class OmsLeastCostFlowDirections method setNodeValues.

private void setNodeValues(GridNode node, int enteringFlow) {
    int col = node.col;
    int row = node.row;
    flowIter.setSample(col, row, 0, enteringFlow);
    pm.worked(1);
    orderedNodes.add(node);
    assignedFlowsMap.mark(col, row);
    if (doSlope) {
        double slope = OmsSlope.calculateSlope(node, enteringFlow);
        if (slope <= 0.0) {
            // put smallest possible slope
            slope = Double.MIN_VALUE;
        }
        slopeIter.setSample(col, row, 0, slope);
    }
    if (doAspect) {
        double aspect = OmsAspect.calculateAspect(node, 1.0, false);
        aspectIter.setSample(col, row, 0, aspect);
    }
    /*
         * once a flow value is set, if tca is meant to be calculated,
         * the flow has to be followed downstream adding the contributing cell.
         */
    if (doTca) {
        int runningCol = col;
        int runningRow = row;
        while (isInRaster(runningCol, runningRow)) {
            double tmpFlow = flowIter.getSampleDouble(runningCol, runningRow, 0);
            if (!isNovalue(tmpFlow)) {
                double tmpTca = tcaIter.getSampleDouble(runningCol, runningRow, 0);
                if (isNovalue(tmpTca)) {
                    tmpTca = 0.0;
                }
                tcaIter.setSample(runningCol, runningRow, 0, tmpTca + 1.0);
                Direction flowDir = Direction.forFlow((int) tmpFlow);
                if (flowDir != null) {
                    runningCol = runningCol + flowDir.col;
                    runningRow = runningRow + flowDir.row;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }
}
Also used : Direction(org.hortonmachine.gears.libs.modules.Direction)

Example 3 with Direction

use of org.hortonmachine.gears.libs.modules.Direction 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();
    }
}
Also used : BasicStroke(java.awt.BasicStroke) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GeneralPath(java.awt.geom.GeneralPath) GridNode(org.hortonmachine.gears.libs.modules.GridNode) DecimalFormat(java.text.DecimalFormat) RegionMap(org.hortonmachine.gears.utils.RegionMap) TransformException(org.opengis.referencing.operation.TransformException) RandomIter(javax.media.jai.iterator.RandomIter) LineString(org.locationtech.jts.geom.LineString) DirectPosition2D(org.geotools.geometry.DirectPosition2D) Direction(org.hortonmachine.gears.libs.modules.Direction) Point(org.locationtech.jts.geom.Point) Font(java.awt.Font) Coordinate(org.locationtech.jts.geom.Coordinate) Point2D(java.awt.geom.Point2D) FontMetrics(java.awt.FontMetrics) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) InvalidGridGeometryException(org.geotools.coverage.grid.InvalidGridGeometryException)

Aggregations

Direction (org.hortonmachine.gears.libs.modules.Direction)3 BasicStroke (java.awt.BasicStroke)1 Font (java.awt.Font)1 FontMetrics (java.awt.FontMetrics)1 GeneralPath (java.awt.geom.GeneralPath)1 Point2D (java.awt.geom.Point2D)1 DecimalFormat (java.text.DecimalFormat)1 RandomIter (javax.media.jai.iterator.RandomIter)1 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)1 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)1 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)1 InvalidGridGeometryException (org.geotools.coverage.grid.InvalidGridGeometryException)1 DirectPosition2D (org.geotools.geometry.DirectPosition2D)1 GridNode (org.hortonmachine.gears.libs.modules.GridNode)1 RegionMap (org.hortonmachine.gears.utils.RegionMap)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 LineString (org.locationtech.jts.geom.LineString)1 Point (org.locationtech.jts.geom.Point)1 TransformException (org.opengis.referencing.operation.TransformException)1