summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Projection.java
blob: 76395f95645357c72524f060f23cde40859cfa13 (plain)
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
package com.mapbox.mapboxsdk.maps;

import android.graphics.PointF;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;

import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.constants.GeometryConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;
import com.mapbox.mapboxsdk.geometry.VisibleRegion;

import java.util.ArrayList;
import java.util.List;

/**
 * A projection is used to translate between on screen location and geographic coordinates on
 * the surface of the Earth. Screen location is in screen pixels (not display pixels)
 * with respect to the top left corner of the map (and not necessarily of the whole screen).
 */
public class Projection {

  @NonNull
  private final NativeMap nativeMapView;
  @NonNull
  private final MapView mapView;

  Projection(@NonNull NativeMap nativeMapView, @NonNull MapView mapView) {
    this.nativeMapView = nativeMapView;
    this.mapView = mapView;
  }

  void setContentPadding(int[] contentPadding) {
    double[] output = new double[contentPadding.length];
    for (int i = 0; i < contentPadding.length; i++) {
      output[i] = contentPadding[i];
    }
    nativeMapView.setContentPadding(output);
  }

  int[] getContentPadding() {
    double[] padding = nativeMapView.getCameraPosition().padding;
    return new int[] {(int) padding[0], (int) padding[1], (int) padding[2], (int) padding[3]};
  }

  /**
   * @deprecated unused
   */
  @Deprecated
  public void invalidateContentPadding() {
  }

  /**
   * Returns the spherical Mercator projected meters for a LatLng.
   */
  @NonNull
  public ProjectedMeters getProjectedMetersForLatLng(@NonNull LatLng latLng) {
    return nativeMapView.projectedMetersForLatLng(latLng);
  }

  /**
   * Returns the LatLng for a spherical Mercator projected meters.
   */
  @NonNull
  public LatLng getLatLngForProjectedMeters(@NonNull ProjectedMeters projectedMeters) {
    return nativeMapView.latLngForProjectedMeters(projectedMeters);
  }

  /**
   * <p>
   * Returns the distance spanned by one pixel at the specified latitude and current zoom level.
   * </p>
   * The distance between pixels decreases as the latitude approaches the poles.
   * This relationship parallels the relationship between longitudinal coordinates at different latitudes.
   *
   * @param latitude The latitude for which to return the value.
   * @return The distance measured in meters.
   */
  public double getMetersPerPixelAtLatitude(@FloatRange(from = -90, to = 90) double latitude) {
    return nativeMapView.getMetersPerPixelAtLatitude(latitude);
  }

  /**
   * Returns the geographic location that corresponds to a screen location.
   * The screen location is specified in screen pixels (not display pixels) relative to the
   * top left of the map (not the top left of the whole screen).
   *
   * @param point A Point on the screen in screen pixels.
   * @return The LatLng corresponding to the point on the screen, or null if the ray through
   * the given screen point does not intersect the ground plane.
   */
  @NonNull
  public LatLng fromScreenLocation(@NonNull PointF point) {
    return nativeMapView.latLngForPixel(point);
  }

  /**
   * Gets a projection of the viewing frustum for converting between screen coordinates and
   * geo-latitude/longitude coordinates.
   * <p>
   * This method ignores the content padding.
   *
   * @return The projection of the viewing frustum in its current state.
   */
  @NonNull
  public VisibleRegion getVisibleRegion() {
    return getVisibleRegion(true);
  }

  /**
   * Gets a projection of the viewing frustum for converting between screen coordinates and
   * geo-latitude/longitude coordinates.
   *
   * @param ignorePadding True if the padding should be ignored,
   *                      false if the returned region should be reduced by the padding.
   * @return The projection of the viewing frustum in its current state.
   */
  @NonNull
  public VisibleRegion getVisibleRegion(boolean ignorePadding) {
    float left;
    float right;
    float top;
    float bottom;

    if (ignorePadding) {
      left = 0;
      right = mapView.getWidth();
      top = 0;
      bottom = mapView.getHeight();
    } else {
      int[] contentPadding = getContentPadding();
      left = contentPadding[0];
      right = mapView.getWidth() - contentPadding[2];
      top = contentPadding[1];
      bottom = mapView.getHeight() - contentPadding[3];
    }

    LatLng center = fromScreenLocation(new PointF(left + (right - left) / 2, top + (bottom - top) / 2));

    LatLng topLeft = fromScreenLocation(new PointF(left, top));
    LatLng topRight = fromScreenLocation(new PointF(right, top));
    LatLng bottomRight = fromScreenLocation(new PointF(right, bottom));
    LatLng bottomLeft = fromScreenLocation(new PointF(left, bottom));

    List<LatLng> latLngs = new ArrayList<>();
    latLngs.add(topRight);
    latLngs.add(bottomRight);
    latLngs.add(bottomLeft);
    latLngs.add(topLeft);

    double maxEastLonSpan = 0;
    double maxWestLonSpan = 0;

    double east = 0;
    double west = 0;
    double north = GeometryConstants.MIN_LATITUDE;
    double south = GeometryConstants.MAX_LATITUDE;

    for (LatLng latLng : latLngs) {
      double bearing = bearing(center, latLng);

      if (bearing >= 0) {
        double span = getLongitudeSpan(latLng.getLongitude(), center.getLongitude());
        if (span > maxEastLonSpan) {
          maxEastLonSpan = span;
          east = latLng.getLongitude();
        }
      } else {
        double span = getLongitudeSpan(center.getLongitude(), latLng.getLongitude());
        if (span > maxWestLonSpan) {
          maxWestLonSpan = span;
          west = latLng.getLongitude();
        }
      }

      if (north < latLng.getLatitude()) {
        north = latLng.getLatitude();
      }
      if (south > latLng.getLatitude()) {
        south = latLng.getLatitude();
      }
    }

    if (east < west) {
      return new VisibleRegion(topLeft, topRight, bottomLeft, bottomRight,
        LatLngBounds.from(north, east + GeometryConstants.LONGITUDE_SPAN, south, west));
    }
    return new VisibleRegion(topLeft, topRight, bottomLeft, bottomRight,
      LatLngBounds.from(north, east, south, west));
  }

  /**
   * Takes two {@link Point}s and finds the geographic bearing between them.
   *
   * @param latLng1 the first point used for calculating the bearing
   * @param latLng2 the second point used for calculating the bearing
   * @return bearing in decimal degrees
   * @see <a href="http://turfjs.org/docs/#bearing">Turf Bearing documentation</a>
   */
  static double bearing(@NonNull LatLng latLng1, @NonNull LatLng latLng2) {

    double lon1 = degreesToRadians(latLng1.getLongitude());
    double lon2 = degreesToRadians(latLng2.getLongitude());
    double lat1 = degreesToRadians(latLng1.getLatitude());
    double lat2 = degreesToRadians(latLng2.getLatitude());

    double value1 = Math.sin(lon2 - lon1) * Math.cos(lat2);
    double value2 = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
      * Math.cos(lat2) * Math.cos(lon2 - lon1);

    return radiansToDegrees(Math.atan2(value1, value2));
  }

  /**
   * Converts an angle in degrees to radians.
   *
   * @param degrees angle between 0 and 360 degrees
   * @return angle in radians
   */
  static double degreesToRadians(double degrees) {
    double radians = degrees % 360;
    return radians * Math.PI / 180;
  }

  /**
   * Converts an angle in radians to degrees.
   *
   * @param radians angle in radians
   * @return degrees between 0 and 360 degrees
   */
  static double radiansToDegrees(double radians) {
    double degrees = radians % (2 * Math.PI);
    return degrees * 180 / Math.PI;
  }

  /**
   * Get the absolute distance, in degrees, between the west and
   * east boundaries of this LatLngBounds
   *
   * @return Span distance
   */
  static double getLongitudeSpan(double east, double west) {
    double longSpan = Math.abs(east - west);
    if (east > west) {
      return longSpan;
    }

    // shortest span contains antimeridian
    return GeometryConstants.LONGITUDE_SPAN - longSpan;
  }

  /**
   * Returns a screen location that corresponds to a geographical coordinate (LatLng).
   * The screen location is in screen pixels (not display pixels) relative to the top left
   * of the map (not of the whole screen).
   *
   * @param location A LatLng on the map to convert to a screen location.
   * @return A Point representing the screen location in screen pixels.
   */
  @NonNull
  public PointF toScreenLocation(@NonNull LatLng location) {
    return nativeMapView.pixelForLatLng(location);
  }

  float getHeight() {
    return mapView.getHeight();
  }

  float getWidth() {
    return mapView.getWidth();
  }

  /**
   * Calculates a zoom level based on minimum scale and current scale from MapView
   *
   * @param minScale The minimum scale to calculate the zoom level.
   * @return zoom level that fits the MapView.
   */
  public double calculateZoom(float minScale) {
    return nativeMapView.getZoom() + Math.log(minScale) / Math.log(2);
  }
}