/
SimpleFastPointOverlay.java
417 lines (367 loc) · 18.6 KB
/
SimpleFastPointOverlay.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package org.osmdroid.views.overlay.simplefastpoint;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.Overlay;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Overlay to draw a layer of clickable simple points, optimized for rendering speed. Nice
* performance up to 100k points. Supports styling each point and label individually, or shared style
* for all points in a theme. Does not support rotated maps.
* There are three rendering algorithms:
* NO_OPTIMIZATION: all points all drawn in every draw event
* MEDIUM_OPTIMIZATION: not recommended for >10k points. Recalculates the grid index on each draw
* event and only draws one point per grid cell.
* MAXIMUM_OPTIMIZATION: for >10k points, only recalculates the grid on touch up, hence much faster.
* Performs well for 100k points.
* <p>
* TODO: a quadtree index would improve rendering speed!
* TODO: an alternative to the CIRCLE shape, cause this is slow to render
* Created by Miguel Porto on 25-10-2016.
*/
public class SimpleFastPointOverlay extends Overlay {
private final SimpleFastPointOverlayOptions mStyle;
private final PointAdapter mPointList;
private final BoundingBox mBoundingBox;
private Integer mSelectedPoint;
private OnClickListener clickListener;
private List<StyledLabelledPoint> gridIndex;
// grid index for optimizing drawing k's of points
private boolean gridBool[][];
private int gridWid, gridHei, viewWid, viewHei;
private boolean hasMoved = false;
private int numLabels;
private BoundingBox startBoundingBox;
private Projection startProjection;
private BoundingBox prevBoundingBox = new BoundingBox();
/**
* Just a light internal class for storing point data
*/
public class StyledLabelledPoint extends Point {
private String mlabel;
private Paint mPointStyle, mTextStyle;
public StyledLabelledPoint(Point point, String label, Paint pointStyle, Paint textStyle) {
super(point);
this.mlabel = label;
this.mPointStyle = pointStyle;
this.mTextStyle = textStyle;
}
}
/**
* @return fast point overlay options (applied to all points)
*/
public SimpleFastPointOverlayOptions getStyle() {
return mStyle;
}
public interface PointAdapter extends Iterable<IGeoPoint> {
int size();
IGeoPoint get(int i);
/**
* Whether this adapter has labels
*
* @return
*/
boolean isLabelled();
/**
* Whether the points are individually styled
*
* @return
*/
boolean isStyled();
}
public interface OnClickListener {
void onClick(PointAdapter points, Integer point);
}
public SimpleFastPointOverlay(PointAdapter pointList, SimpleFastPointOverlayOptions style) {
mStyle = style;
mPointList = pointList;
Double east = null, west = null, north = null, south = null;
for (IGeoPoint p : mPointList) {
if (p == null) continue;
if (east == null || p.getLongitude() > east) east = p.getLongitude();
if (west == null || p.getLongitude() < west) west = p.getLongitude();
if (north == null || p.getLatitude() > north) north = p.getLatitude();
if (south == null || p.getLatitude() < south) south = p.getLatitude();
}
if (east != null)
mBoundingBox = new BoundingBox(north, east, south, west);
else
mBoundingBox = null;
}
public SimpleFastPointOverlay(PointAdapter pointList) {
this(pointList, SimpleFastPointOverlayOptions.getDefaultStyle());
}
private void updateGrid(MapView mapView) {
viewWid = mapView.getWidth();
viewHei = mapView.getHeight();
gridWid = (int) Math.floor((float) viewWid / mStyle.mCellSize) + 1;
gridHei = (int) Math.floor((float) viewHei / mStyle.mCellSize) + 1;
gridBool = new boolean[gridWid][gridHei];
// TODO the measures on first draw are not the final values.
// MapView should propagate onLayout to overlays
}
/**
* Re-calculates which points to be shown and their coordinates
* TODO: this could be further optimized for speed, for example, the grid could be calculated
* in geographic coordinates, instead of projected. N.B. the speed bottleneck is pj.toPixels()
*
* @param pMapView
*/
private void computeGrid(final MapView pMapView) {
// TODO: 15-11-2016 should take map orientation into account in the BBox!
BoundingBox viewBBox = pMapView.getBoundingBox();
startBoundingBox = viewBBox;
startProjection = pMapView.getProjection();
// do not compute grid if BBox is the same
if (viewBBox.getLatNorth() != prevBoundingBox.getLatNorth()
|| viewBBox.getLatSouth() != prevBoundingBox.getLatSouth()
|| viewBBox.getLonWest() != prevBoundingBox.getLonWest()
|| viewBBox.getLonEast() != prevBoundingBox.getLonEast()) {
prevBoundingBox = new BoundingBox(viewBBox.getLatNorth(), viewBBox.getLonEast()
, viewBBox.getLatSouth(), viewBBox.getLonWest());
if (gridBool == null || viewHei != pMapView.getHeight() || viewWid != pMapView.getWidth()) {
updateGrid(pMapView);
} else {
// empty grid.
// TODO: we might leave the grid as it was before to avoid the "flickering"?
for (boolean[] row : gridBool)
Arrays.fill(row, false);
}
int gridX, gridY;
final Point mPositionPixels = new Point();
final Projection pj = pMapView.getProjection();
gridIndex = new ArrayList<>();
numLabels = 0;
for (IGeoPoint pt1 : mPointList) {
if (pt1 == null) continue;
if (pt1.getLatitude() > viewBBox.getLatSouth()
&& pt1.getLatitude() < viewBBox.getLatNorth()
&& pt1.getLongitude() > viewBBox.getLonWest()
&& pt1.getLongitude() < viewBBox.getLonEast()) {
pj.toPixels(pt1, mPositionPixels);
// test whether in this grid cell there is already a point, skip if yes
gridX = (int) Math.floor((float) mPositionPixels.x / mStyle.mCellSize);
gridY = (int) Math.floor((float) mPositionPixels.y / mStyle.mCellSize);
if (gridX >= gridWid || gridY >= gridHei || gridX < 0 || gridY < 0
|| gridBool[gridX][gridY])
continue;
gridBool[gridX][gridY] = true;
gridIndex.add(new StyledLabelledPoint(mPositionPixels
, mPointList.isLabelled() ? ((LabelledGeoPoint) pt1).getLabel() : null
, mPointList.isStyled() ? ((StyledLabelledGeoPoint) pt1).getPointStyle() : null
, mPointList.isStyled() ? ((StyledLabelledGeoPoint) pt1).getTextStyle() : null
));
numLabels++;
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event, final MapView mapView) {
if (mStyle.mAlgorithm !=
SimpleFastPointOverlayOptions.RenderingAlgorithm.MAXIMUM_OPTIMIZATION) return false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startBoundingBox = mapView.getBoundingBox();
startProjection = mapView.getProjection();
break;
case MotionEvent.ACTION_MOVE:
hasMoved = true;
break;
case MotionEvent.ACTION_UP:
hasMoved = false;
startBoundingBox = mapView.getBoundingBox();
startProjection = mapView.getProjection();
mapView.invalidate();
break;
}
return false;
}
/**
* Default action on tap is to select the nearest point.
*/
@Override
public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) {
if (!mStyle.mClickable) return false;
float hyp;
Float minHyp = null;
int closest = -1;
Point tmp = new Point();
Projection pj = mapView.getProjection();
for (int i = 0; i < mPointList.size(); i++) {
if (mPointList.get(i) == null) continue;
// TODO avoid projecting coordinates, do a test before calling next line
pj.toPixels(mPointList.get(i), tmp);
if (Math.abs(event.getX() - tmp.x) > 50 || Math.abs(event.getY() - tmp.y) > 50) continue;
hyp = (event.getX() - tmp.x) * (event.getX() - tmp.x)
+ (event.getY() - tmp.y) * (event.getY() - tmp.y);
if (minHyp == null || hyp < minHyp) {
minHyp = hyp;
closest = i;
}
}
if (minHyp == null) return false;
setSelectedPoint(closest);
mapView.invalidate();
if (clickListener != null) clickListener.onClick(mPointList, closest);
return true;
}
/**
* Sets the highlighted point. App must invalidate the MapView.
*
* @param toSelect The index of the point (zero-based) in the original list.
*/
public void setSelectedPoint(Integer toSelect) {
if (toSelect == null || toSelect < 0 || toSelect >= mPointList.size())
mSelectedPoint = null;
else
mSelectedPoint = toSelect;
}
public Integer getSelectedPoint() {
return mSelectedPoint;
}
public BoundingBox getBoundingBox() {
return mBoundingBox;
}
public void setOnClickListener(OnClickListener listener) {
clickListener = listener;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean b) {
final BoundingBox viewBBox;
if (b) return;
final Point mPositionPixels = new Point();
final Projection pj = mapView.getProjection();
boolean showLabels;
Paint textStyle;
if (mStyle.mPointStyle != null || mPointList.isStyled()) {
switch (mStyle.mAlgorithm) {
case MAXIMUM_OPTIMIZATION:
// optimized for speed, recommended for > 10k points
// recompute grid only on specific events - only onDraw but when not animating
// and not in the middle of a touch scroll gesture
if (gridBool == null || (!hasMoved && !mapView.isAnimating()))
computeGrid(mapView);
// compute the coordinates of each visible point in the new viewbox
IGeoPoint nw = new GeoPoint(startBoundingBox.getLatNorth(), startBoundingBox.getLonWest());
IGeoPoint se = new GeoPoint(startBoundingBox.getLatSouth(), startBoundingBox.getLonEast());
Point pNw = pj.toPixels(nw, null);
Point pSe = pj.toPixels(se, null);
Point pStartSe = startProjection.toPixels(se, null);
Point dGz = new Point(pSe.x - pStartSe.x, pSe.y - pStartSe.y);
Point dd = new Point(dGz.x - pNw.x, dGz.y - pNw.y);
float tx, ty;
showLabels =
((mStyle.mLabelPolicy == SimpleFastPointOverlayOptions.LabelPolicy.DENSITY_THRESHOLD
&& numLabels <= mStyle.mMaxNShownLabels)
|| (mStyle.mLabelPolicy == SimpleFastPointOverlayOptions.LabelPolicy.ZOOM_THRESHOLD
&& mapView.getZoomLevelDouble() >= mStyle.mMinZoomShowLabels));
// draw points
for (StyledLabelledPoint slp : gridIndex) {
tx = (slp.x * dd.x) / pStartSe.x;
ty = (slp.y * dd.y) / pStartSe.y;
drawPointAt(canvas, slp.x + pNw.x + tx, slp.y + pNw.y + ty
, mPointList.isLabelled() && showLabels, slp.mlabel
, (mPointList.isStyled() && slp.mPointStyle != null) ? slp.mPointStyle : mStyle.mPointStyle
, (mPointList.isStyled() && (textStyle = slp.mTextStyle) != null)
? textStyle : mStyle.mTextStyle, mapView);
}
break;
case MEDIUM_OPTIMIZATION:
// recompute grid index on every draw
if (gridBool == null || viewHei != mapView.getHeight() ||
viewWid != mapView.getWidth())
updateGrid(mapView);
else
for (boolean[] row : gridBool)
Arrays.fill(row, false);
showLabels = (mStyle.mLabelPolicy == SimpleFastPointOverlayOptions.LabelPolicy.ZOOM_THRESHOLD
&& mapView.getZoomLevelDouble() >= mStyle.mMinZoomShowLabels);
int gridX, gridY;
viewBBox = mapView.getBoundingBox();
for (IGeoPoint pt1 : mPointList) {
if (pt1 == null) continue;
if (pt1.getLatitude() > viewBBox.getLatSouth()
&& pt1.getLatitude() < viewBBox.getLatNorth()
&& pt1.getLongitude() > viewBBox.getLonWest()
&& pt1.getLongitude() < viewBBox.getLonEast()) {
pj.toPixels(pt1, mPositionPixels);
// test whether in this grid cell there is already a point, skip if yes
// this makes a lot of difference in rendering speed
gridX = (int) Math.floor((float) mPositionPixels.x / mStyle.mCellSize);
gridY = (int) Math.floor((float) mPositionPixels.y / mStyle.mCellSize);
if (gridX >= gridWid || gridY >= gridHei || gridX < 0 || gridY < 0
|| gridBool[gridX][gridY])
continue;
gridBool[gridX][gridY] = true;
// style may come individually or from the whole theme setting
drawPointAt(canvas, mPositionPixels.x, mPositionPixels.y, mPointList.isLabelled() && showLabels
, mPointList.isLabelled() ? ((LabelledGeoPoint) pt1).getLabel() : null
, (mPointList.isStyled() && ((StyledLabelledGeoPoint) pt1).getPointStyle() != null)
? ((StyledLabelledGeoPoint) pt1).getPointStyle() : mStyle.mPointStyle
, (mPointList.isStyled() && (textStyle = ((StyledLabelledGeoPoint) pt1).getTextStyle()) != null)
? textStyle : mStyle.mTextStyle, mapView);
}
}
break;
case NO_OPTIMIZATION:
// draw all points
showLabels = (mStyle.mLabelPolicy == SimpleFastPointOverlayOptions.LabelPolicy.ZOOM_THRESHOLD
&& mapView.getZoomLevelDouble() >= mStyle.mMinZoomShowLabels);
viewBBox = mapView.getBoundingBox();
for (IGeoPoint pt1 : mPointList) {
if (pt1 == null) continue;
if (pt1.getLatitude() > viewBBox.getLatSouth()
&& pt1.getLatitude() < viewBBox.getLatNorth()
&& pt1.getLongitude() > viewBBox.getLonWest()
&& pt1.getLongitude() < viewBBox.getLonEast()) {
pj.toPixels(pt1, mPositionPixels);
// style may come individually or from the whole theme setting
drawPointAt(canvas, mPositionPixels.x, mPositionPixels.y, mPointList.isLabelled() && showLabels
, mPointList.isLabelled() ? ((LabelledGeoPoint) pt1).getLabel() : null
, (mPointList.isStyled() && ((StyledLabelledGeoPoint) pt1).getPointStyle() != null)
? ((StyledLabelledGeoPoint) pt1).getPointStyle() : mStyle.mPointStyle
, (mPointList.isStyled() && (textStyle = ((StyledLabelledGeoPoint) pt1).getTextStyle()) != null)
? textStyle : mStyle.mTextStyle, mapView);
}
}
break;
}
}
if (mSelectedPoint != null && mSelectedPoint < mPointList.size() &&
mPointList.get(mSelectedPoint) != null
&& mStyle.mSelectedPointStyle != null) {
pj.toPixels(mPointList.get(mSelectedPoint), mPositionPixels);
if (mStyle.mSymbol == SimpleFastPointOverlayOptions.Shape.CIRCLE)
canvas.drawCircle(mPositionPixels.x, mPositionPixels.y
, mStyle.mSelectedCircleRadius, mStyle.mSelectedPointStyle);
else
canvas.drawRect((float) mPositionPixels.x - mStyle.mSelectedCircleRadius
, (float) mPositionPixels.y - mStyle.mSelectedCircleRadius
, (float) mPositionPixels.x + mStyle.mSelectedCircleRadius
, (float) mPositionPixels.y + mStyle.mSelectedCircleRadius
, mStyle.mSelectedPointStyle);
}
}
protected void drawPointAt(Canvas canvas, float x, float y, boolean showLabel, String label, Paint pointStyle, Paint textStyle, MapView pMapView) {
canvas.save();
canvas.rotate(-pMapView.getMapOrientation(), x, y);
if (mStyle.mSymbol == SimpleFastPointOverlayOptions.Shape.CIRCLE)
canvas.drawCircle(x, y, mStyle.mCircleRadius, pointStyle);
else
canvas.drawRect(x - mStyle.mCircleRadius, y - mStyle.mCircleRadius
, x + mStyle.mCircleRadius, y + mStyle.mCircleRadius
, pointStyle);
if (showLabel && label != null)
canvas.drawText(label, x, y - mStyle.mCircleRadius - 5, textStyle);
canvas.restore();
}
}