private boolean incr(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int col = rectIndex[1]; double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); if (Math.abs(diffLat) > Math.abs(diffLon)) { // try lat first rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } else { rectIndex[1] = col + ((diffLon > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; rectIndex[0] = row + ((diffLat > 0) ? 1 : -1); if (contains(wantLat, wantLon, rectIndex)) return true; } // back to original, do box search rectIndex[0] = row; rectIndex[1] = col; return box9(wantLat, wantLon, rectIndex); }
findBounds(); if (wantLat < latMinMax.min) return false; if (wantLat > latMinMax.max) return false; count++; if (debug) System.out.printf("%nIteration %d %n", count); if (contains(wantLat, wantLon, rectIndex)) return true; if (!jump2(wantLat, wantLon, rectIndex)) return false; return incr(wantLat, wantLon, rectIndex);
private static void doOne(GridCoordinate2D g2d, double wantLat, double wantLon) { int[] result = new int[2]; if (g2d.findCoordElementForce(wantLat, wantLon, result)) System.out.printf("Brute (%f %f) == (%d %d) %n", wantLat, wantLon, result[0], result[1]); else { System.out.printf("Brute (%f %f) FAIL", wantLat, wantLon); return; } if (g2d.findCoordElement(wantLat, wantLon, result)) System.out.printf("(%f %f) == (%d %d) %n", wantLat, wantLon, result[0], result[1]); else System.out.printf("(%f %f) FAIL %n", wantLat, wantLon); System.out.printf("----------------------------------------%n"); }
public boolean findCoordElementForce(double wantLat, double wantLon, int[] rectIndex) { findBounds(); if (wantLat < latMinMax.min) return false; if (wantLat > latMinMax.max) return false; if (wantLon < lonMinMax.min) return false; if (wantLon > lonMinMax.max) return false; boolean saveDebug = debug; debug = false; for (int row=0; row<nrows; row++) { for (int col=0; col<ncols; col++) { rectIndex[0] = row; rectIndex[1] = col; if (contains(wantLat, wantLon, rectIndex)) { debug = saveDebug; return true; } } } //debug = saveDebug; return false; }
public static void test1() throws IOException { String filename = "D:/work/asaScience/EGM200_3.ncml"; GridDataset gds = GridDataset.open(filename); GeoGrid grid = gds.findGridByName("u_wind"); GridCoordSystem gcs = grid.getCoordinateSystem(); CoordinateAxis lonAxis = gcs.getXHorizAxis(); assert lonAxis instanceof CoordinateAxis2D; CoordinateAxis latAxis = gcs.getYHorizAxis(); assert latAxis instanceof CoordinateAxis2D; GridCoordinate2D g2d = new GridCoordinate2D((CoordinateAxis2D) latAxis, (CoordinateAxis2D) lonAxis); doOne(g2d, 35.0, -6.0); doOne(g2d, 34.667302, -5.008376); // FAIL doOne(g2d, 34.667303, -6.394240); doOne(g2d, 36.6346, -5.0084); doOne(g2d, 36.6346, -6.394240); gds.close(); }
/** * Given a point in x,y coordinate space, find the x,y index in the coordinate system. * If outside the range, the closest point is returned, eg, 0 or n-1 depending on if the coordinate is too small or too large. * * @param x_coord position in x coordinate space. * @param y_coord position in y coordinate space. * @param result put result in here, may be null * @return int[2], 0=x,1=y indices in the coordinate system of the point. */ @Override public int[] findXYindexFromCoordBounded(double x_coord, double y_coord, int[] result) { if (result == null) result = new int[2]; if ((horizXaxis instanceof CoordinateAxis1D) && (horizYaxis instanceof CoordinateAxis1D)) { result[0] = ((CoordinateAxis1D) horizXaxis).findCoordElementBounded(x_coord); result[1] = ((CoordinateAxis1D) horizYaxis).findCoordElementBounded(y_coord); return result; } else if ((horizXaxis instanceof CoordinateAxis2D) && (horizYaxis instanceof CoordinateAxis2D)) { if (g2d == null) g2d = new GridCoordinate2D((CoordinateAxis2D) horizYaxis, (CoordinateAxis2D) horizXaxis); int[] result2 = new int[2]; g2d.findCoordElement(y_coord, x_coord, result2); // returns best guess result[0] = result2[1]; result[1] = result2[0]; return result; } // cant happen throw new IllegalStateException("GridCoordSystem.findXYindexFromCoord"); }
private boolean jumpOld(double wantLat, double wantLon, int[] rectIndex) { int row = Math.max( Math.min(rectIndex[0], nrows-1), 0); int col = Math.max( Math.min(rectIndex[1], ncols-1), 0); double gradientLat = latEdge.get(row + 1, col) - latEdge.get(row, col); double gradientLon = lonEdge.get(row, col + 1) - lonEdge.get(row, col); double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); int drow = (int) Math.round(diffLat / gradientLat); int dcol = (int) Math.round(diffLon / gradientLon); if (debug) System.out.printf(" jump from %d %d (grad=%f %f) (diff=%f %f) (delta=%d %d)", row, col, gradientLat, gradientLon, diffLat, diffLon, drow, dcol); if ((drow == 0) && (dcol == 0)) { if (debug) System.out.printf("%n incr:"); return incr(wantLat, wantLon, rectIndex); } else { rectIndex[0] = Math.max( Math.min(row + drow, nrows-1), 0); rectIndex[1] = Math.max( Math.min(col + dcol, ncols-1), 0); if (debug) System.out.printf(" to (%d %d)%n", rectIndex[0], rectIndex[1]); if ((row == rectIndex[0]) && (col == rectIndex[1])) return false; // nothing has changed } return true; }
public boolean findCoordElement(double wantLat, double wantLon, int[] rectIndex) { return findCoordElementNoForce(wantLat, wantLon,rectIndex); }
private boolean box9(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int minrow = Math.max(row-1, 0); int maxrow = Math.min(row+1, nrows); int col = rectIndex[1]; int mincol= Math.max(col-1, 0); int maxcol = Math.min(col+1, ncols); if (debug) System.out.printf("%n box9:"); for (int i=minrow; i<=maxrow; i++) for (int j=mincol; j<=maxcol; j++) { rectIndex[0] = i; rectIndex[1] = j; if (contains(wantLat, wantLon, rectIndex)) return true; } return false; }
boolean sign = detIsPositive(x1, y1, x2, y2, wantLon, wantLat); if (sign != detIsPositive(x2, y2, x3, y3, wantLon, wantLat)) return false; if (sign != detIsPositive(x3, y3, x4, y4, wantLon, wantLat)) return false; if (sign != detIsPositive(x4, y4, x1, y1, wantLon, wantLat)) return false;
public static void test1() throws IOException { String filename = "D:/work/asaScience/EGM200_3.ncml"; GridDataset gds = GridDataset.open(filename); GeoGrid grid = gds.findGridByName("u_wind"); GridCoordSystem gcs = grid.getCoordinateSystem(); CoordinateAxis lonAxis = gcs.getXHorizAxis(); assert lonAxis instanceof CoordinateAxis2D; CoordinateAxis latAxis = gcs.getYHorizAxis(); assert latAxis instanceof CoordinateAxis2D; GridCoordinate2D g2d = new GridCoordinate2D((CoordinateAxis2D) latAxis, (CoordinateAxis2D) lonAxis); doOne(g2d, 35.0, -6.0); doOne(g2d, 34.667302, -5.008376); // FAIL doOne(g2d, 34.667303, -6.394240); doOne(g2d, 36.6346, -5.0084); doOne(g2d, 36.6346, -6.394240); gds.close(); }
/** * Given a point in x,y coordinate space, find the x,y index in the coordinate system. * If outside the range, the closest point is returned, eg, 0 or n-1 depending on if the coordinate is too small or too large. * * @param x_coord position in x coordinate space. * @param y_coord position in y coordinate space. * @param result put result in here, may be null * @return int[2], 0=x,1=y indices in the coordinate system of the point. */ @Override public int[] findXYindexFromCoordBounded(double x_coord, double y_coord, int[] result) { if (result == null) result = new int[2]; if ((horizXaxis instanceof CoordinateAxis1D) && (horizYaxis instanceof CoordinateAxis1D)) { result[0] = ((CoordinateAxis1D) horizXaxis).findCoordElementBounded(x_coord); result[1] = ((CoordinateAxis1D) horizYaxis).findCoordElementBounded(y_coord); return result; } else if ((horizXaxis instanceof CoordinateAxis2D) && (horizYaxis instanceof CoordinateAxis2D)) { if (g2d == null) g2d = new GridCoordinate2D((CoordinateAxis2D) horizYaxis, (CoordinateAxis2D) horizXaxis); int[] result2 = new int[2]; g2d.findCoordElement(y_coord, x_coord, result2); // returns best guess result[0] = result2[1]; result[1] = result2[0]; return result; } // cant happen throw new IllegalStateException("GridCoordSystem.findXYindexFromCoord"); }
public boolean findCoordElementForce(double wantLat, double wantLon, int[] rectIndex) { findBounds(); if (wantLat < latMinMax.min) return false; if (wantLat > latMinMax.max) return false; if (wantLon < lonMinMax.min) return false; if (wantLon > lonMinMax.max) return false; boolean saveDebug = debug; debug = false; for (int row=0; row<nrows; row++) { for (int col=0; col<ncols; col++) { rectIndex[0] = row; rectIndex[1] = col; if (contains(wantLat, wantLon, rectIndex)) { debug = saveDebug; return true; } } } //debug = saveDebug; return false; }
private boolean jumpOld(double wantLat, double wantLon, int[] rectIndex) { int row = Math.max( Math.min(rectIndex[0], nrows-1), 0); int col = Math.max( Math.min(rectIndex[1], ncols-1), 0); double gradientLat = latEdge.get(row + 1, col) - latEdge.get(row, col); double gradientLon = lonEdge.get(row, col + 1) - lonEdge.get(row, col); double diffLat = wantLat - latEdge.get(row, col); double diffLon = wantLon - lonEdge.get(row, col); int drow = (int) Math.round(diffLat / gradientLat); int dcol = (int) Math.round(diffLon / gradientLon); if (debug) System.out.printf(" jump from %d %d (grad=%f %f) (diff=%f %f) (delta=%d %d)", row, col, gradientLat, gradientLon, diffLat, diffLon, drow, dcol); if ((drow == 0) && (dcol == 0)) { if (debug) System.out.printf("%n incr:"); return incr(wantLat, wantLon, rectIndex); } else { rectIndex[0] = Math.max( Math.min(row + drow, nrows-1), 0); rectIndex[1] = Math.max( Math.min(col + dcol, ncols-1), 0); if (debug) System.out.printf(" to (%d %d)%n", rectIndex[0], rectIndex[1]); if ((row == rectIndex[0]) && (col == rectIndex[1])) return false; // nothing has changed } return true; }
public boolean findCoordElement(double wantLat, double wantLon, int[] rectIndex) { return findCoordElementNoForce(wantLat, wantLon,rectIndex); }
private boolean box9(double wantLat, double wantLon, int[] rectIndex) { int row = rectIndex[0]; int minrow = Math.max(row-1, 0); int maxrow = Math.min(row+1, nrows); int col = rectIndex[1]; int mincol= Math.max(col-1, 0); int maxcol = Math.min(col+1, ncols); if (debug) System.out.printf("%n box9:"); for (int i=minrow; i<=maxrow; i++) for (int j=mincol; j<=maxcol; j++) { rectIndex[0] = i; rectIndex[1] = j; if (contains(wantLat, wantLon, rectIndex)) return true; } return false; }
boolean sign = detIsPositive(x1, y1, x2, y2, wantLon, wantLat); if (sign != detIsPositive(x2, y2, x3, y3, wantLon, wantLat)) return false; if (sign != detIsPositive(x3, y3, x4, y4, wantLon, wantLat)) return false; if (sign != detIsPositive(x4, y4, x1, y1, wantLon, wantLat)) return false;
findBounds(); if (wantLat < latMinMax.min) return false; if (wantLat > latMinMax.max) return false; count++; if (debug) System.out.printf("%nIteration %d %n", count); if (contains(wantLat, wantLon, rectIndex)) return true; if (!jump2(wantLat, wantLon, rectIndex)) return false; return incr(wantLat, wantLon, rectIndex);
public static void test1() throws IOException { String filename = "D:/work/asaScience/EGM200_3.ncml"; GridDataset gds = GridDataset.open(filename); GeoGrid grid = gds.findGridByName("u_wind"); GridCoordSystem gcs = grid.getCoordinateSystem(); CoordinateAxis lonAxis = gcs.getXHorizAxis(); assert lonAxis instanceof CoordinateAxis2D; CoordinateAxis latAxis = gcs.getYHorizAxis(); assert latAxis instanceof CoordinateAxis2D; GridCoordinate2D g2d = new GridCoordinate2D((CoordinateAxis2D) latAxis, (CoordinateAxis2D) lonAxis); doOne(g2d, 35.0, -6.0); doOne(g2d, 34.667302, -5.008376); // FAIL doOne(g2d, 34.667303, -6.394240); doOne(g2d, 36.6346, -5.0084); doOne(g2d, 36.6346, -6.394240); gds.close(); }
/** * Given a point in x,y coordinate space, find the x,y index in the coordinate system. * If outside the range, the closest point is returned, eg, 0 or n-1 depending on if the coordinate is too small or too large. * * @param x_coord position in x coordinate space. * @param y_coord position in y coordinate space. * @param result put result in here, may be null * @return int[2], 0=x,1=y indices in the coordinate system of the point. */ @Override public int[] findXYindexFromCoordBounded(double x_coord, double y_coord, int[] result) { if (result == null) result = new int[2]; if ((horizXaxis instanceof CoordinateAxis1D) && (horizYaxis instanceof CoordinateAxis1D)) { result[0] = ((CoordinateAxis1D) horizXaxis).findCoordElementBounded(x_coord); result[1] = ((CoordinateAxis1D) horizYaxis).findCoordElementBounded(y_coord); return result; } else if ((horizXaxis instanceof CoordinateAxis2D) && (horizYaxis instanceof CoordinateAxis2D)) { if (g2d == null) g2d = new GridCoordinate2D((CoordinateAxis2D) horizYaxis, (CoordinateAxis2D) horizXaxis); int[] result2 = new int[2]; g2d.findCoordElement(y_coord, x_coord, result2); // returns best guess result[0] = result2[1]; result[1] = result2[0]; return result; } // cant happen throw new IllegalStateException("GridCoordSystem.findXYindexFromCoord"); }