-
Notifications
You must be signed in to change notification settings - Fork 982
/
Projection.java
869 lines (781 loc) · 32 KB
/
Projection.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
package org.osmdroid.views;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.api.IProjection;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.util.GeometryMath;
import org.osmdroid.util.PointL;
import org.osmdroid.util.RectL;
import org.osmdroid.util.TileSystem;
/**
* A Projection serves to translate between the coordinate system of x/y on-screen pixel coordinates
* and that of latitude/longitude points on the surface of the earth. You obtain a Projection from
* MapView.getProjection(). You should not hold on to this object for more than one draw, since the
* projection of the map could change. <br>
* <br>Uses the web mercator projection
* <b>Note:</b> This class will "wrap" all pixel and lat/long values that overflow their bounds
* (rather than clamping to their bounds).
*
* @author Marc Kurtz
* @author Nicolas Gramlich
* @author Manuel Stahl
* @author Fabrice Fontaine
*/
public class Projection implements IProjection {
/**
* The size in pixels of a VERY large map, the "projected" map.
* For optimization purpose, we may compute only once the projection of the GeoPoints
* on this large map, and then just divide in order to get the projection on a corresponding
* smaller map / smaller zoom
*/
public static final double mProjectedMapSize = 1L << 60;
private long mOffsetX;
private long mOffsetY;
private long mScrollX;
private long mScrollY;
private final Matrix mRotateAndScaleMatrix = new Matrix();
private final Matrix mUnrotateAndScaleMatrix = new Matrix();
private final float[] mRotateScalePoints = new float[2];
private final BoundingBox mBoundingBoxProjection = new BoundingBox();
private final double mZoomLevelProjection;
private final Rect mScreenRectProjection = new Rect();
private final Rect mIntrinsicScreenRectProjection;
private boolean horizontalWrapEnabled;
private boolean verticalWrapEnabled;
private final double mMercatorMapSize;
private final double mTileSize;
private final float mOrientation;
private final GeoPoint mCurrentCenter = new GeoPoint(0., 0);
private final TileSystem mTileSystem;
/**
* @since 6.1.1
*/
private final int mMapCenterOffsetX;
private final int mMapCenterOffsetY;
Projection(MapView mapView) {
this(
mapView.getZoomLevelDouble(), mapView.getIntrinsicScreenRect(null),
mapView.getExpectedCenter(),
mapView.getMapScrollX(), mapView.getMapScrollY(),
mapView.getMapOrientation(),
mapView.isHorizontalMapRepetitionEnabled(), mapView.isVerticalMapRepetitionEnabled(),
MapView.getTileSystem(),
mapView.getMapCenterOffsetX(),
mapView.getMapCenterOffsetY());
}
/**
* @since 6.0.0
*/
public Projection(
final double pZoomLevel, final Rect pScreenRect,
final GeoPoint pCenter,
final long pScrollX, final long pScrollY,
final float pOrientation,
final boolean pHorizontalWrapEnabled, final boolean pVerticalWrapEnabled,
final TileSystem pTileSystem,
final int pMapCenterOffsetX, final int pMapCenterOffsetY) {
mMapCenterOffsetX = pMapCenterOffsetX;
mMapCenterOffsetY = pMapCenterOffsetY;
mZoomLevelProjection = pZoomLevel;
horizontalWrapEnabled = pHorizontalWrapEnabled;
verticalWrapEnabled = pVerticalWrapEnabled;
mTileSystem = pTileSystem;
mMercatorMapSize = TileSystem.MapSize(mZoomLevelProjection);
mTileSize = TileSystem.getTileSize(mZoomLevelProjection);
mIntrinsicScreenRectProjection = pScreenRect;
final GeoPoint center = pCenter != null ? pCenter : new GeoPoint(0., 0);
mScrollX = pScrollX;
mScrollY = pScrollY;
mOffsetX = getScreenCenterX() - mScrollX - mTileSystem.getMercatorXFromLongitude(center.getLongitude(), mMercatorMapSize, this.horizontalWrapEnabled);
mOffsetY = getScreenCenterY() - mScrollY - mTileSystem.getMercatorYFromLatitude(center.getLatitude(), mMercatorMapSize, this.verticalWrapEnabled);
mOrientation = pOrientation;
mRotateAndScaleMatrix.preRotate(mOrientation, getScreenCenterX(), getScreenCenterY());
mRotateAndScaleMatrix.invert(mUnrotateAndScaleMatrix);
refresh();
}
/**
* @since 6.1.0
*/
public Projection(
final double pZoomLevel, final int pWidth, final int pHeight,
final GeoPoint pCenter,
final float pOrientation,
final boolean pHorizontalWrapEnabled, final boolean pVerticalWrapEnabled,
final int pMapCenterOffsetX, final int pMapCenterOffsetY) {
this(
pZoomLevel, new Rect(0, 0, pWidth, pHeight),
pCenter,
0, 0,
pOrientation,
pHorizontalWrapEnabled, pVerticalWrapEnabled,
MapView.getTileSystem(),
pMapCenterOffsetX, pMapCenterOffsetY);
}
/**
* @since 6.0.0
*/
public Projection getOffspring(final double pZoomLevel, final Rect pScreenRect) {
return new Projection(
pZoomLevel, pScreenRect,
mCurrentCenter, 0, 0,
mOrientation,
horizontalWrapEnabled, verticalWrapEnabled,
mTileSystem,
0, 0); // 0 looks like the most relevant value
}
public double getZoomLevel() {
return mZoomLevelProjection;
}
public BoundingBox getBoundingBox() {
return mBoundingBoxProjection;
}
public Rect getScreenRect() {
return mScreenRectProjection;
}
public Rect getIntrinsicScreenRect() {
return mIntrinsicScreenRectProjection;
}
@Override
public IGeoPoint fromPixels(int x, int y) {
return fromPixels(x, y, null, false);
}
/**
* note: if {@link MapView#setHorizontalMapRepetitionEnabled(boolean)} or
* {@link MapView#setVerticalMapRepetitionEnabled(boolean)} is false, then this
* can return values that beyond the max extents of the world. This may or may not be
* desired. <a href="https://github.com/osmdroid/osmdroid/pull/722">https://github.com/osmdroid/osmdroid/pull/722</a>
* for more information and the discussion associated with this.
*
* @param pPixelX
* @param pPixelY
* @param pReuse
* @return
*/
public IGeoPoint fromPixels(final int pPixelX, final int pPixelY, final GeoPoint pReuse) {
return fromPixels(pPixelX, pPixelY, pReuse, false);
}
/**
* * note: if {@link MapView#setHorizontalMapRepetitionEnabled(boolean)} or
* {@link MapView#setVerticalMapRepetitionEnabled(boolean)} is false, then this
* can return values that beyond the max extents of the world. This may or may not be
* desired. <a href="https://github.com/osmdroid/osmdroid/pull/722">https://github.com/osmdroid/osmdroid/pull/722</a>
* for more information and the discussion associated with this.
*
* @param pPixelX
* @param pPixelY
* @param pReuse
* @param forceWrap
* @return
*/
public IGeoPoint fromPixels(final int pPixelX, final int pPixelY, final GeoPoint pReuse, boolean forceWrap) {
//reverting https://github.com/osmdroid/osmdroid/issues/459
//due to relapse of https://github.com/osmdroid/osmdroid/issues/507
//reverted functionality is now on the method fromPixelsRotationSensitive
return mTileSystem.getGeoFromMercator(getCleanMercator(getMercatorXFromPixel(pPixelX), horizontalWrapEnabled),
getCleanMercator(getMercatorYFromPixel(pPixelY), verticalWrapEnabled), mMercatorMapSize, pReuse,
horizontalWrapEnabled || forceWrap, verticalWrapEnabled || forceWrap);
}
@Override
public Point toPixels(final IGeoPoint in, final Point reuse) {
return toPixels(in, reuse, false);
}
public Point toPixels(final IGeoPoint in, final Point reuse, boolean forceWrap) {
final Point out = reuse != null ? reuse : new Point();
out.x = TileSystem.truncateToInt(getLongPixelXFromLongitude(in.getLongitude(), forceWrap));
out.y = TileSystem.truncateToInt(getLongPixelYFromLatitude(in.getLatitude(), forceWrap));
return out;
}
/**
* @since 6.0.0
* TODO refactor
*/
public long getLongPixelXFromLongitude(final double pLongitude, boolean forceWrap) {
return getLongPixelXFromMercator(mTileSystem.getMercatorXFromLongitude(pLongitude, mMercatorMapSize, horizontalWrapEnabled || forceWrap), horizontalWrapEnabled);
}
/**
* @since 6.0.0
* TODO refactor
*/
public long getLongPixelXFromLongitude(final double pLongitude) {
return getLongPixelXFromMercator(mTileSystem.getMercatorXFromLongitude(pLongitude, mMercatorMapSize, false), false);
}
/**
* @since 6.0.0
* TODO refactor
*/
public long getLongPixelYFromLatitude(final double pLatitude, boolean forceWrap) {
return getLongPixelYFromMercator(mTileSystem.getMercatorYFromLatitude(pLatitude, mMercatorMapSize, verticalWrapEnabled || forceWrap), verticalWrapEnabled);
}
/**
* @since 6.0.0
* TODO refactor
*/
public long getLongPixelYFromLatitude(final double pLatitude) {
return getLongPixelYFromMercator(mTileSystem.getMercatorYFromLatitude(pLatitude, mMercatorMapSize, false), false);
}
/**
* A wrapper for {@link #toProjectedPixels(double, double, PointL)}
*/
public PointL toProjectedPixels(final GeoPoint geoPoint, final PointL reuse) {
return toProjectedPixels(geoPoint.getLatitude(), geoPoint.getLongitude(), reuse);
}
/**
* Performs only the first computationally heavy part of the projection. Call
* {@link #getLongPixelsFromProjected(PointL, double, boolean, PointL)} to get the final position.
*
* @param latituteE6 the latitute of the point
* @param longitudeE6 the longitude of the point
* @param reuse just pass null if you do not have a PointL to be 'recycled'.
* @return intermediate value to be stored and passed to toMapPixelsTranslated.
* @deprecated Use {@link #toProjectedPixels(double, double, PointL)} instead
*/
@Deprecated
public PointL toProjectedPixels(final long latituteE6, final long longitudeE6, final PointL reuse) {
return toProjectedPixels(latituteE6 * 1E-6, longitudeE6 * 1E-6, reuse);
}
/**
* Performs only the first computationally heavy part of the projection. Call
* {@link #getLongPixelsFromProjected(PointL, double, boolean, PointL)} to get the final position.
*
* @param latitude the latitute of the point
* @param longitude the longitude of the point
* @param reuse just pass null if you do not have a PointL to be 'recycled'.
* @return intermediate value to be stored and passed to toMapPixelsTranslated.
*/
public PointL toProjectedPixels(final double latitude, final double longitude, final PointL reuse) {
return toProjectedPixels(latitude, longitude, true, reuse);
}
/**
* @since 6.0.0
*/
public PointL toProjectedPixels(final double latitude, final double longitude, final boolean pWrapEnabled, final PointL reuse) {
return mTileSystem.getMercatorFromGeo(latitude, longitude, mProjectedMapSize, reuse, pWrapEnabled);
}
/**
* Performs the second computationally light part of the projection.
*
* @param in the PointL calculated by the {@link #toProjectedPixels(double, double, PointL)}
* @param reuse just pass null if you do not have a Point to be 'recycled'.
* @return the Point containing the coordinates of the initial GeoPoint passed to the
* {@link #toProjectedPixels(double, double, PointL)}.
* @deprecated Use {@link #getLongPixelsFromProjected(PointL, double, boolean, PointL)} instead
*/
@Deprecated
public Point toPixelsFromProjected(final PointL in, final Point reuse) {
final Point out = reuse != null ? reuse : new Point();
final double power = getProjectedPowerDifference();
final PointL tmp = new PointL();
getLongPixelsFromProjected(in, power, true, tmp);
out.x = TileSystem.truncateToInt(tmp.x);
out.y = TileSystem.truncateToInt(tmp.y);
return out;
}
/**
* @deprecated Use {@link #getLongPixelsFromProjected(PointL, double, boolean, PointL)} instead
*/
@Deprecated
public Point toPixelsFromMercator(final long pMercatorX, final long pMercatorY, final Point reuse) {
final Point out = reuse != null ? reuse : new Point();
out.x = TileSystem.truncateToInt(getLongPixelXFromMercator(pMercatorX, true));
out.y = TileSystem.truncateToInt(getLongPixelYFromMercator(pMercatorY, true));
return out;
}
public PointL toMercatorPixels(final int pPixelX, final int pPixelY, final PointL reuse) {
final PointL out = reuse != null ? reuse : new PointL();
out.x = getCleanMercator(getMercatorXFromPixel(pPixelX), horizontalWrapEnabled);
out.y = getCleanMercator(getMercatorYFromPixel(pPixelY), verticalWrapEnabled);
return out;
}
@Override
public float metersToEquatorPixels(final float meters) {
return metersToPixels(meters, 0, mZoomLevelProjection);
}
/**
* Converts a distance in meters to one in (horizontal) pixels at the current zoomlevel and at
* the current latitude at the center of the screen.
*
* @param meters the distance in meters
* @return The number of pixels corresponding to the distance, if measured at the center of the
* screen, at the current zoom level. The return value may only be approximate.
*/
public float metersToPixels(final float meters) {
return metersToPixels(meters, getBoundingBox().getCenterWithDateLine().getLatitude(), mZoomLevelProjection);
}
/**
* @since 6.0
*/
public float metersToPixels(final float meters, final double latitude, final double zoomLevel) {
return (float) (meters / TileSystem.GroundResolution(latitude, zoomLevel));
}
@Override
public IGeoPoint getNorthEast() {
return fromPixels(mIntrinsicScreenRectProjection.right, mIntrinsicScreenRectProjection.top, null, true);
}
@Override
public IGeoPoint getSouthWest() {
return fromPixels(mIntrinsicScreenRectProjection.left, mIntrinsicScreenRectProjection.bottom, null, true);
}
/**
* This will provide a Matrix that will revert the current map's scaling and rotation. This can
* be useful when drawing to a fixed location on the screen.
*/
public Matrix getInvertedScaleRotateCanvasMatrix() {
return mUnrotateAndScaleMatrix;
}
/**
* This will revert the current map's scaling and rotation for a point. This can be useful when
* drawing to a fixed location on the screen.
*/
public Point unrotateAndScalePoint(int x, int y, Point reuse) {
return applyMatrixToPoint(x, y, reuse, mUnrotateAndScaleMatrix, mOrientation != 0);
}
/**
* This will apply the current map's scaling and rotation for a point. This can be useful when
* converting MotionEvents to a screen point.
*/
public Point rotateAndScalePoint(int x, int y, Point reuse) {
return applyMatrixToPoint(x, y, reuse, mRotateAndScaleMatrix, mOrientation != 0);
}
/**
* @since 6.0.0
*/
private Point applyMatrixToPoint(final int pX, final int pY, final Point reuse, final Matrix pMatrix, final boolean pCondition) {
final Point out = reuse != null ? reuse : new Point();
if (pCondition) {
mRotateScalePoints[0] = pX;
mRotateScalePoints[1] = pY;
pMatrix.mapPoints(mRotateScalePoints);
out.x = (int) mRotateScalePoints[0];
out.y = (int) mRotateScalePoints[1];
} else {
out.x = pX;
out.y = pY;
}
return out;
}
/**
* @since 5.6
*/
public void detach() {
}
/**
* @since 6.0.0
*/
public Rect getPixelFromTile(final int pTileX, final int pTileY, final Rect pReuse) {
final Rect out = pReuse != null ? pReuse : new Rect();
out.left = TileSystem.truncateToInt(getLongPixelXFromMercator(getMercatorFromTile(pTileX), false));
out.top = TileSystem.truncateToInt(getLongPixelYFromMercator(getMercatorFromTile(pTileY), false));
out.right = TileSystem.truncateToInt(getLongPixelXFromMercator(getMercatorFromTile(pTileX + 1), false));
out.bottom = TileSystem.truncateToInt(getLongPixelYFromMercator(getMercatorFromTile(pTileY + 1), false));
return out;
}
/**
* @since 6.0.0
*/
public long getMercatorFromTile(final int pTile) {
return TileSystem.getMercatorFromTile(pTile, mTileSize);
}
/**
* This will provide a Matrix that will revert the current map's scaling and rotation. This can
* be useful when drawing to a fixed location on the screen.
*/
public Matrix getScaleRotateCanvasMatrix() {
return mRotateAndScaleMatrix;
}
/**
* @since 6.0.0
*/
public double getProjectedPowerDifference() {
return mProjectedMapSize / getWorldMapSize();
}
/**
* @since 6.0.0
* @deprecated Use {@link #getLongPixelsFromProjected(PointL, double, boolean, PointL)} instead
*/
@Deprecated
public Point getPixelsFromProjected(final PointL in, final double powerDifference, final Point reuse) {
final Point out = reuse != null ? reuse : new Point();
final PointL tmp = new PointL();
getLongPixelsFromProjected(in, powerDifference, true, tmp);
out.x = TileSystem.truncateToInt(tmp.x);
out.y = TileSystem.truncateToInt(tmp.y);
return out;
}
/**
* @param in Input point: a geo point projected to the map with the largest zoom level (aka "projected" map)
* @param powerDifference Factor between the large "projected" map and the wanted projection zoom level
* @param pCloser "Should we move the resulting point - modulo the map size - so that it's
* as close to the screen limits as possible?"
* @since 6.0.0
*/
public PointL getLongPixelsFromProjected(final PointL in, final double powerDifference, final boolean pCloser, final PointL reuse) {
final PointL out = reuse != null ? reuse : new PointL();
out.x = getLongPixelXFromMercator((long) (in.x / powerDifference), pCloser);
out.y = getLongPixelYFromMercator((long) (in.y / powerDifference), pCloser);
return out;
}
/**
* @since 6.0.0
* Correction of pixel value.
* Pixel values are identical, modulo mapSize.
* What we explicitly want is either:
* * the visible pixel that is the closest to the left (first choice)
* * the invisible pixel that is the closest to the screen center
*/
private long getCloserPixel(long pPixel, final int pScreenLimitFirst, final int pScreenLimitLast, final double pMapSize) {
final long center = (pScreenLimitFirst + pScreenLimitLast) / 2;
long previous = 0;
if (pPixel < pScreenLimitFirst) {
while (pPixel < pScreenLimitFirst) {
previous = pPixel;
pPixel += pMapSize;
}
if (pPixel < pScreenLimitLast) {
return pPixel;
}
if (Math.abs(center - pPixel) < Math.abs(center - previous)) {
return pPixel;
}
return previous;
}
while (pPixel >= pScreenLimitFirst) {
previous = pPixel;
pPixel -= pMapSize;
}
if (previous < pScreenLimitLast) {
return previous;
}
if (Math.abs(center - pPixel) < Math.abs(center - previous)) {
return pPixel;
}
return previous;
}
/**
* @since 6.0.0
*/
private long getLongPixelXFromMercator(final long pMercatorX, final boolean pCloser) {
return getLongPixelFromMercator(pMercatorX, pCloser, mOffsetX, mIntrinsicScreenRectProjection.left, mIntrinsicScreenRectProjection.right);
}
/**
* @since 6.0.0
*/
private long getLongPixelYFromMercator(final long pMercatorY, final boolean pCloser) {
return getLongPixelFromMercator(pMercatorY, pCloser, mOffsetY, mIntrinsicScreenRectProjection.top, mIntrinsicScreenRectProjection.bottom);
}
/**
* @since 6.0.0
*/
private long getLongPixelFromMercator(final long pMercator, final boolean pCloser, final long pOffset, final int pScreenLimitFirst, final int pScreenLimitLast) {
long result = pMercator + pOffset;
if (pCloser) {
result = getCloserPixel(result, pScreenLimitFirst, pScreenLimitLast, mMercatorMapSize);
}
return result;
}
/**
* @since 6.0.0
*/
public int getTileFromMercator(final long pMercator) {
return TileSystem.getTileFromMercator(pMercator, mTileSize);
}
/**
* @since 6.0.0
*/
public RectL getMercatorViewPort(final RectL pReuse) {
final RectL out = pReuse != null ? pReuse : new RectL();
// in the standard case, that's all we need: the screen rect corners
float left = mIntrinsicScreenRectProjection.left;
float right = mIntrinsicScreenRectProjection.right;
float top = mIntrinsicScreenRectProjection.top;
float bottom = mIntrinsicScreenRectProjection.bottom;
// sometimes we need to expand beyond in order to get all visible tiles
if (mOrientation != 0) {
final float scaleRotatePoints[] = new float[8];
scaleRotatePoints[0] = mIntrinsicScreenRectProjection.left;
scaleRotatePoints[1] = mIntrinsicScreenRectProjection.top;
scaleRotatePoints[2] = mIntrinsicScreenRectProjection.right;
scaleRotatePoints[3] = mIntrinsicScreenRectProjection.bottom;
scaleRotatePoints[4] = mIntrinsicScreenRectProjection.left;
scaleRotatePoints[5] = mIntrinsicScreenRectProjection.bottom;
scaleRotatePoints[6] = mIntrinsicScreenRectProjection.right;
scaleRotatePoints[7] = mIntrinsicScreenRectProjection.top;
mUnrotateAndScaleMatrix.mapPoints(scaleRotatePoints);
for (int i = 0; i < 8; i += 2) {
if (left > scaleRotatePoints[i]) {
left = scaleRotatePoints[i];
}
if (right < scaleRotatePoints[i]) {
right = scaleRotatePoints[i];
}
if (top > scaleRotatePoints[i + 1]) {
top = scaleRotatePoints[i + 1];
}
if (bottom < scaleRotatePoints[i + 1]) {
bottom = scaleRotatePoints[i + 1];
}
}
}
out.left = getMercatorXFromPixel((int) left);
out.top = getMercatorYFromPixel((int) top);
out.right = getMercatorXFromPixel((int) right);
out.bottom = getMercatorYFromPixel((int) bottom);
return out;
}
/**
* @since 6.0.0
*/
public int getScreenCenterX() {
return (mIntrinsicScreenRectProjection.right + mIntrinsicScreenRectProjection.left) / 2 + mMapCenterOffsetX;
}
/**
* @since 6.0.0
*/
public int getScreenCenterY() {
return (mIntrinsicScreenRectProjection.bottom + mIntrinsicScreenRectProjection.top) / 2 + mMapCenterOffsetY;
}
/**
* @since 6.0.0
*/
public long getMercatorXFromPixel(final int pPixelX) {
return pPixelX - mOffsetX;
}
/**
* @since 6.0.0
*/
public long getMercatorYFromPixel(final int pPixelY) {
return pPixelY - mOffsetY;
}
/**
* @since 6.0.0
*/
public long getCleanMercator(final long pMercator, final boolean wrapEnabled) {
return mTileSystem.getCleanMercator(pMercator, mMercatorMapSize, wrapEnabled);
}
/**
* @since 6.0.0
*/
public GeoPoint getCurrentCenter() {
return mCurrentCenter;
}
public long getOffsetX() {
return mOffsetX;
}
public long getOffsetY() {
return mOffsetY;
}
/**
* @since 6.0.0
*/
public void save(final Canvas pCanvas, final boolean pMapRotation, final boolean pForce) {
if (mOrientation != 0 || pForce) {
pCanvas.save();
pCanvas.concat(pMapRotation ? mRotateAndScaleMatrix : mUnrotateAndScaleMatrix);
}
}
/**
* @since 6.0.0
*/
public void restore(final Canvas pCanvas, final boolean pForce) {
if (mOrientation != 0 || pForce) {
pCanvas.restore();
}
}
/**
* @since 6.0.0
*/
private void refresh() {
// of course we could write mIntrinsicScreenRectProjection.centerX() and centerY()
// but we should keep writing it that way (cf. ProjectionTest)
fromPixels(getScreenCenterX(), getScreenCenterY(), mCurrentCenter);
if (mOrientation != 0 && mOrientation != 180) {
GeometryMath.getBoundingBoxForRotatatedRectangle(
mIntrinsicScreenRectProjection, getScreenCenterX(), getScreenCenterY(),
mOrientation, mScreenRectProjection);
} else {
// of course we could write mScreenRectProjection.set(mIntrinsicScreenRectProjection);
// but we should keep writing it that way (cf. ProjectionTest)
mScreenRectProjection.left = mIntrinsicScreenRectProjection.left;
mScreenRectProjection.top = mIntrinsicScreenRectProjection.top;
mScreenRectProjection.right = mIntrinsicScreenRectProjection.right;
mScreenRectProjection.bottom = mIntrinsicScreenRectProjection.bottom;
}
IGeoPoint neGeoPoint = fromPixels(
mScreenRectProjection.right, mScreenRectProjection.top, null, true);
final TileSystem tileSystem = org.osmdroid.views.MapView.getTileSystem();
if (neGeoPoint.getLatitude() > tileSystem.getMaxLatitude()) {
neGeoPoint = new GeoPoint(tileSystem.getMaxLatitude(), neGeoPoint.getLongitude());
}
if (neGeoPoint.getLatitude() < tileSystem.getMinLatitude()) {
neGeoPoint = new GeoPoint(tileSystem.getMinLatitude(), neGeoPoint.getLongitude());
}
IGeoPoint swGeoPoint = fromPixels(
mScreenRectProjection.left, mScreenRectProjection.bottom, null, true);
if (swGeoPoint.getLatitude() > tileSystem.getMaxLatitude()) {
swGeoPoint = new GeoPoint(tileSystem.getMaxLatitude(), swGeoPoint.getLongitude());
}
if (swGeoPoint.getLatitude() < tileSystem.getMinLatitude()) {
swGeoPoint = new GeoPoint(tileSystem.getMinLatitude(), swGeoPoint.getLongitude());
}
mBoundingBoxProjection.set(
neGeoPoint.getLatitude(), neGeoPoint.getLongitude(),
swGeoPoint.getLatitude(), swGeoPoint.getLongitude());
}
/**
* Adjust the offsets so that this geo point projects into that pixel
*
* @since 6.0.0
*/
public void adjustOffsets(final IGeoPoint pGeoPoint, final PointF pPixel) {
if (pPixel == null) {
return;
}
if (pGeoPoint == null) {
return;
}
final Point unRotatedExpectedPixel = unrotateAndScalePoint((int) pPixel.x, (int) pPixel.y, null);
final Point unRotatedActualPixel = toPixels(pGeoPoint, null);
final long deltaX = unRotatedExpectedPixel.x - unRotatedActualPixel.x;
final long deltaY = unRotatedExpectedPixel.y - unRotatedActualPixel.y;
adjustOffsets(deltaX, deltaY);
}
/**
* Adjust the offsets so that
* either this bounding box is bigger than the screen and contains it
* or it is smaller and it is centered
*
* @since 6.0.0
* @deprecated Use {@link #adjustOffsets(double, double, boolean, int)} instead
*/
@Deprecated
public void adjustOffsets(final BoundingBox pBoundingBox) {
if (pBoundingBox == null) {
return;
}
adjustOffsets(pBoundingBox.getLonWest(), pBoundingBox.getLonEast(), false, 0);
adjustOffsets(pBoundingBox.getActualNorth(), pBoundingBox.getActualSouth(), true, 0);
}
/**
* Adjust offsets so that north and south (if latitude, west and east if longitude)
* actually "fit" into the screen, with a tolerance of extraSize pixels.
* Used in order to ensure scroll limits.
*
* @since 6.0.0
*/
void adjustOffsets(final double pNorthOrWest, final double pSouthOrEast,
final boolean isLatitude, final int pExtraSize) {
final long min;
final long max;
final long deltaX;
final long deltaY;
if (isLatitude) {
min = getLongPixelYFromLatitude(pNorthOrWest);
max = getLongPixelYFromLatitude(pSouthOrEast);
deltaX = 0;
deltaY = getScrollableOffset(min, max, mMercatorMapSize, mIntrinsicScreenRectProjection.height(), pExtraSize);
} else {
min = getLongPixelXFromLongitude(pNorthOrWest);
max = getLongPixelXFromLongitude(pSouthOrEast);
deltaX = getScrollableOffset(min, max, mMercatorMapSize, mIntrinsicScreenRectProjection.width(), pExtraSize);
deltaY = 0;
}
adjustOffsets(deltaX, deltaY);
}
/**
* @since 6.0.0
*/
void adjustOffsets(final long pDeltaX, final long pDeltaY) {
if (pDeltaX == 0 && pDeltaY == 0) {
return;
}
mOffsetX += pDeltaX;
mOffsetY += pDeltaY;
mScrollX -= pDeltaX;
mScrollY -= pDeltaY;
refresh();
}
/**
* @param pPixelMin Pixel position of the limit (left)
* @param pPixelMax Pixel position of the limit (right)
* @param pWorldSize World map size - for modulo adjustments
* @param pScreenSize Screen size
* @param pExtraSize Extra size to consider at each side of the screen
* @return the offset to apply so that the limits are within the screen
* @since 6.0.0
*/
public static long getScrollableOffset(final long pPixelMin, long pPixelMax,
final double pWorldSize,
final int pScreenSize, final int pExtraSize) {
while (pPixelMax - pPixelMin < 0) { // date line + several worlds fix
pPixelMax += pWorldSize;
}
long delta;
if (pPixelMax - pPixelMin < pScreenSize - 2 * pExtraSize) {
final long half = (pPixelMax - pPixelMin) / 2;
if ((delta = pScreenSize / 2 - half - pPixelMin) > 0) {
return delta;
}
if ((delta = pScreenSize / 2 + half - pPixelMax) < 0) {
return delta;
}
return 0;
}
if ((delta = pExtraSize - pPixelMin) < 0) {
return delta;
}
if ((delta = pScreenSize - pExtraSize - pPixelMax) > 0) {
return delta;
}
return 0;
}
/**
* @since 6.0.0
*/
boolean setMapScroll(final MapView pMapView) {
if (pMapView.getMapScrollX() == mScrollX && pMapView.getMapScrollY() == mScrollY) {
return false;
}
pMapView.setMapScroll(mScrollX, mScrollY);
return true;
}
/**
* @since 6.1.0
*/
public boolean isHorizontalWrapEnabled() {
return horizontalWrapEnabled;
}
/**
* @since 6.1.0
*/
public boolean isVerticalWrapEnabled() {
return verticalWrapEnabled;
}
/**
* @since 6.1.0
*/
public float getOrientation() {
return mOrientation;
}
/**
* @since 6.1.0
*/
public int getWidth() {
return mIntrinsicScreenRectProjection.width();
}
/**
* @since 6.1.0
*/
public int getHeight() {
return mIntrinsicScreenRectProjection.height();
}
/**
* @since 6.2.0
*/
public double getWorldMapSize() {
return mMercatorMapSize;
}
}