use of org.opengis.referencing.crs.CoordinateReferenceSystem in project structr by structr.
the class LatLonToUTMFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (arrayHasLengthAndAllElementsNotNull(sources, 2)) {
final Double lat = getDoubleOrNull(sources[0]);
final Double lon = getDoubleOrNull(sources[1]);
if (lat != null && lon != null) {
try {
final StringBuilder epsg = new StringBuilder("EPSG:32");
final int utmZone = getUTMZone(lat, lon);
if (lat < 0.0) {
// southern hemisphere
epsg.append("7");
} else {
// northern hemisphere
epsg.append("6");
}
if (utmZone < 10) {
epsg.append("0");
}
epsg.append(utmZone);
final CoordinateReferenceSystem src = CRS.decode("EPSG:4326");
final CoordinateReferenceSystem dst = CRS.decode(epsg.toString());
final MathTransform transform = CRS.findMathTransform(src, dst, true);
final DirectPosition sourcePt = new DirectPosition2D(lat, lon);
final DirectPosition targetPt = transform.transform(sourcePt, null);
final String code = dst.getName().getCode();
final int pos = code.lastIndexOf(" ") + 1;
final String zoneName = code.substring(pos, code.length() - 1);
final String band = getLatitudeBand(lat, lon);
final StringBuilder buf = new StringBuilder();
buf.append(zoneName);
buf.append(band);
buf.append(" ");
buf.append((int) Math.rint(targetPt.getOrdinate(0)));
buf.append(" ");
buf.append((int) Math.rint(targetPt.getOrdinate(1)));
// return result
return buf.toString();
} catch (Throwable t) {
logger.warn("", t);
}
} else {
logger.warn("Invalid argument(s), cannot convert to double: {}, {}", new Object[] { sources[0], sources[1] });
}
}
return usage(ctx != null ? ctx.isJavaScriptContext() : false);
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class AuthorityFactoriesTest method testHttp.
/**
* Tests creation of CRS from codes in the {@code "http://www.opengis.net/gml/srs/"} name space.
*
* @throws FactoryException if a CRS creation failed.
*/
@Test
public void testHttp() throws FactoryException {
final CRSAuthorityFactory factory = AuthorityFactories.ALL;
final CRSAuthorityFactory wms = AuthorityFactories.ALL.getAuthorityFactory(CRSAuthorityFactory.class, Constants.OGC, null);
CoordinateReferenceSystem actual, expected;
actual = factory.createCoordinateReferenceSystem("http://www.opengis.net/gml/srs/CRS#84");
expected = wms.createCoordinateReferenceSystem("84");
assertSame(expected, actual);
actual = factory.createCoordinateReferenceSystem("HTTP://WWW.OPENGIS.NET/GML/SRS/crs#84");
assertSame(expected, actual);
actual = factory.createCoordinateReferenceSystem("http://www.opengis.net/gml/srs/CRS.xml#84");
assertSame(expected, actual);
try {
factory.createCoordinateReferenceSystem("http://www.dummy.net/gml/srs/CRS#84");
fail("Should not accept http://www.dummy.net");
} catch (NoSuchAuthorityCodeException e) {
assertNotNull(e.getMessage());
}
try {
factory.createCoordinateReferenceSystem("http://www.opengis.net/gml/dummy/CRS#84");
fail("Should not accept “dummy” as an authority");
} catch (NoSuchAuthorityCodeException e) {
assertNotNull(e.getMessage());
}
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class CRSTest method testFromWKT.
/**
* Tests simple WKT parsing. It is not the purpose of this class to test extensively the WKT parser;
* those tests are rather done by {@link org.apache.sis.io.wkt.GeodeticObjectParserTest}.
* Here we merely test that {@link CRS#fromWKT(String)} is connected to the parser.
*
* @throws FactoryException if an error occurred while parsing the WKT.
*/
@Test
public void testFromWKT() throws FactoryException {
final CoordinateReferenceSystem crs = CRS.fromWKT("GEOGCS[\"GCS WGS 1984\"," + "DATUM[\"WGS 1984\",SPHEROID[\"WGS 1984\",6378137,298.257223563]]," + "PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]]");
assertInstanceOf("GEOGCS", DefaultGeographicCRS.class, crs);
assertEquals("GCS WGS 1984", crs.getName().getCode());
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class EllipsoidalHeightCombinerTest method testProjectedCRS.
/**
* Tests {@link EllipsoidalHeightCombiner#createCompoundCRS EllipsoidalHeightCombiner.createCompoundCRS(…)}
* with a projected CRS.
*
* @throws FactoryException if a CRS can not be created.
*/
@Test
@DependsOnMethod("testGeographicCRS")
public void testProjectedCRS() throws FactoryException {
final EllipsoidalHeightCombiner services = create();
final GeodeticObjectFactory factory = new GeodeticObjectFactory();
final Map<String, String> properties = Collections.singletonMap(CoordinateReferenceSystem.NAME_KEY, "World Mercator (4D)");
final ProjectedCRS horizontal = factory.createProjectedCRS(properties, HardCodedCRS.WGS84, HardCodedConversions.MERCATOR, HardCodedCS.PROJECTED);
final ProjectedCRS volumetric = factory.createProjectedCRS(properties, HardCodedCRS.WGS84_3D, HardCodedConversions.MERCATOR, HardCodedCS.PROJECTED_3D);
final VerticalCRS vertical = HardCodedCRS.ELLIPSOIDAL_HEIGHT;
final TemporalCRS temporal = HardCodedCRS.TIME;
final VerticalCRS geoidal = HardCodedCRS.GRAVITY_RELATED_HEIGHT;
/*
* createCompoundCRS(…) should not combine ProjectedCRS with non-ellipsoidal height.
*/
CoordinateReferenceSystem compound = services.createCompoundCRS(properties, horizontal, geoidal, temporal);
assertArrayEqualsIgnoreMetadata(new SingleCRS[] { horizontal, geoidal, temporal }, CRS.getSingleComponents(compound).toArray());
/*
* createCompoundCRS(…) should combine ProjectedCRS with ellipsoidal height.
*/
compound = services.createCompoundCRS(properties, horizontal, vertical);
assertArrayEqualsIgnoreMetadata(new SingleCRS[] { volumetric }, CRS.getSingleComponents(compound).toArray());
/*
* createCompoundCRS(…) should combine ProjectedCRS with ellipsoidal height and keep time.
*/
compound = services.createCompoundCRS(properties, horizontal, vertical, temporal);
assertArrayEqualsIgnoreMetadata(new SingleCRS[] { volumetric, temporal }, CRS.getSingleComponents(compound).toArray());
/*
* Non-standard feature: accept (VerticalCRS + ProjectedCRS) order.
*/
compound = services.createCompoundCRS(properties, temporal, vertical, horizontal);
final Object[] components = CRS.getSingleComponents(compound).toArray();
assertEquals(2, components.length);
assertEqualsIgnoreMetadata(temporal, components[0]);
assertInstanceOf("Shall be a three-dimensional projected CRS.", ProjectedCRS.class, components[1]);
assertAxisDirectionsEqual("Shall be a three-dimensional projected CRS.", ((CoordinateReferenceSystem) components[1]).getCoordinateSystem(), AxisDirection.UP, AxisDirection.EAST, AxisDirection.NORTH);
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class IntegrationTest method testCRS.
/**
* Tests usage of {@link CRS#forCode(String)}. Note that the {@code "Proj4::"} prefix needs two colons,
* otherwise the text between {@code "Proj4:"} and {@code ":4326"} is interpreted as a version string.
*
* @throws FactoryException if the coordinate reference system can not be created.
* @throws TransformException if an error occurred while testing a coordinate transformation.
*/
@Test
public void testCRS() throws FactoryException, TransformException {
final CoordinateReferenceSystem sourceCRS = CRS.forCode("Proj4::+init=epsg:4326");
final CoordinateReferenceSystem targetCRS = CRS.forCode("Proj4::+init=epsg:3395");
final CoordinateOperation op = CRS.findOperation(sourceCRS, targetCRS, null);
assertInstanceOf("Expected Proj.4 wrapper.", Transform.class, op.getMathTransform());
Proj4FactoryTest.testMercatorProjection(op.getMathTransform());
}
Aggregations