Package com.vividsolutions.jts.index

Examples of com.vividsolutions.jts.index.SpatialIndex


            } else {
                geom = _geom;
            }

            SpatialIndex spatialIndex = sourceAccessor.two();
            @SuppressWarnings("unchecked")
            List<Pair<FeatureId,String>> fids = spatialIndex.query(geom.getEnvelopeInternal());
            _unrefinedMatches = new HashMap<String,FeatureId>();
            for (Pair<FeatureId, String> match : fids) {
                _unrefinedMatches.put(match.two(), match.one());
            }
        }
View Full Code Here


        final double DY = 0.1;
        final int N_OBJS = 1000;
        final int N_QUERIES = 1000;

        Random rand = new Random(42);
        SpatialIndex hashGrid = new HashGridSpatialIndex<>();
        SpatialIndex strTree = new STRtree();

        for (int i = 0; i < N_OBJS; i++) {
            Coordinate a = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
            Coordinate b = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
            DummyObject obj = new DummyObject();
            obj.envelope = new Envelope(a, b);
            hashGrid.insert(obj.envelope, obj);
            strTree.insert(obj.envelope, obj);
        }

        for (int i = 0; i < N_QUERIES; i++) {
            Coordinate a = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
            Coordinate b = new Coordinate(rand.nextDouble() * DX + X0, rand.nextDouble() * DY + Y0);
View Full Code Here

            return;
        }

        // load shapefile end (docs marker)

        final SpatialIndex index = new STRtree();
        FeatureCollection features = source.getFeatures();
        System.out.println("Slurping in features ...");
        features.accepts(new FeatureVisitor() {

            @Override
            public void visit(Feature feature) {
                SimpleFeature simpleFeature = (SimpleFeature) feature;
                Geometry geom = (MultiLineString) simpleFeature.getDefaultGeometry();
                // Just in case: check for  null or empty geometry
                if (geom != null) {
                    Envelope env = geom.getEnvelopeInternal();
                    if (!env.isNull()) {
                        index.insert(env, new LocationIndexedLine(geom));
                    }
                }
            }
        }, new NullProgressListener());

        // cache features end (docs marker)

        /*
         * For test data, we generate a large number of points placed randomly
         * within the bounding rectangle of the features.
         */
        final int NUM_POINTS = 10000;
        ReferencedEnvelope bounds = features.getBounds();
        Coordinate[] points = new Coordinate[NUM_POINTS];
        Random rand = new Random(file.hashCode());
        for (int i = 0; i < NUM_POINTS; i++) {
            points[i] = new Coordinate(
                    bounds.getMinX() + rand.nextDouble() * bounds.getWidth(),
                    bounds.getMinY() + rand.nextDouble() * bounds.getHeight());
        }

        // generate points end (docs marker)

        /*
         * We defined the maximum distance that a line can be from a point
         * to be a candidate for snapping (1% of the width of the feature
         * bounds for this example).
         */
        final double MAX_SEARCH_DISTANCE = bounds.getSpan(0) / 100.0;

        // Maximum time to spend running the snapping process (milliseconds)
        final long DURATION = 5000;

        int pointsProcessed = 0;
        int pointsSnapped = 0;
        long elapsedTime = 0;
        long startTime = System.currentTimeMillis();
        while (pointsProcessed < NUM_POINTS &&
                (elapsedTime = System.currentTimeMillis() - startTime) < DURATION) {

            // Get point and create search envelope
            Coordinate pt = points[pointsProcessed++];
            Envelope search = new Envelope(pt);
            search.expandBy(MAX_SEARCH_DISTANCE);

            /*
             * Query the spatial index for objects within the search envelope.
             * Note that this just compares the point envelope to the line envelopes
             * so it is possible that the point is actually more distant than
             * MAX_SEARCH_DISTANCE from a line.
             */
            List<LocationIndexedLine> lines = index.query(search);

            // Initialize the minimum distance found to our maximum acceptable
            // distance plus a little bit
            double minDist = MAX_SEARCH_DISTANCE + 1.0e-6;
            Coordinate minDistPoint = null;
View Full Code Here

                polygonizer.add(g);
            }

            ArrayList polys = (ArrayList) polygonizer.getPolygons();

            final SpatialIndex idx = new STRtree();
            FeatureIterator mt = areaMainCollection.features();
            try {
                while (mt.hasNext()) {
                    try {
                        SimpleFeature feat = (SimpleFeature) mt.next();
                        Geometry point = (Geometry) feat.getAttribute(geomName
                                + "_point");
                        idx.insert(point.getEnvelopeInternal(), feat);
                    } catch (NullPointerException e) {
                        // Gibts anscheinend in anderen Kantonen.
                        logger.warn("Empty point found.");
                    }
                }
            } finally {
                mt.close();
            }

            ArrayList<Polygon> holes = new ArrayList();

            for (int i = 0; i < polys.size(); i++) {
                boolean found = false;
                Polygon p = (Polygon) polys.get(i);
                PreparedPolygon ppoly = new PreparedPolygon(p);

                for (final Object o : idx.query(p.getEnvelopeInternal())) {
                    SimpleFeature f = (SimpleFeature) o;

                    // Funktioniert auch intersect? Schneller?
                    if (ppoly.contains(((Geometry) f.getAttribute(geomName
                            + "_point")))) {
                        f.setAttribute(geomName, p);
                        features.add(f);
                        found = true;
                        break;
                    }
                }

                if (!found) {

                    // Falls wirklich bewusste Löcher im Datensatz sind, führt
                    // das zu falschen
                    // Resultaten. Nur zu den "holes" hinzufügen, falls kleiner
                    // als ein bestimmter Wert.
                    if (p.getArea() < 1.0) {
                        holes.add(p);
                    }
                }
            }

            // Restflächen, die aufgrund des neu Verknotens entstanden sind,
            // werden dem Polygon mit der längsten gemeinsamen Kanten
            // zugewiesen.
            // Mal schauen obs schon hinhaut.
            boolean repair = true;
            if (repair) {
                final SpatialIndex spatialIndex = new STRtree();
                Iterator nt = features.iterator();

                while (nt.hasNext()) {
                    SimpleFeature feat = (SimpleFeature) nt.next();
                    Geometry point = (Geometry) feat.getAttribute(geomName);
                    spatialIndex.insert(point.getEnvelopeInternal(), feat);
                }

                logger.debug("Anzahl Löcher: ");
                logger.debug(holes.size());
                for (int i = 0; i < holes.size(); i++) {
                    Polygon p = holes.get(i);
                    PreparedPolygon ppoly = new PreparedPolygon(p);

                    double length = 0.0;
                    SimpleFeature feat = null;

                    for (final Object o : spatialIndex.query(p
                            .getEnvelopeInternal())) {
                        SimpleFeature f = (SimpleFeature) o;
                        Geometry g = ((Geometry) f.getAttribute(geomName));
                        if (ppoly.intersects(g)) {
                            // logger.debug("***************************");
 
View Full Code Here

TOP

Related Classes of com.vividsolutions.jts.index.SpatialIndex

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.