Search in sources :

Example 16 with ProjectedCRS

use of org.opengis.referencing.crs.ProjectedCRS in project sis by apache.

the class CRS method horizontalCode.

/**
 * If the given CRS would quality as horizontal except for its number of dimensions, returns that number.
 * Otherwise returns 0. The number of dimensions can only be 2 or 3.
 */
private static int horizontalCode(final CoordinateReferenceSystem crs) {
    /*
         * In order to determine if the CRS is geographic, checking the CoordinateSystem type is more reliable
         * then checking if the CRS implements the GeographicCRS interface.  This is because the GeographicCRS
         * type did not existed in ISO 19111:2007, so a CRS could be standard-compliant without implementing
         * the GeographicCRS interface.
         */
    boolean isEngineering = false;
    final boolean isGeodetic = (crs instanceof GeodeticCRS);
    if (isGeodetic || crs instanceof ProjectedCRS || (isEngineering = (crs instanceof EngineeringCRS))) {
        final CoordinateSystem cs = crs.getCoordinateSystem();
        final int dim = cs.getDimension();
        if ((dim & ~1) == 2 && (!isGeodetic || (cs instanceof EllipsoidalCS))) {
            if (isEngineering) {
                int n = 0;
                for (int i = 0; i < dim; i++) {
                    if (AxisDirections.isCompass(cs.getAxis(i).getDirection()))
                        n++;
                }
                // If we don't have exactly 2 east, north, etc. directions, consider as non-horizontal.
                if (n != 2)
                    return 0;
            }
            return dim;
        }
    }
    return 0;
}
Also used : EngineeringCRS(org.opengis.referencing.crs.EngineeringCRS) DefaultEngineeringCRS(org.apache.sis.referencing.crs.DefaultEngineeringCRS) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) EllipsoidalCS(org.opengis.referencing.cs.EllipsoidalCS) GeodeticCRS(org.opengis.referencing.crs.GeodeticCRS)

Example 17 with ProjectedCRS

use of org.opengis.referencing.crs.ProjectedCRS in project sis by apache.

the class SubTypes method castOrCopy.

/**
 * Returns a SIS implementation for the given coordinate reference system.
 *
 * @see AbstractCRS#castOrCopy(CoordinateReferenceSystem)
 */
static AbstractCRS castOrCopy(final CoordinateReferenceSystem object) {
    if (object instanceof DerivedCRS) {
        return DefaultDerivedCRS.castOrCopy((DerivedCRS) object);
    }
    if (object instanceof ProjectedCRS) {
        return DefaultProjectedCRS.castOrCopy((ProjectedCRS) object);
    }
    if (object instanceof GeodeticCRS) {
        if (object instanceof GeographicCRS) {
            return DefaultGeographicCRS.castOrCopy((GeographicCRS) object);
        }
        if (object instanceof GeocentricCRS) {
            return DefaultGeocentricCRS.castOrCopy((GeocentricCRS) object);
        }
        /*
             * The GeographicCRS and GeocentricCRS types are not part of ISO 19111.
             * ISO uses a single type, GeodeticCRS, for both of them and infer the
             * geographic or geocentric type from the coordinate system. We do this
             * check here for instantiating the most appropriate SIS type, but only
             * if we need to create a new object anyway (see below for rational).
             */
        if (object instanceof DefaultGeodeticCRS) {
            /*
                 * Result of XML unmarshalling — keep as-is. We avoid creating a new object because it
                 * would break object identities specified in GML document by the xlink:href attribute.
                 * However we may revisit this policy in the future. See SC_CRS.setElement(AbstractCRS).
                 */
            return (DefaultGeodeticCRS) object;
        }
        final Map<String, ?> properties = IdentifiedObjects.getProperties(object);
        final GeodeticDatum datum = ((GeodeticCRS) object).getDatum();
        final CoordinateSystem cs = object.getCoordinateSystem();
        if (cs instanceof EllipsoidalCS) {
            return new DefaultGeographicCRS(properties, datum, (EllipsoidalCS) cs);
        }
        if (cs instanceof SphericalCS) {
            return new DefaultGeocentricCRS(properties, datum, (SphericalCS) cs);
        }
        if (cs instanceof CartesianCS) {
            return new DefaultGeocentricCRS(properties, datum, (CartesianCS) cs);
        }
    }
    if (object instanceof VerticalCRS) {
        return DefaultVerticalCRS.castOrCopy((VerticalCRS) object);
    }
    if (object instanceof TemporalCRS) {
        return DefaultTemporalCRS.castOrCopy((TemporalCRS) object);
    }
    if (object instanceof EngineeringCRS) {
        return DefaultEngineeringCRS.castOrCopy((EngineeringCRS) object);
    }
    if (object instanceof ImageCRS) {
        return DefaultImageCRS.castOrCopy((ImageCRS) object);
    }
    if (object instanceof CompoundCRS) {
        return DefaultCompoundCRS.castOrCopy((CompoundCRS) object);
    }
    /*
         * Intentionally check for AbstractCRS after the interfaces because user may have defined his own
         * subclass implementing the interface. If we were checking for AbstractCRS before the interfaces,
         * the returned instance could have been a user subclass without the JAXB annotations required
         * for XML marshalling.
         */
    if (object == null || object instanceof AbstractCRS) {
        return (AbstractCRS) object;
    }
    return new AbstractCRS(object);
}
Also used : CartesianCS(org.opengis.referencing.cs.CartesianCS) EngineeringCRS(org.opengis.referencing.crs.EngineeringCRS) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) DerivedCRS(org.opengis.referencing.crs.DerivedCRS) CompoundCRS(org.opengis.referencing.crs.CompoundCRS) GeodeticDatum(org.opengis.referencing.datum.GeodeticDatum) GeodeticCRS(org.opengis.referencing.crs.GeodeticCRS) SphericalCS(org.opengis.referencing.cs.SphericalCS) TemporalCRS(org.opengis.referencing.crs.TemporalCRS) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) ImageCRS(org.opengis.referencing.crs.ImageCRS) VerticalCRS(org.opengis.referencing.crs.VerticalCRS) EllipsoidalCS(org.opengis.referencing.cs.EllipsoidalCS) GeographicCRS(org.opengis.referencing.crs.GeographicCRS) GeocentricCRS(org.opengis.referencing.crs.GeocentricCRS)

Example 18 with ProjectedCRS

use of org.opengis.referencing.crs.ProjectedCRS in project sis by apache.

the class EllipsoidalHeightCombiner method createCompoundCRS.

/**
 * Creates a compound CRS, but we special processing for (two-dimensional Geographic + ellipsoidal heights) tupples.
 * If any such tupple is found, a three-dimensional geographic CRS is created instead than the compound CRS.
 *
 * @param  properties  name and other properties to give to the new object.
 * @param  components  ordered array of {@code CoordinateReferenceSystem} objects.
 * @return the coordinate reference system for the given properties.
 * @throws FactoryException if the object creation failed.
 */
public final CoordinateReferenceSystem createCompoundCRS(final Map<String, ?> properties, CoordinateReferenceSystem... components) throws FactoryException {
    for (int i = 0; i < components.length; i++) {
        final CoordinateReferenceSystem vertical = components[i];
        if (vertical instanceof VerticalCRS) {
            final VerticalDatum datum = ((VerticalCRS) vertical).getDatum();
            if (datum != null && datum.getVerticalDatumType() == VerticalDatumTypes.ELLIPSOIDAL) {
                int axisPosition = 0;
                CoordinateSystem cs = null;
                CoordinateReferenceSystem crs = null;
                if (i == 0 || (cs = getCsIfHorizontal2D(crs = components[i - 1])) == null) {
                    /*
                         * GeographicCRS are normally before VerticalCRS. But Apache SIS is tolerant to the
                         * opposite order (note however that such ordering is illegal according ISO 19162).
                         */
                    if (i + 1 >= components.length || (cs = getCsIfHorizontal2D(crs = components[i + 1])) == null) {
                        continue;
                    }
                    axisPosition = 1;
                }
                /*
                     * At this point we have the horizontal and vertical components. The horizontal component
                     * begins at 'axisPosition', which is almost always zero. Create the three-dimensional CRS.
                     * If the result is the CRS to be returned directly by this method (components.length == 2),
                     * use the properties given in argument. Otherwise we need to use other properties; current
                     * implementation recycles the properties of the existing two-dimensional CRS.
                     */
                final CoordinateSystemAxis[] axes = new CoordinateSystemAxis[3];
                axes[axisPosition++] = cs.getAxis(0);
                axes[axisPosition++] = cs.getAxis(1);
                axes[axisPosition %= 3] = vertical.getCoordinateSystem().getAxis(0);
                final ReferencingServices referencing = ReferencingServices.getInstance();
                final Map<String, ?> csProps = referencing.getProperties(cs, false);
                final Map<String, ?> crsProps = (components.length == 2) ? properties : referencing.getProperties(crs, false);
                if (crs instanceof GeodeticCRS) {
                    initialize(CS | CRS);
                    cs = csFactory.createEllipsoidalCS(csProps, axes[0], axes[1], axes[2]);
                    crs = crsFactory.createGeographicCRS(crsProps, ((GeodeticCRS) crs).getDatum(), (EllipsoidalCS) cs);
                } else {
                    initialize(CS | CRS | OPERATION);
                    final ProjectedCRS proj = (ProjectedCRS) crs;
                    GeographicCRS base = proj.getBaseCRS();
                    if (base.getCoordinateSystem().getDimension() == 2) {
                        base = (GeographicCRS) createCompoundCRS(referencing.getProperties(base, false), base, vertical);
                    }
                    /*
                         * In Apache SIS implementation, the Conversion contains the source and target CRS together with
                         * a MathTransform2D. We need to recreate the same conversion, but without CRS and MathTransform
                         * for letting SIS create or associate new ones, which will be three-dimensional now.
                         */
                    Conversion fromBase = proj.getConversionFromBase();
                    fromBase = opFactory.createDefiningConversion(referencing.getProperties(fromBase, true), fromBase.getMethod(), fromBase.getParameterValues());
                    cs = csFactory.createCartesianCS(csProps, axes[0], axes[1], axes[2]);
                    crs = crsFactory.createProjectedCRS(crsProps, base, fromBase, (CartesianCS) cs);
                }
                /*
                     * Remove the VerticalCRS and store the three-dimensional GeographicCRS in place of the previous
                     * two-dimensional GeographicCRS. Then let the loop continues in case there is other CRS to merge
                     * (should never happen, but we are paranoiac).
                     */
                components = ArraysExt.remove(components, i, 1);
                // GeographicCRS before VerticalCRS (usual case).
                if (axisPosition != 0)
                    i--;
                components[i] = crs;
            }
        }
    }
    switch(components.length) {
        case 0:
            return null;
        case 1:
            return components[0];
        default:
            initialize(CRS);
            return crsFactory.createCompoundCRS(properties, components);
    }
}
Also used : CartesianCS(org.opengis.referencing.cs.CartesianCS) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) CoordinateSystemAxis(org.opengis.referencing.cs.CoordinateSystemAxis) VerticalDatum(org.opengis.referencing.datum.VerticalDatum) Conversion(org.opengis.referencing.operation.Conversion) GeodeticCRS(org.opengis.referencing.crs.GeodeticCRS) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) VerticalCRS(org.opengis.referencing.crs.VerticalCRS) EllipsoidalCS(org.opengis.referencing.cs.EllipsoidalCS) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeographicCRS(org.opengis.referencing.crs.GeographicCRS)

Example 19 with ProjectedCRS

use of org.opengis.referencing.crs.ProjectedCRS 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 20 with ProjectedCRS

use of org.opengis.referencing.crs.ProjectedCRS in project sis by apache.

the class CRSTest method testComponentsOfProjectedCRS.

/**
 * Tests getting the horizontal and vertical components of a three-dimensional projected CRS.
 *
 * @since 0.8
 */
@Test
public void testComponentsOfProjectedCRS() {
    final ProjectedCRS volumetric = HardCodedConversions.mercator3D();
    assertFalse("isHorizontalCRS", CRS.isHorizontalCRS(volumetric));
    assertNull("getTemporalComponent", CRS.getTemporalComponent(volumetric));
    assertNull("getVerticalComponent", CRS.getVerticalComponent(volumetric, false));
    assertEqualsIgnoreMetadata(HardCodedCRS.ELLIPSOIDAL_HEIGHT, CRS.getVerticalComponent(volumetric, true));
    final SingleCRS horizontal = CRS.getHorizontalComponent(volumetric);
    assertInstanceOf("getHorizontalComponent", ProjectedCRS.class, horizontal);
    assertEquals("dimension", 2, horizontal.getCoordinateSystem().getDimension());
    assertTrue("isHorizontalCRS", CRS.isHorizontalCRS(horizontal));
}
Also used : SingleCRS(org.opengis.referencing.crs.SingleCRS) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) Test(org.junit.Test)

Aggregations

ProjectedCRS (org.opengis.referencing.crs.ProjectedCRS)40 Test (org.junit.Test)31 DependsOnMethod (org.apache.sis.test.DependsOnMethod)23 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)11 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)10 GeodeticCRS (org.opengis.referencing.crs.GeodeticCRS)5 CartesianCS (org.opengis.referencing.cs.CartesianCS)5 Conversion (org.opengis.referencing.operation.Conversion)5 DefaultConversion (org.apache.sis.referencing.operation.DefaultConversion)4 VerticalCRS (org.opengis.referencing.crs.VerticalCRS)4 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)4 EllipsoidalCS (org.opengis.referencing.cs.EllipsoidalCS)4 GeodeticObjectBuilder (org.apache.sis.internal.referencing.GeodeticObjectBuilder)3 DefaultProjectedCRS (org.apache.sis.referencing.crs.DefaultProjectedCRS)3 HashMap (java.util.HashMap)2 Ignore (org.junit.Ignore)2 DirectPosition (org.opengis.geometry.DirectPosition)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 EngineeringCRS (org.opengis.referencing.crs.EngineeringCRS)2 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)2