Search in sources :

Example 11 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class WhereGeospatialServiceImpl method initialize.

@PostConstruct
@Refreshable(dependsOn = RefreshableResources.STOP_GEOSPATIAL_INDEX)
public void initialize() {
    List<StopEntry> stops = _transitGraphDao.getAllStops();
    if (stops.size() == 0) {
        _tree = null;
        return;
    }
    _tree = new STRtree(stops.size());
    for (StopEntry stop : stops) {
        float x = (float) stop.getStopLon();
        float y = (float) stop.getStopLat();
        Envelope env = new Envelope(x, x, y, y);
        _tree.insert(env, stop.getId());
    }
    _tree.build();
}
Also used : STRtree(org.locationtech.jts.index.strtree.STRtree) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) Envelope(org.locationtech.jts.geom.Envelope) Refreshable(org.onebusaway.container.refresh.Refreshable) PostConstruct(javax.annotation.PostConstruct)

Example 12 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class HierarchicalSTRtree method query.

@SuppressWarnings("unchecked")
public List<T> query(CoordinateBounds b) {
    List<T> results = new ArrayList<T>();
    Envelope env = new Envelope(b.getMinLon(), b.getMaxLon(), b.getMinLat(), b.getMaxLat());
    List<STRtree> subTrees = _parentTree.query(env);
    for (STRtree subTree : subTrees) {
        List<T> result = subTree.query(env);
        results.addAll(result);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) STRtree(org.locationtech.jts.index.strtree.STRtree) Envelope(org.locationtech.jts.geom.Envelope)

Example 13 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project onebusaway-application-modules by camsys.

the class HierarchicalSTRtreeFactory method create.

public HierarchicalSTRtree<T> create() {
    STRtree parentTree = new STRtree();
    for (Map.Entry<CoordinateBounds, STRtree> entry : _treesByBounds.entrySet()) {
        CoordinateBounds b = entry.getKey();
        Envelope env = new Envelope(b.getMinLon(), b.getMaxLon(), b.getMinLat(), b.getMaxLat());
        STRtree tree = entry.getValue();
        tree.build();
        parentTree.insert(env, tree);
    }
    parentTree.build();
    return new HierarchicalSTRtree<T>(parentTree);
}
Also used : STRtree(org.locationtech.jts.index.strtree.STRtree) Envelope(org.locationtech.jts.geom.Envelope) Map(java.util.Map) HashMap(java.util.HashMap) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 14 with STRtree

use of org.locationtech.jts.index.strtree.STRtree in project qupath by qupath.

the class DistanceTools method centroidToBoundsDistance2D.

/**
 * Calculate the distance between source object centroids and the boundary of specified target objects, adding the result to the measurement list of the source objects.
 * Calculations are all made in 2D; distances will not be calculated between objects occurring on different z-planes of at different timepoints.
 *
 * @param sourceObjects source objects; measurements will be added based on centroid distances
 * @param targetObjects target objects; no measurements will be added
 * @param pixelWidth pixel width to use in Geometry conversion (use 1 for pixel units)
 * @param pixelHeight pixel height to use in Geometry conversion (use 1 for pixel units)
 * @param measurementName the name of the measurement to add to the measurement list
 */
public static void centroidToBoundsDistance2D(Collection<PathObject> sourceObjects, Collection<PathObject> targetObjects, double pixelWidth, double pixelHeight, String measurementName) {
    boolean preferNucleus = true;
    var timePoints = new TreeSet<Integer>();
    var zSlices = new TreeSet<Integer>();
    for (var temp : sourceObjects) {
        timePoints.add(temp.getROI().getT());
        zSlices.add(temp.getROI().getZ());
    }
    var transform = pixelWidth == 1 && pixelHeight == 1 ? null : AffineTransformation.scaleInstance(pixelWidth, pixelHeight);
    for (int t : timePoints) {
        for (int z : zSlices) {
            PrecisionModel precision = null;
            List<Geometry> areaGeometries = new ArrayList<>();
            List<Geometry> lineGeometries = new ArrayList<>();
            List<Geometry> pointGeometries = new ArrayList<>();
            for (var annotation : targetObjects) {
                var roi = annotation.getROI();
                if (roi != null && roi.getZ() == z && roi.getT() == t) {
                    var geom = annotation.getROI().getGeometry();
                    if (transform != null) {
                        geom = transform.transform(geom);
                        if (precision == null)
                            precision = geom.getPrecisionModel();
                    }
                    // var geom = converter.roiToGeometry(annotation.getROI());
                    if (geom instanceof Puntal)
                        pointGeometries.add(geom);
                    else if (geom instanceof Lineal)
                        lineGeometries.add(geom);
                    else if (geom instanceof Polygonal)
                        areaGeometries.add(geom);
                    else {
                        for (int i = 0; i < geom.getNumGeometries(); i++) {
                            var geom2 = geom.getGeometryN(i);
                            if (geom2 instanceof Puntal)
                                pointGeometries.add(geom2);
                            else if (geom2 instanceof Lineal)
                                lineGeometries.add(geom2);
                            else if (geom2 instanceof Polygonal)
                                areaGeometries.add(geom2);
                            else
                                logger.warn("Unexpected nested Geometry collection, some Geometries may be ignored");
                        }
                    }
                }
            }
            if (areaGeometries.isEmpty() && pointGeometries.isEmpty() && lineGeometries.isEmpty())
                continue;
            var precisionModel = precision == null ? GeometryTools.getDefaultFactory().getPrecisionModel() : precision;
            List<Coordinate> pointCoords = new ArrayList<>();
            Geometry temp = null;
            if (!areaGeometries.isEmpty())
                temp = areaGeometries.size() == 1 ? areaGeometries.get(0) : GeometryCombiner.combine(areaGeometries);
            Geometry shapeGeometry = temp;
            temp = null;
            if (!lineGeometries.isEmpty())
                temp = lineGeometries.size() == 1 ? lineGeometries.get(0) : GeometryCombiner.combine(lineGeometries);
            Geometry lineGeometry = temp;
            // Identify points, and create an STRtree to find nearest neighbors more quickly if there are a lot of them
            if (!pointGeometries.isEmpty()) {
                for (var geom : pointGeometries) {
                    for (var coord : geom.getCoordinates()) {
                        precisionModel.makePrecise(coord);
                        pointCoords.add(coord);
                    }
                }
            }
            STRtree pointTree = pointCoords != null && pointCoords.size() > 1000 ? createCoordinateCache(pointCoords) : null;
            CoordinateDistance coordinateDistance = new CoordinateDistance();
            int zi = z;
            int ti = t;
            var locator = shapeGeometry == null ? null : new IndexedPointInAreaLocator(shapeGeometry);
            // See https://github.com/locationtech/jts/issues/571
            if (locator != null)
                locator.locate(new Coordinate(0, 0));
            sourceObjects.parallelStream().forEach(p -> {
                var roi = PathObjectTools.getROI(p, preferNucleus);
                if (roi.getZ() != zi || roi.getT() != ti)
                    return;
                Coordinate coord = new Coordinate(roi.getCentroidX() * pixelWidth, roi.getCentroidY() * pixelHeight);
                precisionModel.makePrecise(coord);
                double pointDistance = pointCoords == null ? Double.POSITIVE_INFINITY : computeCoordinateDistance(coord, pointCoords, pointTree, coordinateDistance);
                double lineDistance = lineGeometry == null ? Double.POSITIVE_INFINITY : computeDistance(coord, lineGeometry, null);
                double shapeDistance = shapeGeometry == null ? Double.POSITIVE_INFINITY : computeDistance(coord, shapeGeometry, locator);
                double distance = Math.min(lineDistance, Math.min(pointDistance, shapeDistance));
                try (var ml = p.getMeasurementList()) {
                    ml.putMeasurement(measurementName, distance);
                }
            });
        }
    }
}
Also used : Polygonal(org.locationtech.jts.geom.Polygonal) ArrayList(java.util.ArrayList) PrecisionModel(org.locationtech.jts.geom.PrecisionModel) DistanceToPoint(org.locationtech.jts.algorithm.distance.DistanceToPoint) Geometry(org.locationtech.jts.geom.Geometry) IndexedPointInAreaLocator(org.locationtech.jts.algorithm.locate.IndexedPointInAreaLocator) Lineal(org.locationtech.jts.geom.Lineal) Coordinate(org.locationtech.jts.geom.Coordinate) TreeSet(java.util.TreeSet) STRtree(org.locationtech.jts.index.strtree.STRtree) Puntal(org.locationtech.jts.geom.Puntal)

Aggregations

STRtree (org.locationtech.jts.index.strtree.STRtree)14 Envelope (org.locationtech.jts.geom.Envelope)13 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)3 PostConstruct (javax.annotation.PostConstruct)2 Geometry (org.locationtech.jts.geom.Geometry)2 Refreshable (org.onebusaway.container.refresh.Refreshable)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 OperationsPerInvocation (org.openjdk.jmh.annotations.OperationsPerInvocation)2 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)2 Edge (org.opentripplanner.routing.graph.Edge)2 Vertex (org.opentripplanner.routing.graph.Vertex)2 File (java.io.File)1 HashSet (java.util.HashSet)1 List (java.util.List)1