@Test public void testLattice() throws DaoException { GraphDistanceMetric metric = getLatticeMetric(); assertEquals(1.0, metric.distance(lattice[2][0], lattice[0][0]), 0.01); assertEquals(1.0, metric.distance(lattice[2][0], lattice[1][0]), 0.01); assertEquals(0.0, metric.distance(lattice[2][0], lattice[2][0]), 0.01); assertEquals(1.0, metric.distance(lattice[2][0], lattice[3][0]), 0.01); assertEquals(1.0, metric.distance(lattice[2][0], lattice[4][0]), 0.01); for (int i = 0; i < LATTICE_ROWS; i++) { for (int j = 0; j < 20; j++) { int d = (int) Math.round(metric.distance(lattice[2][0], lattice[i][j])); System.err.print(" " + d); } System.err.println(""); } }
@Override public double distance(Geometry g1, Geometry g2) { if (adjacencyList.isEmpty()) { throw new UnsupportedOperationException(); } List<ClosestPointIndex.Result> closest = index.query(g2, 1); int maxSteps = maxDistance; if (maxSteps == 0 || closest.isEmpty()) { return Double.POSITIVE_INFINITY; } if (g1 == g2 || g1.equals(g2)) { return 0; } int targetId = closest.get(0).id; TIntSet seen = new TIntHashSet(); TIntLinkedList queue = new TIntLinkedList(); for (ClosestPointIndex.Result n : index.query(g1, numNeighbors)) { if (n.id== targetId) { return 1; } queue.add(n.id); seen.add(n.id); } for (int level = 2; level <= maxSteps; level++) { int nodes = queue.size(); for (int i = 0; i < nodes; i++) { int id = queue.removeAt(0); if (!adjacencyList.containsKey(id)) { continue; } for (int id2 : adjacencyList.get(id).toArray()) { if (id2 == targetId) { return level; } if (!seen.contains(id2)) { queue.add(id2); seen.add(id2); } } } } return Double.POSITIVE_INFINITY; }
GraphDistanceMetric implements SpatialDistanceMetric { @Override public double distance(Geometry g1, Geometry g2) { if (adjacencyList.isEmpty()) { throw new UnsupportedOperationException(); } List<ClosestPointIndex.Result> closest = index.query(g2, 1); int maxSteps = maxDistance; if (maxSteps == 0 || closest.isEmpty()) { return Double.POSITIVE_INFINITY; } if (g1 == g2 || g1.equals(g2)) { return 0; } int targetId = closest.get(0).id; TIntSet seen = new TIntHashSet(); TIntLinkedList queue = new TIntLinkedList(); for (ClosestPointIndex.Result n : index.query(g1, numNeighbors)) { if (n.id== targetId) { return 1; } queue.add(n.id); seen.add(n.id); } for (int level = 2; level <= maxSteps; level++) { int nodes = queue.size(); for (int i = 0; i < nodes; i++) { int id = queue.removeAt(0); if (!adjacencyList.containsKey(id)) { continue; } for (int id2 : adjacencyList.get(id).toArray()) { if (id2 == targetId) { return level; } if (!seen.contains(id2)) { queue.add(id2); seen.add(id2); } } } } return Double.POSITIVE_INFINITY; } }
GraphDistanceMetric implements SpatialDistanceMetric { @Override public double distance(Geometry g1, Geometry g2) { if (adjacencyList.isEmpty()) { throw new UnsupportedOperationException(); } List<ClosestPointIndex.Result> closest = index.query(g2, 1); int maxSteps = maxDistance; if (maxSteps == 0 || closest.isEmpty()) { return Double.POSITIVE_INFINITY; } if (g1 == g2 || g1.equals(g2)) { return 0; } int targetId = closest.get(0).id; TIntSet seen = new TIntHashSet(); TIntLinkedList queue = new TIntLinkedList(); for (ClosestPointIndex.Result n : index.query(g1, numNeighbors)) { if (n.id== targetId) { return 1; } queue.add(n.id); seen.add(n.id); } for (int level = 2; level <= maxSteps; level++) { int nodes = queue.size(); for (int i = 0; i < nodes; i++) { int id = queue.removeAt(0); if (!adjacencyList.containsKey(id)) { continue; } for (int id2 : adjacencyList.get(id).toArray()) { if (id2 == targetId) { return level; } if (!seen.contains(id2)) { queue.add(id2); seen.add(id2); } } } } return Double.POSITIVE_INFINITY; } GraphDistanceMetric(SpatialDataDao dao, ClosestPointIndex index);  GraphDistanceMetric(SpatialDataDao dao, SphericalDistanceMetric spherical);  GraphDistanceMetric(SpatialDataDao dao); }
GraphDistanceMetric implements SpatialDistanceMetric { @Override public double distance(Geometry g1, Geometry g2) { if (adjacencyList.isEmpty()) { throw new UnsupportedOperationException(); } List<ClosestPointIndex.Result> closest = index.query(g2, 1); int maxSteps = maxDistance; if (maxSteps == 0 || closest.isEmpty()) { return Double.POSITIVE_INFINITY; } if (g1 == g2 || g1.equals(g2)) { return 0; } int targetId = closest.get(0).id; TIntSet seen = new TIntHashSet(); TIntLinkedList queue = new TIntLinkedList(); for (ClosestPointIndex.Result n : index.query(g1, numNeighbors)) { if (n.id== targetId) { return 1; } queue.add(n.id); seen.add(n.id); } for (int level = 2; level <= maxSteps; level++) { int nodes = queue.size(); for (int i = 0; i < nodes; i++) { int id = queue.removeAt(0); if (!adjacencyList.containsKey(id)) { continue; } for (int id2 : adjacencyList.get(id).toArray()) { if (id2 == targetId) { return level; } if (!seen.contains(id2)) { queue.add(id2); seen.add(id2); } } } } return Double.POSITIVE_INFINITY; } GraphDistanceMetric(SpatialDataDao dao, ClosestPointIndex index);  GraphDistanceMetric(SpatialDataDao dao, SphericalDistanceMetric spherical);  GraphDistanceMetric(SpatialDataDao dao); void setNumNeighbors(int numNeighbors); void setMaxDistance(int maxDistance); void setValidNodes(TIntSet nodes); @Override void setValidConcepts(TIntSet concepts); @Override void enableCache(boolean enable); @Override String getName(); @Override double distance(Geometry g1, Geometry g2); @Override float[][] distance(List<Geometry> rowGeometries, List<Geometry> colGeometries); @Override float[][] distance(List<Geometry> geometries); @Override List<Neighbor> getNeighbors(Geometry g, int maxNeighbors); @Override List<Neighbor> getNeighbors(Geometry g, int maxNeighbors, double maxDistance); void setDirected(boolean directed); }
GraphDistanceMetric implements SpatialDistanceMetric { @Override public double distance(Geometry g1, Geometry g2) { if (adjacencyList.isEmpty()) { throw new UnsupportedOperationException(); } List<ClosestPointIndex.Result> closest = index.query(g2, 1); int maxSteps = maxDistance; if (maxSteps == 0 || closest.isEmpty()) { return Double.POSITIVE_INFINITY; } if (g1 == g2 || g1.equals(g2)) { return 0; } int targetId = closest.get(0).id; TIntSet seen = new TIntHashSet(); TIntLinkedList queue = new TIntLinkedList(); for (ClosestPointIndex.Result n : index.query(g1, numNeighbors)) { if (n.id== targetId) { return 1; } queue.add(n.id); seen.add(n.id); } for (int level = 2; level <= maxSteps; level++) { int nodes = queue.size(); for (int i = 0; i < nodes; i++) { int id = queue.removeAt(0); if (!adjacencyList.containsKey(id)) { continue; } for (int id2 : adjacencyList.get(id).toArray()) { if (id2 == targetId) { return level; } if (!seen.contains(id2)) { queue.add(id2); seen.add(id2); } } } } return Double.POSITIVE_INFINITY; } GraphDistanceMetric(SpatialDataDao dao, ClosestPointIndex index);  GraphDistanceMetric(SpatialDataDao dao, SphericalDistanceMetric spherical);  GraphDistanceMetric(SpatialDataDao dao); void setNumNeighbors(int numNeighbors); void setMaxDistance(int maxDistance); void setValidNodes(TIntSet nodes); @Override void setValidConcepts(TIntSet concepts); @Override void enableCache(boolean enable); @Override String getName(); @Override double distance(Geometry g1, Geometry g2); @Override float[][] distance(List<Geometry> rowGeometries, List<Geometry> colGeometries); @Override float[][] distance(List<Geometry> geometries); @Override List<Neighbor> getNeighbors(Geometry g, int maxNeighbors); @Override List<Neighbor> getNeighbors(Geometry g, int maxNeighbors, double maxDistance); void setDirected(boolean directed);  }