Search in sources :

Example 1 with GrassCoverageReader

use of org.geotools.gce.grassraster.GrassCoverageReader in project hortonmachine by TheHortonMachine.

the class ImageGenerator method setLayers.

// private void setTransforms( final ReferencedEnvelope envelope, int width, int height ) {
// 
// double envWidth = envelope.getWidth();
// double envHeight = envelope.getHeight();
// double envValue = envWidth;
// if (envHeight > envWidth) {
// envValue = envHeight;
// }
// 
// double xscale = width / envValue;
// double yscale = height / envValue;
// 
// double median0 = envelope.getMedian(0);
// double xoff = median0 * xscale - width / 2.0;
// double median1 = envelope.getMedian(1);
// double yoff = median1 * yscale + height / 2.0;
// 
// worldToScreen = new AffineTransform(xscale, 0, 0, -yscale, -xoff, yoff);
// }
/**
 * Set the layers that have to be drawn.
 *
 * <p><b>This has to be called before the drawing process.</p>
 * @return the max envelope of the data.
 * @throws Exception
 */
public ReferencedEnvelope setLayers() throws Exception {
    ReferencedEnvelope maxExtent = null;
    // wms first
    if (wmsURL != null) {
        String[] split = wmsURL.split("#");
        WebMapServer server = new WebMapServer(new URL(split[0]));
        org.geotools.ows.wms.Layer wmsLayer = getWMSLayer(server, split[1]);
        WMSLayer layer = new WMSLayer(server, wmsLayer);
        layers.add(layer);
        ReferencedEnvelope originalEnvelope = layer.getBounds();
        if (originalEnvelope != null) {
            if (maxExtent == null) {
                maxExtent = new ReferencedEnvelope(originalEnvelope.getCoordinateReferenceSystem());
            }
            expandToIncludeEnvelope(maxExtent, originalEnvelope);
        }
    }
    // coverages
    monitor.beginTask("Reading raster maps...", coveragePaths.size());
    for (int r = 0; r < coveragePaths.size(); r++) {
        String coveragePath = coveragePaths.get(r);
        GridGeometry2D region = null;
        if (coverageRegions != null && coverageRegions.size() == coveragePaths.size()) {
            region = coverageRegions.get(r);
        }
        File file = new File(coveragePath);
        GridCoverage2D raster = null;
        AbstractGridCoverage2DReader reader = null;
        try {
            try {
                // first try a format that gives back a reader
                AbstractGridFormat format = GridFormatFinder.findFormat(file);
                reader = format.getReader(file);
                if (reader instanceof GrassCoverageReader) {
                    reader = null;
                }
            } catch (Exception e1) {
            // ignore and try others
            }
            if (reader == null) {
                if (region == null) {
                    raster = OmsRasterReader.readRaster(coveragePath);
                } else {
                    RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(region);
                    double n = regionMap.getNorth();
                    double s = regionMap.getSouth();
                    double w = regionMap.getWest();
                    double e = regionMap.getEast();
                    double xres = regionMap.getXres();
                    double yres = regionMap.getYres();
                    OmsRasterReader rreader = new OmsRasterReader();
                    rreader.file = coveragePath;
                    rreader.pNorth = n;
                    rreader.pSouth = s;
                    rreader.pWest = w;
                    rreader.pEast = e;
                    rreader.pXres = xres;
                    rreader.pYres = yres;
                    rreader.process();
                    raster = rreader.outRaster;
                }
            }
        // if (crs == null) {
        // crs = raster.getCoordinateReferenceSystem();
        // }
        } catch (Exception e) {
            monitor.errorMessage(e.getLocalizedMessage());
            monitor.errorMessage("Trying to find other coverage source...");
            // try with available readers
            try {
                AbstractGridFormat format = GridFormatFinder.findFormat(file);
                reader = format.getReader(file);
            // if (crs == null) {
            // crs = reader.getCrs();
            // }
            } catch (Exception ex) {
                throw ex;
            }
        }
        File styleFile = FileUtilities.substituteExtention(file, "sld");
        Style style;
        if (styleFile.exists()) {
            style = SldUtilities.getStyleFromFile(styleFile);
        } else {
            style = SldUtilities.getStyleFromRasterFile(styleFile);
        }
        if (raster != null) {
            GridCoverageLayer layer = new GridCoverageLayer(raster, style);
            layers.add(layer);
            org.opengis.geometry.Envelope envelope = raster.getEnvelope();
            if (maxExtent == null) {
                maxExtent = new ReferencedEnvelope(envelope.getCoordinateReferenceSystem());
            }
            expandToIncludeEnvelope(maxExtent, envelope);
        }
        if (reader != null) {
            GridReaderLayer layer = new GridReaderLayer(reader, style);
            // SimpleFeatureSource featureSource = layer.getFeatureSource();
            layers.add(layer);
            org.opengis.geometry.Envelope envelope = reader.getOriginalEnvelope();
            if (maxExtent == null) {
                maxExtent = new ReferencedEnvelope(envelope.getCoordinateReferenceSystem());
            }
            expandToIncludeEnvelope(maxExtent, envelope);
        }
        monitor.worked(1);
    }
    monitor.done();
    monitor.beginTask("Reading vector maps...", featurePaths.size());
    for (int i = 0; i < featurePaths.size(); i++) {
        String featurePath = featurePaths.get(i);
        String filter = featureFilter.get(i);
        FileDataStore store = FileDataStoreFinder.getDataStore(new File(featurePath));
        SimpleFeatureSource featureSource = store.getFeatureSource();
        SimpleFeatureCollection featureCollection;
        if (filter.length() == 0) {
            featureCollection = featureSource.getFeatures();
        } else {
            featureCollection = featureSource.getFeatures(ECQL.toFilter(filter));
        }
        // if (crs == null) {
        // crs = featureSource.getSchema().getCoordinateReferenceSystem();
        // }
        File styleFile = FileUtilities.substituteExtention(new File(featurePath), "sld");
        Style style;
        if (styleFile.exists()) {
            style = SldUtilities.getStyleFromFile(styleFile);
        } else {
            style = SLD.createSimpleStyle(featureSource.getSchema());
        }
        FeatureLayer layer = new FeatureLayer(featureCollection, style);
        layers.add(layer);
        if (maxExtent == null) {
            maxExtent = new ReferencedEnvelope(featureCollection.getSchema().getCoordinateReferenceSystem());
        }
        expandToIncludeEnvelope(maxExtent, featureCollection.getBounds());
        monitor.worked(1);
    }
    synchronizedLayers = Collections.synchronizedList(layers);
    monitor.done();
    return maxExtent;
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) RegionMap(org.hortonmachine.gears.utils.RegionMap) GridCoverageLayer(org.geotools.map.GridCoverageLayer) URL(java.net.URL) WMSLayer(org.geotools.ows.wms.map.WMSLayer) GridReaderLayer(org.geotools.map.GridReaderLayer) FeatureLayer(org.geotools.map.FeatureLayer) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) WebMapServer(org.geotools.ows.wms.WebMapServer) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) Style(org.geotools.styling.Style) FileDataStore(org.geotools.data.FileDataStore) GrassCoverageReader(org.geotools.gce.grassraster.GrassCoverageReader) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) OmsRasterReader(org.hortonmachine.gears.io.rasterreader.OmsRasterReader) FactoryException(org.opengis.referencing.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) File(java.io.File)

Example 2 with GrassCoverageReader

use of org.geotools.gce.grassraster.GrassCoverageReader in project hortonmachine by TheHortonMachine.

the class OmsRasterReader method readGrass.

private void readGrass(File mapFile) throws Exception {
    JGrassMapEnvironment mapEnvironment = new JGrassMapEnvironment(new File(file));
    CoordinateReferenceSystem crs = mapEnvironment.getCoordinateReferenceSystem();
    JGrassRegion readRegion = mapEnvironment.getFileRegion();
    double n = readRegion.getNorth();
    double s = readRegion.getSouth();
    double w = readRegion.getWest();
    double e = readRegion.getEast();
    Envelope env = readRegion.getEnvelope();
    originalEnvelope = new GeneralEnvelope(new ReferencedEnvelope(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), crs));
    // if bounds supplied, use them as region
    if (pBounds != null) {
        // n, s, w, e
        n = pBounds[0];
        s = pBounds[1];
        w = pBounds[2];
        e = pBounds[3];
    }
    if (pRes != null) {
        readRegion = new JGrassRegion(w, e, s, n, pRes[0], pRes[1]);
    }
    if (pRowcol != null) {
        readRegion = new JGrassRegion(w, e, s, n, pRowcol[0], pRowcol[1]);
    }
    if (!doEnvelope) {
        if (generalParameter == null) {
            generalParameter = createGridGeometryGeneralParameter(readRegion.getCols(), readRegion.getRows(), readRegion.getNorth(), readRegion.getSouth(), readRegion.getEast(), readRegion.getWest(), crs);
        }
        GrassCoverageFormat format = new GrassCoverageFormatFactory().createFormat();
        GrassCoverageReader reader = format.getReader(mapEnvironment.getCELL());
        outRaster = (GridCoverage2D) reader.read(generalParameter);
    }
}
Also used : JGrassMapEnvironment(org.geotools.gce.grassraster.JGrassMapEnvironment) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) JGrassRegion(org.geotools.gce.grassraster.JGrassRegion) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.geotools.geometry.GeneralEnvelope) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) GeneralEnvelope(org.geotools.geometry.GeneralEnvelope) GrassCoverageFormatFactory(org.geotools.gce.grassraster.format.GrassCoverageFormatFactory) File(java.io.File) GrassCoverageReader(org.geotools.gce.grassraster.GrassCoverageReader) GrassCoverageFormat(org.geotools.gce.grassraster.format.GrassCoverageFormat)

Aggregations

File (java.io.File)2 GrassCoverageReader (org.geotools.gce.grassraster.GrassCoverageReader)2 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)2 IOException (java.io.IOException)1 URL (java.net.URL)1 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)1 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)1 AbstractGridCoverage2DReader (org.geotools.coverage.grid.io.AbstractGridCoverage2DReader)1 AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)1 FileDataStore (org.geotools.data.FileDataStore)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 JGrassMapEnvironment (org.geotools.gce.grassraster.JGrassMapEnvironment)1 JGrassRegion (org.geotools.gce.grassraster.JGrassRegion)1 GrassCoverageFormat (org.geotools.gce.grassraster.format.GrassCoverageFormat)1 GrassCoverageFormatFactory (org.geotools.gce.grassraster.format.GrassCoverageFormatFactory)1 GeneralEnvelope (org.geotools.geometry.GeneralEnvelope)1 FeatureLayer (org.geotools.map.FeatureLayer)1 GridCoverageLayer (org.geotools.map.GridCoverageLayer)1 GridReaderLayer (org.geotools.map.GridReaderLayer)1