Package org.geotools.coverage.grid.io

Examples of org.geotools.coverage.grid.io.AbstractGridCoverage2DReader


        } else if (rinfo instanceof CoverageInfo) {
            // the coverage bounds computation path is a bit more linear, the
            // readers always return the bounds and in the proper CRS (afaik)
            CoverageInfo cinfo = (CoverageInfo) rinfo;           
            AbstractGridCoverage2DReader reader = (AbstractGridCoverage2DReader) cinfo.getGridCoverageReader(null, GeoTools.getDefaultHints());  

            // get  bounds
            bounds = new ReferencedEnvelope(reader.getOriginalEnvelope());
          
        }

        // apply the bounds, taking into account the reprojection policy if need be
        if (rinfo.getProjectionPolicy() == ProjectionPolicy.REPROJECT_TO_DECLARED && bounds != null) {
View Full Code Here


        if (store == null || !(store instanceof CoverageStoreInfo)) {
            throw new IllegalStateException("Coverage store not set.");
        }

        CoverageStoreInfo csinfo = (CoverageStoreInfo) store;
        AbstractGridCoverage2DReader reader = (AbstractGridCoverage2DReader) catalog
                .getResourcePool().getGridCoverageReader(csinfo, GeoTools.getDefaultHints());

        if (reader == null)
            throw new Exception("Unable to acquire a reader for this coverage with format: "
                    + csinfo.getFormat().getName());
View Full Code Here

                if (csinfo == null)
                    throw new RuntimeException(
                            "Unable to acquire coverage store resource for layer: "
                                    + layer.getName());

                AbstractGridCoverage2DReader reader = null;
                try {
                    reader = (AbstractGridCoverage2DReader) catalog.getResourcePool()
                            .getGridCoverageReader(csinfo, GeoTools.getDefaultHints());
                } catch (Throwable t) {
                    LOGGER.severe("Unable to acquire a reader for this coverage with format: "
                            + csinfo.getFormat().getName());
                }

                if (reader == null)
                    throw new RuntimeException(
                            "Unable to acquire a reader for this coverage with format: "
                                    + csinfo.getFormat().getName());

                final String[] metadataNames = reader.getMetadataNames();

                if (metadataNames != null && metadataNames.length > 0) {
                    // TIME DIMENSION
                    timeMetadataExtent = reader.getMetadataValue("TIME_DOMAIN");

                    if (timeMetadataExtent != null) {
                        AttributesImpl timeDim = new AttributesImpl();
                        timeDim.addAttribute("", "name", "name", "", "time");
                        timeDim.addAttribute("", "units", "units", "", "ISO8601");
                        timeDim.addAttribute("", "current", "current", "", "true");
                        timeDim.addAttribute("", "default", "default", "", "current");
                        element("Dimension", timeMetadataExtent, timeDim);
                    }

                    // ELEVATION DIMENSION
                    elevationMetadataExtent = reader.getMetadataValue("ELEVATION_DOMAIN");
                    if (elevationMetadataExtent != null) {
                        final String[] elevationLevels = elevationMetadataExtent.split(",");
                        AttributesImpl elevDim = new AttributesImpl();
                        elevDim.addAttribute("", "name", "name", "", "elevation");
                        elevDim.addAttribute("", "units", "units", "", "EPSG:5030");
View Full Code Here

                // /////////////////////////////////////////////////////////
                //
                // Adding a coverage layer
                //
                // /////////////////////////////////////////////////////////
                final AbstractGridCoverage2DReader reader = (AbstractGridCoverage2DReader) mapLayerInfo
                        .getCoverageReader();
                if (reader != null) {

                    // get the group of parameters tha this reader supports
                    GeneralParameterValue[] readParameters = wms.getWMSReadParameters(request,
View Full Code Here

      // updating grid if is a coverage
                      if(resourceInfo instanceof CoverageInfo) {
                            // the coverage bounds computation path is a bit more linear, the
                            // readers always return the bounds and in the proper CRS (afaik)
                            CoverageInfo cinfo = (CoverageInfo) resourceInfo;    
                            AbstractGridCoverage2DReader reader = (AbstractGridCoverage2DReader) cinfo.getGridCoverageReader(null, GeoTools.getDefaultHints());

                            // get  bounds
                            final ReferencedEnvelope bounds = new ReferencedEnvelope(reader.getOriginalEnvelope());
                            // apply the bounds, taking into account the reprojection policy if need be
                            final ProjectionPolicy projectionPolicy=resourceInfo.getProjectionPolicy();
                            if (projectionPolicy != ProjectionPolicy.NONE && bounds != null) {
                                // we need to fix the registered grid for this coverage
                                final GridGeometry grid = cinfo.getGrid();
View Full Code Here

        ci = catalog.getCoverageByName("usa");
        assertEquals(ProjectionPolicy.FORCE_DECLARED, ci.getProjectionPolicy());
        assertEquals("EPSG:3857", ci.getSRS());
       
        // now get the reader via the coverage info
        AbstractGridCoverage2DReader r;
        r = (AbstractGridCoverage2DReader) ci.getGridCoverageReader(null, GeoTools.getDefaultHints());
        assertTrue(CRS.equalsIgnoreMetadata(CRS.decode("EPSG:3857"), r.getCrs()));
       
        // and again without any hint
        r = (AbstractGridCoverage2DReader) ci.getGridCoverageReader(null, null);
        assertTrue(CRS.equalsIgnoreMetadata(CRS.decode("EPSG:3857"), r.getCrs()));
       
        // get the reader straight: we should get back the native projection
        CoverageStoreInfo store = catalog.getCoverageStoreByName("usa");
        final ResourcePool rpool = catalog.getResourcePool();
        r = (AbstractGridCoverage2DReader) rpool.getGridCoverageReader(store, GeoTools.getDefaultHints());
        assertTrue(CRS.equalsIgnoreMetadata(CRS.decode("EPSG:4326"), r.getCrs()));

   }
View Full Code Here

            checkDomainSubset(meta, request.getDomainSubset(), wcs);
            checkRangeSubset(meta, request.getRangeSubset());
            checkOutput(meta, request.getOutput());

            // grab the format, the reader using the default params
            final AbstractGridCoverage2DReader reader = (AbstractGridCoverage2DReader) meta.getGridCoverageReader(null, WCSUtils.getReaderHints(wcs));

            // handle spatial domain subset, if needed
            final GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope();
            final BoundingBoxType bbox = request.getDomainSubset().getBoundingBox();
            final CoordinateReferenceSystem nativeCRS = originalEnvelope.getCoordinateReferenceSystem();
            final GeneralEnvelope requestedEnvelopeInSourceCRS;
            final GeneralEnvelope requestedEnvelope;
            MathTransform bboxToNativeTx=null;
            if (bbox != null) {
                // first off, parse the envelope corners
                double[] lowerCorner = new double[bbox.getLowerCorner().size()];
                double[] upperCorner = new double[bbox.getUpperCorner().size()];
                for (int i = 0; i < lowerCorner.length; i++) {
                    lowerCorner[i] = (Double) bbox.getLowerCorner().get(i);
                    upperCorner[i] = (Double) bbox.getUpperCorner().get(i);
                }
                requestedEnvelope = new GeneralEnvelope(lowerCorner, upperCorner);
                // grab the native crs
                // if no crs has beens specified, the native one is assumed
                if (bbox.getCrs() == null) {
                    requestedEnvelope.setCoordinateReferenceSystem(nativeCRS);
                    requestedEnvelopeInSourceCRS = requestedEnvelope;
                } else {
                    // otherwise we need to transform
                    final CoordinateReferenceSystem bboxCRS = CRS.decode(bbox.getCrs());
                    requestedEnvelope.setCoordinateReferenceSystem(bboxCRS);
                    bboxToNativeTx = CRS.findMathTransform(bboxCRS, nativeCRS,true);
                    if(!bboxToNativeTx.isIdentity()){
                      requestedEnvelopeInSourceCRS = CRS.transform(bboxToNativeTx,requestedEnvelope);
                      requestedEnvelopeInSourceCRS.setCoordinateReferenceSystem(nativeCRS);
                    }
                    else
                      requestedEnvelopeInSourceCRS= new GeneralEnvelope(requestedEnvelope);
                }
            } else {
                requestedEnvelopeInSourceCRS = reader.getOriginalEnvelope();
                requestedEnvelope = requestedEnvelopeInSourceCRS;
            }
           
            final GridCrsType gridCRS = request.getOutput().getGridCRS();

            // Compute the target crs, the crs that the final coverage will be
            // served into
            final CoordinateReferenceSystem targetCRS;
            if (gridCRS == null)
                targetCRS = reader.getOriginalEnvelope().getCoordinateReferenceSystem();
            else
                targetCRS = CRS.decode(gridCRS.getGridBaseCRS());

            //
            // Raster destination size
            //
            int elevationLevels=0;
            double[] elevations = null;
           
            // grab the grid to world transformation
            MathTransform gridToCRS = reader.getOriginalGridToWorld(PixelInCell.CELL_CORNER);
            if (gridCRS != null) {
                Double[] origin = (Double[]) gridCRS.getGridOrigin();
                Double[] offsets = (Double[]) gridCRS.getGridOffsets();

                // from the specification if grid origin is omitted and the crs
                // is 2d the default it's 0,0
                if (origin == null) {
                    origin = new Double[] { 0.0, 0.0 };
                }

                // if no offsets has been specified we try to default on the
                // native ones
                if (offsets == null) {
                    if (!(gridToCRS instanceof AffineTransform2D)&& !(gridToCRS instanceof IdentityTransform))
                        throw new WcsException(
                                "Internal error, the coverage we're playing with does not have an affine transform...");

                    if (gridToCRS instanceof IdentityTransform) {
                        if (gridCRS.getGridType().equals(GridType.GT2dSimpleGrid.getXmlConstant()) ||
                                gridCRS.getGridType().equals(GridType.GT2dGridIn2dCrs.getXmlConstant()))
                            offsets = new Double[] { 1.0, 1.0 };
                        else
                            offsets = new Double[] { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0 };
                    } else {
                        AffineTransform2D affine = (AffineTransform2D) gridToCRS;
                        if (gridCRS.getGridType().equals(GridType.GT2dSimpleGrid.getXmlConstant()) ||
                                gridCRS.getGridType().equals(GridType.GT2dGridIn2dCrs.getXmlConstant()))
                            offsets = new Double[] { affine.getScaleX(), affine.getScaleY() };
                        else
                            offsets = new Double[] { affine.getScaleX(), affine.getShearX(), affine.getShearY(), affine.getScaleY() };
                    }
                }

                // building the actual transform for the resulting grid geometry
                AffineTransform tx;
                if (gridCRS.getGridType().equals(GridType.GT2dSimpleGrid.getXmlConstant())) {
                    tx = new AffineTransform(
                            offsets[0], 0,
                            0, offsets[1],
                            origin[0], origin[1]
                    );
                } else if(gridCRS.getGridType().equals(GridType.GT2dGridIn2dCrs.getXmlConstant())) {
                    tx = new AffineTransform(
                            offsets[0], offsets[1],
                            offsets[2], offsets[3],
                            origin[0], origin[1]
                    );
                } else {
                    tx = new AffineTransform(
                            offsets[0], offsets[4],
                            offsets[1], offsets[3],
                            origin[0], origin[1]
                    );
                   
                    if (origin.length != 3 || offsets.length != 6)
                        throw new WcsException("", InvalidParameterValue, "GridCRS");
             
                    //
                    //  ELEVATIONS
                    //
                   
                    // TODO: draft code ... it needs more study!
                    elevationLevels = (int) Math.round(requestedEnvelope.getUpperCorner().getOrdinate(2) - requestedEnvelope.getLowerCorner().getOrdinate(2));

                    // compute the elevation levels, we have elevationLevels values
                    if (elevationLevels > 0) {
                        elevations=new double[elevationLevels];

                        elevations[0]=requestedEnvelope.getLowerCorner().getOrdinate(2); // TODO put the extrema
                        elevations[elevationLevels-1]=requestedEnvelope.getUpperCorner().getOrdinate(2);
                        if(elevationLevels>2){
                            final int adjustedLevelsNum=elevationLevels-1;
                            double step = (elevations[elevationLevels-1]-elevations[0])/adjustedLevelsNum;
                            for(int i=1;i<adjustedLevelsNum;i++)
                                elevations[i]=elevations[i-1]+step;
                        }
                    }
                }
                gridToCRS = new AffineTransform2D(tx);
            }
           
            //
            //   TIME Values
            //
            final List<Date> timeValues = new LinkedList<Date>();

            TimeSequenceType temporalSubset = request.getDomainSubset().getTemporalSubset();
           
            if (temporalSubset != null && temporalSubset.getTimePosition() != null
                    && temporalSubset.getTimePosition().size() > 0) {
                for (Iterator it = temporalSubset.getTimePosition().iterator(); it.hasNext(); ) {
                    Date tp = (Date) it.next();
                    timeValues.add(tp);
                }
            } else if (temporalSubset != null &&  temporalSubset.getTimePeriod() != null
                    && temporalSubset.getTimePeriod().size() > 0) {
                for (Iterator it = temporalSubset.getTimePeriod().iterator(); it.hasNext(); ) {
                    TimePeriodType tp = (TimePeriodType) it.next();
                    Date beginning = (Date)tp.getBeginPosition();
                    Date ending = (Date)tp.getEndPosition();

                    timeValues.add(beginning);
                    timeValues.add(ending);
                }
            }

            // now we have enough info to read the coverage, grab the parameters
            // and add the grid geometry info
            final GeneralEnvelope intersectionEnvelopeInSourceCRS = new GeneralEnvelope(requestedEnvelopeInSourceCRS);
            intersectionEnvelopeInSourceCRS.intersect(originalEnvelope);
            final GeneralEnvelope intersectionEnvelope=
              bboxToNativeTx.isIdentity()?
                  new GeneralEnvelope(intersectionEnvelopeInSourceCRS):
                  CRS.transform(bboxToNativeTx.inverse(), intersectionEnvelopeInSourceCRS);
            intersectionEnvelope.setCoordinateReferenceSystem(targetCRS);
           
           
            final GridGeometry2D requestedGridGeometry = new GridGeometry2D(PixelInCell.CELL_CORNER, gridToCRS, intersectionEnvelopeInSourceCRS, null);

            final ParameterValueGroup readParametersDescriptor = reader.getFormat().getReadParameters();
            GeneralParameterValue[] readParameters = CoverageUtils.getParameters(readParametersDescriptor, meta.getParameters());
                                    readParameters = (readParameters != null ? readParameters : new GeneralParameterValue[0]);
           
            //
            // Setting coverage reading params.
            //
            final ParameterValue requestedGridGeometryParam = new DefaultParameterDescriptor(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString(), GeneralGridGeometry.class, null, requestedGridGeometry).createValue();

            /*
             * Test if the parameter "TIME" is present in the WMS
             * request, and by the way in the reading parameters. If
             * it is the case, one can adds it to the request. If an
             * exception is thrown, we have nothing to do.
             */
            final List<GeneralParameterDescriptor> parameterDescriptors = readParametersDescriptor.getDescriptor().descriptors();
            ParameterValue time=null;
            boolean hasTime=timeValues.size()>0;
            ParameterValue elevation=null;
            boolean hasElevation=elevations != null && !Double.isNaN(elevations[0]);
           
            if(hasElevation||hasTime){
                for(GeneralParameterDescriptor pd:parameterDescriptors){

                        final String code=pd.getName().getCode();
                       
                        //
                        // TIME
                        //
                        if(code.equalsIgnoreCase("TIME")){
                            time=(ParameterValue) pd.createValue();
                            time.setValue(timeValues);                }
                       
                        //
                        // ELEVATION
                        //
                        if(code.equalsIgnoreCase("ELEVATION")){
                            elevation=(ParameterValue) pd.createValue();
                            elevation.setValue(elevations[0]);
                        }
                       
                       
                        // leave?
                        if((hasElevation&&elevation!=null&&hasTime&&time!=null)||
                                        !hasElevation&&hasTime&&time!=null||
                                        hasElevation&&elevation!=null&&!hasTime)
                                break;
                    }
            }
            //
            // add read parameters
            //
            int addedParams=1+(hasTime?1:0)+(hasElevation?1:0);
            // add to the list
            GeneralParameterValue[] readParametersClone = new GeneralParameterValue[readParameters.length+addedParams--];
            System.arraycopy(readParameters, 0,readParametersClone , 0, readParameters.length);
            readParametersClone[readParameters.length+addedParams--]=requestedGridGeometryParam;
            if(hasTime)
                readParametersClone[readParameters.length+addedParams--]=time;
            if(hasElevation)
                readParametersClone[readParameters.length+addedParams--]=elevation;           
            readParameters=readParametersClone;
           
            // Check we're not being requested to read too much data from input (first check,
            // guesses the grid size using the information contained in CoverageInfo)
            WCSUtils.checkInputLimits(wcs, meta, reader, requestedGridGeometry);
           
            //
            // perform Read ...
            //
            coverage = (GridCoverage2D) reader.read(readParameters);
            if ((coverage == null) || !(coverage instanceof GridCoverage2D)) {
                throw new IOException("The requested coverage could not be found.");
            }
           
            // now that we have read the coverage double check the input size
View Full Code Here

    }

    @Override
    public List<ImportTask> list(ImportData data, Catalog catalog, ProgressMonitor monitor)
            throws IOException {
        AbstractGridCoverage2DReader reader = gridReader(data);
       
        List<ImportTask> tasks = new ArrayList<ImportTask>();
        if (reader != null) {
            CatalogBuilder cb = new CatalogBuilder(catalog);
View Full Code Here

        if (format instanceof GridFormat) {
            Granule g = new Granule(super.newSpatialFile(f, format));

            //process the granule
            try {
                AbstractGridCoverage2DReader r = ((GridFormat)format).gridReader(g);
                try {
                    //get the envelope
                    GridCoverage2D cov = r.read(null);

                    g.setEnvelope(cov.getEnvelope2D());
                    g.setGrid(cov.getGridGeometry());

                    cov.dispose(false);

                    //compute time stamp
                    g.setTimestamp(timeHandler.computeTimestamp(g));

                    return g;
                }
                finally {
                    if (r != null) {
                        r.dispose();
                    }
                }
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "Could not read file " + f + ", unable to get coverage info");
            }
View Full Code Here

        if ( coverage.indexOf( '.') != -1 ) {
            coverage = coverage.substring( 0, coverage.indexOf( '.') );
        }
       
        try {
            AbstractGridCoverage2DReader reader =
                (AbstractGridCoverage2DReader) ((AbstractGridFormat) coverageFormat).getReader(uploadedFile.toURL());
            if ( reader == null ) {
                throw new RestletException( "Could not aquire reader for coverage.", Status.SERVER_ERROR_INTERNAL );
            }
           
View Full Code Here

TOP

Related Classes of org.geotools.coverage.grid.io.AbstractGridCoverage2DReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.