Search in sources :

Example 31 with Length

use of javax.measure.quantity.Length in project sis by apache.

the class FranceGeocentricInterpolation method load.

/**
 * Unconditionally loads the grid for the given file without in-memory compression.
 *
 * @param  in    reader of the RGF93 datum shift file.
 * @param  file  path to the file being read, used only for error reporting.
 * @throws IOException if an I/O error occurred.
 * @throws NumberFormatException if a number can not be parsed.
 * @throws NoSuchElementException if a data line is missing a value.
 * @throws FactoryException if an problem is found with the file content.
 * @throws ArithmeticException if the width or the height exceed the integer capacity.
 */
static DatumShiftGridFile.Float<Angle, Length> load(final BufferedReader in, final Path file) throws IOException, FactoryException, NoninvertibleTransformException {
    DatumShiftGridFile.Float<Angle, Length> grid = null;
    double x0 = 0;
    double xf = 0;
    double y0 = 0;
    double yf = 0;
    double Δx = 0;
    double Δy = 0;
    int nx = 0;
    int ny = 0;
    /*
         * The header should be like below, but the only essential line for this class is the one
         * starting with "GR3D1". We also check that "GR3D2" declares the expected interpolation.
         *
         *     GR3D  002024 024 20370201
         *     GR3D1   -5.5000  10.0000  41.0000  52.0000    .1000    .1000
         *     GR3D2 INTERPOLATION BILINEAIRE
         *     GR3D3 PREC CM 01:5 02:10 03:20 04:50 99>100
         */
    String line;
    while (true) {
        line = in.readLine();
        if (line == null) {
            throw new EOFException(Errors.format(Errors.Keys.UnexpectedEndOfFile_1, file));
        }
        final int length = CharSequences.skipTrailingWhitespaces(line, 0, line.length());
        if (length <= 0) {
            // Skip empty lines.
            continue;
        }
        int p = CharSequences.skipLeadingWhitespaces(line, 0, length);
        if (line.charAt(p) == '#') {
            // Skip comment lines (not officially part of the format).
            continue;
        }
        if (!line.regionMatches(true, p, HEADER, 0, HEADER.length())) {
            // End of header.
            break;
        }
        if ((p += HEADER.length()) < length) {
            final char c = line.charAt(p);
            p = CharSequences.skipLeadingWhitespaces(line, p + 1, length);
            switch(c) {
                case '1':
                    {
                        if (grid != null) {
                            throw new FactoryException(Errors.format(Errors.Keys.DuplicatedElement_1, HEADER));
                        }
                        final double[] gridGeometry = CharSequences.parseDoubles(line.substring(p, length), ' ');
                        if (gridGeometry.length == 6) {
                            x0 = gridGeometry[0];
                            xf = gridGeometry[1];
                            y0 = gridGeometry[2];
                            yf = gridGeometry[3];
                            Δx = gridGeometry[4];
                            Δy = gridGeometry[5];
                            nx = Math.toIntExact(Math.round((xf - x0) / Δx + 1));
                            ny = Math.toIntExact(Math.round((yf - y0) / Δy + 1));
                            grid = new DatumShiftGridFile.Float<>(3, Units.DEGREE, Units.METRE, false, x0, y0, Δx, Δy, nx, ny, PARAMETERS, file);
                        }
                        break;
                    }
                case '2':
                    {
                        final String interp = line.substring(p, length);
                        if (!interp.matches("(?i)INTERPOLATION[^A-Z]+BILINEAIRE")) {
                            final LogRecord record = Errors.getResources((Locale) null).getLogRecord(Level.WARNING, Errors.Keys.UnsupportedInterpolation_1, interp);
                            record.setLoggerName(Loggers.COORDINATE_OPERATION);
                            Logging.log(FranceGeocentricInterpolation.class, "createMathTransform", record);
                        // We declare 'createMathTransform' method because it is closer to public API.
                        }
                        break;
                    }
            }
        }
    }
    if (grid == null) {
        throw new FactoryException(Errors.format(Errors.Keys.CanNotParseFile_2, HEADER, file));
    }
    /*
         * Loads the data with the sign of all offsets reversed. Data columns are
         *
         *     (unknown), longitude, latitude, tX, tY, tZ, accuracy code, data sheet (ignored)
         *
         * where the longitude and latitude values are in RGF93 system.
         * Example:
         *
         *     00002   -5.500000000   41.000000000  -165.027   -67.100   315.813  99  -0158
         *     00002   -5.500000000   41.100000000  -165.169   -66.948   316.007  99  -0157
         *     00002   -5.500000000   41.200000000  -165.312   -66.796   316.200  99  -0157
         *
         * Translation values in the IGN file are from NTF to RGF93, but Apache SIS implementation needs
         * the opposite direction (from RGF93 to NTF). The reason is that SIS expect the source datum to
         * be the datum in which longitude and latitude values are expressed.
         */
    final float[] tX = grid.offsets[0];
    final float[] tY = grid.offsets[1];
    final float[] tZ = grid.offsets[2];
    do {
        final StringTokenizer t = new StringTokenizer(line.trim());
        // Ignored
        t.nextToken();
        // Longitude in degrees
        final double x = Double.parseDouble(t.nextToken());
        // Latitude in degrees
        final double y = Double.parseDouble(t.nextToken());
        // Column index
        final int i = Math.toIntExact(Math.round((x - x0) / Δx));
        // Row index
        final int j = Math.toIntExact(Math.round((y - y0) / Δy));
        if (i < 0 || i >= nx) {
            throw new FactoryException(Errors.format(Errors.Keys.ValueOutOfRange_4, "x", x, x0, xf));
        }
        if (j < 0 || j >= ny) {
            throw new FactoryException(Errors.format(Errors.Keys.ValueOutOfRange_4, "y", y, y0, yf));
        }
        final int p = j * nx + i;
        if (!Double.isNaN(tX[p]) || !Double.isNaN(tY[p]) || !Double.isNaN(tZ[p])) {
            throw new FactoryException(Errors.format(Errors.Keys.ValueAlreadyDefined_1, x + ", " + y));
        }
        // See javadoc for the reason why we reverse the sign.
        tX[p] = -parseFloat(t.nextToken());
        tY[p] = -parseFloat(t.nextToken());
        tZ[p] = -parseFloat(t.nextToken());
        final double accuracy = ACCURACY[Math.min(ACCURACY.length - 1, Math.max(0, Integer.parseInt(t.nextToken()) - 1))];
        if (!(accuracy >= grid.accuracy)) {
            // Use '!' for replacing the initial NaN.
            grid.accuracy = accuracy;
        }
    } while ((line = in.readLine()) != null);
    return grid;
}
Also used : FactoryException(org.opengis.util.FactoryException) Float.parseFloat(java.lang.Float.parseFloat) StringTokenizer(java.util.StringTokenizer) Angle(javax.measure.quantity.Angle) Length(javax.measure.quantity.Length) LogRecord(java.util.logging.LogRecord) EOFException(java.io.EOFException)

Example 32 with Length

use of javax.measure.quantity.Length in project sis by apache.

the class CRSBuilder method verify.

/**
 * Verifies if the user-defined CRS created from GeoTIFF values
 * matches the given CRS created from the EPSG geodetic dataset.
 * This method does not verify the EPSG code of the given CRS.
 *
 * @param  crs  the CRS created from the EPSG geodetic dataset.
 */
private void verify(final ProjectedCRS crs) throws FactoryException {
    final Unit<Length> linearUnit = createUnit(GeoKeys.LinearUnits, GeoKeys.LinearUnitSize, Length.class, Units.METRE);
    final Unit<Angle> angularUnit = createUnit(GeoKeys.AngularUnits, GeoKeys.AngularUnitSize, Angle.class, Units.DEGREE);
    final GeographicCRS baseCRS = crs.getBaseCRS();
    verifyIdentifier(crs, baseCRS, GeoKeys.GeographicType);
    verify(baseCRS, angularUnit);
    final Conversion projection = crs.getConversionFromBase();
    verifyIdentifier(crs, projection, GeoKeys.Projection);
    verify(projection, angularUnit, linearUnit);
}
Also used : Angle(javax.measure.quantity.Angle) Length(javax.measure.quantity.Length) GeographicCRS(org.opengis.referencing.crs.GeographicCRS) DefaultGeographicCRS(org.apache.sis.referencing.crs.DefaultGeographicCRS) Conversion(org.opengis.referencing.operation.Conversion)

Example 33 with Length

use of javax.measure.quantity.Length in project sis by apache.

the class CRSBuilder method verify.

/**
 * Verifies if the user-defined CRS created from GeoTIFF values
 * matches the given CRS created from the EPSG geodetic dataset.
 * This method does not verify the EPSG code of the given CRS.
 *
 * @param  crs  the CRS created from the EPSG geodetic dataset.
 */
private void verify(final GeocentricCRS crs) throws FactoryException {
    /*
         * Note: current createUnit(…) implementation does not allow us to distinguish whether METRE ou DEGREE units
         * were specified in the GeoTIFF file or if we got the default values. We do not compare units of that reason.
         */
    final Unit<Length> linearUnit = createUnit(GeoKeys.GeogLinearUnits, GeoKeys.GeogLinearUnitSize, Length.class, Units.METRE);
    final Unit<Angle> angularUnit = createUnit(GeoKeys.AngularUnits, GeoKeys.AngularUnitSize, Angle.class, Units.DEGREE);
    final GeodeticDatum datum = crs.getDatum();
    verifyIdentifier(crs, datum, GeoKeys.GeodeticDatum);
    verify(datum, angularUnit, linearUnit);
}
Also used : Angle(javax.measure.quantity.Angle) Length(javax.measure.quantity.Length) GeodeticDatum(org.opengis.referencing.datum.GeodeticDatum)

Example 34 with Length

use of javax.measure.quantity.Length in project sis by apache.

the class CRSBuilder method verify.

/**
 * Verifies if the user-defined CRS created from GeoTIFF values
 * matches the given CRS created from the EPSG geodetic dataset.
 * This method does not verify the EPSG code of the given CRS.
 *
 * @param  crs          the CRS created from the EPSG geodetic dataset.
 * @param  angularUnit  the angular unit of the latitude and longitude values.
 */
private void verify(final GeographicCRS crs, final Unit<Angle> angularUnit) throws FactoryException {
    /*
         * Note: current createUnit(…) implementation does not allow us to distinguish whether METRE ou DEGREE units
         * were specified in the GeoTIFF file or if we got the default values. We do not compare units of that reason.
         */
    final Unit<Length> linearUnit = createUnit(GeoKeys.GeogLinearUnits, GeoKeys.GeogLinearUnitSize, Length.class, Units.METRE);
    final GeodeticDatum datum = crs.getDatum();
    verifyIdentifier(crs, datum, GeoKeys.GeodeticDatum);
    verify(datum, angularUnit, linearUnit);
}
Also used : Length(javax.measure.quantity.Length) GeodeticDatum(org.opengis.referencing.datum.GeodeticDatum)

Example 35 with Length

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

the class PdfViewport method setGeometryStyle.

private void setGeometryStyle(final GeometryStyle style) throws IOException {
    String styleName = this.styleNames.get(style);
    if (styleName == null) {
        styleName = "rgStyle" + this.styleId++;
        final PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
        final int lineOpacity = style.getLineOpacity();
        if (lineOpacity != 255) {
            graphicsState.setStrokingAlphaConstant(lineOpacity / 255f);
        }
        final Quantity<Length> lineWidth = style.getLineWidth();
        final Unit<Length> unit = lineWidth.getUnit();
        graphicsState.setLineWidth((float) toDisplayValue(lineWidth));
        final List<Double> lineDashArray = style.getLineDashArray();
        if (lineDashArray != null && !lineDashArray.isEmpty()) {
            int size = lineDashArray.size();
            if (size == 1) {
                size++;
            }
            final float[] dashArray = new float[size];
            for (int i = 0; i < dashArray.length; i++) {
                if (i < lineDashArray.size()) {
                    final Double dashDouble = lineDashArray.get(i);
                    final Quantity<Length> dashMeasure = Quantities.getQuantity(dashDouble, unit);
                    final float dashFloat = (float) toDisplayValue(dashMeasure);
                    dashArray[i] = dashFloat;
                } else {
                    dashArray[i] = dashArray[i - 1];
                }
            }
            final int offset = (int) toDisplayValue(Quantities.getQuantity(style.getLineDashOffset(), unit));
            final COSArray dashCosArray = new COSArray();
            dashCosArray.setFloatArray(dashArray);
            final PDLineDashPattern pattern = new PDLineDashPattern(dashCosArray, offset);
            graphicsState.setLineDashPattern(pattern);
        }
        switch(style.getLineCap()) {
            case BUTT:
                graphicsState.setLineCapStyle(0);
                break;
            case ROUND:
                graphicsState.setLineCapStyle(1);
                break;
            case SQUARE:
                graphicsState.setLineCapStyle(2);
                break;
        }
        switch(style.getLineJoin()) {
            case MITER:
                graphicsState.setLineJoinStyle(0);
                break;
            case ROUND:
                graphicsState.setLineJoinStyle(1);
                break;
            case BEVEL:
                graphicsState.setLineJoinStyle(2);
                break;
        }
        final int polygonFillOpacity = style.getPolygonFillOpacity();
        if (polygonFillOpacity != 255) {
            graphicsState.setNonStrokingAlphaConstant(polygonFillOpacity / 255f);
        }
        final PDResources resources = this.page.findResources();
        Map<String, PDExtendedGraphicsState> graphicsStateDictionary = resources.getGraphicsStates();
        if (graphicsStateDictionary == null) {
            graphicsStateDictionary = new TreeMap<>();
        }
        graphicsStateDictionary.put(styleName, graphicsState);
        resources.setGraphicsStates(graphicsStateDictionary);
        this.styleNames.put(style, styleName);
    }
    this.contentStream.appendRawCommands("/" + styleName + " gs\n");
}
Also used : PDExtendedGraphicsState(org.apache.pdfbox.pdmodel.graphics.PDExtendedGraphicsState) PDResources(org.apache.pdfbox.pdmodel.PDResources) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) COSArray(org.apache.pdfbox.cos.COSArray) Length(javax.measure.quantity.Length) PDLineDashPattern(org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern)

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