summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java
blob: 9e809c2ce047e93aa5eca30edbceae2a91dff8ad (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
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
package com.mapbox.mapboxsdk.style.sources;

import android.support.annotation.Keep;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Geometry;
import com.mapbox.mapboxsdk.style.expressions.Expression;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * GeoJson source, allows using FeatureCollections from Json.
 *
 * @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">the style specification</a>
 */
@UiThread
public class GeoJsonSource extends Source {

  /**
   * Internal use
   *
   * @param nativePtr - pointer to native peer
   */
  @Keep
  GeoJsonSource(long nativePtr) {
    super(nativePtr);
  }

  /**
   * Create an empty GeoJsonSource
   *
   * @param id the source id
   */
  public GeoJsonSource(String id) {
    super();
    initialize(id, null);
    setGeoJson(FeatureCollection.fromFeatures(new ArrayList<Feature>()));
  }

  /**
   * Create an empty GeoJsonSource with non-default GeoJsonOptions.
   *
   * @param id      the source id
   * @param options options
   */
  public GeoJsonSource(String id, GeoJsonOptions options) {
    super();
    initialize(id, options);
    setGeoJson(FeatureCollection.fromFeatures(new ArrayList<Feature>()));
  }

  /**
   * Create a GeoJsonSource from a raw json string
   *
   * @param id      the source id
   * @param geoJson raw Json FeatureCollection
   */
  public GeoJsonSource(String id, @Nullable String geoJson) {
    super();
    if (geoJson == null || geoJson.startsWith("http")) {
      throw new IllegalArgumentException("Expected a raw json body");
    }
    initialize(id, null);
    setGeoJson(geoJson);
  }

  /**
   * Create a GeoJsonSource from a raw json string and non-default GeoJsonOptions
   *
   * @param id      the source id
   * @param geoJson raw Json body
   * @param options options
   */
  public GeoJsonSource(String id, @Nullable String geoJson, GeoJsonOptions options) {
    super();
    if (geoJson == null || geoJson.startsWith("http")) {
      throw new IllegalArgumentException("Expected a raw json body");
    }
    initialize(id, options);
    setGeoJson(geoJson);
  }

  /**
   * Create a GeoJsonSource from a remote geo json file
   *
   * @param id  the source id
   * @param url remote json file
   */
  public GeoJsonSource(String id, URL url) {
    super();
    initialize(id, null);
    nativeSetUrl(url.toExternalForm());
  }

  /**
   * Create a GeoJsonSource from a remote geo json file and non-default GeoJsonOptions
   *
   * @param id      the source id
   * @param url     remote json file
   * @param options options
   */
  public GeoJsonSource(String id, URL url, GeoJsonOptions options) {
    super();
    initialize(id, options);
    nativeSetUrl(url.toExternalForm());
  }

  /**
   * Create a GeoJsonSource from a FeatureCollection.
   *
   * @param id       the source id
   * @param features the features
   */
  public GeoJsonSource(String id, FeatureCollection features) {
    super();
    initialize(id, null);
    setGeoJson(features);
  }

  /**
   * Create a GeoJsonSource from a FeatureCollection and non-default GeoJsonOptions.
   *
   * @param id       the source id
   * @param features the features
   * @param options  options
   */
  public GeoJsonSource(String id, FeatureCollection features, GeoJsonOptions options) {
    super();
    initialize(id, options);
    setGeoJson(features);
  }

  /**
   * Create a GeoJsonSource from a {@link Feature}
   *
   * @param id      the source id
   * @param feature the feature
   */
  public GeoJsonSource(String id, Feature feature) {
    super();
    initialize(id, null);
    setGeoJson(feature);
  }

  /**
   * Create a GeoJsonSource from a {@link Feature} and non-default {@link GeoJsonOptions}
   *
   * @param id      the source id
   * @param feature the feature
   * @param options options
   */
  public GeoJsonSource(String id, Feature feature, GeoJsonOptions options) {
    super();
    initialize(id, options);
    setGeoJson(feature);
  }

  /**
   * Create a GeoJsonSource from a {@link Geometry}
   *
   * @param id       the source id
   * @param geometry the geometry
   */
  public GeoJsonSource(String id, Geometry geometry) {
    super();
    initialize(id, null);
    setGeoJson(geometry);
  }

  /**
   * Create a GeoJsonSource from a {@link Geometry} and non-default {@link GeoJsonOptions}
   *
   * @param id       the source id
   * @param geometry the geometry
   * @param options  options
   */
  public GeoJsonSource(String id, Geometry geometry, GeoJsonOptions options) {
    super();
    initialize(id, options);
    setGeoJson(geometry);
  }

  /**
   * Updates the GeoJson with a single feature. The update is performed asynchronously,
   * so the data won't be immediately visible or available to query when this method returns.
   *
   * @param feature the GeoJSON {@link Feature} to set
   */
  public void setGeoJson(Feature feature) {
    if (detached) {
      return;
    }
    checkThread();
    nativeSetFeature(feature);
  }

  /**
   * Updates the GeoJson with a single geometry. The update is performed asynchronously,
   * so the data won't be immediately visible or available to query when this method returns.
   *
   * @param geometry the GeoJSON {@link Geometry} to set
   */
  public void setGeoJson(Geometry geometry) {
    if (detached) {
      return;
    }
    checkThread();
    nativeSetGeometry(geometry);
  }

  /**
   * Updates the GeoJson. The update is performed asynchronously,
   * so the data won't be immediately visible or available to query when this method returns.
   *
   * @param features the GeoJSON FeatureCollection
   */
  public void setGeoJson(FeatureCollection features) {
    if (detached) {
      return;
    }
    checkThread();
    nativeSetFeatureCollection(features);
  }

  /**
   * Updates the GeoJson. The update is performed asynchronously,
   * so the data won't be immediately visible or available to query when this method returns.
   *
   * @param json the raw GeoJson FeatureCollection string
   */
  public void setGeoJson(String json) {
    if (detached) {
      return;
    }
    checkThread();
    nativeSetGeoJsonString(json);
  }

  /**
   * Updates the url
   *
   * @param url the GeoJSON FeatureCollection url
   */
  public void setUrl(@NonNull URL url) {
    checkThread();
    setUrl(url.toExternalForm());
  }

  /**
   * Updates the url
   *
   * @param url the GeoJSON FeatureCollection url
   */
  public void setUrl(String url) {
    checkThread();
    nativeSetUrl(url);
  }

  /**
   * @return The url or null
   */
  @Nullable
  public String getUrl() {
    checkThread();
    return nativeGetUrl();
  }

  /**
   * Queries the source for features.
   *
   * @param filter an optional filter expression to filter the returned Features
   * @return the features
   */
  @NonNull
  public List<Feature> querySourceFeatures(@Nullable Expression filter) {
    checkThread();
    Feature[] features = querySourceFeatures(filter != null ? filter.toArray() : null);
    return features != null ? Arrays.asList(features) : new ArrayList<Feature>();
  }

  /**
   * Returns the children of a cluster (on the next zoom level) given its id (cluster_id value from feature properties).
   * <p>
   * Requires configuring this source as a cluster by calling {@link GeoJsonOptions#withCluster(boolean)}.
   * </p>
   *
   * @param cluster cluster from which to retrieve children from
   * @return a list of features for the underlying children
   */
  @NonNull
  public FeatureCollection getClusterChildren(@NonNull Feature cluster) {
    checkThread();
    return FeatureCollection.fromFeatures(nativeGetClusterChildren(cluster));
  }

  /**
   * Returns all the leaves of a cluster (given its cluster_id), with pagination support: limit is the number of leaves
   * to return (set to Infinity for all points), and offset is the amount of points to skip (for pagination).
   * <p>
   * Requires configuring this source as a cluster by calling {@link GeoJsonOptions#withCluster(boolean)}.
   * </p>
   *
   * @param cluster cluster from which to retrieve leaves from
   * @param limit  limit is the number of points to return
   * @param offset  offset is the amount of points to skip (for pagination)
   * @return a list of features for the underlying leaves
   */
  @NonNull
  public FeatureCollection getClusterLeaves(@NonNull Feature cluster, long limit, long offset) {
    checkThread();
    return FeatureCollection.fromFeatures(nativeGetClusterLeaves(cluster, limit, offset));
  }

  /**
   * Returns the zoom on which the cluster expands into several children (useful for "click to zoom" feature)
   * given the cluster's cluster_id (cluster_id value from feature properties).
   * <p>
   * Requires configuring this source as a cluster by calling {@link GeoJsonOptions#withCluster(boolean)}.
   * </p>
   *
   * @param cluster cluster from which to retrieve the expansion zoom from
   * @return the zoom on which the cluster expands into several children
   */
  public int getClusterExpansionZoom(@NonNull Feature cluster) {
    checkThread();
    return nativeGetClusterExpansionZoom(cluster);
  }

  @Keep
  protected native void initialize(String layerId, Object options);

  @Keep
  protected native void nativeSetUrl(String url);

  @NonNull
  @Keep
  protected native String nativeGetUrl();

  @Keep
  private native void nativeSetGeoJsonString(String geoJson);

  @Keep
  private native void nativeSetFeatureCollection(FeatureCollection geoJson);

  @Keep
  private native void nativeSetFeature(Feature feature);

  @Keep
  private native void nativeSetGeometry(Geometry geometry);

  @NonNull
  @Keep
  private native Feature[] querySourceFeatures(Object[] filter);

  @Keep
  private native Feature[]  nativeGetClusterChildren(Feature feature);

  @Keep
  private native Feature[]  nativeGetClusterLeaves(Feature feature, long limit, long offset);

  @Keep
  private native int nativeGetClusterExpansionZoom(Feature feature);

  @Override
  @Keep
  protected native void finalize() throws Throwable;

}