Search in sources :

Example 21 with ParameterValueGroup

use of org.opengis.parameter.ParameterValueGroup in project sis by apache.

the class TensorParametersTest method testMatrixConversion.

/**
 * Tests {@link TensorParameters#createValueGroup(Map, Matrix)} and its converse
 * {@link TensorParameters#toMatrix(ParameterValueGroup)}.
 */
@Test
@DependsOnMethod("testGetAllDescriptors")
public void testMatrixConversion() {
    final int size = StrictMath.min(6, TensorParameters.CACHE_SIZE);
    final Random random = TestUtilities.createRandomNumberGenerator();
    for (int numRow = 2; numRow <= size; numRow++) {
        for (int numCol = 2; numCol <= size; numCol++) {
            final Matrix matrix = Matrices.createZero(numRow, numCol);
            for (int j = 0; j < numRow; j++) {
                for (int i = 0; i < numCol; i++) {
                    matrix.setElement(j, i, 200 * random.nextDouble() - 100);
                }
            }
            final ParameterValueGroup group = param.createValueGroup(singletonMap(ParameterDescriptor.NAME_KEY, "Test"), matrix);
            assertEquals(NUM_ROW, numRow, group.parameter(NUM_ROW).intValue());
            assertEquals(NUM_COL, numCol, group.parameter(NUM_COL).intValue());
            assertEquals("elements", matrix, param.toMatrix(group));
            assertEquals("elements", matrix, param.toMatrix(new ParameterValueGroupWrapper(group)));
        }
    }
}
Also used : Matrix(org.opengis.referencing.operation.Matrix) Random(java.util.Random) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 22 with ParameterValueGroup

use of org.opengis.parameter.ParameterValueGroup in project sis by apache.

the class Proj4Factory method createCRS.

/**
 * Creates a coordinate reference system from the given {@literal Proj.4} wrapper.
 * The given {@code pj} will be stored as the CRS identifier.
 *
 * @param  pj          the Proj.4 object to wrap.
 * @param  withHeight  whether to include a height axis.
 * @throws IllegalArgumentException if a Proj.4 parameter value can not be parsed or assigned.
 * @throws ParserException if a unit symbol can not be parsed.
 */
private CoordinateReferenceSystem createCRS(final PJ pj, final boolean withHeight) throws FactoryException {
    final PJ.Type type = pj.getType();
    final boolean geographic = PJ.Type.GEOGRAPHIC.equals(type);
    final boolean geocentric = PJ.Type.GEOCENTRIC.equals(type);
    final Proj4Parser parser = new Proj4Parser(pj.getCode());
    final String dir = parser.value("axis", "enu");
    final CoordinateSystemAxis[] axes = new CoordinateSystemAxis[geocentric | withHeight ? dir.length() : 2];
    for (int i = 0; i < axes.length; i++) {
        final char d = Character.toLowerCase(dir.charAt(i));
        char abbreviation = Character.toUpperCase(d);
        boolean vertical = false;
        final AxisDirection c;
        final String name;
        if (geocentric)
            switch(d) {
                case 'e':
                    c = AxisDirection.GEOCENTRIC_X;
                    name = "Geocentric X";
                    break;
                case 'n':
                    c = AxisDirection.GEOCENTRIC_Y;
                    name = "Geocentric Y";
                    break;
                case 'u':
                    c = AxisDirection.GEOCENTRIC_Z;
                    name = "Geocentric Z";
                    break;
                default:
                    c = AxisDirection.OTHER;
                    name = "Unknown";
                    break;
            }
        else
            switch(d) {
                case 'e':
                    c = AxisDirection.EAST;
                    name = geographic ? "Geodetic longitude" : "Easting";
                    break;
                case 'w':
                    c = AxisDirection.WEST;
                    name = geographic ? "Geodetic longitude" : "Westing";
                    break;
                case 'n':
                    c = AxisDirection.NORTH;
                    name = geographic ? "Geodetic latitude" : "Northing";
                    break;
                case 's':
                    c = AxisDirection.SOUTH;
                    name = geographic ? "Geodetic latitude" : "Southing";
                    break;
                case 'u':
                    c = AxisDirection.UP;
                    name = "Height";
                    vertical = true;
                    abbreviation = 'h';
                    break;
                case 'd':
                    c = AxisDirection.DOWN;
                    name = "Depth";
                    vertical = true;
                    break;
                default:
                    c = AxisDirection.OTHER;
                    name = "Unknown";
                    break;
            }
        if (geographic && AxisDirections.isCardinal(c)) {
            abbreviation = (d == 'e' || d == 'w') ? 'λ' : 'φ';
        }
        final Unit<?> unit = (vertical || !geographic) ? parser.unit(vertical) : Units.DEGREE;
        axes[i] = csFactory.createCoordinateSystemAxis(identifier(name), String.valueOf(abbreviation).intern(), c, unit);
    }
    /*
         * At this point we got the coordinate system axes. Now create the CRS. The given Proj.4 object
         * will be stored as the CRS identifier for allowing OperationFactory to get it back before to
         * attempt to create a new one for a given CRS.
         */
    final Map<String, Object> csName = identifier(UNNAMED);
    final Map<String, Object> name = new HashMap<>(identifier(parser.name(type == PJ.Type.PROJECTED)));
    name.put(CoordinateReferenceSystem.IDENTIFIERS_KEY, pj);
    switch(type) {
        case GEOGRAPHIC:
            {
                return crsFactory.createGeographicCRS(name, createDatum(pj, parser), withHeight ? csFactory.createEllipsoidalCS(csName, axes[0], axes[1], axes[2]) : csFactory.createEllipsoidalCS(csName, axes[0], axes[1]));
            }
        case GEOCENTRIC:
            {
                return crsFactory.createGeocentricCRS(name, createDatum(pj, parser), csFactory.createCartesianCS(csName, axes[0], axes[1], axes[2]));
            }
        case PROJECTED:
            {
                final PJ base = unique(new PJ(pj));
                final CoordinateReferenceSystem baseCRS = createCRS(base, withHeight);
                final Transform tr = new Transform(pj, withHeight, base, withHeight);
                /*
                 * Try to convert the Proj.4 parameters into OGC parameters in order to have a less opaque structure.
                 * Failure to perform this conversion will not cause a failure to create the ProjectedCRS. After all,
                 * maybe the user invokes this method for using a map projection not yet supported by Apache SIS.
                 * Instead, fallback on the more opaque Transform.METHOD description. Apache SIS will not be able to
                 * perform analysis on those parameters, but it will not prevent the Proj.4 transformation to work.
                 */
                OperationMethod method;
                ParameterValueGroup parameters;
                try {
                    method = parser.method(opFactory());
                    parameters = parser.parameters();
                } catch (IllegalArgumentException | FactoryException e) {
                    Logging.recoverableException(Logging.getLogger(Modules.GDAL), Proj4Factory.class, "createProjectedCRS", e);
                    method = Transform.METHOD;
                    // Will let Apache SIS infers the parameters from the Transform instance.
                    parameters = null;
                }
                final Conversion fromBase = new DefaultConversion(name, method, tr, parameters);
                return crsFactory.createProjectedCRS(name, (GeographicCRS) baseCRS, fromBase, withHeight ? csFactory.createCartesianCS(csName, axes[0], axes[1], axes[2]) : csFactory.createCartesianCS(csName, axes[0], axes[1]));
            }
        default:
            {
                throw new FactoryException(Errors.getResources(defaultProperties).getString(Errors.Keys.UnknownEnumValue_2, type, PJ.Type.class));
            }
    }
}
Also used : WeakValueHashMap(org.apache.sis.util.collection.WeakValueHashMap) HashMap(java.util.HashMap) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) UnavailableFactoryException(org.apache.sis.referencing.factory.UnavailableFactoryException) FactoryException(org.opengis.util.FactoryException) DefaultConversion(org.apache.sis.referencing.operation.DefaultConversion) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) DefaultConversion(org.apache.sis.referencing.operation.DefaultConversion) IdentifiedObject(org.opengis.referencing.IdentifiedObject)

Example 23 with ParameterValueGroup

use of org.opengis.parameter.ParameterValueGroup in project sis by apache.

the class Proj4Parser method parameters.

/**
 * Returns the parameter value group filled from the {@literal Proj.4} parameters.
 *
 * @throws IllegalArgumentException if a Proj.4 parameter can not be converted to the expected type.
 */
final ParameterValueGroup parameters() throws IllegalArgumentException {
    final ParameterValueGroup pg = method.getParameters().createValue();
    for (final Map.Entry<String, String> entry : parameters.entrySet()) {
        final String keyword = entry.getKey();
        if (!EXCLUDES.contains(keyword)) {
            final ParameterValue<?> value = pg.parameter(keyword);
            value.setValue(Double.parseDouble(entry.getValue()));
        }
    }
    return pg;
}
Also used : ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 24 with ParameterValueGroup

use of org.opengis.parameter.ParameterValueGroup in project sis by apache.

the class Transform method getParameterValues.

/**
 * Returns a copy of the parameter values for this parameterized object.
 */
@Override
public ParameterValueGroup getParameterValues() {
    final ParameterValueGroup pg = getParameterDescriptors().createValue();
    pg.parameter("srcdefn").setValue(source.getCode().trim());
    pg.parameter("dstdefn").setValue(target.getCode().trim());
    return pg;
}
Also used : ParameterValueGroup(org.opengis.parameter.ParameterValueGroup)

Example 25 with ParameterValueGroup

use of org.opengis.parameter.ParameterValueGroup in project sis by apache.

the class Proj4FactoryTest method testParameterizedTransform.

/**
 * Tests {@link Proj4Factory#createParameterizedTransform(ParameterValueGroup)}.
 *
 * @throws FactoryException if an error occurred while creating the CRS objects.
 * @throws TransformException if an error occurred while projecting a test point.
 */
@Test
public void testParameterizedTransform() throws FactoryException, TransformException {
    final Proj4Factory factory = Proj4Factory.INSTANCE;
    final ParameterValueGroup pg = factory.getDefaultParameters("merc");
    pg.parameter("a").setValue(6378137.0);
    pg.parameter("b").setValue(6356752.314245179);
    testMercatorProjection(factory.createParameterizedTransform(pg));
}
Also used : ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Test(org.junit.Test)

Aggregations

ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)98 Test (org.junit.Test)54 DependsOnMethod (org.apache.sis.test.DependsOnMethod)27 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)12 ProjectedCRS (org.opengis.referencing.crs.ProjectedCRS)11 OperationMethod (org.opengis.referencing.operation.OperationMethod)11 ParameterValue (org.opengis.parameter.ParameterValue)8 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)7 ParameterNotFoundException (org.opengis.parameter.ParameterNotFoundException)7 SingleOperation (org.opengis.referencing.operation.SingleOperation)6 FactoryException (org.opengis.util.FactoryException)6 DefaultGeodeticDatum (org.apache.sis.referencing.datum.DefaultGeodeticDatum)5 IdentifiedObject (org.opengis.referencing.IdentifiedObject)5 Matrix (org.opengis.referencing.operation.Matrix)5 ArrayList (java.util.ArrayList)4 DefaultConversion (org.apache.sis.referencing.operation.DefaultConversion)4 ParameterDescriptorGroup (org.opengis.parameter.ParameterDescriptorGroup)4 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)4 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)4 URL (java.net.URL)3