Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #5906 - initial peer model implementation for sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen authored and tobrun committed Sep 13, 2016
1 parent 8f3736f commit 12f9e30
Show file tree
Hide file tree
Showing 22 changed files with 704 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,31 @@ public void removeLayer(@NonNull String layerId) throws NoSuchLayerException {
getMapView().getNativeMapView().removeLayer(layerId);
}

@Nullable
@UiThread
public Source getSource(@NonNull String sourceId) {
return getMapView().getNativeMapView().getSource(sourceId);
}

/**
* Tries to cast the Source to T, returns null if it's another type.
*
* @param sourceId the id used to look up a layer
* @param <T> the generic type of a Source
* @return the casted Source, null if another type
*/
@Nullable
@UiThread
public <T extends Source> T getSourceAs(@NonNull String sourceId) {
try {
//noinspection unchecked
return (T) getMapView().getNativeMapView().getSource(sourceId);
} catch (ClassCastException e) {
Log.e(TAG, String.format("Source: %s is a different type: %s", sourceId, e.getMessage()));
return null;
}
}

@UiThread
public void addSource(@NonNull Source source) {
getMapView().getNativeMapView().addSource(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public void removeLayer(@NonNull String layerId) throws NoSuchLayerException {
nativeRemoveLayer(mNativeMapViewPtr, layerId);
}

public Source getSource(@NonNull String sourceId) {
return nativeGetSource(mNativeMapViewPtr, sourceId);
}

public void addSource(@NonNull Source source) {
nativeAddSource(mNativeMapViewPtr, source.getId(), source);
}
Expand Down Expand Up @@ -708,9 +712,11 @@ private native void nativeSetVisibleCoordinateBounds(long mNativeMapViewPtr, Lat

private native void nativeRemoveLayer(long nativeMapViewPtr, String layerId) throws NoSuchLayerException;

private native void nativeAddSource(long mNativeMapViewPtr, String id, Source source);
private native Source nativeGetSource(long nativeMapViewPtr, String sourceId);

private native void nativeAddSource(long nativeMapViewPtr, String id, Source source);

private native void nativeRemoveSource(long mNativeMapViewPtr, String sourceId) throws NoSuchSourceException;
private native void nativeRemoveSource(long nativeMapViewPtr, String sourceId) throws NoSuchSourceException;

private native void nativeUpdatePolygon(long nativeMapViewPtr, long polygonId, Polygon polygon);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">the style specification</a>
*/
public class GeoJsonSource extends Source {
public static final String TYPE = "geojson";
private static final String DATA_KEY = "data";

/**
* Create an empty GeoJsonSource
*
* @param id the source id
*/
public GeoJsonSource(String id) {
initialize(id);
}

/**
* Create a GeoJsonSource from a raw json string
Expand All @@ -22,11 +29,11 @@ public class GeoJsonSource extends Source {
* @param geoJson raw Json body
*/
public GeoJsonSource(String id, String geoJson) {
super(id, TYPE);
if (geoJson == null || geoJson.startsWith("http")) {
throw new IllegalArgumentException("Expected a raw json body");
}
setRawJson(geoJson);
initialize(id);
nativeSetGeoJson(geoJson);
}

/**
Expand All @@ -36,8 +43,8 @@ public GeoJsonSource(String id, String geoJson) {
* @param url remote json file
*/
public GeoJsonSource(String id, URL url) {
super(id, TYPE);
this.put(DATA_KEY, url.toExternalForm());
initialize(id);
nativeSetUrl(url.toExternalForm());
}

/**
Expand All @@ -47,30 +54,48 @@ public GeoJsonSource(String id, URL url) {
* @param features the features
*/
public GeoJsonSource(String id, FeatureCollection features) {
super(id, TYPE);
setRawJson(features.toJson());
initialize(id);
nativeSetGeoJson(features.toJson());
}

public void setGeoJson(FeatureCollection features) {
nativeSetGeoJson(features.toJson());
}

public void setGeoJson(String json) {
nativeSetGeoJson(json);
}

public void setUrl(URL url) {
nativeSetUrl(url.toExternalForm());
}

public void setUrl(String url) {
nativeSetUrl(url);
}

public GeoJsonSource withCluster(boolean cluster) {
this.put("cluster", cluster);
//TODO this.put("cluster", cluster);
return this;
}

public GeoJsonSource withClusterMaxZoom(float zoom) {
this.put("clusterMaxZoom", zoom);
//TODO this.put("clusterMaxZoom", zoom);
return this;
}

public GeoJsonSource withClusterRadius(float radius) {
this.put("clusterRadius", radius);
//TODO this.put("clusterRadius", radius);
return this;
}

private void setRawJson(String geoJson) {
//Wrap the String in a map as an Object is expected by the
//style conversion template
HashMap<String, String> wrapper = new HashMap<>();
wrapper.put(DATA_KEY, geoJson);
this.put(DATA_KEY, wrapper);
}
protected native void initialize(String layerId);

protected native void nativeSetUrl(String url);

protected native void nativeSetGeoJson(String geoJson);

@Override
protected native void finalize() throws Throwable;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-raster">The style specificition</a>
*/
public class RasterSource extends Source {
public static final String TYPE = "raster";
private static final String URL_KEY = "url";
private static final String TILE_SIZE_KEY = "tileSize";
public static final int DEFAULT_TILE_SIZE = 512;

public RasterSource(String id, URL url) {
this(id, url.toExternalForm());
}

public RasterSource(String id, String url) {
super(id, TYPE);
this.put(URL_KEY, url);
initialize(id, url, DEFAULT_TILE_SIZE);
}

public RasterSource(String id, String url, int tileSize) {
initialize(id, url, tileSize);
}

public RasterSource(String id, TileSet tileSet) {
super(id, TYPE);
this.putAll(tileSet.toValueObject());
initialize(id, tileSet.toValueObject(), DEFAULT_TILE_SIZE);
}

public RasterSource withTileSize(int tileSize) {
this.put(TILE_SIZE_KEY, (float) tileSize);
return this;
public RasterSource(String id, TileSet tileSet, int tileSize) {
initialize(id, tileSet.toValueObject(), tileSize);
}

protected native void initialize(String layerId, Object payload, int tileSize);

@Override
protected native void finalize() throws Throwable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,38 @@

import java.util.HashMap;

public abstract class Source extends HashMap<String, Object> {
private final String id;
/**
* Base Peer class for sources. see source.hpp for the other half of the peer.
*/
public abstract class Source {
private long nativePtr;
private boolean invalidated;

protected Source(String id, String type) {
this.put("type", type);
this.id = id;
public Source(long nativePtr) {
this.nativePtr = nativePtr;
}

public Source() {
}

public String getId() {
return id;
checkValidity();
return nativeGetId();
}

public long getNativePtr() {
return nativePtr;
}

protected native String nativeGetId();

protected void checkValidity() {
if (invalidated) {
throw new RuntimeException("Layer has been invalidated. Request a new reference after adding");
}
}

public final void invalidate() {
this.invalidated = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-vector">the style specification</a>
*/
public class VectorSource extends Source {
public static final String TYPE = "vector";
private static final String URL_KEY = "url";

/**
* Create a vector source from a remote url
Expand All @@ -28,8 +26,7 @@ public VectorSource(String id, URL url) {
* @param url the url
*/
public VectorSource(String id, String url) {
super(id, TYPE);
this.put(URL_KEY, url);
initialize(id, url);
}

/**
Expand All @@ -39,7 +36,11 @@ public VectorSource(String id, String url) {
* @param tileSet the tileset
*/
public VectorSource(String id, TileSet tileSet) {
super(id, TYPE);
this.putAll(tileSet.toValueObject());
initialize(id, tileSet.toValueObject());
}

protected native void initialize(String layerId, Object payload);

@Override
protected native void finalize() throws Throwable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RawRes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -30,6 +31,8 @@
import com.mapbox.mapboxsdk.style.sources.TileSet;
import com.mapbox.mapboxsdk.style.sources.VectorSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -38,6 +41,8 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import static com.mapbox.mapboxsdk.style.layers.Filter.all;
import static com.mapbox.mapboxsdk.style.layers.Filter.eq;
Expand Down Expand Up @@ -154,6 +159,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_add_parks_layer:
addParksLayer();
return true;
case R.id.action_add_dynamic_parks_layer:
addDynamicParksLayer();
return true;
case R.id.action_add_terrain_layer:
addTerrainLayer();
return true;
Expand Down Expand Up @@ -272,6 +280,61 @@ private void addParksLayer() {
mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}

private void addDynamicParksLayer() {
//Load some data
FeatureCollection parks;
try {
String json = readRawResource(R.raw.amsterdam);
parks = FeatureCollection.fromJson(json);
} catch (IOException e) {
Toast.makeText(RuntimeStyleActivity.this, "Couldn't add source: " + e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}

//Add a source
mapboxMap.addSource(new GeoJsonSource("dynamic-park-source"));

FillLayer layer = new FillLayer("dynamic-parks-layer", "dynamic-park-source");
layer.setProperties(
fillColor(Color.RED),
fillOutlineColor(Color.BLUE),
fillOpacity(0.8f),
fillAntialias(true)
);

//Only show me parks
layer.setFilter(all(eq("type", "park")));

mapboxMap.addLayer(layer);

//Get a good look at it all
mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(12));

//Animate the parks source
animateParksSource(parks, 0);
}

private void animateParksSource(final FeatureCollection parks, final int counter) {
Handler handler = new Handler(getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Updating parks source");
//change the source
int park = counter < parks.getFeatures().size() - 1 ? counter : 0;

GeoJsonSource source = mapboxMap.getSourceAs("dynamic-park-source");

List<Feature> features = new ArrayList<>();
features.add(parks.getFeatures().get(park));
source.setGeoJson(FeatureCollection.fromFeatures(features));

//Re-post
animateParksSource(parks, park + 1);
}
}, counter == 0 ? 100 : 1000);
}

private void addTerrainLayer() {
//Add a source
Source source = new VectorSource("my-terrain-source", "mapbox://mapbox.mapbox-terrain-v2");
Expand Down Expand Up @@ -302,7 +365,7 @@ private void addTerrainLayer() {

private void addSatelliteLayer() {
//Add a source
Source source = new RasterSource("my-raster-source", "mapbox://mapbox.satellite").withTileSize(512);
Source source = new RasterSource("my-raster-source", "mapbox://mapbox.satellite", 512);
mapboxMap.addSource(source);

//Add a layer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
android:id="@+id/action_add_parks_layer"
android:title="Add a parks layer"
mapbox:showAsAction="never" />
<item
android:id="@+id/action_add_dynamic_parks_layer"
android:title="Add a dynamic geojson source"
mapbox:showAsAction="never" />
<item
android:id="@+id/action_remove_layer"
android:title="Remove buildings layer"
Expand Down
Loading

0 comments on commit 12f9e30

Please sign in to comment.