/** Visit all files to keep only XML files. */ @Override protected void visit() { final ParameterValue<URL> paramUrl = (ParameterValue<URL>) getSourceConfiguration(getSource()).parameter(URL.getName().getCode()); if (paramUrl == null || paramUrl.getValue() == null) { getLogger().log(Level.WARNING, "Provided File path is not defined."); return; } final URL urlPath = paramUrl.getValue(); try { path = new File(urlPath.toURI()); } catch (URISyntaxException e) { getLogger().log(Level.INFO, "Fails to convert path url to file."); path = new File(urlPath.getPath()); } List<File> candidates = new ArrayList<>(); if (path.isDirectory()) { candidates.addAll( FileUtilities.scanDirectory( path, new FileFilter() { @Override public boolean accept(File pathname) { final String fullName = pathname.getName(); final int idx = fullName.lastIndexOf('.'); final String extension = fullName.substring(idx + 1); return extension.equalsIgnoreCase("xml"); } })); } else { candidates.add(path); } index = new HashMap<>(); for (final File candidate : candidates) { try { final MapContext mapContext = MapContextIO.readMapContextFile(candidate, "", "", styleBusiness); if (mapContext != null) { final GenericName name = NamesExt.create(mapContext.getName()); index.put(name, candidate); } } catch (JAXBException e) { getLogger() .log( Level.WARNING, "Candidate MapContext file can't be read : " + candidate.getAbsolutePath(), e); } } super.visit(); }
@Override public WebMapTileClient open(ParameterValueGroup params) throws DataStoreException { ensureCanProcess(params); final WebMapTileClient server = new WebMapTileClient(params); try { final ParameterValue val = params.parameter(NIO_QUERIES.getName().getCode()); boolean useNIO = Boolean.TRUE.equals(val.getValue()); server.setUserProperty(CachedPyramidSet.PROPERTY_NIO, useNIO); } catch (ParameterNotFoundException ex) { } return server; }
/** * Applies a crop operation to a coverage. * * @see * org.geotools.coverage.processing.AbstractOperation#doOperation(org.opengis.parameter.ParameterValueGroup, * org.geotools.factory.Hints) */ @SuppressWarnings("unchecked") public Coverage doOperation(ParameterValueGroup parameters, Hints hints) { final Geometry cropRoi; // extracted from parameters GeneralEnvelope cropEnvelope = null; // extracted from parameters final GridCoverage2D source; // extracted from parameters final double roiTolerance = parameters.parameter(Crop.PARAMNAME_ROITOLERANCE).doubleValue(); final boolean forceMosaic = parameters.parameter(Crop.PARAMNAME_FORCEMOSAIC).booleanValue(); // ///////////////////////////////////////////////////////////////////// // // Assigning and checking input parameters // // /////////////////////////////////////////////////////////////////// // source coverage final ParameterValue sourceParameter = parameters.parameter("Source"); if (sourceParameter == null || !(sourceParameter.getValue() instanceof GridCoverage2D)) { throw new CannotCropException( Errors.format(ErrorKeys.NULL_PARAMETER_$2, "Source", GridCoverage2D.class.toString())); } source = (GridCoverage2D) sourceParameter.getValue(); // Check Envelope and ROI existence - we need at least one of them final ParameterValue envelopeParameter = parameters.parameter(PARAMNAME_ENVELOPE); final ParameterValue roiParameter = parameters.parameter(PARAMNAME_ROI); if ((envelopeParameter == null || envelopeParameter.getValue() == null) && (roiParameter == null || roiParameter.getValue() == null)) throw new CannotCropException( Errors.format( ErrorKeys.NULL_PARAMETER_$2, PARAMNAME_ENVELOPE, GeneralEnvelope.class.toString())); Object envelope = envelopeParameter.getValue(); if (envelope != null) { if (envelope instanceof GeneralEnvelope) { cropEnvelope = (GeneralEnvelope) envelope; } else if (envelope instanceof Envelope) { cropEnvelope = new GeneralEnvelope((Envelope) envelope); } } // may be null // Check crop ROI try { cropRoi = IntersectUtils.unrollGeometries( (Geometry) roiParameter.getValue()); // may throw if format not correct } catch (IllegalArgumentException ex) { throw new CannotCropException( Errors.format(ErrorKeys.ILLEGAL_ARGUMENT_$2, PARAMNAME_ROI, ex.getMessage()), ex); } // Setting a GeneralEnvelope from ROI if needed if (cropRoi != null && cropEnvelope == null) { Envelope e2d = JTS.getEnvelope2D(cropRoi.getEnvelopeInternal(), source.getCoordinateReferenceSystem()); cropEnvelope = new GeneralEnvelope(e2d); } // ///////////////////////////////////////////////////////////////////// // // Initialization // // We take the crop envelope and the source envelope then we check their // crs and we also check if they ever overlap. // // ///////////////////////////////////////////////////////////////////// // envelope of the source coverage final Envelope2D sourceEnvelope = source.getEnvelope2D(); // crop envelope Envelope2D destinationEnvelope = new Envelope2D(cropEnvelope); CoordinateReferenceSystem sourceCRS = sourceEnvelope.getCoordinateReferenceSystem(); CoordinateReferenceSystem destinationCRS = destinationEnvelope.getCoordinateReferenceSystem(); if (destinationCRS == null) { // Do not change the user provided object - clone it first. final Envelope2D ge = new Envelope2D(destinationEnvelope); destinationCRS = source.getCoordinateReferenceSystem2D(); ge.setCoordinateReferenceSystem(destinationCRS); destinationEnvelope = ge; } // // // // Source and destination crs must be equals // // // if (!CRS.equalsIgnoreMetadata(sourceCRS, destinationCRS)) { throw new CannotCropException( Errors.format( ErrorKeys.MISMATCHED_ENVELOPE_CRS_$2, sourceCRS.getName().getCode(), destinationCRS.getName().getCode())); } if (cropRoi != null) { // TODO: check ROI SRID } // // // // Check the intersection and, if needed, do the crop operation. // // // final GeneralEnvelope intersectionEnvelope = new GeneralEnvelope((Envelope) destinationEnvelope); intersectionEnvelope.setCoordinateReferenceSystem(source.getCoordinateReferenceSystem()); // intersect the envelopes intersectionEnvelope.intersect(sourceEnvelope); if (intersectionEnvelope.isEmpty()) throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP)); // intersect the ROI with the intersection envelope and throw an error if they do not intersect if (cropRoi != null) { final Geometry jis = JTS.toGeometry( (com.vividsolutions.jts.geom.Envelope) new ReferencedEnvelope(intersectionEnvelope)); if (!IntersectUtils.intersects(cropRoi, jis)) throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP)); } // // // // Get the grid-to-world transform by keeping into account translation // of grid geometry constructor for respecting OGC PIXEL-IS-CENTER // ImageDatum assumption. // // // final AffineTransform sourceCornerGridToWorld = (AffineTransform) ((GridGeometry2D) source.getGridGeometry()).getGridToCRS(PixelInCell.CELL_CORNER); // // // // I set the tolerance as half the scale factor of the grid-to-world // transform. This should more or less means in most cases "don't bother // to crop if the new envelope is as close to the old one that we go // deep under pixel size." // // // final double tolerance = XAffineTransform.getScale(sourceCornerGridToWorld); if (cropRoi != null || !intersectionEnvelope.equals(sourceEnvelope, tolerance / 2.0, false)) { cropEnvelope = intersectionEnvelope.clone(); return buildResult( cropEnvelope, cropRoi, roiTolerance, forceMosaic, (hints instanceof Hints) ? (Hints) hints : new Hints(hints), source, sourceCornerGridToWorld); } else { // // // // Note that in case we don't crop at all, WE DO NOT UPDATE the // envelope. If we did we might end up doing multiple successive // crop without actually cropping the image but, still, we would // shrink the envelope each time. Just think about having a loop // that crops recursively the same coverage specifying each time an // envelope whose URC is only a a scale quarter close to the LLC of // the old one. We would never crop the raster but we would modify // the grid-to-world transform each time. // // // return source; } }
/** * This method reads in the TIFF image, constructs an appropriate CRS, determines the math * transform from raster to the CRS model, and constructs a GridCoverage. * * @param params currently ignored, potentially may be used for hints. * @return grid coverage represented by the image * @throws IOException on any IO related troubles */ public GridCoverage2D read(GeneralParameterValue[] params) throws IOException { GeneralEnvelope requestedEnvelope = null; Rectangle dim = null; Color inputTransparentColor = null; OverviewPolicy overviewPolicy = null; int[] suggestedTileSize = null; if (params != null) { // // Checking params // if (params != null) { for (int i = 0; i < params.length; i++) { final ParameterValue param = (ParameterValue) params[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); requestedEnvelope = new GeneralEnvelope((Envelope) gg.getEnvelope2D()); dim = gg.getGridRange2D().getBounds(); continue; } if (name.equals(AbstractGridFormat.OVERVIEW_POLICY.getName())) { overviewPolicy = (OverviewPolicy) param.getValue(); continue; } if (name.equals(AbstractGridFormat.INPUT_TRANSPARENT_COLOR.getName())) { inputTransparentColor = (Color) param.getValue(); continue; } if (name.equals(AbstractGridFormat.SUGGESTED_TILE_SIZE.getName())) { String suggestedTileSize_ = (String) param.getValue(); if (suggestedTileSize_ != null && suggestedTileSize_.length() > 0) { suggestedTileSize_ = suggestedTileSize_.trim(); int commaPosition = suggestedTileSize_.indexOf(","); if (commaPosition < 0) { int tileDim = Integer.parseInt(suggestedTileSize_); suggestedTileSize = new int[] {tileDim, tileDim}; } else { int tileW = Integer.parseInt(suggestedTileSize_.substring(0, commaPosition)); int tileH = Integer.parseInt(suggestedTileSize_.substring(commaPosition + 1)); suggestedTileSize = new int[] {tileW, tileH}; } } continue; } } } } // // set params // Integer imageChoice = new Integer(0); final ImageReadParam readP = new ImageReadParam(); try { imageChoice = setReadParams(overviewPolicy, readP, requestedEnvelope, dim); } catch (TransformException e) { new DataSourceException(e); } // // IMAGE READ OPERATION // Hints newHints = null; if (suggestedTileSize != null) { newHints = hints.clone(); final ImageLayout layout = new ImageLayout(); layout.setTileGridXOffset(0); layout.setTileGridYOffset(0); layout.setTileHeight(suggestedTileSize[1]); layout.setTileWidth(suggestedTileSize[0]); newHints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout)); } final ParameterBlock pbjRead = new ParameterBlock(); if (extOvrImgChoice >= 0 && imageChoice >= extOvrImgChoice) { pbjRead.add( ovrInStreamSPI.createInputStreamInstance( ovrSource, ImageIO.getUseCache(), ImageIO.getCacheDirectory())); pbjRead.add(imageChoice - extOvrImgChoice); } else { pbjRead.add( inStreamSPI != null ? inStreamSPI.createInputStreamInstance( source, ImageIO.getUseCache(), ImageIO.getCacheDirectory()) : ImageIO.createImageInputStream(source)); pbjRead.add(imageChoice); } pbjRead.add(Boolean.FALSE); pbjRead.add(Boolean.FALSE); pbjRead.add(Boolean.FALSE); pbjRead.add(null); pbjRead.add(null); pbjRead.add(readP); pbjRead.add(READER_SPI.createReaderInstance()); RenderedOp coverageRaster = JAI.create("ImageRead", pbjRead, newHints != null ? (RenderingHints) newHints : null); // // MASKING INPUT COLOR as indicated // if (inputTransparentColor != null) { coverageRaster = new ImageWorker(coverageRaster) .setRenderingHints(newHints) .makeColorTransparent(inputTransparentColor) .getRenderedOperation(); } AffineTransform rasterToModel = getRescaledRasterToModel(coverageRaster); try { return createCoverage(coverageRaster, ProjectiveTransform.create(rasterToModel)); } catch (Exception e) { // dispose and close file ImageUtilities.disposePlanarImageChain(coverageRaster); // rethrow if (e instanceof DataSourceException) { throw (DataSourceException) e; } throw new DataSourceException(e); } }
private ArcSDEGridCoverage2DReaderJAI.ReadParameters parseReadParams( final GeneralParameterValue[] params) throws IllegalArgumentException { if (params == null) { throw new IllegalArgumentException("No GeneralParameterValue given to read operation"); } GeneralEnvelope reqEnvelope = null; GridEnvelope dim = null; OverviewPolicy overviewPolicy = null; // ///////////////////////////////////////////////////////////////////// // // Checking params // // ///////////////////////////////////////////////////////////////////// for (int i = 0; i < params.length; i++) { final ParameterValue<?> param = (ParameterValue<?>) params[i]; final String name = param.getDescriptor().getName().getCode(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); reqEnvelope = new GeneralEnvelope((Envelope) gg.getEnvelope2D()); final GeneralEnvelope coverageEnvelope = getOriginalEnvelope(); CoordinateReferenceSystem nativeCrs = coverageEnvelope.getCoordinateReferenceSystem(); CoordinateReferenceSystem requestCrs = reqEnvelope.getCoordinateReferenceSystem(); if (!CRS.equalsIgnoreMetadata(nativeCrs, requestCrs)) { LOGGER.fine( "Request CRS and native CRS differ, " + "reprojecting request envelope to native CRS"); ReferencedEnvelope nativeCrsEnv; nativeCrsEnv = toNativeCrs(reqEnvelope, nativeCrs); reqEnvelope = new GeneralEnvelope(nativeCrsEnv); } dim = gg.getGridRange2D(); continue; } if (name.equals(AbstractGridFormat.OVERVIEW_POLICY.getName().toString())) { overviewPolicy = (OverviewPolicy) param.getValue(); continue; } } if (reqEnvelope == null && dim == null) { reqEnvelope = getOriginalEnvelope(); dim = getOriginalGridRange(); } if (reqEnvelope == null) { reqEnvelope = getOriginalEnvelope(); } if (dim == null) { final GeneralEnvelope adjustedGRange; try { MathTransform gridToWorld = getOriginalGridToWorld(PixelInCell.CELL_CENTER); MathTransform worldToGrid = gridToWorld.inverse(); adjustedGRange = CRS.transform(worldToGrid, reqEnvelope); } catch (Exception e) { throw new RuntimeException(e); } int xmin = (int) Math.floor(adjustedGRange.getMinimum(0)); int ymin = (int) Math.floor(adjustedGRange.getMinimum(1)); int xmax = (int) Math.ceil(adjustedGRange.getMaximum(0)); int ymax = (int) Math.ceil(adjustedGRange.getMaximum(1)); dim = new GridEnvelope2D(xmin, ymin, xmax - xmin, ymax - ymin); } if (!reqEnvelope.intersects(getOriginalEnvelope(), true)) { throw new IllegalArgumentException( "The requested extend does not overlap the coverage extent: " + getOriginalEnvelope()); } if (dim.getSpan(0) <= 0 || dim.getSpan(1) <= 0) { throw new IllegalArgumentException("The requested coverage dimension can't be null: " + dim); } if (overviewPolicy == null) { overviewPolicy = OverviewPolicy.NEAREST; LOGGER.finer("No overview policy requested, defaulting to " + overviewPolicy); } LOGGER.fine("Overview policy is " + overviewPolicy); ArcSDEGridCoverage2DReaderJAI.ReadParameters parsedParams = new ArcSDEGridCoverage2DReaderJAI.ReadParameters(); parsedParams.requestedEnvelope = reqEnvelope; parsedParams.dim = dim; parsedParams.overviewPolicy = overviewPolicy; return parsedParams; }