Search in sources :

Example 1 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project spatial-portal by AtlasOfLivingAustralia.

the class Util method createCircle.

public static String createCircle(double x, double y, final double radius, int sides) {
    try {
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
        String wkt4326 = "GEOGCS[" + "\"WGS 84\"," + "  DATUM[" + "    \"WGS_1984\"," + "    SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," + "    TOWGS84[0,0,0,0,0,0,0]," + "    AUTHORITY[\"EPSG\",\"6326\"]]," + "  PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]]," + "  UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]]," + "  AXIS[\"Lat\",NORTH]," + "  AXIS[\"Long\",EAST]," + "  AUTHORITY[\"EPSG\",\"4326\"]]";
        String wkt900913 = "PROJCS[\"WGS84 / Google Mercator\", " + "  GEOGCS[\"WGS 84\", " + "   DATUM[\"World Geodetic System 1984\", " + "   SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], " + "  AUTHORITY[\"EPSG\",\"6326\"]], " + " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], " + " UNIT[\"degree\", 0.017453292519943295], " + " AXIS[\"Longitude\", EAST], " + " AXIS[\"Latitude\", NORTH], " + " AUTHORITY[\"EPSG\",\"4326\"]], " + " PROJECTION[\"Mercator_1SP\"], " + " PARAMETER[\"semi_minor\", 6378137.0], " + " PARAMETER[\"latitude_of_origin\", 0.0]," + " PARAMETER[\"central_meridian\", 0.0], " + " PARAMETER[\"scale_factor\", 1.0], " + " PARAMETER[\"false_easting\", 0.0], " + " PARAMETER[\"false_northing\", 0.0], " + " UNIT[\"m\", 1.0], " + " AXIS[\"x\", EAST], " + " AXIS[\"y\", NORTH], " + " AUTHORITY[\"EPSG\",\"3857\"]] ";
        CoordinateReferenceSystem wgsCRS = CRS.parseWKT(wkt4326);
        CoordinateReferenceSystem googleCRS = CRS.parseWKT(wkt900913);
        MathTransform transform = CRS.findMathTransform(wgsCRS, googleCRS);
        Point point = geometryFactory.createPoint(new Coordinate(y, x));
        Geometry geom = JTS.transform(point, transform);
        Point gPoint = geometryFactory.createPoint(new Coordinate(geom.getCoordinate()));
        LOGGER.debug("Google point:" + gPoint.getCoordinate().x + "," + gPoint.getCoordinate().y);
        MathTransform reverseTransform = CRS.findMathTransform(googleCRS, wgsCRS);
        Coordinate[] coords = new Coordinate[sides + 1];
        for (int i = 0; i < sides; i++) {
            double angle = ((double) i / (double) sides) * Math.PI * 2.0;
            double dx = Math.cos(angle) * radius;
            double dy = Math.sin(angle) * radius;
            geom = JTS.transform(geometryFactory.createPoint(new Coordinate(gPoint.getCoordinate().x + dx, gPoint.getCoordinate().y + dy)), reverseTransform);
            coords[i] = new Coordinate(geom.getCoordinate().y, geom.getCoordinate().x);
        }
        coords[sides] = coords[0];
        LinearRing ring = geometryFactory.createLinearRing(coords);
        Polygon polygon = geometryFactory.createPolygon(ring, null);
        WKTWriter writer = new WKTWriter();
        String wkt = writer.write(polygon);
        return wkt.replaceAll(StringConstants.POLYGON + " ", StringConstants.POLYGON).replaceAll(", ", ",");
    } catch (Exception e) {
        LOGGER.debug("Circle fail!");
        return StringConstants.NONE;
    }
}
Also used : WKTWriter(com.vividsolutions.jts.io.WKTWriter) MathTransform(org.opengis.referencing.operation.MathTransform) ParseException(com.vividsolutions.jts.io.ParseException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 2 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.

the class WKTPreferencesCRSFactory method createCoordinateReferenceSystem.

/**
 * @see CRSAuthorityFactory#createCoordinateReferenceSystem(String)
 */
@Override
public synchronized CoordinateReferenceSystem createCoordinateReferenceSystem(String code) throws FactoryException {
    if (code == null) {
        return null;
    }
    if (!code.startsWith(AUTHORITY_PREFIX)) {
        throw new NoSuchAuthorityCodeException("This factory only understands EPSG codes", AUTHORITY, // $NON-NLS-1$
        code);
    }
    final String epsgNumber = code.substring(code.indexOf(':') + 1).trim();
    if (cache.containsKey(epsgNumber)) {
        CoordinateReferenceSystem value = cache.get(epsgNumber);
        if (value != null) {
            // CRS was already created
            return value;
        }
    }
    try {
        node.sync();
    } catch (BackingStoreException e) {
        // $NON-NLS-1$
        _log.warn("Error synchronizing preferences", e);
    }
    String wkt = node.get(epsgNumber, null);
    if (wkt == null) {
        // $NON-NLS-1$
        throw new NoSuchAuthorityCodeException("Unknown EPSG code", AUTHORITY, code);
    }
    if (wkt.indexOf(epsgNumber) == -1) {
        wkt = wkt.trim();
        wkt = wkt.substring(0, wkt.length() - 1);
        // $NON-NLS-1$ //$NON-NLS-2$
        wkt += ",AUTHORITY[\"EPSG\",\"" + epsgNumber + "\"]]";
        // $NON-NLS-1$ //$NON-NLS-2$
        _log.warn("EPSG:" + epsgNumber + " lacks a proper identifying authority in its Well-Known Text. It is being added programmatically.");
    }
    try {
        CoordinateReferenceSystem crs = crsFactory.createFromWKT(wkt);
        cache.put(epsgNumber, crs);
        return crs;
    } catch (FactoryException fex) {
        throw fex;
    }
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) FactoryException(org.opengis.referencing.FactoryException) BackingStoreException(java.util.prefs.BackingStoreException) InternationalString(org.opengis.util.InternationalString) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 3 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.

the class PostGISGeometries method convertGeometry.

@Override
public Object convertGeometry(GeometryProperty<?> geom, TypeDefinition columnType, PGConnection pgconn) throws Exception {
    PGgeometry pGeometry = null;
    // Transform from sourceCRS to targetCRS
    GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
    // transform
    CoordinateReferenceSystem targetCRS = null;
    String authName = columnTypeMetadata.getAuthName();
    if (authName != null && authName.equals("EPSG")) {
        // PostGIS assumes lon/lat
        targetCRS = CRS.decode(authName + ":" + columnTypeMetadata.getSrs(), true);
    } else {
        String wkt = columnTypeMetadata.getSrsText();
        if (wkt != null && !wkt.isEmpty()) {
            targetCRS = CRS.parseWKT(wkt);
        }
    }
    Geometry targetGeometry;
    if (targetCRS != null) {
        MathTransform transform = CRS.findMathTransform(geom.getCRSDefinition().getCRS(), targetCRS);
        targetGeometry = JTS.transform(geom.getGeometry(), transform);
    } else {
        targetGeometry = geom.getGeometry();
    }
    // Convert the jts Geometry to postgis PGgeometry and set the SRSID
    pGeometry = new PGgeometry(targetGeometry.toText());
    try {
        pGeometry.getGeometry().setSrid(Integer.parseInt(columnTypeMetadata.getSrs()));
    } catch (Exception e) {
    // ignore
    }
    return pGeometry;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) MathTransform(org.opengis.referencing.operation.MathTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) SQLException(java.sql.SQLException) PGgeometry(org.postgis.PGgeometry)

Example 4 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.

the class GeotoolsConverter method convert.

/**
 * @see GeoConverter#convert(GeoPosition, int)
 */
@Override
public GeoPosition convert(GeoPosition pos, int targetEpsg) throws IllegalGeoPositionException {
    if (targetEpsg == pos.getEpsgCode())
        return new GeoPosition(pos.getX(), pos.getY(), targetEpsg);
    try {
        CoordinateReferenceSystem sourceCRS = getCRS(pos.getEpsgCode());
        CoordinateReferenceSystem targetCRS = getCRS(targetEpsg);
        MathTransform math = getTransform(pos.getEpsgCode(), targetEpsg, sourceCRS, targetCRS);
        int dimension = sourceCRS.getCoordinateSystem().getDimension();
        DirectPosition pt1;
        boolean flipSource = flipCRS(sourceCRS);
        switch(dimension) {
            case 2:
                pt1 = new GeneralDirectPosition((flipSource) ? (pos.getY()) : (pos.getX()), (flipSource) ? (pos.getX()) : (pos.getY()));
                break;
            case 3:
                pt1 = new GeneralDirectPosition((flipSource) ? (pos.getY()) : (pos.getX()), (flipSource) ? (pos.getX()) : (pos.getY()), 0);
                break;
            default:
                log.error("Unsupported dimension: " + dimension);
                throw new IllegalArgumentException("Unsupported dimension: " + dimension);
        }
        DirectPosition pt2 = math.transform(pt1, null);
        if (flipCRS(targetCRS))
            return new GeoPosition(pt2.getOrdinate(1), pt2.getOrdinate(0), targetEpsg);
        else
            return new GeoPosition(pt2.getOrdinate(0), pt2.getOrdinate(1), targetEpsg);
    } catch (Exception e) {
        throw new IllegalGeoPositionException(e);
    }
}
Also used : GeneralDirectPosition(org.geotools.geometry.GeneralDirectPosition) DirectPosition(org.opengis.geometry.DirectPosition) GeneralDirectPosition(org.geotools.geometry.GeneralDirectPosition) MathTransform(org.opengis.referencing.operation.MathTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.referencing.FactoryException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException)

Example 5 with CoordinateReferenceSystem

use of org.opengis.referencing.crs.CoordinateReferenceSystem in project hale by halestudio.

the class GeotoolsConverter method getCRS.

private CoordinateReferenceSystem getCRS(int epsg) throws NoSuchAuthorityCodeException, FactoryException {
    CoordinateReferenceSystem r = crsMap.get(epsg);
    if (r == null) {
        r = CRS.decode("EPSG:" + epsg, true);
        crsMap.put(epsg, r);
    }
    return r;
}
Also used : CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)210 Test (org.junit.Test)80 MathTransform (org.opengis.referencing.operation.MathTransform)32 FactoryException (org.opengis.referencing.FactoryException)25 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)24 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)23 Geometry (com.vividsolutions.jts.geom.Geometry)21 TransformException (org.opengis.referencing.operation.TransformException)21 DependsOnMethod (org.apache.sis.test.DependsOnMethod)19 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)13 Geometry (org.locationtech.jts.geom.Geometry)11 FactoryException (org.opengis.util.FactoryException)11 SimpleFeature (org.opengis.feature.simple.SimpleFeature)9 DirectPosition (org.opengis.geometry.DirectPosition)9 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)9 VerticalCRS (org.opengis.referencing.crs.VerticalCRS)9 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)9 ArrayList (java.util.ArrayList)8 GeometryType (org.opengis.feature.type.GeometryType)8 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)7