Search in sources :

Example 1 with Pair

use of com.revolsys.util.Pair in project com.revolsys.open by revolsys.

the class LasProjection method convertGeoTiffProjection.

@SuppressWarnings("unused")
private static Object convertGeoTiffProjection(final LasPointCloudHeader header, final byte[] bytes) {
    try {
        final List<Double> doubleParams = new ArrayList<>();
        {
            final LasVariableLengthRecord doubleParamsProperty = header.getLasProperty(new Pair<>(LASF_PROJECTION, LASF_PROJECTION_TIFF_GEO_DOUBLE_PARAMS));
            if (doubleParamsProperty != null) {
                final byte[] doubleParamBytes = doubleParamsProperty.getBytes();
                final ByteBuffer buffer = ByteBuffer.wrap(doubleParamBytes);
                buffer.order(ByteOrder.LITTLE_ENDIAN);
                for (int i = 0; i < doubleParamBytes.length / 8; i++) {
                    final double value = buffer.getDouble();
                    doubleParams.add(value);
                }
            }
        }
        byte[] asciiParamsBytes;
        {
            final LasVariableLengthRecord asciiParamsProperty = header.getLasProperty(new Pair<>(LASF_PROJECTION, LASF_PROJECTION_TIFF_GEO_ASCII_PARAMS));
            if (asciiParamsProperty == null) {
                asciiParamsBytes = new byte[0];
            } else {
                asciiParamsBytes = asciiParamsProperty.getBytes();
            }
        }
        final Map<Integer, Object> properties = new LinkedHashMap<>();
        final ByteBuffer buffer = ByteBuffer.wrap(bytes);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        final int keyDirectoryVersion = Buffers.getLEUnsignedShort(buffer);
        final int keyRevision = Buffers.getLEUnsignedShort(buffer);
        final int minorRevision = Buffers.getLEUnsignedShort(buffer);
        final int numberOfKeys = Buffers.getLEUnsignedShort(buffer);
        for (int i = 0; i < numberOfKeys; i++) {
            final int keyId = Buffers.getLEUnsignedShort(buffer);
            final int tagLocation = Buffers.getLEUnsignedShort(buffer);
            final int count = Buffers.getLEUnsignedShort(buffer);
            final int offset = Buffers.getLEUnsignedShort(buffer);
            if (tagLocation == 0) {
                properties.put(keyId, offset);
            } else if (tagLocation == LASF_PROJECTION_TIFF_GEO_DOUBLE_PARAMS) {
                final double value = doubleParams.get(offset);
                properties.put(keyId, value);
            } else if (tagLocation == LASF_PROJECTION_TIFF_GEO_ASCII_PARAMS) {
                final String value = new String(asciiParamsBytes, offset, count, StandardCharsets.US_ASCII);
                properties.put(keyId, value);
            }
        }
        CoordinateSystem coordinateSystem = null;
        int coordinateSystemId = Maps.getInteger(properties, TiffImage.PROJECTED_COORDINATE_SYSTEM_ID, 0);
        if (coordinateSystemId == 0) {
            coordinateSystemId = Maps.getInteger(properties, TiffImage.GEOGRAPHIC_COORDINATE_SYSTEM_ID, 0);
            if (coordinateSystemId != 0) {
                coordinateSystem = EpsgCoordinateSystems.getCoordinateSystem(coordinateSystemId);
            }
        } else if (coordinateSystemId <= 0 || coordinateSystemId == 32767) {
            final int geoSrid = Maps.getInteger(properties, TiffImage.GEOGRAPHIC_COORDINATE_SYSTEM_ID, 0);
            if (geoSrid != 0) {
                if (geoSrid > 0 && geoSrid < 32767) {
                    final GeographicCoordinateSystem geographicCoordinateSystem = EpsgCoordinateSystems.getCoordinateSystem(geoSrid);
                    final String name = "unknown";
                    final CoordinateOperationMethod coordinateOperationMethod = TiffImage.getProjection(properties);
                    final Map<ParameterName, ParameterValue> parameters = new LinkedHashMap<>();
                    TiffImage.addDoubleParameter(parameters, ParameterNames.STANDARD_PARALLEL_1, properties, TiffImage.STANDARD_PARALLEL_1_KEY);
                    TiffImage.addDoubleParameter(parameters, ParameterNames.STANDARD_PARALLEL_2, properties, TiffImage.STANDARD_PARALLEL_2_KEY);
                    TiffImage.addDoubleParameter(parameters, ParameterNames.CENTRAL_MERIDIAN, properties, TiffImage.LONGITUDE_OF_CENTER_2_KEY);
                    TiffImage.addDoubleParameter(parameters, ParameterNames.LATITUDE_OF_ORIGIN, properties, TiffImage.LATITUDE_OF_CENTER_2_KEY);
                    TiffImage.addDoubleParameter(parameters, ParameterNames.FALSE_EASTING, properties, TiffImage.FALSE_EASTING_KEY);
                    TiffImage.addDoubleParameter(parameters, ParameterNames.FALSE_NORTHING, properties, TiffImage.FALSE_NORTHING_KEY);
                    final LinearUnit linearUnit = TiffImage.getLinearUnit(properties);
                    final ProjectedCoordinateSystem projectedCoordinateSystem = new ProjectedCoordinateSystem(coordinateSystemId, name, geographicCoordinateSystem, coordinateOperationMethod, parameters, linearUnit);
                    coordinateSystem = EpsgCoordinateSystems.getCoordinateSystem(projectedCoordinateSystem);
                }
            }
        } else {
            coordinateSystem = EpsgCoordinateSystems.getCoordinateSystem(coordinateSystemId);
        }
        header.setCoordinateSystemInternal(coordinateSystem);
        return coordinateSystem;
    } catch (final IOException e) {
        throw Exceptions.wrap(e);
    }
}
Also used : LinearUnit(com.revolsys.geometry.cs.unit.LinearUnit) CoordinateSystem(com.revolsys.geometry.cs.CoordinateSystem) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) ArrayList(java.util.ArrayList) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) LinkedHashMap(java.util.LinkedHashMap) CoordinateOperationMethod(com.revolsys.geometry.cs.CoordinateOperationMethod) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(com.revolsys.util.Pair)

Example 2 with Pair

use of com.revolsys.util.Pair in project com.revolsys.open by revolsys.

the class LineString method findClosestGeometryComponent.

@Override
default Pair<GeometryComponent, Double> findClosestGeometryComponent(final double x, final double y) {
    if (isEmpty()) {
        return new Pair<>();
    } else {
        final GeometryFactory geometryFactory = getGeometryFactory();
        boolean closestIsVertex = false;
        int closestIndex = 0;
        double x1 = getX(0);
        double y1 = getY(0);
        if (x == x1 && y == y1) {
            final AbstractVertex closestVertex = getVertex(0);
            return new Pair<>(closestVertex, 0.0);
        } else {
            double closestDistance = geometryFactory.makePrecise(0, MathUtil.distance(x, y, x1, y1));
            final int vertexCount = getVertexCount();
            for (int vertexIndex = 1; vertexIndex < vertexCount; vertexIndex++) {
                final double x2 = getX(vertexIndex);
                final double y2 = getY(vertexIndex);
                if (x == x2 && y == y2) {
                    final AbstractVertex closestVertex = getVertex(vertexIndex);
                    return new Pair<>(closestVertex, 0.0);
                } else {
                    final double toDistance = geometryFactory.makePrecise(0, MathUtil.distance(x, y, x2, y2));
                    if (toDistance <= closestDistance) {
                        if (!closestIsVertex || toDistance < closestDistance) {
                            closestIndex = vertexIndex;
                            closestIsVertex = true;
                            closestDistance = toDistance;
                        }
                    }
                    final double segmentDistance = geometryFactory.makePrecise(0, LineSegmentUtil.distanceLinePoint(x1, y1, x2, y2, x, y));
                    if (segmentDistance == 0) {
                        final Segment closestSegment = getSegment(vertexIndex - 1);
                        return new Pair<>(closestSegment, 0.0);
                    } else if (segmentDistance < closestDistance) {
                        closestIsVertex = false;
                        closestIndex = vertexIndex - 1;
                        closestDistance = segmentDistance;
                    }
                }
                x1 = x2;
                y1 = y2;
            }
            if (closestIsVertex) {
                final Vertex closestVertex = getVertex(closestIndex);
                return new Pair<>(closestVertex, closestDistance);
            } else {
                final Segment closestSegment = getSegment(closestIndex);
                return new Pair<>(closestSegment, closestDistance);
            }
        }
    }
}
Also used : AbstractVertex(com.revolsys.geometry.model.vertex.AbstractVertex) Vertex(com.revolsys.geometry.model.vertex.Vertex) LineStringVertex(com.revolsys.geometry.model.vertex.LineStringVertex) AbstractVertex(com.revolsys.geometry.model.vertex.AbstractVertex) LineStringSegment(com.revolsys.geometry.model.segment.LineStringSegment) Segment(com.revolsys.geometry.model.segment.Segment) Pair(com.revolsys.util.Pair)

Example 3 with Pair

use of com.revolsys.util.Pair in project com.revolsys.open by revolsys.

the class Geometry method findClosestGeometryComponent.

default Pair<GeometryComponent, Double> findClosestGeometryComponent(final double x, final double y, final double maxDistance) {
    if (isEmpty()) {
        return new Pair<>();
    } else {
        GeometryComponent closestComponent = null;
        double closestDistance = Double.POSITIVE_INFINITY;
        for (final Segment segment : segments()) {
            boolean matched = false;
            if (segment.isLineStart()) {
                final Vertex from = segment.getGeometryVertex(0);
                if (from.equalsVertex(x, y)) {
                    return new Pair<>(from, 0.0);
                } else {
                    final double fromDistance = from.distance(x, y);
                    if (fromDistance <= maxDistance) {
                        if (// 
                        fromDistance < closestDistance || fromDistance == closestDistance && !(closestComponent instanceof Vertex)) {
                            closestDistance = fromDistance;
                            closestComponent = from.clone();
                            matched = true;
                        }
                    }
                }
            }
            {
                final Vertex to = segment.getGeometryVertex(1);
                if (to.equalsVertex(x, y)) {
                    return new Pair<>(to, 0.0);
                } else {
                    final double toDistance = to.distance(x, y);
                    if (toDistance <= maxDistance) {
                        if (// 
                        toDistance < closestDistance || toDistance == closestDistance && !(closestComponent instanceof Vertex)) {
                            closestDistance = toDistance;
                            closestComponent = to.clone();
                            matched = true;
                        }
                    }
                }
            }
            if (!matched) {
                final double segmentDistance = segment.distance(x, y);
                if (segmentDistance == 0) {
                    return new Pair<>(segment, 0.0);
                } else if (segmentDistance <= maxDistance) {
                    if (// 
                    segmentDistance < closestDistance || segmentDistance == closestDistance && !(closestComponent instanceof Vertex)) {
                        closestDistance = segmentDistance;
                        closestComponent = segment.clone();
                    }
                }
            }
        }
        if (Double.isFinite(closestDistance)) {
            return new Pair<>(closestComponent, closestDistance);
        } else {
            return new Pair<>();
        }
    }
}
Also used : Vertex(com.revolsys.geometry.model.vertex.Vertex) Segment(com.revolsys.geometry.model.segment.Segment) Pair(com.revolsys.util.Pair)

Example 4 with Pair

use of com.revolsys.util.Pair in project com.revolsys.open by revolsys.

the class LasPointCloudHeader method readVariableLengthRecords.

private int readVariableLengthRecords(final ChannelReader reader, final long numberOfVariableLengthRecords) throws IOException {
    int byteCount = 0;
    for (int i = 0; i < numberOfVariableLengthRecords; i++) {
        @SuppressWarnings("unused") final int // Ignore reserved
        reserved = reader.getUnsignedShort();
        // value;
        final String userId = reader.getUsAsciiString(16);
        final int recordId = reader.getUnsignedShort();
        final int valueLength = reader.getUnsignedShort();
        final String description = reader.getUsAsciiString(32);
        final byte[] bytes = reader.getBytes(valueLength);
        final LasVariableLengthRecord property = new LasVariableLengthRecord(userId, recordId, description, bytes);
        addProperty(property);
        byteCount += 54 + valueLength;
    }
    for (final Entry<Pair<String, Integer>, LasVariableLengthRecord> entry : this.lasProperties.entrySet()) {
        final Pair<String, Integer> key = entry.getKey();
        final LasVariableLengthRecord property = entry.getValue();
        final BiFunction<LasPointCloudHeader, byte[], Object> converter = PROPERTY_FACTORY_BY_KEY.get(key);
        if (converter != null) {
            property.convertValue(converter, this);
        }
    }
    return byteCount;
}
Also used : LasPoint(com.revolsys.elevation.cloud.las.pointformat.LasPoint) Pair(com.revolsys.util.Pair)

Example 5 with Pair

use of com.revolsys.util.Pair in project com.revolsys.open by revolsys.

the class MavenRepository method getSnapshotVersion.

protected Pair<Long, String> getSnapshotVersion(final String groupId, final String artifactId, final String version, final String type, final String classifier, final String algorithm) {
    final MapEx mavenMetadata = getMavenMetadata(groupId, artifactId, version);
    if (mavenMetadata == MapEx.EMPTY) {
        return null;
    } else {
        String specificVersion = version;
        long time = 0;
        final MapEx versioning = mavenMetadata.getValue("versioning");
        if (versioning != null) {
            final MapEx snapshot = versioning.getValue("snapshot");
            time = versioning.getLong("lastUpdated", 0);
            if (snapshot != null) {
                final String timestamp = snapshot.getString("timestamp");
                if (Property.hasValue(timestamp)) {
                    final String buildNumber = snapshot.getString("buildNumber");
                    final StringBuilder specificVersionBuilder = new StringBuilder(version.substring(0, version.length() - 8));
                    specificVersionBuilder.append(timestamp);
                    specificVersionBuilder.append('-');
                    if (Property.hasValue(buildNumber)) {
                        specificVersionBuilder.append(buildNumber);
                    } else {
                        specificVersionBuilder.append('1');
                    }
                    specificVersion = specificVersionBuilder.toString();
                }
            }
        }
        final Pair<Long, String> timeAndVersion = new Pair<>(time, specificVersion);
        return timeAndVersion;
    }
}
Also used : MapEx(com.revolsys.collection.map.MapEx) Pair(com.revolsys.util.Pair)

Aggregations

Pair (com.revolsys.util.Pair)6 Segment (com.revolsys.geometry.model.segment.Segment)2 Vertex (com.revolsys.geometry.model.vertex.Vertex)2 MapEx (com.revolsys.collection.map.MapEx)1 LasPoint (com.revolsys.elevation.cloud.las.pointformat.LasPoint)1 CoordinateOperationMethod (com.revolsys.geometry.cs.CoordinateOperationMethod)1 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)1 GeographicCoordinateSystem (com.revolsys.geometry.cs.GeographicCoordinateSystem)1 ProjectedCoordinateSystem (com.revolsys.geometry.cs.ProjectedCoordinateSystem)1 LinearUnit (com.revolsys.geometry.cs.unit.LinearUnit)1 LineStringSegment (com.revolsys.geometry.model.segment.LineStringSegment)1 AbstractVertex (com.revolsys.geometry.model.vertex.AbstractVertex)1 LineStringVertex (com.revolsys.geometry.model.vertex.LineStringVertex)1 BaseCloseable (com.revolsys.io.BaseCloseable)1 Field (com.revolsys.swing.field.Field)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1