-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapMaker.java
More file actions
102 lines (85 loc) · 3.17 KB
/
MapMaker.java
File metadata and controls
102 lines (85 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package mapmaker;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import javax.json.*;
public class MapMaker {
float[] bounds;
HashMap<Integer, Location> nodes = new HashMap<Integer, Location>();
public MapMaker(float[] bounds) {
this.bounds = bounds;
}
public boolean parseData(String filename) {
DataFetcher fetcher = new DataFetcher(bounds);
JsonObject data = fetcher.getData();
JsonArray elements = data.getJsonArray("elements");
for (JsonObject elem : elements.getValuesAs(JsonObject.class)) {
if (elem.getString("type").equals("node")) {
nodes.put(elem.getInt("id"), new Location(elem.getJsonNumber("lat").doubleValue(), elem.getJsonNumber("lon").doubleValue()));
}
}
PrintWriter outfile;
try {
outfile = new PrintWriter(filename);
} catch (Exception e) {
e.printStackTrace();
return false;
}
for (JsonObject elem : elements.getValuesAs(JsonObject.class)) {
if (elem.getString("type").equals("way")) {
String street = elem.getJsonObject("tags").getString("name", "");
String type = elem.getJsonObject("tags").getString("highway", "");
String oneway = elem.getJsonObject("tags").getString("oneway", "no");
List<JsonNumber> nodelist = elem.getJsonArray("nodes").getValuesAs(JsonNumber.class);
for (int i = 0; i < nodelist.size() - 1; i++) {
Location start = nodes.get(nodelist.get(i).intValue());
Location end = nodes.get(nodelist.get(i + 1).intValue());
if (start.outsideBounds(bounds) || end.outsideBounds(bounds)) {
continue;
}
outfile.println("" + start + end + "\"" + street + "\" " + type);
if (oneway.equals("no")) {
outfile.println("" + end + start + "\"" + street + "\" " + type);
}
}
}
}
outfile.close();
return true;
}
public static void main(String[] args) {
if (args.length != 4) {
System.out.println("Incorrect number of arguments.");
System.out.println(args.length);
return;
}
float[] bound_arr = new float[4];
try {
for (int i = 0; i < args.length; i++) {
bound_arr[i] = Float.parseFloat(args[i]);
}
} catch (Exception e) {
e.printStackTrace();
return;
}
MapMaker map = new MapMaker(bound_arr);
map.parseData("ucsd.map");
}
}
class Location {
private double lat;
private double lon;
public Location(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
public String toString() {
return "" + lat + " " + lon + " ";
}
/**
* @param bounds [south, west, north, east]
*/
public boolean outsideBounds(float[] bounds) {
return (lat < bounds[0] || lat > bounds[2] || lon < bounds[1] || lon > bounds[3]);
}
}