Search in sources :

Example 1 with DefaultProjectedCRS

use of org.apache.sis.referencing.crs.DefaultProjectedCRS in project sis by apache.

the class DefaultMathTransformFactoryTest method testAllMapProjections.

/**
 * Tests the creation of all registered map projections.
 * Only the semi-axis lengths are specified. For the rest, we rely on default values.
 *
 * @throws FactoryException if the construction of a map projection failed.
 *
 * @since 0.7
 */
@Test
public void testAllMapProjections() throws FactoryException {
    /*
         * Gets all map projections and creates a projection using the WGS84 ellipsoid
         * and default parameter values.
         */
    final Map<String, ?> dummyName = Collections.singletonMap(DefaultProjectedCRS.NAME_KEY, "Test");
    final MathTransformFactory mtFactory = DefaultFactories.forBuildin(MathTransformFactory.class);
    final Collection<OperationMethod> methods = mtFactory.getAvailableMethods(Projection.class);
    for (final OperationMethod method : methods) {
        final String classification = method.getName().getCode();
        ParameterValueGroup param = mtFactory.getDefaultParameters(classification);
        param.parameter("semi_major").setValue(6377563.396);
        param.parameter("semi_minor").setValue(6356256.909237285);
        final MathTransform mt;
        try {
            mt = mtFactory.createParameterizedTransform(param);
        } catch (InvalidGeodeticParameterException e) {
            /*
                 * Some map projections have mandatory parameters which we ignore for now
                 * except for a few well-known projection that we know should not fail.
                 */
            if (classification.contains("Mercator")) {
                throw e;
            }
            out.print(classification);
            out.print(CharSequences.spaces(42 - classification.length()));
            out.print(": ");
            out.println(e.getLocalizedMessage());
            continue;
        }
        /*
             * Verifies that the map projection properties are the ones that we specified.
             * Note that the Equirectangular projection has been optimized as an affine transform, which we skip.
             */
        if (mt instanceof LinearTransform) {
            continue;
        }
        assertInstanceOf(classification, Parameterized.class, mt);
        param = ((Parameterized) mt).getParameterValues();
        assertEquals(classification, param.getDescriptor().getName().getCode());
        assertEquals(classification, 6377563.396, param.parameter("semi_major").doubleValue(), 1E-4);
        assertEquals(classification, 6356256.909237285, param.parameter("semi_minor").doubleValue(), 1E-4);
        /*
             * Creates a ProjectedCRS from the map projection. This part is more an integration test than
             * a DefaultMathTransformFactory test. Again, the intent is to verify that the properties are
             * the one that we specified.
             */
        final DefaultProjectedCRS crs = new DefaultProjectedCRS(dummyName, CommonCRS.WGS84.normalizedGeographic(), new DefaultConversion(dummyName, method, mt, null), HardCodedCS.PROJECTED);
        final Conversion projection = crs.getConversionFromBase();
        assertSame(classification, mt, projection.getMathTransform());
        assertEquals(classification, projection.getMethod().getName().getCode());
    }
}
Also used : InvalidGeodeticParameterException(org.apache.sis.referencing.factory.InvalidGeodeticParameterException) MathTransform(org.opengis.referencing.operation.MathTransform) MathTransformFactory(org.opengis.referencing.operation.MathTransformFactory) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) DefaultConversion(org.apache.sis.referencing.operation.DefaultConversion) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) Conversion(org.opengis.referencing.operation.Conversion) DefaultConversion(org.apache.sis.referencing.operation.DefaultConversion) OperationMethod(org.opengis.referencing.operation.OperationMethod) Test(org.junit.Test)

Example 2 with DefaultProjectedCRS

use of org.apache.sis.referencing.crs.DefaultProjectedCRS in project sis by apache.

the class StandardDefinitions method createUniversal.

/**
 * Creates a Universal Transverse Mercator (UTM) or a Universal Polar Stereographic (UPS) projected CRS
 * using the Apache SIS factory implementation. This method restricts the factory to SIS implementation
 * instead than arbitrary factory in order to meet the contract saying that {@link CommonCRS} methods
 * should never fail.
 *
 * @param code       the EPSG code, or 0 if none.
 * @param baseCRS    the geographic CRS on which the projected CRS is based.
 * @param isUTM      {@code true} for UTM or {@code false} for UPS. Note: redundant with the given latitude.
 * @param latitude   a latitude in the zone of the desired projection, to be snapped to 0°, 90°S or 90°N.
 * @param longitude  a longitude in the zone of the desired projection, to be snapped to UTM central meridian.
 * @param derivedCS  the projected coordinate system.
 */
static ProjectedCRS createUniversal(final int code, final GeographicCRS baseCRS, final boolean isUTM, final double latitude, final double longitude, final CartesianCS derivedCS) {
    final OperationMethod method;
    try {
        method = DefaultFactories.forBuildin(MathTransformFactory.class, DefaultMathTransformFactory.class).getOperationMethod(isUTM ? TransverseMercator.NAME : PolarStereographicA.NAME);
    } catch (NoSuchIdentifierException e) {
        // Should not happen with SIS implementation.
        throw new IllegalStateException(e);
    }
    final ParameterValueGroup parameters = method.getParameters().createValue();
    String name = isUTM ? TransverseMercator.Zoner.UTM.setParameters(parameters, latitude, longitude) : PolarStereographicA.setParameters(parameters, latitude >= 0);
    final DefaultConversion conversion = new DefaultConversion(properties(0, name, null, false), method, null, parameters);
    name = baseCRS.getName().getCode() + " / " + name;
    return new DefaultProjectedCRS(properties(code, name, null, false), baseCRS, conversion, derivedCS);
}
Also used : ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) DefaultConversion(org.apache.sis.referencing.operation.DefaultConversion) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) OperationMethod(org.opengis.referencing.operation.OperationMethod)

Example 3 with DefaultProjectedCRS

use of org.apache.sis.referencing.crs.DefaultProjectedCRS in project sis by apache.

the class CRSTest method testSuggestCommonTarget.

/**
 * Tests {@link CRS#suggestCommonTarget(GeographicBoundingBox, CoordinateReferenceSystem...)}.
 *
 * @since 0.8
 */
@Test
public void testSuggestCommonTarget() {
    /*
         * Prepare 4 CRS with different datum (so we can more easily differentiate them in the assertions) and
         * different domain of validity. CRS[1] is given a domain large enough for all CRS except the last one.
         */
    final Map<String, Object> properties = new HashMap<>(4);
    final CartesianCS cs = (CartesianCS) StandardDefinitions.createCoordinateSystem(Constants.EPSG_PROJECTED_CS);
    final ProjectedCRS[] crs = new ProjectedCRS[4];
    for (int i = 0; i < crs.length; i++) {
        final CommonCRS baseCRS;
        final double ymin, ymax;
        switch(i) {
            case 0:
                baseCRS = CommonCRS.WGS84;
                ymin = 2;
                ymax = 4;
                break;
            case 1:
                baseCRS = CommonCRS.WGS72;
                ymin = 1;
                ymax = 4;
                break;
            case 2:
                baseCRS = CommonCRS.SPHERE;
                ymin = 2;
                ymax = 3;
                break;
            case 3:
                baseCRS = CommonCRS.NAD27;
                ymin = 3;
                ymax = 5;
                break;
            default:
                throw new AssertionError(i);
        }
        properties.put(DefaultProjectedCRS.NAME_KEY, "CRS #" + i);
        properties.put(DefaultProjectedCRS.DOMAIN_OF_VALIDITY_KEY, new DefaultExtent(null, new DefaultGeographicBoundingBox(-1, +1, ymin, ymax), null, null));
        crs[i] = new DefaultProjectedCRS(properties, baseCRS.geographic(), HardCodedConversions.MERCATOR, cs);
    }
    // Exclude the last CRS only.
    final ProjectedCRS[] overlappingCRS = Arrays.copyOf(crs, 3);
    /*
         * Test between the 3 overlapping CRS without region of interest. We expect the CRS having a domain
         * of validity large enough for all CRS; this is the second CRS created in above 'switch' statement.
         */
    assertSame("Expected CRS with widest domain of validity.", crs[1], CRS.suggestCommonTarget(null, overlappingCRS));
    /*
         * If we specify a smaller region of interest, we should get the CRS having the smallest domain of validity that
         * cover the ROI. Following lines gradually increase the ROI size and verify that we get CRS for larger domain.
         */
    final DefaultGeographicBoundingBox regionOfInterest = new DefaultGeographicBoundingBox(-1, +1, 2.1, 2.9);
    assertSame("Expected best fit for [2.1 … 2.9]°N", crs[2], CRS.suggestCommonTarget(regionOfInterest, overlappingCRS));
    regionOfInterest.setNorthBoundLatitude(3.1);
    assertSame("Expected best fit for [2.1 … 3.1]°N", crs[0], CRS.suggestCommonTarget(regionOfInterest, overlappingCRS));
    regionOfInterest.setSouthBoundLatitude(1.9);
    assertSame("Expected best fit for [1.9 … 3.1]°N", crs[1], CRS.suggestCommonTarget(regionOfInterest, overlappingCRS));
    /*
         * All above tests returned one of the CRS in the given array. Test now a case where none of those CRS
         * have a domain of validity wide enough, so suggestCommonTarget(…) need to search among the base CRS.
         */
    assertSame("Expected a GeodeticCRS since none of the ProjectedCRS have a domain of validity wide enough.", crs[0].getBaseCRS(), CRS.suggestCommonTarget(null, crs));
    /*
         * With the same domain of validity than above, suggestCommonTarget(…) should not need to fallback on the
         * base CRS anymore.
         */
    assertSame("Expected best fit for [1.9 … 3.1]°N", crs[1], CRS.suggestCommonTarget(regionOfInterest, crs));
}
Also used : CartesianCS(org.opengis.referencing.cs.CartesianCS) HashMap(java.util.HashMap) DefaultGeographicBoundingBox(org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) DefaultExtent(org.apache.sis.metadata.iso.extent.DefaultExtent) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) Test(org.junit.Test)

Example 4 with DefaultProjectedCRS

use of org.apache.sis.referencing.crs.DefaultProjectedCRS in project sis by apache.

the class WKTFormatTest method testVariousConventions.

/**
 * Tests the formatting using the name of different authorities.
 * This test uses WKT 1 for parsing and formatting.
 *
 * @throws ParseException if the parsing failed.
 */
@Test
public void testVariousConventions() throws ParseException {
    final Symbols symbols = new Symbols(Symbols.SQUARE_BRACKETS);
    symbols.setPairedQuotes("“”");
    parser = format = new WKTFormat(null, null);
    format.setSymbols(symbols);
    final DefaultProjectedCRS crs = (DefaultProjectedCRS) parser.parseObject("PROJCS[“OSGB 1936 / British National Grid”,\n" + "  GEOGCS[“OSGB 1936”,\n" + "    DATUM[“OSGB_1936”,\n" + "      SPHEROID[“Airy 1830”, 6377563.396, 299.3249646],\n" + "      TOWGS84[375.0, -111.0, 431.0, 0.0, 0.0, 0.0, 0.0]],\n" + "      PRIMEM[“Greenwich”,0.0],\n" + "    UNIT[“DMSH”,0.0174532925199433],\n" + "    AXIS[“Lat”,NORTH],AXIS[“Long”,EAST]],\n" + "  PROJECTION[“Transverse_Mercator”],\n" + "  PARAMETER[“latitude_of_origin”, 49.0],\n" + "  PARAMETER[“central_meridian”, -2.0],\n" + "  PARAMETER[“scale_factor”, 0.999601272],\n" + "  PARAMETER[“false_easting”, 400000.0],\n" + "  PARAMETER[“false_northing”, -100000.0],\n" + "  UNIT[“metre”, 1],\n" + "  AXIS[“E”,EAST],\n" + "  AXIS[“N”,NORTH]]");
    /*
         * Formats using OGC identifiers. Use of OGC identifiers is implicit with Convention.WKT1, unless we
         * set explicitely another authority. The result should be the same than the above string, except:
         *
         *   - The TOWGS84 parameters have been trimmed to 3 values.
         *   - The AUTHORITY elements has been inferred for the PROJECTION element.
         *   - "Latitude of origin" parameter is before "central meridian" parameter.
         */
    format.setConvention(Convention.WKT1);
    assertMultilinesEquals("PROJCS[“OSGB 1936 / British National Grid”,\n" + "  GEOGCS[“OSGB 1936”,\n" + "    DATUM[“OSGB_1936”,\n" + "      SPHEROID[“Airy 1830”, 6377563.396, 299.3249646],\n" + // Trimmed to 3 parameters.
    "      TOWGS84[375.0, -111.0, 431.0]],\n" + "      PRIMEM[“Greenwich”, 0.0],\n" + "    UNIT[“degree”, 0.017453292519943295],\n" + "    AXIS[“Latitude”, NORTH],\n" + "    AXIS[“Longitude”, EAST]],\n" + // AUTHORITY code automatically found.
    "  PROJECTION[“Transverse_Mercator”, AUTHORITY[“EPSG”, “9807”]],\n" + // Sorted before central meridian.
    "  PARAMETER[“latitude_of_origin”, 49.0],\n" + "  PARAMETER[“central_meridian”, -2.0],\n" + "  PARAMETER[“scale_factor”, 0.999601272],\n" + "  PARAMETER[“false_easting”, 400000.0],\n" + "  PARAMETER[“false_northing”, -100000.0],\n" + "  UNIT[“metre”, 1],\n" + "  AXIS[“Easting”, EAST],\n" + "  AXIS[“Northing”, NORTH]]", format.format(crs));
    /*
         * Formats using GeoTiff identifiers. We should get different strings in PROJECTION[...]
         * and PARAMETER[...] elements, but the other ones (especially DATUM[...]) are unchanged.
         */
    format.setNameAuthority(Citations.GEOTIFF);
    assertMultilinesEquals("PROJCS[“OSGB 1936 / British National Grid”,\n" + "  GEOGCS[“OSGB 1936”,\n" + "    DATUM[“OSGB_1936”,\n" + "      SPHEROID[“Airy 1830”, 6377563.396, 299.3249646],\n" + "      TOWGS84[375.0, -111.0, 431.0]],\n" + "      PRIMEM[“Greenwich”, 0.0],\n" + "    UNIT[“degree”, 0.017453292519943295],\n" + "    AXIS[“Latitude”, NORTH],\n" + "    AXIS[“Longitude”, EAST]],\n" + "  PROJECTION[“CT_TransverseMercator”, AUTHORITY[“GeoTIFF”, “1”]],\n" + "  PARAMETER[“NatOriginLat”, 49.0],\n" + "  PARAMETER[“NatOriginLong”, -2.0],\n" + "  PARAMETER[“ScaleAtNatOrigin”, 0.999601272],\n" + "  PARAMETER[“FalseEasting”, 400000.0],\n" + "  PARAMETER[“FalseNorthing”, -100000.0],\n" + "  UNIT[“metre”, 1],\n" + "  AXIS[“Easting”, EAST],\n" + "  AXIS[“Northing”, NORTH]]", format.format(crs));
    /*
         * Formats using ESRI identifiers. The most important change we are looking for is
         * the name inside DATUM[...].
         */
    format.setNameAuthority(Citations.ESRI);
    format.setConvention(Convention.WKT1_COMMON_UNITS);
    assertMultilinesEquals("PROJCS[“OSGB 1936 / British National Grid”,\n" + "  GEOGCS[“OSGB 1936”,\n" + "    DATUM[“D_OSGB_1936”,\n" + "      SPHEROID[“Airy 1830”, 6377563.396, 299.3249646],\n" + "      TOWGS84[375.0, -111.0, 431.0]],\n" + "      PRIMEM[“Greenwich”, 0.0],\n" + "    UNIT[“degree”, 0.017453292519943295],\n" + "    AXIS[“Latitude”, NORTH],\n" + "    AXIS[“Longitude”, EAST]],\n" + "  PROJECTION[“Transverse_Mercator”, AUTHORITY[“EPSG”, “9807”]],\n" + "  PARAMETER[“Latitude_Of_Origin”, 49.0],\n" + "  PARAMETER[“Central_Meridian”, -2.0],\n" + "  PARAMETER[“Scale_Factor”, 0.999601272],\n" + "  PARAMETER[“False_Easting”, 400000.0],\n" + "  PARAMETER[“False_Northing”, -100000.0],\n" + "  UNIT[“meter”, 1],\n" + "  AXIS[“Easting”, EAST],\n" + "  AXIS[“Northing”, NORTH]]", format.format(crs));
    /*
         * Formats using EPSG identifiers. We expect different names in
         * DATUM[...], PROJECTION[...] and PARAMETER[...].
         */
    format.setNameAuthority(Citations.EPSG);
    assertMultilinesEquals("PROJCS[“OSGB 1936 / British National Grid”,\n" + "  GEOGCS[“OSGB 1936”,\n" + "    DATUM[“OSGB_1936”,\n" + "      SPHEROID[“Airy 1830”, 6377563.396, 299.3249646],\n" + "      TOWGS84[375.0, -111.0, 431.0]],\n" + "      PRIMEM[“Greenwich”, 0.0],\n" + "    UNIT[“degree”, 0.017453292519943295],\n" + "    AXIS[“Latitude”, NORTH],\n" + "    AXIS[“Longitude”, EAST]],\n" + "  PROJECTION[“Transverse Mercator”, AUTHORITY[“EPSG”, “9807”]],\n" + "  PARAMETER[“Latitude of natural origin”, 49.0],\n" + "  PARAMETER[“Longitude of natural origin”, -2.0],\n" + "  PARAMETER[“Scale factor at natural origin”, 0.999601272],\n" + "  PARAMETER[“False easting”, 400000.0],\n" + "  PARAMETER[“False northing”, -100000.0],\n" + "  UNIT[“meter”, 1],\n" + "  AXIS[“Easting”, EAST],\n" + "  AXIS[“Northing”, NORTH]]", format.format(crs));
}
Also used : DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) Test(org.junit.Test)

Aggregations

DefaultProjectedCRS (org.apache.sis.referencing.crs.DefaultProjectedCRS)4 Test (org.junit.Test)3 DefaultConversion (org.apache.sis.referencing.operation.DefaultConversion)2 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)2 OperationMethod (org.opengis.referencing.operation.OperationMethod)2 HashMap (java.util.HashMap)1 DefaultExtent (org.apache.sis.metadata.iso.extent.DefaultExtent)1 DefaultGeographicBoundingBox (org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox)1 InvalidGeodeticParameterException (org.apache.sis.referencing.factory.InvalidGeodeticParameterException)1 ProjectedCRS (org.opengis.referencing.crs.ProjectedCRS)1 CartesianCS (org.opengis.referencing.cs.CartesianCS)1 Conversion (org.opengis.referencing.operation.Conversion)1 MathTransform (org.opengis.referencing.operation.MathTransform)1 MathTransformFactory (org.opengis.referencing.operation.MathTransformFactory)1 NoSuchIdentifierException (org.opengis.util.NoSuchIdentifierException)1