Class GraphDB
- java.lang.Object
-
- GraphDB
-
public class GraphDB extends java.lang.Object
Graph for storing all of the intersection (vertex) and road (edge) information. Uses your GraphBuildingHandler to convert the XML files into a graph. Your code must include the vertices, adjacent, distance, closest, lat, and lon methods. You'll also need to include instance variables and methods for modifying the graph (e.g. addNode and addEdge).
-
-
Field Summary
Fields Modifier and Type Field Description private static double
K0
Scale factor at the natural origin, Berkeley.private static int
R
Radius of the Earth in miles.private static double
ROOT_LAT
Latitude centered on Berkeley.private static double
ROOT_LON
Longitude centered on Berkeley.
-
Constructor Summary
Constructors Constructor Description GraphDB(java.lang.String dbPath)
This constructor creates and starts an XML parser, cleans the nodes, and prepares the data structures for processing.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) java.lang.Iterable<java.lang.Long>
adjacent(long v)
Returns an iterable over the IDs of all vertices adjacent tov
.(package private) double
bearing(long v, long w)
Returns the initial bearing between verticesv
andw
in degrees.private void
clean()
Remove nodes with no connections from the graph.private static java.lang.String
cleanString(java.lang.String s)
Helper to process strings into their "cleaned" form, ignoring punctuation and capitalization.long
closest(double lon, double lat)
Returns the ID of the vertex closest to the given longitude and latitude.double
distance(long v, long w)
Returns the great-circle distance between two vertices, v and w, in miles.java.util.List<LocationParams>
getLocations(java.lang.String locationName)
Collect all locations that match a cleanedlocationName
, and return information about each node that matches.java.util.List<java.lang.String>
getLocationsByPrefix(java.lang.String prefix)
In linear time, collect all the names of OSM locations that prefix-match the query string.(package private) double
lat(long v)
Returns the latitude of vertexv
.(package private) double
lon(long v)
Returns the longitude of vertexv
.(package private) static double
projectToX(double lon, double lat)
Return the Euclidean x-value for some point, p, in Berkeley.(package private) static double
projectToY(double lon, double lat)
Return the Euclidean y-value for some point, p, in Berkeley.(package private) java.lang.Iterable<java.lang.Long>
vertices()
Returns an iterable of all vertex IDs in the graph.
-
-
-
Field Detail
-
R
private static final int R
Radius of the Earth in miles.- See Also:
- Constant Field Values
-
ROOT_LAT
private static final double ROOT_LAT
Latitude centered on Berkeley.- See Also:
- Constant Field Values
-
ROOT_LON
private static final double ROOT_LON
Longitude centered on Berkeley.- See Also:
- Constant Field Values
-
K0
private static final double K0
Scale factor at the natural origin, Berkeley. Prefer to use 1 instead of 0.9996 as in UTM.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GraphDB
public GraphDB(java.lang.String dbPath)
This constructor creates and starts an XML parser, cleans the nodes, and prepares the data structures for processing. Modify this constructor to initialize your data structures.- Parameters:
dbPath
- Path to the XML file to be parsed.
-
-
Method Detail
-
cleanString
private static java.lang.String cleanString(java.lang.String s)
Helper to process strings into their "cleaned" form, ignoring punctuation and capitalization.- Parameters:
s
- Input string.- Returns:
- Cleaned string.
-
clean
private void clean()
Remove nodes with no connections from the graph. While this does not guarantee that any two nodes in the remaining graph are connected, we can reasonably assume this since typically roads are connected.
-
lon
double lon(long v)
Returns the longitude of vertexv
.- Parameters:
v
- The ID of a vertex in the graph.- Returns:
- The longitude of that vertex, or 0.0 if the vertex is not in the graph.
-
lat
double lat(long v)
Returns the latitude of vertexv
.- Parameters:
v
- The ID of a vertex in the graph.- Returns:
- The latitude of that vertex, or 0.0 if the vertex is not in the graph.
-
vertices
java.lang.Iterable<java.lang.Long> vertices()
Returns an iterable of all vertex IDs in the graph.- Returns:
- An iterable of all vertex IDs in the graph.
-
adjacent
java.lang.Iterable<java.lang.Long> adjacent(long v)
Returns an iterable over the IDs of all vertices adjacent tov
.- Parameters:
v
- The ID for any vertex in the graph.- Returns:
- An iterable over the IDs of all vertices adjacent to
v
, or an empty iterable if the vertex is not in the graph.
-
distance
public double distance(long v, long w)
Returns the great-circle distance between two vertices, v and w, in miles. Assumes the lon/lat methods are implemented properly.- Parameters:
v
- The ID for the first vertex.w
- The ID for the second vertex.- Returns:
- The great-circle distance between vertices and w.
-
closest
public long closest(double lon, double lat)
Returns the ID of the vertex closest to the given longitude and latitude.- Parameters:
lon
- The given longitude.lat
- The given latitude.- Returns:
- The ID for the vertex closest to the
lon
andlat
.
-
projectToX
static double projectToX(double lon, double lat)
Return the Euclidean x-value for some point, p, in Berkeley. Found by computing the Transverse Mercator projection centered at Berkeley.- Parameters:
lon
- The longitude for p.lat
- The latitude for p.- Returns:
- The flattened, Euclidean x-value for p.
-
projectToY
static double projectToY(double lon, double lat)
Return the Euclidean y-value for some point, p, in Berkeley. Found by computing the Transverse Mercator projection centered at Berkeley.- Parameters:
lon
- The longitude for p.lat
- The latitude for p.- Returns:
- The flattened, Euclidean y-value for p.
-
getLocationsByPrefix
public java.util.List<java.lang.String> getLocationsByPrefix(java.lang.String prefix)
In linear time, collect all the names of OSM locations that prefix-match the query string.- Parameters:
prefix
- Prefix string to be searched for. Could be any case, with our without punctuation.- Returns:
- A
List
of the full names of locations whose cleaned name matches the cleanedprefix
.
-
getLocations
public java.util.List<LocationParams> getLocations(java.lang.String locationName)
Collect all locations that match a cleanedlocationName
, and return information about each node that matches.- Parameters:
locationName
- A full name of a location searched for.- Returns:
- A
List
ofLocationParams
whose cleaned name matches the cleanedlocationName
-
bearing
double bearing(long v, long w)
Returns the initial bearing between verticesv
andw
in degrees. The initial bearing is the angle that, if followed in a straight line along a great-circle arc from the starting point, would take you to the end point. Assumes the lon/lat methods are implemented properly.- Parameters:
v
- The ID for the first vertex.w
- The ID for the second vertex.- Returns:
- The bearing between
v
andw
in degrees.
-
-