/** * Return the row and column of the active region matrix for a give coordinate * * * <p>NOTE: basically the inverse of {@link * JGrassUtilities#rowColToCenterCoordinates(JGrassRegion, int, int)} * * @param active the active region * @param coord * @return and int array containing row and col */ public static int[] coordinateToNearestRowCol(JGrassRegion active, Coordinate coord) { double easting = coord.x; double northing = coord.y; int[] rowcol = new int[2]; if (easting > active.getEast() || easting < active.getWest() || northing > active.getNorth() || northing < active.getSouth()) { return null; } double minx = active.getWest(); double ewres = active.getWEResolution(); for (int i = 0; i < active.getCols(); i++) { minx = minx + ewres; if (easting < minx) { rowcol[1] = i; break; } } double maxy = active.getNorth(); double nsres = active.getNSResolution(); for (int i = 0; i < active.getRows(); i++) { maxy = maxy - nsres; if (northing > maxy) { rowcol[0] = i; break; } } return rowcol; }
/** * Transforms row and column index of the active region into the regarding northing and easting * coordinates. The center of the cell is taken. * * <p>NOTE: basically the inverse of {@link * JGrassUtilities#coordinateToNearestRowCol(JGrassRegion, Coordinate)} * * @param active - the active region (can be null) * @param row - row number of the point to transform * @param col - column number of the point to transform * @return the point in N/E coordinates of the supplied row and column */ public static Coordinate rowColToCenterCoordinates(JGrassRegion active, int row, int col) { double north = active.getNorth(); double west = active.getWest(); double nsres = active.getNSResolution(); double ewres = active.getWEResolution(); double northing = north - row * nsres - nsres / 2.0; double easting = west + col * ewres + ewres / 2.0; return new Coordinate(easting, northing); }
/** * Transforms row and column index of the active region into an array of the coordinates of the * edgaes, i.e. n, s, e, w * * @param active - the active region (can be null) * @param row - row number of the point to transform * @param col - column number of the point to transform * @return the array of north, south, east, west */ public static double[] rowColToNodeboundCoordinates(JGrassRegion active, int row, int col) { double anorth = active.getNorth(); double awest = active.getWest(); double nsres = active.getNSResolution(); double ewres = active.getWEResolution(); double[] nsew = new double[4]; nsew[0] = anorth - row * nsres; nsew[1] = anorth - row * nsres - nsres; nsew[2] = awest + col * ewres + ewres; nsew[3] = awest + col * ewres; return nsew; }
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException { hasListeners = (this.progressListeners != null && (!(this.progressListeners.isEmpty()))) ? true : false; if (hasListeners) { clearAbortRequest(); // Broadcast the start of the image write operation processImageStarted(0); } RenderedImage renderedImage = image.getRenderedImage(); // PlanarImage inputRenderedImage = PlanarImage.wrapRenderedImage(renderedImage); // Raster data = renderedImage.getData(); // final RectIter iterator = RectIterFactory.create(data, null); // writing noDataValue = rasterWriter.getNoData(); writeRegion.setCols(renderedImage.getWidth()); writeRegion.setRows(renderedImage.getHeight()); int nColumns = writeRegion.getCols(); int nRows = writeRegion.getRows(); double west = writeRegion.getWest(); double south = writeRegion.getSouth(); double cellsizeX = writeRegion.getWEResolution(); double cellsizeY = writeRegion.getNSResolution(); rasterWriter.writeRaster( renderedImage, nColumns, nRows, west, south, cellsizeX, cellsizeY, noDataValue); if (hasListeners) { // Checking the status of the write operation (aborted/completed) if (rasterWriter.isAborting()) processWriteAborted(); else processImageComplete(); } // rasterWriter.close(); }
/** * return the rectangle of the cell of the active region, that surrounds the given coordinates * * @param activeRegion * @param x the given easting coordinate * @param y given northing coordinate * @return the rectangle localizing the cell inside which the x and y stay */ public static JGrassRegion getRectangleAroundPoint( JGrassRegion activeRegion, double x, double y) { double minx = activeRegion.getRectangle().getBounds2D().getMinX(); double ewres = activeRegion.getWEResolution(); double snapx = minx + (Math.round((x - minx) / ewres) * ewres); double miny = activeRegion.getRectangle().getBounds2D().getMinY(); double nsres = activeRegion.getNSResolution(); double snapy = miny + (Math.round((y - miny) / nsres) * nsres); double xmin = 0.0; double xmax = 0.0; double ymin = 0.0; double ymax = 0.0; if (x >= snapx) { xmin = snapx; xmax = xmin + ewres; } else { xmax = snapx; xmin = xmax - ewres; } if (y <= snapy) { ymax = snapy; ymin = ymax - nsres; } else { ymin = snapy; ymax = ymin + nsres; } // why do I have to put ymin, Rectangle requires the upper left // corner?????!!!! Is it a BUG // in the Rectangle2D class? or docu? // Rectangle2D rect = new Rectangle2D.Double(xmin, ymin, ewres, nsres); return new JGrassRegion(xmin, xmax, ymin, ymax, 1, 1); }