@Override public GridCoverage2D getGridCoverage() { try { // There is a serious standardization failure around the axis order of WGS84. See issue #1930. // GeoTools assumes strict EPSG axis order of (latitude, longitude) unless told otherwise. // Both NED and SRTM data use the longitude-first axis order, so OTP makes grid coverages // for unprojected DEMs assuming coordinates are in (longitude, latitude) order. Hints forceLongLat = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE); GeoTiffFormat format = new GeoTiffFormat(); GeoTiffReader reader = format.getReader(path, forceLongLat); coverage = reader.read(null); LOG.info("Elevation model CRS is: {}", coverage.getCoordinateReferenceSystem2D()); } catch (IOException e) { throw new RuntimeException("Error getting coverage automatically. ", e); } return coverage; }
public void write(OutputStream outStream) { try { long t0 = System.currentTimeMillis(); GeoTiffWriteParams wp = new GeoTiffWriteParams(); wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); wp.setCompressionType("LZW"); ParameterValueGroup params = new GeoTiffFormat().getWriteParameters(); params.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp); new GeoTiffWriter(outStream).write(coverage, (GeneralParameterValue[]) params.values().toArray(new GeneralParameterValue[1])); //new GeoTiffWriter(outStream).write(coverage, null); //wasn't this line writing twice and trashing compressed version? long t1 = System.currentTimeMillis(); LOG.debug("wrote geotiff in {}msec", t1-t0); } catch (Exception e) { LOG.error("exception while preparing geotiff : {}", e.getMessage()); throw new WebApplicationException(e); } } };
/** Returns the original source file, is present in the metadata, and if the coverage */ private File getSourceFile(GridCoverage2D coverage) { final Object fileSource = coverage.getProperty(AbstractGridCoverage2DReader.FILE_SOURCE_PROPERTY); if (fileSource != null && fileSource instanceof String) { File file = new File((String) fileSource); if (file.exists()) { GeoTiffReader reader = null; try { reader = new GeoTiffReader(file); GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope(); Envelope envelope = coverage.getEnvelope(); if (originalEnvelope.equals(envelope, 1e-9, false)) { GridCoverage2D test = reader.read(null); ImageUtilities.disposeImage(test.getRenderedImage()); return file; } } catch (Exception e) { // ok, not a geotiff! } finally { if (reader != null) { reader.dispose(); } } } } return null; }
GridCoverage2D test = factory.create("test", bi, envelope); ByteArrayOutputStream bos = new ByteArrayOutputStream(); GeoTiffWriter writer = new GeoTiffWriter(bos); writer.write(test, null); writer.dispose();
if (compressionS != null && !compressionS.equalsIgnoreCase("none")) { if (compressionS.equals("LZW")) { wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); wp.setCompressionType("LZW"); if (predictorS != null) { if (predictorS.equals("Horizontal")) { wp.setTIFFCompressor( new TIFFLZWCompressor( BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING)); wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); wp.setCompressionType("JPEG"); wp.setCompressionQuality(DEFAULT_JPEG_COMPRESSION_QUALITY); final int quality = Integer.valueOf(quality_); if (quality > 0 && quality <= 100) { wp.setCompressionQuality(quality / 100.f); } else { wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); wp.setCompressionType("PackBits"); } else if (compressionS.equals("DEFLATE") || compressionS.equals("Deflate")) { wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); wp.setCompressionType("Deflate"); } else if (compressionS.equals("Huffman")) { wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); wp.setCompressionType("CCITT RLE");
private GeoTiffWriteParams buildWriteParams(GridCoverage2D coverage) { final RenderedImage renderedImage = coverage.getRenderedImage(); int tileWidth = renderedImage.getTileWidth(); int tileHeight = renderedImage.getTileHeight(); // avoid tiles bigger than the image final GridEnvelope gr = coverage.getGridGeometry().getGridRange(); if (gr.getSpan(0) < tileWidth) { tileWidth = gr.getSpan(0); } if (gr.getSpan(1) < tileHeight) { tileHeight = gr.getSpan(1); } GeoTiffWriteParams writeParams = new GeoTiffWriteParams(); writeParams.setTilingMode(GeoToolsWriteParams.MODE_EXPLICIT); writeParams.setTiling(tileWidth, tileHeight); return writeParams; }
private void initReaderFromFile(Hints inHints) throws IOException { if (!this.dataDirectory.isDirectory()) { throw new IOException(this.dataDirectory + " is not a directory"); } File dataFile = null; for (String filename : this.dataDirectory.list()) { if (isDataFile(filename)) { dataFile = new File(this.dataDirectory, filename); break; } } if (dataFile == null) { throw new IOException("No data file found"); } File clazzFile = new File(dataDirectory, "clazz"); if (clazzFile.exists()) { clazz = new String(Files.readAllBytes(clazzFile.toPath())).trim(); } final GeoTiffReader geotiffReader = new GeoTiffReader(dataFile, hints); this.crs = geotiffReader.getCoordinateReferenceSystem(); this.originalGridRange = geotiffReader.getOriginalGridRange(); this.originalEnvelope = geotiffReader.getOriginalEnvelope(); }
/** @see org.opengis.coverage.grid.GridCoverageReader#getFormat() */ public Format getFormat() { return new GeoTiffFormat(); }
wp.setTilingMode(GeoToolsWriteParams.MODE_EXPLICIT); wp.setTiling(tileDimensions.width, tileDimensions.height);
/** * Creates and returns a new instance of the <CODE>GeoTiffFormat</CODE> class if the required * libraries are present. If JAI and JAI Image I/O are not present, will throw an <CODE> * UnsupportedOperationException</CODE>. * * @return <CODE>GeoTiffFormat</CODE> object. * @throws UnsupportedOperationException if this format is unavailable. */ public AbstractGridFormat createFormat() { if (!isAvailable()) { throw new UnsupportedOperationException( "The GeoTiff plugin requires the JAI and JAI ImageI/O libraries!"); } return new GeoTiffFormat(); }
/** * Retrieves a {@link GeoTiffWriter} or <code>null</code> if the provided <code>destination * </code> is suitable. * * <p>This file does not use hints in the construction of the geotiff reader. * * @param destination must be a GeoTiff File * @return a GeoTiffReader object initialized to the specified File. */ @Override public GridCoverageWriter getWriter(Object destination) { try { return new GeoTiffWriter(destination); } catch (IOException e) { if (LOGGER.isLoggable(Level.WARNING)) LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); return null; } }
RenderedImage ri = coverage.getRenderedImage(); int tileWidth, tileHeight; if (imageIoWriteParams.getTilingMode() == GeoToolsWriteParams.MODE_EXPLICIT) { tileWidth = imageIoWriteParams.getTileWidth(); tileHeight = imageIoWriteParams.getTileHeight(); } else { tileWidth = ri.getTileWidth(); if (imageIoWriteParams.getCompressionMode() == GeoToolsWriteParams.MODE_EXPLICIT) { compression = imageIoWriteParams.getCompressionType(); quality = imageIoWriteParams.getCompressionQuality(); (AbstractGridCoverageWriter) TIFF_FORMAT.getWriter(stream); if (writer == null) throw new ServiceException(
/** * Returns an instance of {@link GeoTiffWriteParams} for controlling an hypothetic writing * process. * * @return an instance of {@link GeoTiffWriteParams}. */ @Override public GeoToolsWriteParams getDefaultImageIOWriteParameters() { return new GeoTiffWriteParams(); } }
private ParameterValueGroup buildGeoToolsWriteParams(GeoTiffWriteParams writeParams) { final ParameterValueGroup wparams = TIFF_FORMAT.getWriteParameters(); wparams.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()) .setValue(writeParams); return wparams; }
@After public void close() { MapProjection.SKIP_SANITY_CHECKS = false; reader.dispose(); }
/** * If <CODE>source</CODE> is a file, this will return a reader object. This file does not use * hints in the construction of the geotiff reader. * * @param source must be a GeoTiff File * @return a GeoTiffReader object initialized to the specified File. */ @Override public GeoTiffReader getReader(Object source) { return getReader(source, null); }
/** @throws IOException */ static MapInfoFileReader parseMapInfoFile(Object source) throws IOException { if (source instanceof File) { final File sourceFile = ((File) source); File file2Parse = getSibling(sourceFile, ".tab"); if (file2Parse != null && file2Parse.exists()) { final MapInfoFileReader reader = new MapInfoFileReader(file2Parse); return reader; } } return null; }
@Override public PropertiesCollector collect(final GridCoverage2DReader gridCoverageReader) { String value = ((org.geotools.gce.geotiff.GeoTiffReader) gridCoverageReader) .getMetadata() .getAsciiTIFFTag("306"); if (value != null) { addMatch("" + value); } else { addMatch(""); } return this; }
public void writeGeotiff(String fileName, ResultSet results) { LOG.info("writing geotiff."); float[][] imagePixelData = new float[rows][cols]; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { int index = row * cols + col; float pixel = (float) (results.results[index]); if (unitySeconds > 0) pixel /= unitySeconds; imagePixelData[row][col] = pixel; } } GridCoverage2D coverage = new GridCoverageFactory().create("OTPAnalyst", imagePixelData, refEnvelope); try { GeoTiffWriteParams wp = new GeoTiffWriteParams(); wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); wp.setCompressionType("LZW"); ParameterValueGroup params = new GeoTiffFormat().getWriteParameters(); params.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp); GeoTiffWriter writer = new GeoTiffWriter(new File(fileName)); writer.write(coverage, (GeneralParameterValue[]) params.values().toArray(new GeneralParameterValue[1])); } catch (Exception e) { LOG.error("exception while writing geotiff.", e); } LOG.info("done writing geotiff."); }
public Format getFormat() { return new GeoTiffFormat(); }