Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 279 lines (243 sloc) 9.951 kB
3675745 @camillol added MapView
authored
1 import processing.opengl.*;
2 import com.modestmaps.*;
3 import com.modestmaps.core.*;
4 import com.modestmaps.geo.*;
5 import com.modestmaps.providers.*;
6
7 class MapView extends View {
8 InteractiveMap mmap;
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
9 float zoomValue = 4;
10 float minZoom = 4;
11 float maxZoom = 12;
60a76c2 @pjimen5 InformationalBox
pjimen5 authored
12 int minPointSize= 5;
13 int maxPointSize = 45;
f051126 @pjimen5 DrawingSightings+SizeIconsAccordingZoom
pjimen5 authored
14 int minIconSize= 8;
15 int maxIconSize = 25;
16 int minDistSize = 1;
17 int maxDistSize = 100;
e9fc876 @pjimen5 AddedDetailViewForSightings
pjimen5 authored
18 Sighting clickedSighting;
60a76c2 @pjimen5 InformationalBox
pjimen5 authored
19 Place clickedPlace;
3675745 @camillol added MapView
authored
20
d4325f8 @camillol tiled map overlay
authored
21 int MAX_BUFFERS_TO_KEEP = 64;
22
afeb1eb @camillol experimental buffer for map overlay
authored
23 PImage tempIcon;
d4325f8 @camillol tiled map overlay
authored
24
25 Map<Coordinate, PGraphics> buffers;
ac40bd5 @camillol optional tiled buffering
authored
26 boolean USE_BUFFERS = true;
afeb1eb @camillol experimental buffer for map overlay
authored
27
3675745 @camillol added MapView
authored
28 MapView(float x_, float y_, float w_, float h_)
29 {
30 super(x_, y_, w_, h_);
31 mmap = new InteractiveMap(papplet, new Microsoft.HybridProvider(), w, h);
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
32 // mmap = new InteractiveMap(papplet, new Microsoft.AerialProvider());
33 /* String template = "http://{S}.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.png";
34 String[] subdomains = new String[] { "otile1", "otile2", "otile3", "otile4"}; // optional
35 mmap = new InteractiveMap(papplet, new TemplatedMapProvider(template, subdomains));*/
36
d4325f8 @camillol tiled map overlay
authored
37 mmap.MAX_IMAGES_TO_KEEP = 64;
afeb1eb @camillol experimental buffer for map overlay
authored
38 mmap.setCenterZoom(new Location(39,-98), int(zoomValue));
39 tempIcon = loadImage("yellow.png");
40
d4325f8 @camillol tiled map overlay
authored
41 buffers = new LinkedHashMap<Coordinate, PGraphics>(MAX_BUFFERS_TO_KEEP, 0.75, true) {
42 protected boolean removeEldestEntry(Map.Entry eldest) {
43 return size() > MAX_BUFFERS_TO_KEEP;
44 }
45 };
46 }
47
48 void drawOverlay()
49 {
50 double sc = mmap.sc;
51 double tx = mmap.tx;
52 double ty = mmap.ty;
53
54 // translate and scale, from the middle
55 pushMatrix();
56 translate(width/2, height/2);
57 scale((float)sc);
58 translate((float)tx, (float)ty);
59
60 // find the bounds of the ur-tile in screen-space:
61 float minX = screenX(0,0);
62 float minY = screenY(0,0);
63 float maxX = screenX(mmap.TILE_WIDTH, mmap.TILE_HEIGHT);
64 float maxY = screenY(mmap.TILE_WIDTH, mmap.TILE_HEIGHT);
65
66 int zoom = mmap.getZoom();
67 int cols = (int)pow(2,zoom);
68 int rows = (int)pow(2,zoom);
69
70 // find start and end columns
71 // println("minX " + minX + " maxX " + maxX + " cols " + cols);
72 int minCol = (int)floor(cols * (0-minX) / (maxX-minX));
3dcf3e8 @camillol overlay with chicago location
authored
73 int maxCol = (int)ceil(cols * (w-minX) / (maxX-minX)) - 1;
d4325f8 @camillol tiled map overlay
authored
74 int minRow = (int)floor(rows * (0-minY) / (maxY-minY));
3dcf3e8 @camillol overlay with chicago location
authored
75 int maxRow = (int)ceil(rows * (h-minY) / (maxY-minY)) - 1;
d4325f8 @camillol tiled map overlay
authored
76
77 minCol = constrain(minCol, 0, cols);
78 maxCol = constrain(maxCol, 0, cols);
79 minRow = constrain(minRow, 0, rows);
80 maxRow = constrain(maxRow, 0, rows);
81
82 scale(1.0f/pow(2, zoom));
83 int count = 0;
84 for (int col = minCol; col <= maxCol; col++) {
85 for (int row = minRow; row <= maxRow; row++) {
86 // source coordinate wraps around the world:
87 Coordinate coord = mmap.provider.sourceCoordinate(new Coordinate(row,col,zoom));
88
89 // let's make sure we still have ints:
90 coord.row = round(coord.row);
91 coord.column = round(coord.column);
92 coord.zoom = round(coord.zoom);
93
94 if (!buffers.containsKey(coord))
95 buffers.put(coord, makeOverlayBuffer(coord));
96
97 image(buffers.get(coord), coord.column*mmap.TILE_WIDTH, coord.row*mmap.TILE_HEIGHT, mmap.TILE_WIDTH, mmap.TILE_HEIGHT);
98 count++;
99 }
100 }
101 popMatrix();
102 // println("images: " + count + " col " + minCol + " " + maxCol + " rows " + minRow + " " + maxRow);
103 }
104
ac40bd5 @camillol optional tiled buffering
authored
105 void applyMapToTileMatrix(PGraphics buffer, Coordinate tileCoord) {
106 buffer.translate(-tileCoord.column * mmap.TILE_WIDTH, -tileCoord.row * mmap.TILE_HEIGHT);
107 buffer.scale(pow(2, tileCoord.zoom));
108 buffer.translate(-(float)mmap.tx, -(float)mmap.ty);
109 buffer.scale(1.0/(float)mmap.sc);
110 buffer.translate(-mmap.width/2, -mmap.height/2);
111 }
112
d4325f8 @camillol tiled map overlay
authored
113 PGraphics makeOverlayBuffer(Coordinate coord) {
ac40bd5 @camillol optional tiled buffering
authored
114 // println("makebuf: " + coord);
d4325f8 @camillol tiled map overlay
authored
115 PGraphics buf = createGraphics(mmap.TILE_WIDTH, mmap.TILE_HEIGHT, JAVA2D);
116 buf.beginDraw();
ac40bd5 @camillol optional tiled buffering
authored
117 /* if ((coord.row + coord.column) % 2 == 0) {
d4325f8 @camillol tiled map overlay
authored
118 buf.background(255,0,0,128);
119 }
ac40bd5 @camillol optional tiled buffering
authored
120 buf.text(coord.toString(), 50, 50);*/
3dcf3e8 @camillol overlay with chicago location
authored
121
122 // we want to be compatible with drawing code that calls mmap.locationPoint
ac40bd5 @camillol optional tiled buffering
authored
123 applyMapToTileMatrix(buf, coord);
124 drawPlaces(buf);
3dcf3e8 @camillol overlay with chicago location
authored
125
d4325f8 @camillol tiled map overlay
authored
126 buf.endDraw();
127 return buf;
3675745 @camillol added MapView
authored
128 }
129
130 void drawContent()
131 {
ff69b0a @camillol added mouse wheel support to map
authored
132 imageMode(CORNER); // modestmaps needs this - I sent a patch, but who knows when it'll be committed
3675745 @camillol added MapView
authored
133 mmap.draw();
9874feb @camillol drag map, fix map positioning
authored
134
ac40bd5 @camillol optional tiled buffering
authored
135 if (USE_BUFFERS) drawOverlay();
136 else drawPlaces(papplet.g);
137
60a76c2 @pjimen5 InformationalBox
pjimen5 authored
138 drawPlacesInformationBox();
139 // drawSightings();
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
140 if (showAirports)
141 drawAirports();
60a76c2 @pjimen5 InformationalBox
pjimen5 authored
142 // drawSightingsInformationBox();
9874feb @camillol drag map, fix map positioning
authored
143 }
144
ff69b0a @camillol added mouse wheel support to map
authored
145 boolean contentMouseWheel(float lx, float ly, int delta)
146 {
147 float sc = 1.0;
148 if (delta < 0) {
149 sc = 1.05;
150 }
151 else if (delta > 0) {
152 sc = 1.0/1.05;
153 }
154 float mx = lx - w/2;
155 float my = ly - h/2;
156 mmap.tx -= mx/mmap.sc;
157 mmap.ty -= my/mmap.sc;
e9fc876 @pjimen5 AddedDetailViewForSightings
pjimen5 authored
158 if (mmap.sc*sc > 16 && mmap.sc*sc < 800){
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
159 mmap.sc *= sc;
e9fc876 @pjimen5 AddedDetailViewForSightings
pjimen5 authored
160 zoomValue = ceil(map((int)mmap.sc,16,800,minZoom,maxZoom));
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
161 }
ff69b0a @camillol added mouse wheel support to map
authored
162 mmap.tx += mx/mmap.sc;
163 mmap.ty += my/mmap.sc;
164 return true;
165 }
e9fc876 @pjimen5 AddedDetailViewForSightings
pjimen5 authored
166
167 boolean mouseClicked(float px, float py)
168 {
169 return true;
170 }
171
9874feb @camillol drag map, fix map positioning
authored
172 boolean mouseDragged(float px, float py)
173 {
174 mmap.mouseDragged();
175 return true;
3675745 @camillol added MapView
authored
176 }
f051126 @pjimen5 DrawingSightings+SizeIconsAccordingZoom
pjimen5 authored
177
178 void drawAirports(){
179 noStroke();
180 fill(airportAreaColor,40);
181 Point2f p2 = mmap.locationPoint(new Location(41.97, -87.905));
182 ellipse(p2.x,p2.y,map(zoomValue,minZoom,maxZoom,minDistSize,maxDistSize),map(zoomValue,minZoom,maxZoom,minDistSize,maxDistSize));
183 image(airplaneImage,p2.x,p2.y,map(zoomValue,minZoom,maxZoom,minIconSize,maxIconSize),map(zoomValue,minZoom,maxZoom,minIconSize,maxIconSize));
184 }
185
afeb1eb @camillol experimental buffer for map overlay
authored
186 void drawPlaces(PGraphics buffer) {
187 buffer.imageMode(CENTER);
a675529 @camillol simplified data loading
authored
188 for (Iterator<Place> it = places.iterator(); it.hasNext();) {
189 Place place = it.next();
6d613de @pjimen5 drawingSightings
pjimen5 authored
190 float maxPointValue = map(zoomValue, minZoom, maxZoom, minPointSize, maxPointSize);
191 float dotSize = map(place.sightingCount, minCountSightings, maxCountSightings, minPointSize, maxPointValue);
192
a675529 @camillol simplified data loading
authored
193 Point2f p = mmap.locationPoint(place.loc);
6d613de @pjimen5 drawingSightings
pjimen5 authored
194
afeb1eb @camillol experimental buffer for map overlay
authored
195 buffer.image(tempIcon, p.x, p.y, dotSize, dotSize);
a675529 @camillol simplified data loading
authored
196 }
197 }
198
60a76c2 @pjimen5 InformationalBox
pjimen5 authored
199 void drawPlacesInformationBox() {
200 imageMode(CENTER);
201 PImage icon = loadImage("yellow.png");
202 for (Iterator<Place> it = places.iterator(); it.hasNext();) {
203 Place place = it.next();
204 float maxPointValue = map(zoomValue, minZoom, maxZoom, minPointSize, maxPointSize);
205 float dotSize = map(place.sightingCount, minCountSightings, maxCountSightings, minPointSize, maxPointValue);
206 Point2f p = mmap.locationPoint(place.loc);
207 if (dist(mouseX,mouseY,p.x,p.y) < dotSize/2){
208 textSize(normalFontSize);
209 strokeWeight(1);
210 String textToPrint = "Click on it to see details";
211 String numOfSightings = "Total # of sightings = " + str(place.sightingCount);
212 if (textToPrint.length() < place.name.length())
213 textToPrint = place.name;
214 if (textToPrint.length() < numOfSightings.length())
215 textToPrint = numOfSightings;
216 fill(infoBoxBackground);
217 float w_ = textWidth(textToPrint)+10;
218 float x_ = (p.x+w_ > w)?w-w_-5:p.x;
219 float h_ = (textAscent() + textDescent()) *3 + 10;
220 float y_ = (p.y+h_ > h)?h-h_-5:p.y;
221 rect(x_,y_,w_,h_);
222 fill(textColor);
223 text(place.name, x_ + (w_ - textWidth(place.name))/2 ,y_+5);
224 text(numOfSightings,x_ + (w_ - textWidth(numOfSightings))/2, (y_+ h_/3)+5);
225 textSize(smallFontSize);
226 text("Click on it to see details",x_+5,y_+h_-10);
227 if (mousePressed){
228 clickedPlace = place;
229 }
230 }
231 else if (clickedPlace == place){
232 clickedPlace = null;
233 }
234 }
235 }
236
f051126 @pjimen5 DrawingSightings+SizeIconsAccordingZoom
pjimen5 authored
237 void drawSightings(){
238 imageMode(CENTER);
239 for (Iterator<Sighting> sightingList = sightings.activeSightingIterator(); sightingList.hasNext();) {
240 Sighting newSighting = sightingList.next();
241 Point2f p = mmap.locationPoint(((Place)(newSighting.location)).loc);
242 image(((SightingType)newSighting.type).icon,p.x,p.y,map(zoomValue,minZoom,maxZoom,minPointSize,maxPointSize),map(zoomValue,minZoom,maxZoom,minPointSize,maxPointSize));
243 }
244 }
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
245
246 void drawSightingsInformationBox(){
247 for (Iterator<Sighting> sightingList = sightings.activeSightingIterator(); sightingList.hasNext();) {
248 Sighting newSighting = sightingList.next();
249 Point2f p = mmap.locationPoint(((Place)(newSighting.location)).loc);
250 if (dist(mouseX,mouseY,p.x,p.y) < map(zoomValue,minZoom,maxZoom,minPointSize/2,maxPointSize/2)){
e9fc876 @pjimen5 AddedDetailViewForSightings
pjimen5 authored
251 textSize(normalFontSize);
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
252 strokeWeight(1);
253 stroke(((SightingType)newSighting.type).colr);
254 String textToPrint = dateFormat.format(newSighting.localTime);
255 if (dateFormat.format(newSighting.localTime).length() < ((Place)(newSighting.location)).name.length())
256 textToPrint = ((Place)(newSighting.location)).name;
257 fill(infoBoxBackground);
258 float w_ = textWidth(textToPrint)+10;
259 float x_ = (p.x+w_ > w)?w-w_-5:p.x;
e9fc876 @pjimen5 AddedDetailViewForSightings
pjimen5 authored
260 float h_ = (textAscent() + textDescent()) *3 + 10;
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
261 float y_ = (p.y+h_ > h)?h-h_-5:p.y;
262 rect(x_,y_,w_,h_);
263 fill(textColor);
264 text(dateFormat.format(newSighting.localTime), x_ + (w_ - textWidth(dateFormat.format(newSighting.localTime)))/2 ,y_+5);
e9fc876 @pjimen5 AddedDetailViewForSightings
pjimen5 authored
265 text(((Place)(newSighting.location)).name,x_ + (w_ - textWidth(((Place)(newSighting.location)).name))/2, (y_+ h_/2));
266 textSize(smallFontSize);
267 text("Click on it to see details",x_+5,y_+h_-10);
268 if (mousePressed){
269 clickedSighting = newSighting;
270 }
271 }
272 else if (clickedSighting == newSighting){
273 clickedSighting = null;
d46044f @pjimen5 UFOInfoBox+LimitMapZoom
pjimen5 authored
274 }
275 }
276 }
3675745 @camillol added MapView
authored
277 }
278
Something went wrong with that request. Please try again.