/** * Prints a list of CRS that can't be instantiated. This is used for implementation of {@linkplain * #main main method} in order to check the content of the {@value #FILENAME} file (or whatever * property file used as backing store for this factory) from the command line. * * @param out The writer where to print the report. * @return The set of codes that can't be instantiated. * @throws FactoryException if an error occured while {@linkplain #getAuthorityCodes fetching * authority codes}. * @since 2.4 */ protected Set reportInstantiationFailures(final PrintWriter out) throws FactoryException { final Set<String> codes = getAuthorityCodes(CoordinateReferenceSystem.class); final Map<String, String> failures = new TreeMap<String, String>(); for (final String code : codes) { try { createCoordinateReferenceSystem(code); } catch (FactoryException exception) { failures.put(code, exception.getLocalizedMessage()); } } if (!failures.isEmpty()) { final TableWriter writer = new TableWriter(out, " "); for (final Map.Entry<String, String> entry : failures.entrySet()) { writer.write(entry.getKey()); writer.write(':'); writer.nextColumn(); writer.write(entry.getValue()); writer.nextLine(); } try { writer.flush(); } catch (IOException e) { // Should not happen, since we are writting to a PrintWriter throw new AssertionError(e); } } return failures.keySet(); }
/** * Returns a new bounding box which contains the transformed shape of this bounding box. This is a * convenience method that delegate its work to the {@link #transform transform} method. * * @since 2.4 */ public BoundingBox toBounds(final CoordinateReferenceSystem targetCRS) throws TransformException { try { return transform(targetCRS, true); } catch (FactoryException e) { throw new TransformException(e.getLocalizedMessage(), e); } }
/** Returns a projected CRS for test purpose. */ private static CoordinateReferenceSystem getProjectedCRS(final GridCoverage2D coverage) { try { final GeographicCRS base = (GeographicCRS) coverage.getCoordinateReferenceSystem(); final Ellipsoid ellipsoid = base.getDatum().getEllipsoid(); final DefaultMathTransformFactory factory = new DefaultMathTransformFactory(); final ParameterValueGroup parameters = factory.getDefaultParameters("Oblique_Stereographic"); parameters.parameter("semi_major").setValue(ellipsoid.getSemiMajorAxis()); parameters.parameter("semi_minor").setValue(ellipsoid.getSemiMinorAxis()); parameters.parameter("central_meridian").setValue(5); parameters.parameter("latitude_of_origin").setValue(-5); final MathTransform mt; try { mt = factory.createParameterizedTransform(parameters); } catch (FactoryException exception) { fail(exception.getLocalizedMessage()); return null; } return new DefaultProjectedCRS("Stereographic", base, mt, DefaultCartesianCS.PROJECTED); } catch (NoSuchIdentifierException exception) { fail(exception.getLocalizedMessage()); return null; } }
private CoordinateReferenceSystem getCRS(Object source) { CoordinateReferenceSystem crs = null; if (source instanceof File || (source instanceof URL && (((URL) source).getProtocol() == "file"))) { // getting name for the prj file final String sourceAsString; if (source instanceof File) { sourceAsString = ((File) source).getAbsolutePath(); } else { String auth = ((URL) source).getAuthority(); String path = ((URL) source).getPath(); if (auth != null && !auth.equals("")) { sourceAsString = "//" + auth + path; } else { sourceAsString = path; } } final int index = sourceAsString.lastIndexOf("."); final String base = index > 0 ? sourceAsString.substring(0, index) + ".prj" : sourceAsString + ".prj"; // does it exist? final File prjFile = new File(base.toString()); if (prjFile.exists()) { // it exists then we have top read it PrjFileReader projReader = null; FileInputStream instream = null; try { instream = new FileInputStream(prjFile); final FileChannel channel = instream.getChannel(); projReader = new PrjFileReader(channel); crs = projReader.getCoordinateReferenceSystem(); } catch (FileNotFoundException e) { // warn about the error but proceed, it is not fatal // we have at least the default crs to use LOGGER.log(Level.INFO, e.getLocalizedMessage(), e); } catch (IOException e) { // warn about the error but proceed, it is not fatal // we have at least the default crs to use LOGGER.log(Level.INFO, e.getLocalizedMessage(), e); } catch (FactoryException e) { // warn about the error but proceed, it is not fatal // we have at least the default crs to use LOGGER.log(Level.INFO, e.getLocalizedMessage(), e); } finally { if (projReader != null) try { projReader.close(); } catch (IOException e) { // warn about the error but proceed, it is not fatal // we have at least the default crs to use LOGGER.log(Level.FINE, e.getLocalizedMessage(), e); } if (instream != null) try { instream.close(); } catch (IOException e) { // warn about the error but proceed, it is not fatal // we have at least the default crs to use LOGGER.log(Level.FINE, e.getLocalizedMessage(), e); } } } } return crs; }
/** * Gets the coordinate reference system that will be associated to the {@link GridCoverage} by * looking for a related PRJ. */ protected void parsePRJFile() { String prjPath = null; this.crs = null; prjPath = this.parentPath + File.separatorChar + coverageName + ".prj"; // read the prj serviceInfo from the file PrjFileReader projReader = null; FileInputStream inStream = null; FileChannel channel = null; try { final File prj = new File(prjPath); if (prj.exists() && prj.canRead()) { inStream = new FileInputStream(prj); channel = inStream.getChannel(); projReader = new PrjFileReader(channel); this.crs = projReader.getCoordinateReferenceSystem(); } // If some exception occurs, warn about the error but proceed // using a default CRS } catch (FileNotFoundException e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); } } catch (IOException e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); } } catch (FactoryException e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); } } finally { if (projReader != null) { try { projReader.close(); } catch (IOException e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); } } } if (inStream != null) { try { inStream.close(); } catch (Throwable e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); } } } if (channel != null) { try { channel.close(); } catch (Throwable e) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e); } } } } }
private void computeCropBBOX() throws DataSourceException { // get the crs for the requested bbox if (requestCRS == null) requestCRS = CRS.getHorizontalCRS(requestedBBox.getCoordinateReferenceSystem()); try { // // The destination to source transform has been computed (and eventually erased) already // by inspectCoordinateSystem() // now transform the requested envelope to source crs if (destinationToSourceTransform != null && !destinationToSourceTransform.isIdentity()) { final GeneralEnvelope temp = new GeneralEnvelope(CRS.transform(requestedBBox, coverageProperties.crs2D)); temp.setCoordinateReferenceSystem(coverageProperties.crs2D); cropBBox = new ReferencedEnvelope(temp); needsReprojection = true; } else { // we do not need to do anything, but we do this in order to aboid problems with the // envelope checks cropBBox = new ReferencedEnvelope( requestedBBox.getMinX(), requestedBBox.getMaxX(), requestedBBox.getMinY(), requestedBBox.getMaxY(), coverageProperties.crs2D); } // intersect requested BBox in native CRS with coverage native bbox to get the crop bbox // intersect the requested area with the bounds of this layer in native crs if (!cropBBox.intersects((BoundingBox) coverageProperties.bbox)) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine( new StringBuilder("The computed CropBoundingBox ") .append(cropBBox) .append(" Doesn't intersect the coverage BoundingBox ") .append(coverageProperties.bbox) .append(" resulting in an empty request") .toString()); } cropBBox = null; empty = true; return; } // TODO XXX Optimize when referenced envelope has intersection method that actually retains // the CRS, this is the JTS one cropBBox = new ReferencedEnvelope( ((ReferencedEnvelope) cropBBox).intersection(coverageProperties.bbox), coverageProperties.crs2D); return; } catch (TransformException te) { // something bad happened while trying to transform this // envelope. let's try with wgs84 if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, te.getLocalizedMessage(), te); } try { // can we proceed? Do we have geo stuff to do all these operations? if (coverageProperties.geographicCRS2D != null && coverageProperties.geographicBBox != null) { // // If we can not reproject the requested envelope to the native CRS, // we go back to reproject in the geographic crs of the native // coverage since this usually happens for conversions between CRS // whose area of definition is different // // STEP 1 reproject the requested envelope to the coverage geographic bbox if (!CRS.equalsIgnoreMetadata(coverageProperties.geographicCRS2D, requestCRS)) { // try to convert the requested bbox to the coverage geocrs requestCRSToCoverageGeographicCRS2D = CRS.findMathTransform(requestCRS, coverageProperties.geographicCRS2D, true); if (!requestCRSToCoverageGeographicCRS2D.isIdentity()) { requestedBBOXInCoverageGeographicCRS = CRS.transform(requestedBBox, coverageProperties.geographicCRS2D); requestedBBOXInCoverageGeographicCRS.setCoordinateReferenceSystem( coverageProperties.geographicCRS2D); } } if (requestedBBOXInCoverageGeographicCRS == null) { requestedBBOXInCoverageGeographicCRS = new GeneralEnvelope(requestCRS); } // STEP 2 intersection with the geographic bbox for this coverage if (!requestedBBOXInCoverageGeographicCRS.intersects( coverageProperties.geographicBBox, true)) { cropBBox = null; empty = true; return; } // intersect with the coverage native geographic bbox // note that for the moment we got to use general envelope since there is no intersection // otherwise requestedBBOXInCoverageGeographicCRS.intersect(coverageProperties.geographicBBox); requestedBBOXInCoverageGeographicCRS.setCoordinateReferenceSystem( coverageProperties.geographicCRS2D); // now go back to the coverage native CRS in order to compute an approximate requested // resolution approximateRequestedBBoInNativeCRS = CRS.transform(requestedBBOXInCoverageGeographicCRS, coverageProperties.crs2D); approximateRequestedBBoInNativeCRS.setCoordinateReferenceSystem(coverageProperties.crs2D); cropBBox = new ReferencedEnvelope(approximateRequestedBBoInNativeCRS); return; } } catch (TransformException te) { // something bad happened while trying to transform this // envelope. let's try with wgs84 if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, te.getLocalizedMessage(), te); } catch (FactoryException fe) { // something bad happened while trying to transform this // envelope. let's try with wgs84 if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, fe.getLocalizedMessage(), fe); } LOGGER.log( Level.INFO, "We did not manage to crop the requested envelope, we fall back onto loading the whole coverage."); cropBBox = null; }