private static CrsGeoCoding createMapGeoCoding(Rectangle outputRegion, double pixelSize) { CrsGeoCoding geoCoding; try { geoCoding = new CrsGeoCoding(DefaultGeographicCRS.WGS84, outputRegion.width, outputRegion.height, -180.0 + pixelSize * outputRegion.x, 90.0 - pixelSize * outputRegion.y, pixelSize, pixelSize, 0.0, 0.0); } catch (FactoryException e) { throw new IllegalStateException(e); } catch (TransformException e) { throw new IllegalStateException(e); } return geoCoding; } }
private void writeCrsGeoCoding(CrsGeoCoding geoCoding, String nodeType, StringBuffer sb) { sb.append("\n"); sb.append("\nThe ").append(nodeType).append(" uses a geo-coding based on a cartographic map CRS.\n"); sb.append("\n"); sb.append("Well-known text format (WKT) of the map CRS:\n\n"); sb.append(geoCoding.getMapCRS().toString()); sb.append("\n"); sb.append("Image-to-map transformation:\n\n"); sb.append(geoCoding.getImageToMapTransform().toString()); }
final CoordinateReferenceSystem crs = crsGeoCoding.getMapCRS(); GeoPos centrePos = crsGeoCoding.getGeoPos(new PixelPos(product.getSceneRasterWidth() / 2, product.getSceneRasterHeight() / 2), null); utmHemisphere = centrePos.getLat() > 0 ? "North" : "South"; pixelSizeX = imgGeom.getPixelSizeX(); pixelSizeY = imgGeom.getPixelSizeY(); datumName = crsGeoCoding.getDatum().getName();
@Override public boolean transferGeoCoding(Scene srcScene, Scene destScene, ProductSubsetDef subsetDef) { final AffineTransform destTransform = new AffineTransform(imageToMap); Rectangle destBounds = new Rectangle(destScene.getRasterWidth(), destScene.getRasterHeight()); if (subsetDef != null) { final Rectangle region = subsetDef.getRegion(); double scaleX = subsetDef.getSubSamplingX(); double scaleY = subsetDef.getSubSamplingY(); if (region != null) { destTransform.translate(region.getX(), region.getY()); destBounds.setRect(0, 0, region.getWidth() / scaleX, region.getHeight() / scaleY); } destTransform.scale(scaleX, scaleY); } try { destScene.setGeoCoding(new CrsGeoCoding(getMapCRS(), destBounds, destTransform)); } catch (Exception e) { Debug.trace(e); return false; } return true; }
private boolean detect180MeridianCrossing() throws TransformException, FactoryException { final Rectangle bounds = this.imageBounds; ReferencedEnvelope referencedEnvelope = new ReferencedEnvelope(bounds.getMinX() + 0.5, bounds.getMaxX() - 0.5, bounds.getMinY() + 0.5, bounds.getMaxY() - 0.5, getImageCRS()); referencedEnvelope = referencedEnvelope.transform(getGeoCRS(), true); final DirectPosition uc = referencedEnvelope.getUpperCorner(); final DirectPosition lc = referencedEnvelope.getLowerCorner(); return uc.getOrdinate(0) > 180 || lc.getOrdinate(0) < -180; }
public void testCreateGeoCodingForCrsGeoCoding() throws Exception { final Rectangle imageBounds = new Rectangle(product.getSceneRasterWidth(), product.getSceneRasterHeight()); final AffineTransform expectedI2m = new AffineTransform(0.12, 1.23, 2.34, 3.45, 4.56, 5.67); final CoordinateReferenceSystem expectedCrs = CRS.decode("EPSG:4326"); final byte[] bytes = createCrsGeoCodingString( new CrsGeoCoding(expectedCrs, imageBounds, expectedI2m)).getBytes(); final Document dom = DimapProductHelpers.createDom(new ByteArrayInputStream(bytes)); final GeoCoding geoCoding = DimapProductHelpers.createGeoCoding(dom, product)[0]; assertNotNull(geoCoding); assertEquals(CrsGeoCoding.class, geoCoding.getClass()); final CrsGeoCoding crsGeoCoding = (CrsGeoCoding) geoCoding; final CoordinateReferenceSystem mapCRS = crsGeoCoding.getMapCRS(); // ignoring metadata because scope and domainOfValidity are not restored // but not important for our GeoCoding assertTrue(CRS.equalsIgnoreMetadata(expectedCrs, mapCRS)); assertEquals(expectedI2m, crsGeoCoding.getImageToMapTransform()); }
@Test public void testCustomSpheroidDatum() throws TransformException, FactoryException { String wkt = "PROJCS[\"MODIS_Sinusoidal\", \n" + " GEOGCS[\"Unknown datum based upon the custom spheroid\", \n" + " DATUM[\"Not specified (based on custom spheroid)\", \n" + " SPHEROID[\"Custom spheroid\", 6371007.181, 0.0]], \n" + " PRIMEM[\"Greenwich\", 0.0], \n" + " UNIT[\"degree\", 0.017453292519943295], \n" + " AXIS[\"Longitude\", EAST], \n" + " AXIS[\"Latitude\", NORTH]], \n" + " PROJECTION[\"Sinusoidal\"], \n" + " PARAMETER[\"central_meridian\", 0.0], \n" + " PARAMETER[\"scale_factor\", 1.0], \n" + " PARAMETER[\"false_easting\", 0.0], \n" + " PARAMETER[\"false_northing\", 0.0], \n" + " UNIT[\"m\", 1.0], \n" + " AXIS[\"x\", EAST], \n" + " AXIS[\"y\", NORTH]]"; CoordinateReferenceSystem crs = CRS.parseWKT(wkt); CrsGeoCoding geoCoding = new CrsGeoCoding(crs, new Rectangle(10, 10, 10, 10), new AffineTransform()); DefaultGeographicCRS defaultCrs = (DefaultGeographicCRS) geoCoding.getGeoCRS(); Ellipsoid wgs84Spheroid = DefaultGeographicCRS.WGS84.getDatum().getEllipsoid(); Ellipsoid customSpheroid = defaultCrs.getDatum().getEllipsoid(); assertNotSame(DefaultGeographicCRS.WGS84, defaultCrs); assertTrue(wgs84Spheroid.getSemiMinorAxis() != customSpheroid.getSemiMinorAxis()); assertTrue(wgs84Spheroid.getSemiMajorAxis() != customSpheroid.getSemiMajorAxis()); assertSame(DefaultEllipsoidalCS.GEODETIC_2D, defaultCrs.getCoordinateSystem()); }
@Test public void testTransferGeoCodingWithSubset_Subsampling() { final ProductSubsetDef subsetDef = new ProductSubsetDef("subset"); subsetDef.setSubSampling(2, 4); final boolean transfered = srcScene.transferGeoCodingTo(destScene, subsetDef); assertTrue(transfered); final GeoCoding destGeoCoding = destScene.getGeoCoding(); assertNotNull(destGeoCoding); assertNotSame(srcGeoCoding, destGeoCoding); assertEquals(srcGeoCoding.getDatum(), destGeoCoding.getDatum()); assertEquals(srcGeoCoding.getMapCRS(), destGeoCoding.getMapCRS()); assertEquals(srcGeoCoding.getGeoCRS(), destGeoCoding.getGeoCRS()); comparePixelPos(destGeoCoding, new PixelPos(0, 0), new PixelPos(0, 0)); comparePixelPos(destGeoCoding, new PixelPos(8, 0), new PixelPos(4, 0)); comparePixelPos(destGeoCoding, new PixelPos(8, 16), new PixelPos(4, 4)); comparePixelPos(destGeoCoding, new PixelPos(0, 16), new PixelPos(0, 4)); }
@Test public void testTransferGeoCodingWithoutSubset() { final boolean returnValue = srcScene.transferGeoCodingTo(destScene, null); assertTrue(returnValue); final GeoCoding destGeoCoding = destScene.getGeoCoding(); assertNotNull(destGeoCoding); assertNotSame(srcGeoCoding, destGeoCoding); assertEquals(srcGeoCoding.getDatum(), destGeoCoding.getDatum()); assertEquals(srcGeoCoding.getMapCRS(), destGeoCoding.getMapCRS()); assertEquals(srcGeoCoding.getGeoCRS(), destGeoCoding.getGeoCRS()); assertEquals(srcGeoCoding.getGeoPos(new PixelPos(3.5f, 0.5f), null), destGeoCoding.getGeoPos(new PixelPos(3.5f, 0.5f), null)); }
this.imageBounds = imageBounds; this.imageToMap = imageToMap; setMapCRS(mapCRS); org.opengis.referencing.datum.Ellipsoid gtEllipsoid = CRS.getEllipsoid(mapCRS); String ellipsoidName = gtEllipsoid.getName().getCode(); DerivedCRS derivedCRS = (DerivedCRS) mapCRS; CoordinateReferenceSystem baseCRS = derivedCRS.getBaseCRS(); setGeoCRS(baseCRS); } else if (gtDatum instanceof GeodeticDatum) { setGeoCRS(new DefaultGeographicCRS((GeodeticDatum) gtDatum, DefaultEllipsoidalCS.GEODETIC_2D)); } else { setGeoCRS(DefaultGeographicCRS.WGS84); setImageCRS(createImageCRS(mapCRS, i2m.inverse())); MathTransform map2Geo = CRS.findMathTransform(mapCRS, getGeoCRS(), true); Hints hints = new Hints(Hints.LENIENT_DATUM_SHIFT, Boolean.TRUE); crossingMeridianAt180 = detect180MeridianCrossing();
@Test public void testTransferGeoCodingWithSubset_Region() { final ProductSubsetDef subsetDef = new ProductSubsetDef("subset"); subsetDef.setRegion(2, 2, 4, 4); final boolean transfered = srcScene.transferGeoCodingTo(destScene, subsetDef); assertTrue(transfered); final GeoCoding destGeoCoding = destScene.getGeoCoding(); assertNotNull(destGeoCoding); assertNotSame(srcGeoCoding, destGeoCoding); assertEquals(srcGeoCoding.getDatum(), destGeoCoding.getDatum()); assertEquals(srcGeoCoding.getMapCRS(), destGeoCoding.getMapCRS()); // position (3,3) in source equals (1,1) in dest comparePixelPos(destGeoCoding, new PixelPos(3, 3), new PixelPos(1, 1)); }
@Override public String toString() { final String s = super.toString(); return s + "\n\n" + "Map CRS:\n" + getMapCRS().toString() + "\n" + "Image To Map:\n" + imageToMap.toString(); }
@Test public void testTransferGeoCodingWithSubset_SubsamplingAndRegion() { final ProductSubsetDef subsetDef = new ProductSubsetDef("subset"); subsetDef.setRegion(2, 2, 8, 8); subsetDef.setSubSampling(2, 2); final boolean transfered = srcScene.transferGeoCodingTo(destScene, subsetDef); assertTrue(transfered); final GeoCoding destGeoCoding = destScene.getGeoCoding(); assertNotNull(destGeoCoding); assertNotSame(srcGeoCoding, destGeoCoding); assertEquals(srcGeoCoding.getDatum(), destGeoCoding.getDatum()); assertEquals(srcGeoCoding.getMapCRS(), destGeoCoding.getMapCRS()); assertEquals(srcGeoCoding.getGeoCRS(), destGeoCoding.getGeoCRS()); comparePixelPos(destGeoCoding, new PixelPos(2, 2), new PixelPos(0, 0)); comparePixelPos(destGeoCoding, new PixelPos(10, 2), new PixelPos(4, 0)); comparePixelPos(destGeoCoding, new PixelPos(10, 10), new PixelPos(4, 4)); comparePixelPos(destGeoCoding, new PixelPos(2, 10), new PixelPos(0, 4)); }
private void initGeoCoding() throws IOException { float pixelX = 0.0f; float pixelY = 0.0f; float easting = -180f; float northing = +90f; float pixelSizeX = 360.0f / _sceneRasterWidth; float pixelSizeY = 180.0f / _sceneRasterHeight; try { _product.setGeoCoding(new CrsGeoCoding(DefaultGeographicCRS.WGS84, _sceneRasterWidth, _sceneRasterHeight, easting, northing, pixelSizeX, pixelSizeY, pixelX, pixelY)); } catch (FactoryException e) { throw new IOException(e); } catch (TransformException e) { throw new IOException(e); } }
private void writeCrsGeoCoding(CrsGeoCoding geoCoding, String nodeType) { addRow("The " + nodeType + " uses a geo-coding based on a cartographic map CRS."); addEmptyRow(); addRow("WKT of the map CRS", geoCoding.getMapCRS().toString()); addEmptyRow(); addRow("Image-to-map transformation", geoCoding.getImageToMapTransform().toString()); }
private void initGeoCoding(Product product) throws IOException { float pixelX = 0.0f; float pixelY = 0.0f; float easting = -180f; float northing = +90f; float pixelSizeX = 360.0f / sceneWidth; float pixelSizeY = 180.0f / sceneHeight; try { product.setGeoCoding(new CrsGeoCoding(DefaultGeographicCRS.WGS84, sceneWidth, sceneHeight, easting, northing, pixelSizeX, pixelSizeY, pixelX, pixelY)); } catch (FactoryException e) { throw new IOException(e); } catch (TransformException e) { throw new IOException(e); } }
private String createCrsGeoCodingString(CrsGeoCoding geoCoding) { final double[] matrix = new double[6]; final MathTransform transform = geoCoding.getImageToMapTransform(); if (transform instanceof AffineTransform) { ((AffineTransform) transform).getMatrix(matrix); } return "<" + DimapProductConstants.TAG_ROOT + ">" + LS + " <Coordinate_Reference_System>" + LS + " <WKT>" + LS + geoCoding.getMapCRS().toString() + " </WKT>" + LS + " </Coordinate_Reference_System>" + LS + " <Geoposition>" + LS + " <IMAGE_TO_MODEL_TRANSFORM>" + StringUtils.arrayToCsv( matrix) + "</IMAGE_TO_MODEL_TRANSFORM>" + LS + " </Geoposition>" + LS + "</" + DimapProductConstants.TAG_ROOT + ">"; }
private void initGeoCoding() throws IOException { float pixelX = 0.0f; float pixelY = 0.0f; float easting = -180f; float northing = +90f; pixelSizeX = 360.0 / sceneRasterWidth; double pixelSizeY = 180.0 / sceneRasterHeight; try { product.setGeoCoding(new CrsGeoCoding(DefaultGeographicCRS.WGS84, sceneRasterWidth, sceneRasterHeight, easting, northing, pixelSizeX, pixelSizeY, pixelX, pixelY)); } catch (FactoryException e) { throw new IOException(e); } catch (TransformException e) { throw new IOException(e); } }
private void writeGeoCoding(final CrsGeoCoding crsGeoCoding, int indent) { final CoordinateReferenceSystem crs = crsGeoCoding.getMapCRS(); final double[] matrix = new double[6]; final MathTransform transform = crsGeoCoding.getImageToMapTransform(); if (transform instanceof AffineTransform) { ((AffineTransform) transform).getMatrix(matrix); } final String[] crsTags = createTags(indent, DimapProductConstants.TAG_COORDINATE_REFERENCE_SYSTEM); println(crsTags[0]); final String[] wktTags = createTags(indent + 1, DimapProductConstants.TAG_WKT); println(wktTags[0]); final char[] wsChars = new char[wktTags[0].length()]; Arrays.fill(wsChars, ' '); final String ws = new String(wsChars); for (String wktLine : crs.toString().split(SystemUtils.LS)) { print(ws); println(wktLine); } println(wktTags[1]); println(crsTags[1]); final String[] geopositionTags = createTags(indent, DimapProductConstants.TAG_GEOPOSITION); println(geopositionTags[0]); printLine(indent + 1, DimapProductConstants.TAG_IMAGE_TO_MODEL_TRANSFORM, StringUtils.arrayToCsv(matrix)); println(geopositionTags[1]); }
private void initGeoCoding(Product product) throws IOException { float pixelX = 0.0f; float pixelY = 0.0f; float easting = -180f; float northing = +90f; float pixelSizeX = 360.0f / sceneWidth; float pixelSizeY = 180.0f / sceneHeight; try { product.setGeoCoding(new CrsGeoCoding(DefaultGeographicCRS.WGS84, sceneWidth, sceneHeight, easting, northing, pixelSizeX, pixelSizeY, pixelX, pixelY)); } catch (FactoryException e) { throw new IOException(e); } catch (TransformException e) { throw new IOException(e); } }