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;
});
}
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();
// }
}
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();
}
}
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;
}
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;
}
Aggregations