Search in sources :

Example 1 with GeoTiffFormat

use of org.geotools.gce.geotiff.GeoTiffFormat in project OpenTripPlanner by opentripplanner.

the class Renderer method generateStreamingGeotiffResponse.

private static Response generateStreamingGeotiffResponse(final GridCoverage2D coverage) {
    StreamingOutput streamingOutput = new StreamingOutput() {

        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);
            }
        }
    };
    CacheControl cc = new CacheControl();
    cc.setMaxAge(3600);
    cc.setNoCache(false);
    return Response.ok(streamingOutput).type("image/geotiff").cacheControl(cc).build();
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) GeoTiffFormat(org.geotools.gce.geotiff.GeoTiffFormat) WebApplicationException(javax.ws.rs.WebApplicationException) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) OutputStream(java.io.OutputStream) GeoTiffWriteParams(org.geotools.gce.geotiff.GeoTiffWriteParams) StreamingOutput(javax.ws.rs.core.StreamingOutput) GeoTiffWriter(org.geotools.gce.geotiff.GeoTiffWriter) CacheControl(javax.ws.rs.core.CacheControl) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 2 with GeoTiffFormat

use of org.geotools.gce.geotiff.GeoTiffFormat in project OpenTripPlanner by opentripplanner.

the class RasterPopulation method writeGeotiff.

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.");
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) GeoTiffFormat(org.geotools.gce.geotiff.GeoTiffFormat) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) GeoTiffWriteParams(org.geotools.gce.geotiff.GeoTiffWriteParams) GeoTiffWriter(org.geotools.gce.geotiff.GeoTiffWriter) File(java.io.File)

Example 3 with GeoTiffFormat

use of org.geotools.gce.geotiff.GeoTiffFormat in project OpenTripPlanner by opentripplanner.

the class GeotiffGridCoverageFactoryImpl method getGridCoverage.

@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;
}
Also used : Hints(org.geotools.factory.Hints) GeoTiffFormat(org.geotools.gce.geotiff.GeoTiffFormat) GeoTiffReader(org.geotools.gce.geotiff.GeoTiffReader) IOException(java.io.IOException)

Aggregations

GeoTiffFormat (org.geotools.gce.geotiff.GeoTiffFormat)3 GeoTiffWriteParams (org.geotools.gce.geotiff.GeoTiffWriteParams)2 GeoTiffWriter (org.geotools.gce.geotiff.GeoTiffWriter)2 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)2 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)2 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 CacheControl (javax.ws.rs.core.CacheControl)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 Hints (org.geotools.factory.Hints)1 GeoTiffReader (org.geotools.gce.geotiff.GeoTiffReader)1