use of org.opengis.referencing.crs.CoordinateReferenceSystem in project spatial-portal by AtlasOfLivingAustralia.
the class AreaUploadShapefileWizardController method loadOnMap.
private void loadOnMap(Set<FeatureId> ids, String filename) {
try {
final FilterFactory ff = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
Filter filter = ff.id(ids);
// set up the math transform used to process the data
SimpleFeatureType schema = features.getSchema();
CoordinateReferenceSystem dataCRS = schema.getCoordinateReferenceSystem();
CoordinateReferenceSystem wgsCRS = DefaultGeographicCRS.WGS84;
// allow for some error due to different datums
boolean lenient = true;
if (dataCRS == null) {
// attempt to parse prj
try {
File prjFile = new File(filename.substring(0, filename.length() - 3) + "prj");
if (prjFile.exists()) {
String prj = FileUtils.readFileToString(prjFile);
if (prj.equals("PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1.0]]")) {
// support for arcgis online default shp exports
dataCRS = CRS.decode("EPSG:3857");
} else {
dataCRS = CRS.parseWKT(FileUtils.readFileToString(prjFile));
}
}
} catch (Exception e) {
LOGGER.error("failed to read prj for " + filename);
}
if (dataCRS == null) {
dataCRS = DefaultGeographicCRS.WGS84;
}
}
MathTransform transform = CRS.findMathTransform(dataCRS, wgsCRS, lenient);
SimpleFeatureCollection sff = source.getFeatures(filter);
SimpleFeatureIterator fif = sff.features();
StringBuilder sb = new StringBuilder();
StringBuilder sbGeometryCollection = new StringBuilder();
boolean isGeometryCollection = false;
List<Geometry> geoms = new ArrayList<Geometry>();
while (fif.hasNext()) {
SimpleFeature f = fif.next();
LOGGER.debug("Selected Feature: " + f.getID() + " -> " + f.getAttribute("ECOREGION"));
Geometry geom = (Geometry) f.getDefaultGeometry();
geom = JTS.transform(geom, transform);
String wktString = geom.toText();
wktString = wktString.replaceAll(", ", ",");
boolean valid = true;
boolean multipolygon = false;
boolean polygon = false;
boolean geometrycollection = false;
if (wktString.startsWith(StringConstants.MULTIPOLYGON + " ")) {
wktString = wktString.substring((StringConstants.MULTIPOLYGON + " ").length(), wktString.length() - 1);
multipolygon = true;
} else if (wktString.startsWith(StringConstants.POLYGON + " ")) {
wktString = wktString.substring((StringConstants.POLYGON + " ").length());
polygon = true;
} else if (wktString.startsWith(StringConstants.GEOMETRYCOLLECTION + " (")) {
wktString = wktString.substring((StringConstants.GEOMETRYCOLLECTION + " (").length(), wktString.length() - 1);
geometrycollection = true;
isGeometryCollection = true;
} else {
valid = false;
}
if (valid) {
if (sb.length() > 0) {
sb.append(",");
sbGeometryCollection.append(",");
}
sb.append(wktString);
if (multipolygon) {
sbGeometryCollection.append(StringConstants.MULTIPOLYGON).append("(").append(wktString.replace("(((", "(("));
if (!wktString.endsWith(")))")) {
sbGeometryCollection.append(")");
}
} else if (polygon) {
sbGeometryCollection.append(StringConstants.POLYGON).append(wktString);
} else if (geometrycollection) {
sbGeometryCollection.append(wktString);
}
}
}
String wkt;
if (!isGeometryCollection) {
if (!sb.toString().contains(")))")) {
sb.append(")");
}
wkt = StringConstants.MULTIPOLYGON + "(" + sb.toString().replace("(((", "((");
} else {
sbGeometryCollection.append(")");
wkt = StringConstants.GEOMETRYCOLLECTION + "(" + sbGeometryCollection.toString();
getMapComposer().showMessage("Shape is invalid: " + "GEOMETRYCOLLECTION not supported.");
}
GeometryFactory gf = new GeometryFactory();
gf.createGeometryCollection(GeometryFactory.toGeometryArray(geoms));
String msg = "";
boolean invalid = false;
try {
WKTReader wktReader = new WKTReader();
com.vividsolutions.jts.geom.Geometry g = wktReader.read(wkt);
// NC 20130319: Ensure that the WKT is valid according to the WKT standards.
IsValidOp op = new IsValidOp(g);
if (!op.isValid()) {
// this will fix some issues
g = g.buffer(0);
op = new IsValidOp(g);
}
if (!op.isValid()) {
invalid = true;
LOGGER.warn(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + op.getValidationError().getMessage());
msg = op.getValidationError().getMessage();
// TODO Fix invalid WKT text using https://github.com/tudelft-gist/prepair maybe???
} else if (g.isRectangle()) {
// NC 20130319: When the shape is a rectangle ensure that the points a specified in the correct order.
// get the new WKT for the rectangle will possibly need to change the order.
com.vividsolutions.jts.geom.Envelope envelope = g.getEnvelopeInternal();
String wkt2 = StringConstants.POLYGON + "((" + envelope.getMinX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMinY() + "))";
if (!wkt.equals(wkt2)) {
LOGGER.debug("NEW WKT for Rectangle: " + wkt);
msg = CommonData.lang("error_wkt_anticlockwise");
invalid = true;
}
}
if (!invalid) {
invalid = !op.isValid();
}
} catch (ParseException parseException) {
LOGGER.error("error testing validity of uploaded shape file wkt", parseException);
}
if (invalid) {
getMapComposer().showMessage(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + msg);
} else {
MapLayer mapLayer = getMapComposer().addWKTLayer(wkt, layername, layername);
UserDataDTO ud = new UserDataDTO(layername);
ud.setFilename(media.getName());
ud.setUploadedTimeInMs(System.currentTimeMillis());
ud.setType("shapefile");
String metadata = "";
metadata += "User uploaded Shapefile \n";
metadata += "Name: " + ud.getName() + " <br />\n";
metadata += "Filename: " + ud.getFilename() + " <br />\n";
metadata += "Date: " + ud.getDisplayTime() + " <br />\n";
metadata += "Selected polygons (fid): <br />\n";
metadata += "<ul>";
metadata += "</ul>";
mapLayer.getMapLayerMetadata().setMoreInfo(metadata);
getMapComposer().replaceWKTwithWMS(mapLayer);
}
} catch (IOException e) {
LOGGER.debug("IO Error retrieving geometry", e);
} catch (Exception e) {
LOGGER.debug("Generic Error retrieving geometry", e);
}
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class EllipsoidalHeightCombiner method properties.
/**
* Suggests properties for a compound CRS made of the given elements.
* This method builds a default CRS name and domain of validity.
*
* @param components the components for which to get a default set of properties.
* @return suggested properties in a modifiable map. Callers can modify the returned map.
*/
public static Map<String, Object> properties(final CoordinateReferenceSystem... components) {
final StringBuilder name = new StringBuilder(40);
Extent domain = null;
for (int i = 0; i < components.length; i++) {
final CoordinateReferenceSystem crs = components[i];
ArgumentChecks.ensureNonNullElement("components", i, crs);
if (i != 0)
name.append(" + ");
name.append(crs.getName().getCode());
domain = Extents.intersection(domain, crs.getDomainOfValidity());
}
final Map<String, Object> properties = new HashMap<>(2);
properties.put(CoordinateReferenceSystem.NAME_KEY, name.toString());
properties.put(CoordinateReferenceSystem.DOMAIN_OF_VALIDITY_KEY, domain);
return properties;
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class CoordinateOperationFinderTest method testPositionVectorTransformation.
/**
* Tests a datum shift applied as a position vector transformation in geocentric domain.
* This test does not use the the EPSG geodetic dataset.
*
* @throws ParseException if a CRS used in this test can not be parsed.
* @throws FactoryException if the operation can not be created.
* @throws TransformException if an error occurred while converting the test points.
*
* @see DefaultCoordinateOperationFactoryTest#testPositionVectorTransformation()
* @see <a href="https://issues.apache.org/jira/browse/SIS-364">SIS-364</a>
*
* @since 0.8
*/
@Test
@DependsOnMethod("testGeographicToProjected")
public void testPositionVectorTransformation() throws ParseException, FactoryException, TransformException {
final CoordinateReferenceSystem sourceCRS = CommonCRS.WGS84.geographic();
final CoordinateReferenceSystem targetCRS = parse(AGD66());
final CoordinateOperation operation = finder.createOperation(sourceCRS, targetCRS);
transform = operation.getMathTransform();
tolerance = LINEAR_TOLERANCE;
λDimension = new int[] { 0 };
verifyTransform(expectedAGD66(true), expectedAGD66(false));
validate();
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class CoordinateOperationFinderTest method testGeocentricTranslationInGeocentricDomain.
/**
* Tests a transformation using the <cite>"Geocentric translations (geocentric domain)"</cite> method,
* together with a longitude rotation and unit conversion. The CRS and sample point are derived from
* the GR3DF97A – <cite>Grille de paramètres de transformation de coordonnées</cite> document.
*
* @throws ParseException if a CRS used in this test can not be parsed.
* @throws FactoryException if the operation can not be created.
* @throws TransformException if an error occurred while converting the test points.
*/
@Test
@DependsOnMethod("testLongitudeRotation")
public void testGeocentricTranslationInGeocentricDomain() throws ParseException, FactoryException, TransformException {
final CoordinateReferenceSystem sourceCRS = parse("GeodeticCRS[“NTF (Paris)”, $NTF,\n" + // in degrees.
" PrimeMeridian[“Paris”, 2.33722917],\n" + " CS[Cartesian, 3],\n" + " Axis[“(X)”, geocentricX],\n" + " Axis[“(Y)”, geocentricY],\n" + " Axis[“(Z)”, geocentricZ],\n" + " Unit[“kilometre”, 1000]]");
final GeocentricCRS targetCRS = CommonCRS.WGS84.geocentric();
final CoordinateOperation operation = finder.createOperation(sourceCRS, targetCRS);
assertSame("sourceCRS", sourceCRS, operation.getSourceCRS());
assertSame("targetCRS", targetCRS, operation.getTargetCRS());
assertFalse("isIdentity", operation.getMathTransform().isIdentity());
assertEquals("name", "Datum shift", operation.getName().getCode());
assertSetEquals(Arrays.asList(DATUM_SHIFT_APPLIED), operation.getCoordinateOperationAccuracy());
assertInstanceOf("operation", Transformation.class, operation);
assertEquals("method", "Geocentric translations (geocentric domain)", ((SingleOperation) operation).getMethod().getName().getCode());
/*
* Same test point than the one used in FranceGeocentricInterpolationTest:
*
* ┌────────────────────────────────────────────┬──────────────────────────────────────────────────────────┐
* │ Geographic coordinates (°) │ Geocentric coordinates (m) │
* ├────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
* │ NTF: 48°50′40.2441″N 2°25′32.4187″E │ X = 4201905.725 Y = 177998.072 Z = 4778904.260 │
* │ RGF: 48°50′39.9967″N 2°25′29.8273″E │ ΔX = -168 ΔY = -60 ΔZ = 320 │
* └────────────────────────────────────────────┴──────────────────────────────────────────────────────────┘
*
* The source coordinate below is different than in the above table because the prime meridian is set to the
* Paris meridian, so there is a longitude rotation to take in account for X and Y axes.
*/
transform = operation.getMathTransform();
tolerance = LINEAR_TOLERANCE;
verifyTransform(// Paris prime meridian
new double[] { 4205.669137, 6.491944, 4778.904260 }, // Greenwich prime meridian
new double[] { 4201737.725, 177938.072, 4779224.260 });
validate();
}
use of org.opengis.referencing.crs.CoordinateReferenceSystem in project sis by apache.
the class CoordinateOperationFinderTest method testProjected4D_to_2D.
/**
* Tests conversion from four-dimensional compound CRS to two-dimensional projected CRS.
*
* @throws ParseException if a CRS used in this test can not be parsed.
* @throws FactoryException if the operation can not be created.
* @throws TransformException if an error occurred while converting the test points.
*/
@Test
@DependsOnMethod("testTemporalConversion")
public void testProjected4D_to_2D() throws ParseException, FactoryException, TransformException {
final CoordinateReferenceSystem targetCRS = parse("ProjectedCRS[“WGS 84 / World Mercator”,\n" + " BaseGeodCRS[“WGS 84”,\n" + " Datum[“World Geodetic System 1984”,\n" + " Ellipsoid[“WGS 84”, 6378137.0, 298.257223563]]],\n" + " Conversion[“WGS 84 / World Mercator”,\n" + " Method[“Mercator (1SP)”]],\n" + " CS[Cartesian, 2],\n" + " Axis[“Easting”, EAST],\n" + " Axis[“Northing”, NORTH],\n" + " Unit[“m”, 1],\n" + " Id[“EPSG”, “3395”]]");
CoordinateReferenceSystem sourceCRS = targetCRS;
sourceCRS = compound("Mercator 3D", sourceCRS, CommonCRS.Vertical.MEAN_SEA_LEVEL.crs());
sourceCRS = compound("Mercator 4D", sourceCRS, CommonCRS.Temporal.MODIFIED_JULIAN.crs());
final CoordinateOperation operation = finder.createOperation(sourceCRS, targetCRS);
assertSame("sourceCRS", sourceCRS, operation.getSourceCRS());
assertSame("targetCRS", targetCRS, operation.getTargetCRS());
transform = operation.getMathTransform();
assertFalse("transform.isIdentity", transform.isIdentity());
assertInstanceOf("The somewhat complex MathTransform chain should have been simplified " + "to a single affine transform.", LinearTransform.class, transform);
assertInstanceOf("The operation should be a simple axis change, not a complex" + "chain of ConcatenatedOperations.", Conversion.class, operation);
assertEquals("sourceDimensions", 4, transform.getSourceDimensions());
assertEquals("targetDimensions", 2, transform.getTargetDimensions());
Assert.assertMatrixEquals("transform.matrix", Matrices.create(3, 5, new double[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 }), ((LinearTransform) transform).getMatrix(), STRICT);
isInverseTransformSupported = false;
verifyTransform(new double[] { 0, 0, 0, 0, 1000, -2000, 20, 4000 }, new double[] { 0, 0, 1000, -2000 });
validate();
}
Aggregations