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