/** * 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; }
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);
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); } }
@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()); }
/** * 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(); }
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."); } }