Search in sources :

Example 1 with AbstractGridCoverage2DReader

use of org.geotools.coverage.grid.io.AbstractGridCoverage2DReader in project polymap4-core by Polymap4.

the class GridCoverageReaderFactory method open.

/**
 * Creates a new {@link GridCoverage2DReader} for the given file.
 *
 * @param f
 * @return Newly created reader.
 * @throws Exception
 */
public static AbstractGridCoverage2DReader open(File f) throws Exception {
    return lock.lockedInterruptibly(() -> {
        // Hints hints = new Hints();
        // hints.put( Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, CRS.decode( "EPSG:9001" ) );
        // hints.put( Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE );
        AbstractGridFormat format = GridFormatFinder.findFormat(f);
        AbstractGridCoverage2DReader reader = format.getReader(f);
        return reader;
    });
}
Also used : AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader)

Example 2 with AbstractGridCoverage2DReader

use of org.geotools.coverage.grid.io.AbstractGridCoverage2DReader in project polymap4-core by Polymap4.

the class PredefinedColorMapEditor method main.

// Test ***********************************************
public static void main(String[] args) throws Exception {
    File f = new File("/home/falko/Data/ncrast/elevation_4326.tif");
    AbstractGridFormat format = GridFormatFinder.findFormat(f);
    AbstractGridCoverage2DReader reader = format.getReader(f);
    String[] names = reader.getGridCoverageNames();
    GridCoverage2D grid = reader.read(names[0], null);
    GridGeometry2D geometry = grid.getGridGeometry();
    GridEnvelope gridRange = geometry.getGridRange();
    int w = gridRange.getHigh(0);
    int h = gridRange.getHigh(1);
    // all
    Timer timer = new Timer();
    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;
    int c = 0;
    double[] buf = new double[1];
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            double[] value = grid.evaluate(new GridCoordinates2D(x, y), buf);
            min = value[0] < min ? value[0] : min;
            max = value[0] > max ? value[0] : max;
            c++;
        }
    }
    System.out.println("min/max: " + min + ".." + max + " (" + c + " in " + timer.elapsedTime() + "ms)");
    // random
    timer.start();
    double[] minMax = new PredefinedColorMapEditor().minMax(grid);
    System.out.println("min/max: " + minMax[0] + ".." + minMax[1] + " (" + c + " in " + timer.elapsedTime() + "ms)");
// final DefaultProcessor proc = new DefaultProcessor(null);
// for (Operation o : proc.getOperations() ){
// System.out.println(o.getName());
// System.out.println(o.getDescription());
// System.out.println();
// }
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridEnvelope(org.opengis.coverage.grid.GridEnvelope) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) Timer(org.polymap.core.runtime.Timer) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) File(java.io.File)

Example 3 with AbstractGridCoverage2DReader

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

the class RenderPanelImpl method renderRasterMap.

/**
 * Render raster map.
 *
 * @param imageSize the image size
 * @param style the style
 * @param dpi the dpi
 */
private void renderRasterMap(Rectangle imageSize, Style style, int dpi) {
    DataSourceInterface dataSource = DataSourceFactory.getDataSource();
    AbstractGridCoverage2DReader gridCoverage = dataSource.getGridCoverageReader();
    if (gridCoverage == null) {
        validSymbol = false;
    }
    GridReaderLayer rasterLayer = null;
    MapViewport viewport = null;
    List<Layer> layerList = new ArrayList<Layer>();
    if (style != null) {
        rasterLayer = new GridReaderLayer(gridCoverage, style);
        layerList.add(rasterLayer);
        viewport = new MapViewport(rasterLayer.getBounds());
    }
    boolean hasGeometry = true;
    MapContent map = new MapContent();
    map.addLayers(layerList);
    map.setViewport(viewport);
    try {
        Map<Object, Object> hints = new HashMap<Object, Object>();
        if (OVERRIDE_DPI) {
            hints.put(StreamingRenderer.DPI_KEY, dpi);
        }
        // This ensures all the labelling is cleared
        hints.put(StreamingRenderer.LABEL_CACHE_KEY, new LabelCacheImpl());
        renderer.setRendererHints(hints);
        renderer.setMapContent(map);
        BufferedImage image = new BufferedImage(imageSize.width, imageSize.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = image.createGraphics();
        if (useAntiAlias) {
            graphics.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        }
        try {
            if (!hasGeometry) {
                graphics.setColor(Color.BLACK);
                int y = imageSize.height / 2;
                Font font = new Font(Font.SERIF, Font.BOLD, 14);
                graphics.setFont(font);
                graphics.drawString(Localisation.getString(RenderPanelImpl.class, "RenderPanelImpl.error1"), 10, y - 14);
            } else {
                if (rasterLayer != null) {
                    ReferencedEnvelope bounds = rasterLayer.getBounds();
                    renderer.paint(graphics, imageSize, bounds);
                }
                this.bImage = image;
            }
        } finally {
            graphics.dispose();
        }
    } finally {
        map.dispose();
    }
}
Also used : MapContent(org.geotools.map.MapContent) HashMap(java.util.HashMap) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) ArrayList(java.util.ArrayList) LabelCacheImpl(org.geotools.renderer.label.LabelCacheImpl) GridReaderLayer(org.geotools.map.GridReaderLayer) FeatureLayer(org.geotools.map.FeatureLayer) Layer(org.geotools.map.Layer) BufferedImage(java.awt.image.BufferedImage) RenderingHints(java.awt.RenderingHints) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) GridReaderLayer(org.geotools.map.GridReaderLayer) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) MapViewport(org.geotools.map.MapViewport)

Example 4 with AbstractGridCoverage2DReader

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

the class CreateExternalDataSource method connect.

/**
 * Connect.
 *
 * @param typeName the type name
 * @param geometryFieldName the geometry field name
 * @param editorFile the editor file
 * @return the list of datastores
 */
@Override
public List<DataSourceInfo> connect(String typeName, String geometryFieldName, SLDEditorFileInterface editorFile) {
    List<DataSourceInfo> dataSourceInfoList = new ArrayList<DataSourceInfo>();
    dataSourceInfoList.add(dsInfo);
    dsInfo.reset();
    if (editorFile != null) {
        SLDDataInterface sldData = editorFile.getSLDData();
        DataSourcePropertiesInterface dataSourceProperties = sldData.getDataSourceProperties();
        Map<String, Object> map = dataSourceProperties.getConnectionProperties();
        if (dataSourceProperties.hasPassword()) {
            String password = dataSourceProperties.getPassword();
            if (password == null) {
                password = "dummy password";
                dataSourceProperties.setPassword(password);
                map = dataSourceProperties.getConnectionProperties();
            }
        }
        DataStore dataStore = null;
        try {
            dataStore = DataStoreFinder.getDataStore(map);
            if (dataStore != null) {
                // Try connecting to a vector data source
                dsInfo.setTypeName(typeName);
                SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
                SimpleFeatureType schema = source.getSchema();
                if (schema.getCoordinateReferenceSystem() == null) {
                    // No crs found to set a default and reload
                    if (dataStore instanceof ShapefileDataStore) {
                        ShapefileDataStore shapeFileDatastore = (ShapefileDataStore) dataStore;
                        CoordinateReferenceSystem crs = JCRSChooser.showDialog(Localisation.getString(CreateExternalDataSource.class, "CRSPanel.title"), defaultCRS.getIdentifiers().iterator().next().toString());
                        if (crs != null) {
                            shapeFileDatastore.forceSchemaCRS(crs);
                        }
                        source = dataStore.getFeatureSource(typeName);
                        schema = source.getSchema();
                    }
                }
                dsInfo.setSchema(schema);
                determineGeometryType(schema.getGeometryDescriptor().getType());
            } else {
                // Try connecting to a raster data source
                Object rasterFilename = map.get(DataSourceConnectorInterface.FILE_MAP_KEY);
                if (rasterFilename != null) {
                    File rasterFile = new File(ExternalFilenames.convertURLToFile((String) rasterFilename));
                    ChooseRasterFormatInterface panel = new ChooseRasterFormatPanel(Controller.getInstance().getFrame());
                    AbstractGridFormat format = DetermineRasterFormat.choose(rasterFile, panel);
                    AbstractGridCoverage2DReader reader = format.getReader(rasterFile);
                    dsInfo.setGridCoverageReader(reader);
                } else {
                    logger.error("No matching datastore");
                }
            }
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
        dsInfo.setDataStore(dataStore);
        if (!dsInfo.hasData()) {
            ConsoleManager.getInstance().error(this, Localisation.getField(CreateExternalDataSource.class, "CreateExternalDataSource.failedToConnect") + dataSourceProperties.getDebugConnectionString());
        }
    }
    return dataSourceInfoList;
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) IOException(java.io.IOException) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SLDDataInterface(com.sldeditor.common.SLDDataInterface) ChooseRasterFormatPanel(com.sldeditor.datasource.chooseraster.ChooseRasterFormatPanel) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) DataStore(org.geotools.data.DataStore) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) ChooseRasterFormatInterface(com.sldeditor.datasource.chooseraster.ChooseRasterFormatInterface) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) File(java.io.File)

Example 5 with AbstractGridCoverage2DReader

use of org.geotools.coverage.grid.io.AbstractGridCoverage2DReader 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)

Aggregations

AbstractGridCoverage2DReader (org.geotools.coverage.grid.io.AbstractGridCoverage2DReader)7 File (java.io.File)5 AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)5 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)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 DataSourceInterface (com.sldeditor.datasource.DataSourceInterface)1 ChooseRasterFormatInterface (com.sldeditor.datasource.chooseraster.ChooseRasterFormatInterface)1 ChooseRasterFormatPanel (com.sldeditor.datasource.chooseraster.ChooseRasterFormatPanel)1 Font (java.awt.Font)1 Graphics2D (java.awt.Graphics2D)1 RenderingHints (java.awt.RenderingHints)1 WritableRaster (java.awt.image.WritableRaster)1 HashMap (java.util.HashMap)1 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)1 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)1