-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlamedaMap02.pde
253 lines (219 loc) · 6.84 KB
/
AlamedaMap02.pde
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
OSMMap theMap;
PrintWriter output;
StringList drawnWayIDs;
// Interactive Mode Flags
boolean clickableWays;
String inFocusWay;
// UI
color backgroundColor;
void setup() {
size(1200, 1400, P3D);
backgroundColor = 200;
background(backgroundColor);
frameRate(120);
theMap = new OSMMap(loadXML("tinymap.xml"));
//theMap = new OSMMap(loadXML("alameda.xml"));
drawnWayIDs = new StringList();
println(theMap.statsString());
// Interactive Mode Flags
clickableWays = true;
noSmooth();
drawMap();
}
void draw() {
camera(mouseX, height / 2, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 0);
drawMap();
}
void drawMap() {
/* Generating map01.png */ // Basic Display v2
//for ( Way way : theMap.ways.values() ) {
// drawWay(way, 255, 0);
//}
/* map02.png */ // Top Highway Tags v1
//drawTopTagValues("highway", 60);
//save("map02_topHighways.png");
/* map03.png */ // Top Building Tags V2
//drawTopTagValues("building", 7);
//save("map03_topBuildings.png");
/* map04.png */ // No Buildings v2
//for ( Way way : theMap.waysWithoutTagKey("building") ) {
// drawWay(way, 255, 0);
//}
//save("map04_noBuildings.png");
/* map05.png */ // No Highways v2
//for ( Way way : theMap.waysWithoutTagKey("highway") ) {
// drawWay(way, 255, 0);
//}
//save("map05_noHighways.png");
/* map06.png */ // No Buildings, Only Closed Ways v1
//for ( Way way : theMap.waysWithoutTagKey("building") ) {
// if ( way.closed ) drawWay(way, 255, 0);
//}
//save("map06_noBuildingsOnlyClosedWays.png");
/* map07.png */ // Natural Tags v.05
//drawTopTagValues("natural", 0);
/* map08.png */ // Coastline v0.5
//drawTag(theMap.tagWithKeyAndValue("natural", "coastline"));
/* map09.png */ // Draw nodes without ways
//for ( Node n : theMap.nodes.values() ) {
// if ( n.wayIDs.size() == 0 ) {
// drawNode(n, 0);
// }
//}
/* map10.png */ // Draw all nodes
//for ( Node n : theMap.nodes.values() ) {
// drawNode(n, 0);
//}
//save("map10_onlyNodes.png");
/* map11.png */ // Draw relations
//for ( Relation relation : theMap.relations.values() ) {
// drawRelation(relation, 0, 255);
//}
// drawTopTagValues("population", 0);
}
void mouseClicked() {
if ( clickableWays ) {
// Draw a rect over any old text
// Find the way clicked
boolean redraw = false;
for ( String wayID : drawnWayIDs ) {
Way way = theMap.ways.get(wayID);
java.awt.Polygon wayPoly = new java.awt.Polygon();
for ( String nodeID : way.nodeIDs ) {
Node node = theMap.nodes.get(nodeID);
Coordinates coords = theMap.remap(node);
wayPoly.addPoint(int(coords.lon), int(coords.lat));
}
if ( wayPoly.contains(mouseX, mouseY) ) {
inFocusWay = way.id;
redraw = true;
println(way);
}
}
if ( redraw ) {
background(backgroundColor);
drawMap();
displayInFocusWayInfo();
}
//mouseX, mouseY
}
}
//
// Drawing Functions
//
void displayInFocusWayInfo() {
if ( inFocusWay != null ) {
Way way = theMap.ways.get(inFocusWay);
// Figure out spacing
float rowHeight = 25;
float rowGap = 10;
float xPos = 20;
float yPos = height - ((rowHeight + rowGap) * way.tagIDs.size()) - (xPos - rowGap);
float countXPos = 300;
noStroke();
for ( String tagID : way.tagIDs ) {
fill(0);
Tag tag = theMap.tags.get(tagID);
textSize(14);
String textString = tag.keyString + " : " + tag.valueString;
text(textString, xPos, yPos + 16);
text(tag.wayIDs.size(), countXPos, yPos + 16);
yPos += rowHeight + rowGap;
}
}
}
void drawRelation(Relation relation, color fillColor, color strokeColor) {
for ( RelationMember m : relation.members ) {
if ( m.type.equals("way") ) {
Way w = theMap.ways.get(m.id);
if ( w != null ) drawWay(w, fillColor, strokeColor, 0);
}
}
}
void drawNode(Node node, color strokeColor) {
stroke(strokeColor);
Coordinates coords = theMap.remap(node);
point(coords.lon, coords.lat);
}
void drawWay(Way way, color fillColor, color strokeColor, float z) {
if (way.closed) {
fill(fillColor);
if ( clickableWays && inFocusWay != null ) {
if (way.id.equals(inFocusWay)) fill(#FF0000);
}
} else {
noFill();
}
stroke(strokeColor);
beginShape();
for ( String nodeID : way.nodeIDs ) {
Node node = theMap.nodes.get(nodeID);
Coordinates coords = theMap.remap(node);
vertex(coords.lon, coords.lat);
}
endShape();
drawnWayIDs.append(way.id);
}
void drawTopTagValues(String keyString, Integer minCount) {
StringList topValues = new StringList();
for ( Tag tag : theMap.tags.values() ) {
if ( tag.keyString.equals(keyString) && tag.wayIDs.size() > minCount ) topValues.append(tag.id);
if ( tag.keyString.equals(keyString) && tag.relationIDs.size() > minCount ) topValues.append(tag.id);
}
HashMap<String, Integer> topValueColors = zipIDsWithColors(topValues.array(), colorArray());
for ( Tag tag : theMap.tags.values() ) {
if ( tag.keyString.equals(keyString) && tag.wayIDs.size() > minCount ) {
for ( String wayID : tag.wayIDs ) {
Way way = theMap.ways.get(wayID);
drawWay(way, topValueColors.get(tag.id), topValueColors.get(tag.id), 0);
}
}
if ( tag.keyString.equals(keyString) && tag.relationIDs.size() > minCount ) {
for ( String relationID : tag.relationIDs ) {
Relation relation = theMap.relations.get(relationID);
drawRelation(relation, topValueColors.get(tag.id), topValueColors.get(tag.id));
}
}
}
drawTagLegend(topValueColors);
}
void drawTag(Tag tag) {
for ( String wayID : tag.wayIDs ) {
Way way = theMap.ways.get(wayID);
drawWay(way, 255, nextColor(), 0);
}
for ( String relationID : tag.relationIDs ) {
Relation relation = theMap.relations.get(relationID);
drawRelation(relation, 255, nextColor());
}
}
void drawTagLegend(HashMap<String, Integer> tagColors) {
// Figure out spacing
float rowHeight = 25;
float rowGap = 10;
float xPos = 20;
float yPos = height - ((rowHeight + rowGap) * tagColors.size()) - (xPos - rowGap);
float countXPos = 300;
noStroke();
for ( String tagID : tagColors.keySet() ) {
fill(tagColors.get(tagID));
rect(xPos, yPos, rowHeight, rowHeight);
fill(0);
Tag tag = theMap.tags.get(tagID);
textSize(14);
String textString = tag.keyString + " : " + tag.valueString;
text(textString, xPos + rowHeight + rowGap, yPos + 16);
text(tag.wayIDs.size(), countXPos, yPos + 16);
yPos += rowHeight + rowGap;
}
}
//
// Helper Functions
//
HashMap<String, Integer> zipIDsWithColors(String[] ids, color[] colors) {
HashMap<String, Integer> zipped = new HashMap<String, Integer>();
for ( int i = 0; i < ids.length; i++ ) {
zipped.put(ids[i], colors[i % colors.length]);
}
return zipped;
}