private static boolean intersects( Envelope requestEnv, String requestCrs, Envelope regionEnv, String regionCrs) throws CRSException, GeometryException, CRSTransformationException, UnknownCRSException { Surface request = GeometryFactory.createSurface(requestEnv, CRSFactory.create(requestCrs)); Surface region = GeometryFactory.createSurface(regionEnv, CRSFactory.create(regionCrs)); if (!requestCrs.equalsIgnoreCase(regionCrs)) { GeoTransformer gt = new GeoTransformer(requestCrs); region = (Surface) gt.transform(region); } return request.intersects(region); }
/** return the LonLatEnvelope of the entire image in "EPSG:4326" */ private Envelope getLLEAsEnvelope() { String code = getNativeSRSCode(); LonLatEnvelope lle = description.getLonLatEnvelope(); Envelope tmp = GeometryFactory.createEnvelope( lle.getMin().getX(), lle.getMin().getY(), lle.getMax().getX(), lle.getMax().getY(), null); try { if (!code.equals("EPSG:4326")) { GeoTransformer trans = new GeoTransformer(code); tmp = trans.transform(tmp, "EPSG:4326"); } } catch (Exception e) { LOGGER.logError(StringTools.stackTraceToString(e)); } return tmp; }
private static void transformRasterFile( String inFile, String inCRS, String outFile, String outCRS, Float imageQuality, int ppgridsize, int polynomOrder) throws Exception { System.out.println("Loading raster " + inFile); BufferedImage image = ImageUtils.loadImage(inFile); CoordinateSystem sourceCRS = CRSFactory.create(inCRS); CoordinateSystem targetCRS = CRSFactory.create(outCRS); WorldFile worldFile = WorldFile.readWorldFile(inFile, WorldFile.TYPE.CENTER, image); Envelope inEnvelope = worldFile.getEnvelope(); // create minimal CoverageOffering for ImageGridCoverage // most parts are not used DomainSet ds = new DomainSet(new SpatialDomain(new Envelope[] {inEnvelope})); RangeSet rs = new RangeSet("", ""); CodeList[] dummyCodeList = new CodeList[] {new CodeList("", new String[] {})}; CodeList[] nativeSRSCodeList = new CodeList[] {new CodeList("", new String[] {inCRS})}; SupportedSRSs supSRSs = new SupportedSRSs(dummyCodeList, dummyCodeList, dummyCodeList, nativeSRSCodeList); SupportedFormats supFormats = new SupportedFormats(dummyCodeList); CoverageOffering coverageOffering = new CoverageOffering("", "", "", null, null, null, ds, rs, supSRSs, supFormats, null, null); ImageGridCoverage igc = new ImageGridCoverage(coverageOffering, inEnvelope, image); GeoTransformer gt = new GeoTransformer(targetCRS); Envelope outEnvelope = gt.transform(inEnvelope, sourceCRS, true); // calculate new output size // use square pixels for output, ie. the aspect ratio changes double deltaX = outEnvelope.getWidth(); double deltaY = outEnvelope.getHeight(); double diagSize = Math.sqrt(deltaX * deltaX + deltaY * deltaY); // pixelSize for calculation of the new image size double pixelSize = diagSize / Math.sqrt(Math.pow(image.getWidth(), 2) + Math.pow(image.getHeight(), 2)); int height = (int) (deltaY / pixelSize + 0.5); int width = (int) (deltaX / pixelSize + 0.5); // realPixelSize for center type world files, etc. double realPixelSize = diagSize / Math.sqrt(Math.pow(image.getWidth() - 1, 2) + Math.pow(image.getHeight() - 1, 2)); System.out.println("Transforming raster from " + inCRS + " to " + outCRS); igc = (ImageGridCoverage) gt.transform( igc, outEnvelope, width, height, ppgridsize, polynomOrder, new InterpolationNearest()); image = igc.getAsImage(-1, -1); System.out.println("Saving raster " + outFile); ImageUtils.saveImage(image, outFile, imageQuality); // save new WorldFile WorldFile outWorldFile = new WorldFile(realPixelSize, realPixelSize, 0.0f, 0.0f, outEnvelope); String basename = outFile.substring(0, outFile.lastIndexOf(".")); WorldFile.writeWorldFile(outWorldFile, basename); }