Search in sources :

Example 1 with Length

use of javax.measure.quantity.Length in project hale by halestudio.

the class AreaCalc method checkEPSG.

/**
 * Checks if GeoPosition are in a metric system and if not convert them if
 * necessary.
 *
 * @param pos List of {@link GeoPosition}
 *
 * @return List of {@link GeoPosition}
 */
public List<GeoPosition> checkEPSG(List<GeoPosition> pos) {
    // list is empty
    if (pos.size() == 0) {
        return pos;
    }
    // 
    int epsg = pos.get(0).getEpsgCode();
    // Worldmercator
    int FALLBACK_EPSG = 3395;
    // WGS84
    FALLBACK_EPSG = 4326;
    try {
        CoordinateSystem cs = CRS.decode("EPSG:" + epsg).getCoordinateSystem();
        for (int i = 0; epsg != FALLBACK_EPSG && i < cs.getDimension(); i++) {
            CoordinateSystemAxis axis = cs.getAxis(i);
            try {
                Unit<Length> unit = axis.getUnit().asType(Length.class);
                if (!unit.toString().equals("m")) {
                    // $NON-NLS-1$
                    // not metric
                    epsg = FALLBACK_EPSG;
                }
            } catch (ClassCastException e) {
                // no length unit
                epsg = FALLBACK_EPSG;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // convert all coordinates
    try {
        GeotoolsConverter g = (GeotoolsConverter) GeotoolsConverter.getInstance();
        pos = g.convertAll(pos, epsg);
    } catch (IllegalGeoPositionException e1) {
        e1.printStackTrace();
    }
    return pos;
}
Also used : GeotoolsConverter(org.jdesktop.swingx.mapviewer.GeotoolsConverter) Length(javax.measure.quantity.Length) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) CoordinateSystemAxis(org.opengis.referencing.cs.CoordinateSystemAxis) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)

Example 2 with Length

use of javax.measure.quantity.Length in project smarthome by eclipse.

the class ItemStateConverterImplTest method numberItemShouldNotConvertUnitsWhereMeasurmentSystemEquals.

@Test
public void numberItemShouldNotConvertUnitsWhereMeasurmentSystemEquals() {
    NumberItem item = mock(NumberItem.class);
    doReturn(Length.class).when(item).getDimension();
    UnitProvider unitProvider = mock(UnitProvider.class);
    when(unitProvider.getUnit(Length.class)).thenReturn(SIUnits.METRE);
    itemStateConverter.setUnitProvider(unitProvider);
    QuantityType<Length> originalState = new QuantityType<>("100 cm");
    @SuppressWarnings("unchecked") QuantityType<Length> convertedState = (QuantityType<Length>) itemStateConverter.convertToAcceptedState(originalState, item);
    assertThat(convertedState.getUnit(), is(originalState.getUnit()));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) Length(javax.measure.quantity.Length) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) Test(org.junit.Test)

Example 3 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class ProjectedCoordinateSystem method getCoordinatesOperation.

@Override
public CoordinatesOperation getCoordinatesOperation(final CoordinateSystem coordinateSystem) {
    if (coordinateSystem == null || this == coordinateSystem) {
        return null;
    } else {
        final List<CoordinatesOperation> operations = new ArrayList<>();
        final CoordinatesOperation inverseOperation = this.getInverseCoordinatesOperation();
        if (inverseOperation == null) {
            return null;
        }
        final Unit<Length> linearUnit1 = this.getLengthUnit();
        if (!linearUnit1.equals(Units.METRE)) {
            operations.add(new UnitConverstionOperation(linearUnit1, Units.METRE));
        }
        operations.add(inverseOperation);
        if (coordinateSystem instanceof ProjectedCoordinateSystem) {
            final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
            final CoordinatesOperation projectOperation = projectedCoordinateSystem.getProjectCoordinatesOperation();
            if (projectOperation != null) {
                operations.add(projectOperation);
            }
            final Unit<Length> linearUnit2 = projectedCoordinateSystem.getLengthUnit();
            if (!linearUnit2.equals(Units.METRE)) {
                operations.add(new UnitConverstionOperation(Units.METRE, linearUnit2));
            }
        } else if (coordinateSystem instanceof GeographicCoordinateSystem) {
            final GeographicCoordinateSystem geographicCoordinateSystem = (GeographicCoordinateSystem) coordinateSystem;
            final Unit<Angle> angularUnit2 = geographicCoordinateSystem.getUnit();
            if (!angularUnit2.equals(NonSI.DEGREE_ANGLE)) {
                operations.add(new UnitConverstionOperation(NonSI.DEGREE_ANGLE, angularUnit2, 2));
            }
        } else {
            return null;
        }
        switch(operations.size()) {
            case 0:
                return null;
            case 1:
                return operations.get(0);
            default:
                return new ChainedCoordinatesOperation(operations);
        }
    }
}
Also used : Length(javax.measure.quantity.Length) ArrayList(java.util.ArrayList) ChainedCoordinatesOperation(com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation) ChainedCoordinatesOperation(com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation) CoordinatesOperation(com.revolsys.geometry.cs.projection.CoordinatesOperation) Unit(javax.measure.Unit) LinearUnit(com.revolsys.geometry.cs.unit.LinearUnit) UnitConverstionOperation(com.revolsys.geometry.cs.projection.UnitConverstionOperation)

Example 4 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class GeographicCoordinateSystem method getCoordinatesOperation.

@Override
public CoordinatesOperation getCoordinatesOperation(final CoordinateSystem coordinateSystem) {
    if (coordinateSystem == null || this == coordinateSystem) {
        return null;
    } else if (coordinateSystem instanceof GeographicCoordinateSystem) {
        final GeographicCoordinateSystem geographicCoordinateSystem = (GeographicCoordinateSystem) coordinateSystem;
        final Unit<Angle> angularUnit1 = getUnit();
        // TODO GeodeticDatum shift
        final Unit<Angle> angularUnit2 = geographicCoordinateSystem.getUnit();
        if (!angularUnit1.equals(angularUnit2)) {
            return new UnitConverstionOperation(angularUnit1, angularUnit2, 2);
        } else {
            return null;
        }
    } else if (coordinateSystem instanceof ProjectedCoordinateSystem) {
        final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
        final List<CoordinatesOperation> operations = new ArrayList<>();
        final Unit<Angle> angularUnit1 = getUnit();
        if (!angularUnit1.equals(NonSI.DEGREE_ANGLE)) {
            CoordinatesOperation converstionOperation;
            if (angularUnit1.equals(Units.RADIAN)) {
                converstionOperation = RadiansToDegreesOperation.INSTANCE;
            } else {
                converstionOperation = new UnitConverstionOperation(angularUnit1, NonSI.DEGREE_ANGLE, 2);
            }
            operations.add(converstionOperation);
        }
        // TODO geodeticDatum shift
        final CoordinatesOperation projectOperation = projectedCoordinateSystem.getProjectCoordinatesOperation();
        if (projectOperation != null) {
            operations.add(projectOperation);
        }
        final Unit<Length> linearUnit2 = projectedCoordinateSystem.getLengthUnit();
        if (!linearUnit2.equals(Units.METRE)) {
            operations.add(new UnitConverstionOperation(Units.METRE, linearUnit2));
        }
        switch(operations.size()) {
            case 0:
                return null;
            case 1:
                return operations.get(0);
            default:
                return new ChainedCoordinatesOperation(operations);
        }
    } else {
        return null;
    }
}
Also used : Angle(javax.measure.quantity.Angle) Length(javax.measure.quantity.Length) ArrayList(java.util.ArrayList) ChainedCoordinatesOperation(com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation) Unit(javax.measure.Unit) AngularUnit(com.revolsys.geometry.cs.unit.AngularUnit) LinearUnit(com.revolsys.geometry.cs.unit.LinearUnit) ChainedCoordinatesOperation(com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation) CoordinatesOperation(com.revolsys.geometry.cs.projection.CoordinatesOperation) UnitConverstionOperation(com.revolsys.geometry.cs.projection.UnitConverstionOperation)

Example 5 with Length

use of javax.measure.quantity.Length in project com.revolsys.open by revolsys.

the class LineString method getLength.

@Override
default double getLength(final Unit<Length> unit) {
    double length = 0;
    final CoordinateSystem coordinateSystem = getCoordinateSystem();
    if (coordinateSystem instanceof GeographicCoordinateSystem) {
        final int vertexCount = getVertexCount();
        if (vertexCount > 1) {
            double lon0 = getX(0);
            double lat0 = getY(0);
            for (int i = 1; i < vertexCount; i++) {
                final double lon1 = getX(i);
                final double lat1 = getY(i);
                length += GeographicCoordinateSystem.distanceMetres(lon0, lat0, lon1, lat1);
                lon0 = lon1;
                lat0 = lat1;
            }
        }
        final Quantity<Length> lengthMeasure = Quantities.getQuantity(length, Units.METRE);
        length = QuantityType.doubleValue(lengthMeasure, unit);
    } else if (coordinateSystem instanceof ProjectedCoordinateSystem) {
        final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
        final Unit<Length> lengthUnit = projectedCoordinateSystem.getLengthUnit();
        length = getLength();
        final Quantity<Length> lengthMeasure = Quantities.getQuantity(length, lengthUnit);
        length = QuantityType.doubleValue(lengthMeasure, unit);
    } else {
        length = getLength();
    }
    return length;
}
Also used : Length(javax.measure.quantity.Length) CoordinateSystem(com.revolsys.geometry.cs.CoordinateSystem) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) Quantity(javax.measure.Quantity) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) Unit(javax.measure.Unit)

Aggregations

Length (javax.measure.quantity.Length)44 Angle (javax.measure.quantity.Angle)7 Test (org.junit.Test)7 Color (java.awt.Color)5 Unit (javax.measure.Unit)5 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 AffineTransform (java.awt.geom.AffineTransform)4 ArrayList (java.util.ArrayList)4 GeographicCoordinateSystem (com.revolsys.geometry.cs.GeographicCoordinateSystem)3 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 LineString (com.revolsys.geometry.model.LineString)3 Point (com.revolsys.geometry.model.Point)3 Shape (java.awt.Shape)3 Rectangle2D (java.awt.geom.Rectangle2D)3 Path (java.nio.file.Path)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Quantity (javax.measure.Quantity)3 CloseableAffineTransform (com.revolsys.awt.CloseableAffineTransform)2 ProjectedCoordinateSystem (com.revolsys.geometry.cs.ProjectedCoordinateSystem)2 ChainedCoordinatesOperation (com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation)2