Search in sources :

Example 6 with AbstractGridFormat

use of org.geotools.coverage.grid.io.AbstractGridFormat in project sldeditor by robward-scisys.

the class DetermineRasterFormat method choose.

/**
 * Get user to choose raster format.
 *
 * @param rasterFile the raster file
 * @param selectionPanel the selection panel
 * @return the abstract grid format
 */
public static AbstractGridFormat choose(File rasterFile, ChooseRasterFormatInterface selectionPanel) {
    if (rasterFile != null) {
        final Set<AbstractGridFormat> formats = GridFormatFinder.findFormats(rasterFile, GeoTools.getDefaultHints());
        if (formats.size() > 1) {
            if (selectionPanel != null) {
                AbstractGridFormat selectedFormat = selectionPanel.showPanel(formats);
                if (selectedFormat != null) {
                    return selectedFormat;
                }
            }
        }
        // otherwise just pick the first
        final Iterator<AbstractGridFormat> it = formats.iterator();
        if (it.hasNext()) {
            return it.next();
        }
    }
    return new UnknownFormat();
}
Also used : UnknownFormat(org.geotools.coverage.grid.io.UnknownFormat) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat)

Example 7 with AbstractGridFormat

use of org.geotools.coverage.grid.io.AbstractGridFormat in project sldeditor by robward-scisys.

the class RasterReader method createRasterSLDData.

/**
 * Creates the raster sld.
 *
 * @param rasterFile the raster file
 * @return the styled layer descriptor
 */
@Override
public SLDDataInterface createRasterSLDData(File rasterFile) {
    if (rasterFile == null) {
        return null;
    }
    StyledLayerDescriptor sld = null;
    AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
    AbstractGridCoverage2DReader reader = null;
    try {
        reader = format.getReader(rasterFile);
    } catch (UnsupportedOperationException e) {
        ConsoleManager.getInstance().error(this, Localisation.getField(RasterTool.class, "RasterReader.unknownFormat") + rasterFile.getAbsolutePath());
        return null;
    }
    BufferedImage img = null;
    try {
        img = ImageIO.read(rasterFile);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
        return null;
    }
    WritableRaster raster = img.getRaster();
    Style style = createRGBStyle(reader, raster);
    sld = sf.createStyledLayerDescriptor();
    NamedLayer namedLayer = sf.createNamedLayer();
    namedLayer.addStyle(style);
    sld.addStyledLayer(namedLayer);
    File sldFilename = ExternalFilenames.createSLDFilename(rasterFile);
    StyleWrapper styleWrapper = new StyleWrapper(sldFilename.getName());
    String sldContents = sldWriter.encodeSLD(null, sld);
    SLDData sldData = new SLDData(styleWrapper, sldContents);
    sldData.setSLDFile(sldFilename);
    sldData.setReadOnly(false);
    return sldData;
}
Also used : SLDData(com.sldeditor.common.data.SLDData) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) StyleWrapper(com.sldeditor.common.data.StyleWrapper) WritableRaster(java.awt.image.WritableRaster) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) Style(org.geotools.styling.Style) NamedLayer(org.geotools.styling.NamedLayer) File(java.io.File)

Example 8 with AbstractGridFormat

use of org.geotools.coverage.grid.io.AbstractGridFormat in project OpenTripPlanner by opentripplanner.

the class RasterPopulation method createIndividuals.

@Override
public void createIndividuals() {
    LOG.info("Loading population from raster file {}", sourceFilename);
    try {
        File rasterFile = new File(sourceFilename);
        // determine file format and CRS, then load raster
        AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
        AbstractGridCoverage2DReader reader = format.getReader(rasterFile);
        GridCoverage2D coverage = reader.read(null);
        this.coverageCRS = coverage.getCoordinateReferenceSystem();
        GridGeometry2D gridGeometry = coverage.getGridGeometry();
        GridEnvelope2D gridEnvelope = gridGeometry.getGridRange2D();
        gridGeometry.getGridToCRS();
        // because we may want to produce an empty raster rather than loading one, alongside the coverage we
        // store the row/col dimensions and the referenced envelope in the original coordinate reference system.
        this.cols = gridEnvelope.width;
        this.rows = gridEnvelope.height;
        this.createIndividuals0();
    } catch (Exception ex) {
        throw new IllegalStateException("Error loading population from raster file: ", ex);
    }
    LOG.info("Done loading raster from file.");
}
Also used : AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) File(java.io.File)

Aggregations

AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)8 File (java.io.File)5 AbstractGridCoverage2DReader (org.geotools.coverage.grid.io.AbstractGridCoverage2DReader)5 ChooseRasterFormatInterface (com.sldeditor.datasource.chooseraster.ChooseRasterFormatInterface)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 UnknownFormat (org.geotools.coverage.grid.io.UnknownFormat)2 WorldImageFormat (org.geotools.gce.image.WorldImageFormat)2 DataSourcePropertiesInterface (com.sldeditor.common.DataSourcePropertiesInterface)1 SLDDataInterface (com.sldeditor.common.SLDDataInterface)1 SLDData (com.sldeditor.common.data.SLDData)1 StyleWrapper (com.sldeditor.common.data.StyleWrapper)1 ChooseRasterFormatPanel (com.sldeditor.datasource.chooseraster.ChooseRasterFormatPanel)1 SLDTreeTest (com.sldeditor.test.unit.ui.tree.SLDTreeTest)1 BufferedImage (java.awt.image.BufferedImage)1 WritableRaster (java.awt.image.WritableRaster)1 URL (java.net.URL)1 Set (java.util.Set)1 DefaultListModel (javax.swing.DefaultListModel)1 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)1