@Override public int hashCode() { return grid.hashCode(); }
/** * Performs a NTv2 grid file lookup given its name, and checks for file format correctness. * * @param name The NTv2 grid file name * @return {@code true} if file exists and is valid, {@code false} otherwise */ public boolean isNTv2Grid(URL location) { if (location != null) { return isNTv2GridFileValid(location); // Check } else { return false; } }
@Override public boolean equals(Object object) { if (object == this) { // Slight optimization return true; } if (super.equals(object)) { final NADCONTransform that = (NADCONTransform) object; return this.grid.equals(that.grid); } else { return false; } }
if (((x < grid.getMinX()) || (x > grid.getMaxX())) || ((y < grid.getMinY()) || (y > grid.getMaxY()))) { throw new TransformException( "Point (" + y + ") is not outside of ((" + grid.getMinX() + " " + grid.getMinY() + ")(" + grid.getMaxX() + " " + grid.getMaxY() + "))"); final double xgrid = (x - grid.getMinX()) / grid.getDx(); final double ygrid = (y - grid.getMinY()) / grid.getDy(); double[] array = new double[] {xgrid, ygrid};
latChannel = getReadChannel(latGridUrl); latBuffer = fillBuffer(latChannel, HEADER_BYTES); longChannel = getReadChannel(longGridUrl); longBuffer = fillBuffer(longChannel, HEADER_BYTES); final int START_OF_DATA = RECORD_LENGTH - HEADER_BYTES; latBuffer = fillBuffer(latChannel, NUM_BYTES_LEFT); longBuffer = fillBuffer(longChannel, NUM_BYTES_LEFT); longBuffer.position(START_OF_DATA); gridShift = new NADConGridShift(xmin, ymin, xmax, ymax, dx, dy, nc, nr); gridShift.setLocalizationPoint( j, i, longBuffer.getFloat(), latBuffer.getFloat());
/** * Constructs a {@code NADCONTransform} from the specified grid shift files. * * @param latGridName path and name (or just name if {@link #GRID_LOCATION} is set) to the * latitude difference file. This will have a {@code .las} or {@code .laa} file extention. * @param longGridName path and name (or just name if {@link #GRID_LOCATION} is set) to the * longitude difference file. This will have a {@code .los} or {@code .loa} file extention. * @throws ParameterNotFoundException if a math transform parameter cannot be found. * @throws FactoryException if there is a problem creating this math transform (ie file * extentions are unknown or there is an error reading the grid files) */ public NADCONTransform(final URI latGridName, final URI longGridName) throws ParameterNotFoundException, FactoryException { if (latGridName == null) { throw new NoSuchIdentifierException("Latitud grid shift file name is null", null); } if (longGridName == null) { throw new NoSuchIdentifierException("Latitud grid shift file name is null", null); } this.latGridName = latGridName; this.longGridName = longGridName; URL latGridURL = locateGrid(latGridName); URL longGridURL = locateGrid(longGridName); this.grid = FACTORY.loadGridShift(latGridURL, longGridURL); this.gridShiftTransform = grid.getMathTransform(); }
@Test public void testReleaseGrids() throws IOException, FactoryException { File gridShifts = new File("src/test/resources/org/geotools/referencing/factory/gridshift"); File las = new File(gridShifts, "stpaul.las"); File los = new File(gridShifts, "stpaul.los"); File tlas = new File("./target/stpaul.las"); File tlos = new File("./target/stpaul.los"); copyFile(las, tlas); copyFile(los, tlos); NADCONGridShiftFactory factory = new NADCONGridShiftFactory(); NADConGridShift shift = factory.loadGridShift(URLs.fileToUrl(tlas), URLs.fileToUrl(tlos)); // minor checks on the grid assertNotNull(shift); // now the good part, try to delete the files, on windows this will fail // unless the sources were properly closed assertTrue(tlas.delete()); assertTrue(tlos.delete()); }
/** * Test method for {@link * org.geotools.referencing.factory.gridshift.NTv2GridShiftFactory#isNTv2Grid(java.lang.String)}. */ @Test public void testIsNTv2GridAvailable() { assertFalse(factory.isNTv2Grid(null)); assertFalse(factory.isNTv2Grid(INEXISTENT_GRID)); // Will log a FINEST message assertFalse(factory.isNTv2Grid(MALFORMED_GRID)); // Will log a WARNING message assertTrue(factory.isNTv2Grid(TEST_GRID)); }
factory.createNTv2Grid(null); } catch (FactoryException e) { factoryExceptionThrown = true; factory.createNTv2Grid(INEXISTENT_GRID); // Will log a FINEST message } catch (FactoryException e) { factoryExceptionThrown = true; factory.createNTv2Grid(MALFORMED_GRID); // Will log a SEVERE message } catch (FactoryException e) { factoryExceptionThrown = true; GridShiftFile grid = null; try { grid = factory.createNTv2Grid(TEST_GRID); } catch (FactoryException e) { factoryExceptionThrown = true;
public NADConGridShift loadGridShift(URL latGridURL, URL longGridURL) throws FactoryException { NADCONKey key = new NADCONKey(latGridURL.toExternalForm(), longGridURL.toExternalForm()); synchronized (gridCache) { // Prevent simultaneous threads trying to load same grid NADConGridShift grid = gridCache.get(key); if (grid != null) { // Cached: return grid; // - Return } else { // Not cached: grid = loadGridShiftInternal(latGridURL, longGridURL); // - Load if (grid != null) { gridCache.put(key, grid); // - Cache return grid; // - Return } } throw new FactoryException( "NTv2 Grid " + latGridURL + ", " + longGridURL + " could not be created."); } }
private NADConGridShift loadGridShiftInternal(URL latGridURL, URL longGridURL) throws FactoryException { // decide if text or binary grid will be used String latGridName = URLs.urlToFile(latGridURL).getPath(); String longGridName = URLs.urlToFile(longGridURL).getPath(); try { if ((latGridName.endsWith(".las") && longGridName.endsWith(".los")) || (latGridName.endsWith(".LAS") && longGridName.endsWith(".LOS"))) { return loadBinaryGrid(latGridURL, longGridURL); } else if ((latGridName.endsWith(".laa") && longGridName.endsWith(".loa")) || (latGridName.endsWith(".LAA") && longGridName.endsWith(".LOA"))) { return loadTextGrid(latGridURL, longGridURL); } else { throw new FactoryException( Errors.format( ErrorKeys.UNSUPPORTED_FILE_TYPE_$2, latGridName.substring(latGridName.lastIndexOf('.') + 1), longGridName.substring(longGridName.lastIndexOf('.') + 1))); // Note: the +1 above hide the dot, but also make sure that the code is // valid even if the path do not contains '.' at all (-1 + 1 == 0). } } catch (IOException exception) { final Throwable cause = exception.getCause(); if (cause instanceof FactoryException) { throw (FactoryException) cause; } throw new FactoryException(exception.getLocalizedMessage(), exception); } }
NADConGridShift gridShift = new NADConGridShift(xmin, ymin, xmax, ymax, dx, dy, nc, nr); gridShift.setLocalizationPoint( j, i,
URL locateGrid(String grid) { for (GridShiftLocator locator : ReferencingFactoryFinder.getGridShiftLocators(null)) { URL result = locator.locateGrid(grid); if (result != null) { return result; } } return null; }
/** * Returns a new bytebuffer, of numBytes length and little endian byte order, filled from the * channel. * * @param channel the channel to fill the buffer from * @param numBytes number of bytes to read * @return a new bytebuffer filled from the channel * @throws IOException if there is a problem reading the channel * @throws EOFException if the end of the channel is reached */ private ByteBuffer fillBuffer(ReadableByteChannel channel, int numBytes) throws IOException { ByteBuffer buf = ByteBuffer.allocate(numBytes); if (fill(buf, channel) == -1) { throw new EOFException(Errors.format(ErrorKeys.END_OF_DATA_FILE)); } buf.flip(); buf.order(ByteOrder.LITTLE_ENDIAN); return buf; }
/** * Instantiates the test {@link NTv2GridShiftFactory} * * @throws java.lang.Exception */ @Before public void setUp() throws Exception { factory = new NTv2GridShiftFactory(); }
/** * Creates a NTv2 Grid. * * @param name The NTv2 grid name * @return the grid * @throws FactoryException if grid cannot be created */ public GridShiftFile createNTv2Grid(URL gridLocation) throws FactoryException { if (gridLocation == null) { throw new FactoryException("The grid location must be not null"); } synchronized (ntv2GridCache) { // Prevent simultaneous threads trying to load same grid GridShiftFile grid = ntv2GridCache.get(gridLocation.toExternalForm()); if (grid != null) { // Cached: return grid; // - Return } else { // Not cached: if (gridLocation != null) { grid = loadNTv2Grid(gridLocation); // - Load if (grid != null) { ntv2GridCache.put(gridLocation.toExternalForm(), grid); // - Cache return grid; // - Return } } throw new FactoryException("NTv2 Grid " + gridLocation + " could not be created."); } } }
/** * Constructs a {@code NTv2Transform} from the specified grid shift file. * * <p>This constructor checks for grid shift file availability, but doesn't actually load the * full grid into memory to preserve resources. * * @param file NTv2 grid file name * @throws NoSuchIdentifierException if the grid is not available. */ public NTv2Transform(URI file) throws NoSuchIdentifierException { if (file == null) { throw new NoSuchIdentifierException("No NTv2 Grid File specified.", null); } this.grid = file; gridLocation = locateGrid(grid.toString()); if (gridLocation == null) { throw new NoSuchIdentifierException("Could not locate NTv2 Grid File " + file, null); } // Search for grid file if (!FACTORY.isNTv2Grid(gridLocation)) { throw new NoSuchIdentifierException("NTv2 Grid File not available.", file.toString()); } }
gridShift = FACTORY.createNTv2Grid(gridLocation); } catch (FactoryException e) { throw new TransformException(
protected URL locateGrid(URI uri) throws FactoryException { String grid = uri.toString(); for (GridShiftLocator locator : ReferencingFactoryFinder.getGridShiftLocators(null)) { URL result = locator.locateGrid(grid); if (result != null) { return result; } } throw new FactoryException("Could not locate grid file " + grid); }