com.vividsolutions.jts.index.strtree.STRtree Java Examples

The following examples show how to use com.vividsolutions.jts.index.strtree.STRtree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MinimumClearance.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void compute()
{
  // already computed
  if (minClearancePts != null) return;
  
  // initialize to "No Distance Exists" state
  minClearancePts = new Coordinate[2];
  minClearance = Double.MAX_VALUE;
  
  // handle empty geometries
  if (inputGeom.isEmpty()) {
    return;
  }
  
  STRtree geomTree = FacetSequenceTreeBuilder.build(inputGeom);
  
  Object[] nearest = geomTree.nearestNeighbour(new MinClearanceDistance());
  MinClearanceDistance mcd = new MinClearanceDistance();
  minClearance = mcd.distance(
      (FacetSequence) nearest[0],
      (FacetSequence) nearest[1]);
  minClearancePts = mcd.getCoordinates();
}
 
Example #2
Source File: GeoFencingAPI.java    From google-geoEngine with Apache License 2.0 5 votes vote down vote up
/**
 * Endpoint for finding the fences a certain point is in.
 */@ApiMethod(name = "point", httpMethod = "get", path = "point")
public ArrayList < MyFence > queryPoint(@Named("group") String group, @Named("lng") double lng, @Named("lat") double lat) {
    ArrayList < MyFence > fences = new ArrayList < MyFence > ();

    //Get the Index from Memcache.
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    GeometryFactory gf = new GeometryFactory();
    STRtree index = (STRtree) syncCache.get(group); // read from cache
    if (index != null) {
        Coordinate coord = new Coordinate(lng, lat);
        Point point = gf.createPoint(coord);
        List < MyPolygon > items = index.query(point.getEnvelopeInternal());
        if (!items.isEmpty()) {
            for (MyPolygon poly: items) {
                if (poly.contains(point)) {
                    long id = poly.getID();
                    MyFence newFence = new MyFence();
                    newFence.setId(id);
                    fences.add(newFence);
                }
            }
        }
    } else {
        try {
            MyIndex.buildIndex(group);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return fences;
}
 
Example #3
Source File: STRtreeTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testRemove() {
  STRtree tree = new STRtree();
  tree.insert(new Envelope(0, 10, 0, 10), "1");
  tree.insert(new Envelope(5, 15, 5, 15), "2");
  tree.insert(new Envelope(10, 20, 10, 20), "3");
  tree.insert(new Envelope(15, 25, 15, 25), "4");
  tree.remove(new Envelope(10, 20, 10, 20), "4");
  assertEquals(3, tree.size());
}
 
Example #4
Source File: SpatialIndexFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry strTreeBounds(Geometry geoms)
{
  STRtree index = buildSTRtree(geoms);
  List bounds = new ArrayList();
  addBounds(index.getRoot(), bounds, geoms.getFactory());
  return geoms.getFactory().buildGeometry(bounds);
}
 
Example #5
Source File: SpatialIndexFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry strTreeQuery(Geometry geoms, Geometry queryEnv)
{
  STRtree index = buildSTRtree(geoms);
  // if no query env provided query everything inserted 
  if (queryEnv == null) queryEnv = geoms;
  List result = index.query(queryEnv.getEnvelopeInternal());
  return geoms.getFactory().buildGeometry(result);
}
 
Example #6
Source File: CascadedPolygonUnion.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
	 * Computes the union of the input geometries.
	 * <p>
	 * This method discards the input geometries as they are processed.
	 * In many input cases this reduces the memory retained
	 * as the operation proceeds. 
	 * Optimal memory usage is achieved 
	 * by disposing of the original input collection 
	 * before calling this method.
	 * 
	 * @return the union of the input geometries
	 * or null if no input geometries were provided
	 * @throws IllegalStateException if this method is called more than once
	 */
	public Geometry union()
	{
	  if (inputPolys == null)
	    throw new IllegalStateException("union() method cannot be called twice");
		if (inputPolys.isEmpty())
			return null;
		geomFactory = ((Geometry) inputPolys.iterator().next()).getFactory();
		
		/**
		 * A spatial index to organize the collection
		 * into groups of close geometries.
		 * This makes unioning more efficient, since vertices are more likely 
		 * to be eliminated on each round.
		 */
//    STRtree index = new STRtree();
    STRtree index = new STRtree(STRTREE_NODE_CAPACITY);
    for (Iterator i = inputPolys.iterator(); i.hasNext(); ) {
      Geometry item = (Geometry) i.next();
      index.insert(item.getEnvelopeInternal(), item);
    }
    // To avoiding holding memory remove references to the input geometries,
    inputPolys = null;
    
    List itemTree = index.itemsTree();
//    printItemEnvelopes(itemTree);
    Geometry unionAll = unionTree(itemTree);
    return unionAll;
	}
 
Example #7
Source File: IndexedNestedRingTester.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void buildIndex()
{
  index = new STRtree();

  for (int i = 0; i < rings.size(); i++) {
    LinearRing ring = (LinearRing) rings.get(i);
    Envelope env = ring.getEnvelopeInternal();
    index.insert(env, ring);
  }
}
 
Example #8
Source File: FacetSequenceTreeBuilder.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static STRtree build(Geometry g) {
  STRtree tree = new STRtree(STR_TREE_NODE_CAPACITY);
  List sections = computeFacetSequences(g);
  for (Iterator i = sections.iterator(); i.hasNext();) {
    FacetSequence section = (FacetSequence) i.next();
    tree.insert(section.getEnvelope(), section);
  }
  tree.build();
  return tree;
}
 
Example #9
Source File: STRtreeTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testDisallowedInserts() {
  STRtree t = new STRtree(5);
  t.insert(new Envelope(0, 0, 0, 0), new Object());
  t.insert(new Envelope(0, 0, 0, 0), new Object());
  t.query(new Envelope());
  try {
    t.insert(new Envelope(0, 0, 0, 0), new Object());
    assertTrue(false);
  }
  catch (AssertionFailedException e) {
    assertTrue(true);
  }
}
 
Example #10
Source File: STRtreeTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSpatialIndex()
throws Exception
{
  SpatialIndexTester tester = new SpatialIndexTester();
  tester.setSpatialIndex(new STRtree(4));
  tester.init();
  tester.run();
  assertTrue(tester.isSuccess());
}
 
Example #11
Source File: STRtreeTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testEmptyTreeUsingItemVisitorQuery()  
{
  STRtree tree = new STRtree();
  tree.query(new Envelope(0,0,1,1), new ItemVisitor() {
    public void visitItem(Object item) {
      assertTrue("Should never reach here", true);
    }
  });  
}
 
Example #12
Source File: GeoFencingAPI.java    From google-geoEngine with Apache License 2.0 4 votes vote down vote up
/**
 * Endpoint for finding the fences that intersect with a polyline.
 */@ApiMethod(name = "polyline", httpMethod = "post", path = "polyline")
public ArrayList < MyFence > queryPolyLine(@Named("group") String group, MyPolyLine polyline) {
    ArrayList < MyFence > fences = new ArrayList < MyFence > ();

    //Get the index from Memcache.
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    GeometryFactory gf = new GeometryFactory();
    STRtree index = (STRtree) syncCache.get(group); // read from cache
    if (index != null) {
        //Create coordinate array.
        double[][] points = polyline.getCoordinates();
        Coordinate[] coordinates = new Coordinate[points.length];
        int i = 0;
        for (double[] point: points) {
            Coordinate coordinate = new Coordinate(point[0], point[1]);
            coordinates[i++] = coordinate;
        }
        //Create polyline.
        GeometryFactory fact = new GeometryFactory();
        LineString linestring = new GeometryFactory().createLineString(coordinates);

        List < MyPolygon > items = index.query(linestring.getEnvelopeInternal());
        if (!items.isEmpty()) {
            for (MyPolygon poly: items) {
                if (linestring.crosses(poly) || poly.contains(linestring)) {
                    long id = poly.getID();
                    MyFence newFence = new MyFence();
                    newFence.setId(id);
                    fences.add(newFence);
                }
            }
        }
    } else {
        try {
            MyIndex.buildIndex(group);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return fences;
}
 
Example #13
Source File: TreeTimeTest.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public STRtreeIndex(int nodeCapacity)
{
  index = new STRtree(nodeCapacity);
}
 
Example #14
Source File: STRtreeTest.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testEmptyTreeUsingListQuery()  
{
  STRtree tree = new STRtree();
  List list = tree.query(new Envelope(0, 0, 1, 1));
  assertTrue(list.isEmpty());
}
 
Example #15
Source File: MCIndexPointSnapper.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public MCIndexPointSnapper(SpatialIndex index) {
  this.index = (STRtree) index;
}
 
Example #16
Source File: MyIndex.java    From google-geoEngine with Apache License 2.0 4 votes vote down vote up
public static int buildIndex(String group) throws IOException{
    //Get all fences of group from DataStore.
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key fenceKey = KeyFactory.createKey("Geofence", group);

    Query query = new Query("Fence", fenceKey).addSort("id", Query.SortDirection.DESCENDING);
    List<Entity> fencesFromStore = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());

    if (!fencesFromStore.isEmpty()) {
    	//Create STRTree-Index.
        STRtree index = new STRtree();
        //Loop through the fences from DataStore.
        for (Entity fenceFromStore : fencesFromStore) {
            long id = (long) fenceFromStore.getProperty("id");
            Gson gson = new Gson();
            Text vText = (Text) fenceFromStore.getProperty("vertices");
            String vString = vText.getValue();
            double[][] vertices = gson.fromJson(vString, double[][].class);

            //Store coordinates in an array.
            Coordinate[] coordinates = new Coordinate[vertices.length];
            int i = 0;
            for(double[] point : vertices){
                Coordinate coordinate = new Coordinate(point[0],point[1]);
                coordinates[i++] = coordinate;
            }
            //Create polygon from the coordinates.
            GeometryFactory fact = new GeometryFactory();
            LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
            MyPolygon polygon = new MyPolygon(linear, null, fact, id);
            //Add polygon to index.
            index.insert(polygon.getEnvelopeInternal(), polygon);
        }
        //Build the index.
        index.build();
        //Write the index to Memcache.
        MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
        //Last param is expiration date. Set to null to keep it in Memcache forever.
        syncCache.put(group, index, null); 
    }
    return fencesFromStore.size();
}
 
Example #17
Source File: GeoFencingAPI.java    From google-geoEngine with Apache License 2.0 4 votes vote down vote up
/**
 * Endpoint for finding the fences that intersect with a polygon.
 */@ApiMethod(name = "polygon", httpMethod = "post", path = "polygon")
public ArrayList < MyFence > queryPolygon(@Named("group") String group, MyPolyLine polyline) {
    ArrayList < MyFence > fences = new ArrayList < MyFence > ();

    //Get index from Memcache
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    STRtree index = (STRtree) syncCache.get(group); // read from cache
    if (index != null) {
        //Create coordinate array.
        double[][] points = polyline.getCoordinates();
        Coordinate[] coordinates = new Coordinate[points.length];
        int i = 0;
        for (double[] point: points) {
            Coordinate coordinate = new Coordinate(point[0], point[1]);
            coordinates[i++] = coordinate;
        }
        //Create polygon.
        GeometryFactory fact = new GeometryFactory();
        LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
        Polygon polygon = new Polygon(linear, null, fact);

        List < MyPolygon > items = index.query(polygon.getEnvelopeInternal());
        if (!items.isEmpty()) {
            for (MyPolygon poly: items) {
                if (polygon.contains(poly) || !polygon.disjoint(poly)) {
                    long id = poly.getID();
                    MyFence newFence = new MyFence();
                    newFence.setId(id);
                    fences.add(newFence);
                }
            }
        }
    } else {
        try {
            MyIndex.buildIndex(group);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return fences;
}
 
Example #18
Source File: IndexedFacetDistance.java    From jts with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Computes the distance from the base geometry to 
 * the given geometry.
 *  
 * @param g the geometry to compute the distance to
 * 
 * @return the computed distance
 */
public double getDistance(Geometry g)
{
  STRtree tree2 = FacetSequenceTreeBuilder.build(g);
  Object[] obj = cachedTree.nearestNeighbour(tree2, 
      new FacetSequenceDistance());
  return facetDistance(obj);
}