Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Refactoring changes to switch loadGeoJsonFromAsset() usage to URI (#1179
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Langston Smith authored Aug 23, 2019
1 parent f133644 commit 680e619
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.mapbox.geojson.FeatureCollection;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand All @@ -18,8 +15,11 @@
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.expressions.Expression.eq;
Expand All @@ -44,7 +44,6 @@ public class BathymetryActivity extends AppCompatActivity implements OnMapReadyC
.include(new LatLng(44.936236, -85.673450))
.include(new LatLng(44.932955, -85.669272))
.build();
private FeatureCollection featureCollection;
private MapView mapView;

@Override
Expand Down Expand Up @@ -75,13 +74,12 @@ public void onStyleLoaded(@NonNull Style style) {
// Remove lake label layer
style.removeLayer("water-label");

// Initialize FeatureCollection object for future use with layers
featureCollection = FeatureCollection.fromJson(loadGeoJsonFromAsset(
"bathymetry-data.geojson"));

// Retrieve GeoJSON from local file and add it to the map
style.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID,
featureCollection));
try {
// Retrieve GeoJSON from local file and add it to the map's style
style.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID, new URI("asset://bathymetry-data.geojson")));
} catch (URISyntaxException exception) {
Timber.d(exception);
}

setUpDepthFillLayers(style);
setUpDepthNumberSymbolLayer(style);
Expand Down Expand Up @@ -166,21 +164,4 @@ protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

private String loadGeoJsonFromAsset(String filename) {
try {
// Load GeoJSON file
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");

} catch (Exception exception) {
Timber.e("Exception Loading GeoJSON: %s", exception.toString());
exception.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
Expand All @@ -16,8 +14,12 @@
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.expressions.Expression.eq;
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
Expand Down Expand Up @@ -56,9 +58,13 @@ public void onStyleLoaded(@NonNull Style style) {
}

private void createGeoJsonSource(@NonNull Style loadedMapStyle) {
// Load data from GeoJSON file in the assets folder
loadedMapStyle.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID,
loadJsonFromAsset("fake_norway_campsites.geojson")));
try {
// Load data from GeoJSON file in the assets folder
loadedMapStyle.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID,
new URI("asset://fake_norway_campsites.geojson")));
} catch (URISyntaxException exception) {
Timber.d(exception);
}
}

private void addPolygonLayer(@NonNull Style loadedMapStyle) {
Expand Down Expand Up @@ -123,19 +129,4 @@ protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

private String loadJsonFromAsset(String filename) {
try {
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");

} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.mapbox.mapboxandroiddemo.examples.dds;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;

import com.mapbox.mapboxandroiddemo.R;
Expand All @@ -18,8 +16,11 @@
import com.mapbox.mapboxsdk.style.layers.HeatmapLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.expressions.Expression.heatmapDensity;
Expand Down Expand Up @@ -71,8 +72,11 @@ public void onStyleLoaded(@NonNull final Style style) {
.build();
mapboxMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPositionForFragmentMap), 2600);
style.addSource(new GeoJsonSource(HEATMAP_SOURCE_ID,
loadGeoJsonFromAsset("la_heatmap_styling_points.geojson")));
try {
style.addSource(new GeoJsonSource(HEATMAP_SOURCE_ID, new URI("asset://la_heatmap_styling_points.geojson")));
} catch (URISyntaxException exception) {
Timber.d(exception);
}
initHeatmapColors();
initHeatmapRadiusStops();
initHeatmapIntensityStops();
Expand Down Expand Up @@ -412,21 +416,4 @@ private void initHeatmapIntensityStops() {
0.5f
};
}

private String loadGeoJsonFromAsset(String filename) {
try {
// Load GeoJSON file
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");

} catch (Exception exception) {
Timber.e("Exception loading GeoJSON: %s", exception.toString());
exception.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.mapbox.mapboxandroiddemo.examples.dds;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
Expand All @@ -15,8 +13,11 @@
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
Expand Down Expand Up @@ -51,10 +52,13 @@ public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {

// Retrieve GeoJSON from local file and add it to the map
style.addSource(new GeoJsonSource("lines",
loadGeoJsonFromAsset("golden_gate_lines.geojson")));
try {
// Retrieve GeoJSON from local file and add it to the map
style.addSource(new GeoJsonSource("lines",
new URI("asset://golden_gate_lines.geojson")));
} catch (URISyntaxException exception) {
Timber.d(exception);
}

// Create a LineLayer. Use lineColor and stops to draw red and blue lines on the map
style.addLayer(new LineLayer("finalLines", "lines").withProperties(
Expand Down Expand Up @@ -112,23 +116,4 @@ protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

private String loadGeoJsonFromAsset(String filename) {

try {
// Load GeoJSON file
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");

} catch (Exception exception) {
Timber.e("Exception Loading GeoJSON: %s", exception.toString());
exception.printStackTrace();
return null;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.mapbox.mapboxandroiddemo.examples.extrusions;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
Expand All @@ -14,8 +11,13 @@
import com.mapbox.mapboxsdk.style.layers.FillExtrusionLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionBase;
Expand Down Expand Up @@ -49,17 +51,19 @@ public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
style.addSource(
new GeoJsonSource("room-data",
loadJsonFromAsset("indoor-3d-map.geojson")));

style.addLayer(new FillExtrusionLayer(
"room-extrusion", "room-data").withProperties(
fillExtrusionColor(get("color")),
fillExtrusionHeight(get("height")),
fillExtrusionBase(get("base_height")),
fillExtrusionOpacity(0.5f)
));
try {
style.addSource(new GeoJsonSource("room-data", new URI("asset://indoor-3d-map.geojson")));

style.addLayer(new FillExtrusionLayer(
"room-extrusion", "room-data").withProperties(
fillExtrusionColor(get("color")),
fillExtrusionHeight(get("height")),
fillExtrusionBase(get("base_height")),
fillExtrusionOpacity(0.5f)
));
} catch (URISyntaxException exception) {
Timber.d(exception);
}
}
});
}
Expand Down Expand Up @@ -107,21 +111,4 @@ protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

private String loadJsonFromAsset(String filename) {
// Using this method to load in GeoJSON files from the assets folder.

try {
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");

} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}
Loading

0 comments on commit 680e619

Please sign in to comment.