Search in sources :

Example 6 with PointDoubleXYZ

use of com.revolsys.geometry.model.impl.PointDoubleXYZ in project com.revolsys.open by revolsys.

the class LengthIndexedLineTest method testComputeZ.

/**
 * Tests that z values are interpolated
 */
public void testComputeZ() {
    final Geometry linearGeom = read("LINESTRINGZ(0 0 0, 10 10 10)");
    final LengthIndexedLine indexedLine = new LengthIndexedLine(linearGeom);
    final double projIndex = indexedLine.project(new PointDoubleXY(5, 5));
    final Point projPt = indexedLine.extractPoint(projIndex);
    // System.out.println(projPt);
    assertTrue(projPt.equals(3, new PointDoubleXYZ(5.0, 5, 5)));
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) PointDoubleXYZ(com.revolsys.geometry.model.impl.PointDoubleXYZ) LengthIndexedLine(com.revolsys.geometry.linearref.LengthIndexedLine) PointDoubleXY(com.revolsys.geometry.model.impl.PointDoubleXY) Point(com.revolsys.geometry.model.Point)

Example 7 with PointDoubleXYZ

use of com.revolsys.geometry.model.impl.PointDoubleXYZ in project com.revolsys.open by revolsys.

the class GeoNamesService method mapToObjects.

private List<Record> mapToObjects(final RecordDefinition recordDefinition, final Map<String, Object> result) {
    final List<Record> results = new ArrayList<>();
    final List<Map<String, Object>> names = (List<Map<String, Object>>) result.get("geonames");
    for (final Map<String, Object> name : names) {
        final Record record = recordDefinition.newRecord();
        for (final String fieldName : recordDefinition.getFieldNames()) {
            final Object value = name.get(fieldName);
            if (value != null) {
                record.setValue(fieldName, value);
            }
        }
        final double lat = ((Number) name.get("lat")).doubleValue();
        final double lon = ((Number) name.get("lng")).doubleValue();
        Point coordinate = new PointDoubleXY(lon, lat);
        final Number elevation = (Number) name.get("elevation");
        if (elevation == null) {
            coordinate = new PointDoubleXY(lon, lat);
        } else {
            coordinate = new PointDoubleXYZ(lon, lat, elevation.doubleValue());
        }
        record.setGeometryValue(GeometryFactory.DEFAULT_3D.point(coordinate));
        results.add(record);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) PointDoubleXYZ(com.revolsys.geometry.model.impl.PointDoubleXYZ) Record(com.revolsys.record.Record) ArrayList(java.util.ArrayList) List(java.util.List) PointDoubleXY(com.revolsys.geometry.model.impl.PointDoubleXY) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with PointDoubleXYZ

use of com.revolsys.geometry.model.impl.PointDoubleXYZ in project com.revolsys.open by revolsys.

the class TriangulatedIrregularNetwork method getElevation.

default double getElevation(Point point) {
    point = convertGeometry(point);
    final List<Triangle> triangles = getTriangles(point);
    for (final Triangle triangle : triangles) {
        final Point t0 = triangle.getP0();
        if (t0.equals(point)) {
            return t0.getZ();
        }
        final Point t1 = triangle.getP1();
        if (t1.equals(point)) {
            return t1.getZ();
        }
        final Point t2 = triangle.getP2();
        if (t2.equals(point)) {
            return t2.getZ();
        }
        Point closestCorner = t0;
        LineSegment oppositeEdge = new LineSegmentDoubleGF(t1, t2);
        double closestDistance = point.distancePoint(closestCorner);
        final double t1Distance = point.distancePoint(t1);
        if (closestDistance > t1Distance) {
            closestCorner = t1;
            oppositeEdge = new LineSegmentDoubleGF(t2, t0);
            closestDistance = t1Distance;
        }
        if (closestDistance > point.distancePoint(t2)) {
            closestCorner = t2;
            oppositeEdge = new LineSegmentDoubleGF(t0, t1);
        }
        LineSegment segment = new LineSegmentDoubleGF(closestCorner, point).extend(0, t0.distancePoint(t1) + t1.distancePoint(t2) + t0.distancePoint(t2));
        final Geometry intersectCoordinates = oppositeEdge.getIntersection(segment);
        if (intersectCoordinates.getVertexCount() > 0) {
            final Point intersectPoint = intersectCoordinates.getVertex(0);
            final double z = oppositeEdge.getElevation(intersectPoint);
            if (!Double.isNaN(z)) {
                final double x = intersectPoint.getX();
                final double y = intersectPoint.getY();
                final Point end = new PointDoubleXYZ(x, y, z);
                segment = new LineSegmentDoubleGF(t0, end);
                return segment.getElevation(point);
            }
        }
    }
    return Double.NaN;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) PointDoubleXYZ(com.revolsys.geometry.model.impl.PointDoubleXYZ) Triangle(com.revolsys.geometry.model.Triangle) Point(com.revolsys.geometry.model.Point) LineSegmentDoubleGF(com.revolsys.geometry.model.segment.LineSegmentDoubleGF) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 9 with PointDoubleXYZ

use of com.revolsys.geometry.model.impl.PointDoubleXYZ in project com.revolsys.open by revolsys.

the class CGAlgorithms3D method distanceSegmentSegment.

/**
 * Computes the distance between two 3D segments.
 *
 * @param A the start point of the first segment
 * @param B the end point of the first segment
 * @param C the start point of the second segment
 * @param D the end point of the second segment
 * @return the distance between the segments
 */
public static double distanceSegmentSegment(final Point A, final Point B, final Point C, final Point D) {
    /**
     * This calculation is susceptible to roundoff errors when
     * passed large ordinate values.
     * It may be possible to improve this by using {@link DD} arithmetic.
     */
    if (A.equals(3, B)) {
        return distancePointSegment(A, C, D);
    }
    if (C.equals(3, B)) {
        return distancePointSegment(C, A, B);
    }
    /**
     * Algorithm derived from http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
     */
    final double a = Vector3D.dot(A, B, A, B);
    final double b = Vector3D.dot(A, B, C, D);
    final double c = Vector3D.dot(C, D, C, D);
    final double d = Vector3D.dot(A, B, C, A);
    final double e = Vector3D.dot(C, D, C, A);
    final double denom = a * c - b * b;
    if (Double.isNaN(denom)) {
        throw new IllegalArgumentException("Ordinates must not be NaN");
    }
    double s;
    double t;
    if (denom <= 0.0) {
        /**
         * The lines are parallel.
         * In this case solve for the parameters s and t by assuming s is 0.
         */
        s = 0;
        // choose largest denominator for optimal numeric conditioning
        if (b > c) {
            t = d / b;
        } else {
            t = e / c;
        }
    } else {
        s = (b * e - c * d) / denom;
        t = (a * e - b * d) / denom;
    }
    if (s < 0) {
        return distancePointSegment(A, C, D);
    } else if (s > 1) {
        return distancePointSegment(B, C, D);
    } else if (t < 0) {
        return distancePointSegment(C, A, B);
    } else if (t > 1) {
        return distancePointSegment(D, A, B);
    }
    /**
     * The closest points are in interiors of segments,
     * so compute them directly
     */
    final double x1 = A.getX() + s * (B.getX() - A.getX());
    final double y1 = A.getY() + s * (B.getY() - A.getY());
    final double z1 = A.getZ() + s * (B.getZ() - A.getZ());
    final double x2 = C.getX() + t * (D.getX() - C.getX());
    final double y2 = C.getY() + t * (D.getY() - C.getY());
    final double z2 = C.getZ() + t * (D.getZ() - C.getZ());
    // length (p1-p2)
    return distance(new PointDoubleXYZ(x1, y1, z1), new PointDoubleXYZ(x2, y2, z2));
}
Also used : PointDoubleXYZ(com.revolsys.geometry.model.impl.PointDoubleXYZ)

Example 10 with PointDoubleXYZ

use of com.revolsys.geometry.model.impl.PointDoubleXYZ in project com.revolsys.open by revolsys.

the class Graph method splitEdge.

public <V extends Point> List<Edge<T>> splitEdge(final Edge<T> edge, final Collection<V> splitPoints, final double maxDistance) {
    final Collection<V> nodes = new ArrayList<>(splitPoints);
    if (edge.isRemoved()) {
        return Collections.emptyList();
    } else {
        final LineString line = edge.getLineString();
        final LineString points = line;
        final Set<Integer> splitVertices = new TreeSet<>();
        final Set<Integer> splitIndexes = new TreeSet<>();
        for (final Iterator<V> nodeIter = nodes.iterator(); nodeIter.hasNext(); ) {
            final Point node = nodeIter.next();
            final double distance = points.distanceVertex(0, node);
            if (distance < maxDistance) {
                nodeIter.remove();
            }
        }
        final Map<Point, Double> nodeDistanceMap = new HashMap<>();
        final Map<Point, Integer> nodeSegment = new HashMap<>();
        for (int i = 1; i < points.getVertexCount() && !nodes.isEmpty(); i++) {
            for (final Iterator<V> nodeIter = nodes.iterator(); nodeIter.hasNext(); ) {
                final Point node = nodeIter.next();
                final double nodeDistance = points.distanceVertex(i, node);
                if (nodeDistance < maxDistance) {
                    if (i < points.getVertexCount() - 1) {
                        splitVertices.add(i);
                        splitIndexes.add(i);
                    }
                    nodeDistanceMap.remove(node);
                    nodeSegment.remove(node);
                    nodeIter.remove();
                } else {
                    final int segmentIndex = i - 1;
                    final double x = node.getX();
                    final double y = node.getY();
                    final double x1 = points.getX(segmentIndex);
                    final double y1 = points.getY(segmentIndex);
                    final double x2 = points.getX(i);
                    final double y2 = points.getY(i);
                    final double segmentDistance = LineSegmentUtil.distanceLinePoint(x1, y1, x2, y2, x, y);
                    if (segmentDistance == 0) {
                        nodeDistanceMap.put(node, segmentDistance);
                        nodeSegment.put(node, segmentIndex);
                        nodeIter.remove();
                    } else {
                        final double projectionFactor = LineSegmentUtil.projectionFactor(x1, y1, x2, y2, x, y);
                        if (projectionFactor >= 0.0 && projectionFactor <= 1.0) {
                            final Double closestDistance = nodeDistanceMap.get(node);
                            if (closestDistance == null) {
                                nodeSegment.put(node, segmentIndex);
                                nodeDistanceMap.put(node, segmentDistance);
                            } else if (closestDistance.compareTo(segmentDistance) > 0) {
                                nodeSegment.put(node, segmentIndex);
                                nodeDistanceMap.put(node, segmentDistance);
                            }
                        }
                    }
                }
            }
        }
        final T object = edge.getObject();
        final GeometryFactory geometryFactory = line.getGeometryFactory();
        final Map<Integer, Set<Point>> segmentSplitNodes = new TreeMap<>();
        for (final Entry<Point, Integer> entry : nodeSegment.entrySet()) {
            final Point node = entry.getKey();
            final Integer index = entry.getValue();
            Set<Point> splitNodes = segmentSplitNodes.get(index);
            if (splitNodes == null) {
                final double x = points.getX(index);
                final double y = points.getY(index);
                splitNodes = new TreeSet<>(new CoordinatesDistanceComparator(x, y));
                segmentSplitNodes.put(index, splitNodes);
                splitIndexes.add(index);
            }
            splitNodes.add(node);
            nodes.remove(node);
        }
        if (nodes.isEmpty()) {
            final List<LineString> newLines = new ArrayList<>();
            int startIndex = 0;
            Point startPoint = null;
            for (final Integer index : splitIndexes) {
                if (splitVertices.contains(index)) {
                    final LineString newPoints = points.subLine(startPoint, startIndex, index - startIndex + 1, null);
                    newLines.add(newPoints);
                    startPoint = null;
                    startIndex = index;
                }
                final Set<Point> splitNodes = segmentSplitNodes.get(index);
                if (splitNodes != null) {
                    for (final Point splitPoint : splitNodes) {
                        final Node<T> node = getNode(splitPoint);
                        final String typePath = edge.getTypeName();
                        Point point = splitPoint;
                        double splitPointZ = splitPoint.getZ();
                        if (splitPointZ == 0 || Double.isNaN(splitPointZ)) {
                            if (splitPoint instanceof Node<?>) {
                                final Node<?> splitNode = (Node<?>) splitPoint;
                                point = splitNode.get3dCoordinates(typePath);
                                splitPointZ = point.getZ();
                            }
                            if (splitPointZ == 0 || Double.isNaN(splitPointZ)) {
                                point = node.get3dCoordinates(typePath);
                            }
                            if (splitPointZ == 0 || Double.isNaN(splitPointZ)) {
                                final Point p1 = points.getPoint(index);
                                final Point p2 = points.getPoint(index + 1);
                                final double z = LineSegmentUtil.getElevation(p1, p2, point);
                                point = new PointDoubleXYZ(point.getX(), point.getY(), z);
                            }
                        }
                        final LineString newPoints;
                        if (startIndex > index) {
                            newPoints = new LineStringDouble(points.getAxisCount(), startPoint, point);
                        } else {
                            newPoints = points.subLine(startPoint, startIndex, index - startIndex + 1, point);
                        }
                        newLines.add(newPoints);
                        startPoint = point;
                        startIndex = index + 1;
                    }
                }
            }
            final LineString newPoints = points.subLine(startPoint, startIndex, points.getVertexCount(), null);
            newLines.add(newPoints);
            if (newLines.size() > 1) {
                final List<Edge<T>> newEdges = new ArrayList<>();
                for (final LineString edgePoints : newLines) {
                    final Edge<T> newEdge = newEdge(geometryFactory, object, edgePoints);
                    newEdges.add(newEdge);
                }
                edge.remove();
                return newEdges;
            } else {
                return Collections.singletonList(edge);
            }
        } else {
            return Collections.singletonList(edge);
        }
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) Set(java.util.Set) TreeSet(java.util.TreeSet) IntHashMap(com.revolsys.collection.map.IntHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LineString(com.revolsys.geometry.model.LineString) PointDoubleXYZ(com.revolsys.geometry.model.impl.PointDoubleXYZ) TreeSet(java.util.TreeSet) CoordinatesDistanceComparator(com.revolsys.geometry.model.coordinates.comparator.CoordinatesDistanceComparator) Point(com.revolsys.geometry.model.Point) BPlusTreeMap(com.revolsys.collection.bplus.BPlusTreeMap) TreeMap(java.util.TreeMap) LineStringDouble(com.revolsys.geometry.model.impl.LineStringDouble) Point(com.revolsys.geometry.model.Point) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LineString(com.revolsys.geometry.model.LineString) LineStringDouble(com.revolsys.geometry.model.impl.LineStringDouble)

Aggregations

PointDoubleXYZ (com.revolsys.geometry.model.impl.PointDoubleXYZ)20 Point (com.revolsys.geometry.model.Point)17 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)4 Geometry (com.revolsys.geometry.model.Geometry)2 Record (com.revolsys.record.Record)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BPlusTreeMap (com.revolsys.collection.bplus.BPlusTreeMap)1 IntHashMap (com.revolsys.collection.map.IntHashMap)1 LengthIndexedLine (com.revolsys.geometry.linearref.LengthIndexedLine)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 LineString (com.revolsys.geometry.model.LineString)1 Triangle (com.revolsys.geometry.model.Triangle)1 CoordinatesDistanceComparator (com.revolsys.geometry.model.coordinates.comparator.CoordinatesDistanceComparator)1 LineStringDouble (com.revolsys.geometry.model.impl.LineStringDouble)1 PointDouble (com.revolsys.geometry.model.impl.PointDouble)1 LineSegment (com.revolsys.geometry.model.segment.LineSegment)1 LineSegmentDoubleGF (com.revolsys.geometry.model.segment.LineSegmentDoubleGF)1 List (java.util.List)1 Map (java.util.Map)1