/** * Return the ListEdge in the road network which is closest to the given coordinate, within the * given resolution * * @param c * @param resolution * @return */ public ListEdge getClosestEdge(Coordinate c, double resolution) { // find the set of all edges within *resolution* of the given point Bag objects = networkEdgeLayer.getObjectsWithinDistance(fa.createPoint(c), resolution); if (objects == null || networkEdgeLayer.getGeometries().size() <= 0) return null; // problem with the network edge layer Point point = fa.createPoint(c); // find the closest edge among the set of edges double bestDist = resolution; ListEdge bestEdge = null; for (Object o : objects) { double dist = ((MasonGeometry) o).getGeometry().distance(point); if (dist < bestDist) { bestDist = dist; bestEdge = (ListEdge) ((AttributeValue) ((MasonGeometry) o).getAttribute("ListEdge")).getValue(); } } // if it exists, return it if (bestEdge != null) return bestEdge; // otherwise return failure else return null; }
/** * Return the GeoNode in the road network which is closest to the given coordinate * * @param c * @return */ public GeoNode getClosestGeoNode(Coordinate c) { // find the set of all nodes within *resolution* of the given point Bag objects = networkLayer.getObjectsWithinDistance(fa.createPoint(c), resolution); if (objects == null || networkLayer.getGeometries().size() <= 0) return null; // problem with the network layer // among these options, pick the best double bestDist = resolution; // MUST be within resolution to count GeoNode best = null; for (Object o : objects) { double dist = ((GeoNode) o).geometry.getCoordinate().distance(c); if (dist < bestDist) { bestDist = dist; best = ((GeoNode) o); } } // if there is a best option, return that! if (best != null && bestDist == 0) return best; // otherwise, closest GeoNode is associated with the closest Edge, so look for that! ListEdge edge = getClosestEdge(c); // find that edge if (edge == null) { edge = getClosestEdge(c, resolution * 10); if (edge == null) return null; } // of that edge's endpoints, find the closer of the two and return it GeoNode n1 = (GeoNode) edge.getFrom(); GeoNode n2 = (GeoNode) edge.getTo(); if (n1.geometry.getCoordinate().distance(c) <= n2.geometry.getCoordinate().distance(c)) return n1; else return n2; }