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

Commit 680e619

Browse files
author
Langston Smith
authored
Refactoring changes to switch loadGeoJsonFromAsset() usage to URI (#1179)
1 parent f133644 commit 680e619

File tree

7 files changed

+95
-189
lines changed

7 files changed

+95
-189
lines changed

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/BathymetryActivity.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import android.graphics.Color;
44
import android.os.Bundle;
5-
import androidx.annotation.NonNull;
6-
import androidx.appcompat.app.AppCompatActivity;
75

8-
import com.mapbox.geojson.FeatureCollection;
96
import com.mapbox.mapboxandroiddemo.R;
107
import com.mapbox.mapboxsdk.Mapbox;
118
import com.mapbox.mapboxsdk.geometry.LatLng;
@@ -18,8 +15,11 @@
1815
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
1916
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
2017

21-
import java.io.InputStream;
18+
import java.net.URI;
19+
import java.net.URISyntaxException;
2220

21+
import androidx.annotation.NonNull;
22+
import androidx.appcompat.app.AppCompatActivity;
2323
import timber.log.Timber;
2424

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

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

78-
// Initialize FeatureCollection object for future use with layers
79-
featureCollection = FeatureCollection.fromJson(loadGeoJsonFromAsset(
80-
"bathymetry-data.geojson"));
81-
82-
// Retrieve GeoJSON from local file and add it to the map
83-
style.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID,
84-
featureCollection));
77+
try {
78+
// Retrieve GeoJSON from local file and add it to the map's style
79+
style.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID, new URI("asset://bathymetry-data.geojson")));
80+
} catch (URISyntaxException exception) {
81+
Timber.d(exception);
82+
}
8583

8684
setUpDepthFillLayers(style);
8785
setUpDepthNumberSymbolLayer(style);
@@ -166,21 +164,4 @@ protected void onSaveInstanceState(Bundle outState) {
166164
super.onSaveInstanceState(outState);
167165
mapView.onSaveInstanceState(outState);
168166
}
169-
170-
private String loadGeoJsonFromAsset(String filename) {
171-
try {
172-
// Load GeoJSON file
173-
InputStream is = getAssets().open(filename);
174-
int size = is.available();
175-
byte[] buffer = new byte[size];
176-
is.read(buffer);
177-
is.close();
178-
return new String(buffer, "UTF-8");
179-
180-
} catch (Exception exception) {
181-
Timber.e("Exception Loading GeoJSON: %s", exception.toString());
182-
exception.printStackTrace();
183-
return null;
184-
}
185-
}
186167
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/MultipleGeometriesActivity.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import android.graphics.Color;
44
import android.os.Bundle;
5-
import androidx.annotation.NonNull;
6-
import androidx.appcompat.app.AppCompatActivity;
75

86
import com.mapbox.mapboxandroiddemo.R;
97
import com.mapbox.mapboxsdk.Mapbox;
@@ -16,8 +14,12 @@
1614
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
1715
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
1816

19-
import java.io.IOException;
20-
import java.io.InputStream;
17+
import java.net.URI;
18+
import java.net.URISyntaxException;
19+
20+
import androidx.annotation.NonNull;
21+
import androidx.appcompat.app.AppCompatActivity;
22+
import timber.log.Timber;
2123

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

5860
private void createGeoJsonSource(@NonNull Style loadedMapStyle) {
59-
// Load data from GeoJSON file in the assets folder
60-
loadedMapStyle.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID,
61-
loadJsonFromAsset("fake_norway_campsites.geojson")));
61+
try {
62+
// Load data from GeoJSON file in the assets folder
63+
loadedMapStyle.addSource(new GeoJsonSource(GEOJSON_SOURCE_ID,
64+
new URI("asset://fake_norway_campsites.geojson")));
65+
} catch (URISyntaxException exception) {
66+
Timber.d(exception);
67+
}
6268
}
6369

6470
private void addPolygonLayer(@NonNull Style loadedMapStyle) {
@@ -123,19 +129,4 @@ protected void onSaveInstanceState(Bundle outState) {
123129
super.onSaveInstanceState(outState);
124130
mapView.onSaveInstanceState(outState);
125131
}
126-
127-
private String loadJsonFromAsset(String filename) {
128-
try {
129-
InputStream is = getAssets().open(filename);
130-
int size = is.available();
131-
byte[] buffer = new byte[size];
132-
is.read(buffer);
133-
is.close();
134-
return new String(buffer, "UTF-8");
135-
136-
} catch (IOException ex) {
137-
ex.printStackTrace();
138-
return null;
139-
}
140-
}
141132
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/MultipleHeatmapStylingActivity.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.mapbox.mapboxandroiddemo.examples.dds;
22

33
import android.os.Bundle;
4-
import androidx.annotation.NonNull;
5-
import androidx.appcompat.app.AppCompatActivity;
64
import android.view.View;
75

86
import com.mapbox.mapboxandroiddemo.R;
@@ -18,8 +16,11 @@
1816
import com.mapbox.mapboxsdk.style.layers.HeatmapLayer;
1917
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
2018

21-
import java.io.InputStream;
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
2221

22+
import androidx.annotation.NonNull;
23+
import androidx.appcompat.app.AppCompatActivity;
2324
import timber.log.Timber;
2425

2526
import static com.mapbox.mapboxsdk.style.expressions.Expression.heatmapDensity;
@@ -71,8 +72,11 @@ public void onStyleLoaded(@NonNull final Style style) {
7172
.build();
7273
mapboxMap.animateCamera(
7374
CameraUpdateFactory.newCameraPosition(cameraPositionForFragmentMap), 2600);
74-
style.addSource(new GeoJsonSource(HEATMAP_SOURCE_ID,
75-
loadGeoJsonFromAsset("la_heatmap_styling_points.geojson")));
75+
try {
76+
style.addSource(new GeoJsonSource(HEATMAP_SOURCE_ID, new URI("asset://la_heatmap_styling_points.geojson")));
77+
} catch (URISyntaxException exception) {
78+
Timber.d(exception);
79+
}
7680
initHeatmapColors();
7781
initHeatmapRadiusStops();
7882
initHeatmapIntensityStops();
@@ -412,21 +416,4 @@ private void initHeatmapIntensityStops() {
412416
0.5f
413417
};
414418
}
415-
416-
private String loadGeoJsonFromAsset(String filename) {
417-
try {
418-
// Load GeoJSON file
419-
InputStream is = getAssets().open(filename);
420-
int size = is.available();
421-
byte[] buffer = new byte[size];
422-
is.read(buffer);
423-
is.close();
424-
return new String(buffer, "UTF-8");
425-
426-
} catch (Exception exception) {
427-
Timber.e("Exception loading GeoJSON: %s", exception.toString());
428-
exception.printStackTrace();
429-
return null;
430-
}
431-
}
432419
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/StyleLineIdentityPropertyActivity.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.mapbox.mapboxandroiddemo.examples.dds;
22

33
import android.os.Bundle;
4-
import androidx.annotation.NonNull;
5-
import androidx.appcompat.app.AppCompatActivity;
64

75
import com.mapbox.mapboxandroiddemo.R;
86
import com.mapbox.mapboxsdk.Mapbox;
@@ -15,8 +13,11 @@
1513
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
1614
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
1715

18-
import java.io.InputStream;
16+
import java.net.URI;
17+
import java.net.URISyntaxException;
1918

19+
import androidx.annotation.NonNull;
20+
import androidx.appcompat.app.AppCompatActivity;
2021
import timber.log.Timber;
2122

2223
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
@@ -51,10 +52,13 @@ public void onMapReady(@NonNull final MapboxMap mapboxMap) {
5152
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
5253
@Override
5354
public void onStyleLoaded(@NonNull Style style) {
54-
55-
// Retrieve GeoJSON from local file and add it to the map
56-
style.addSource(new GeoJsonSource("lines",
57-
loadGeoJsonFromAsset("golden_gate_lines.geojson")));
55+
try {
56+
// Retrieve GeoJSON from local file and add it to the map
57+
style.addSource(new GeoJsonSource("lines",
58+
new URI("asset://golden_gate_lines.geojson")));
59+
} catch (URISyntaxException exception) {
60+
Timber.d(exception);
61+
}
5862

5963
// Create a LineLayer. Use lineColor and stops to draw red and blue lines on the map
6064
style.addLayer(new LineLayer("finalLines", "lines").withProperties(
@@ -112,23 +116,4 @@ protected void onSaveInstanceState(Bundle outState) {
112116
super.onSaveInstanceState(outState);
113117
mapView.onSaveInstanceState(outState);
114118
}
115-
116-
private String loadGeoJsonFromAsset(String filename) {
117-
118-
try {
119-
// Load GeoJSON file
120-
InputStream is = getAssets().open(filename);
121-
int size = is.available();
122-
byte[] buffer = new byte[size];
123-
is.read(buffer);
124-
is.close();
125-
return new String(buffer, "UTF-8");
126-
127-
} catch (Exception exception) {
128-
Timber.e("Exception Loading GeoJSON: %s", exception.toString());
129-
exception.printStackTrace();
130-
return null;
131-
}
132-
133-
}
134119
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/extrusions/Indoor3DMapActivity.java

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.mapbox.mapboxandroiddemo.examples.extrusions;
22

33
import android.os.Bundle;
4-
import androidx.annotation.NonNull;
5-
import androidx.annotation.Nullable;
6-
import androidx.appcompat.app.AppCompatActivity;
74

85
import com.mapbox.mapboxandroiddemo.R;
96
import com.mapbox.mapboxsdk.Mapbox;
@@ -14,8 +11,13 @@
1411
import com.mapbox.mapboxsdk.style.layers.FillExtrusionLayer;
1512
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
1613

17-
import java.io.IOException;
18-
import java.io.InputStream;
14+
import java.net.URI;
15+
import java.net.URISyntaxException;
16+
17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
import androidx.appcompat.app.AppCompatActivity;
20+
import timber.log.Timber;
1921

2022
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
2123
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionBase;
@@ -49,17 +51,19 @@ public void onMapReady(@NonNull final MapboxMap mapboxMap) {
4951
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
5052
@Override
5153
public void onStyleLoaded(@NonNull Style style) {
52-
style.addSource(
53-
new GeoJsonSource("room-data",
54-
loadJsonFromAsset("indoor-3d-map.geojson")));
55-
56-
style.addLayer(new FillExtrusionLayer(
57-
"room-extrusion", "room-data").withProperties(
58-
fillExtrusionColor(get("color")),
59-
fillExtrusionHeight(get("height")),
60-
fillExtrusionBase(get("base_height")),
61-
fillExtrusionOpacity(0.5f)
62-
));
54+
try {
55+
style.addSource(new GeoJsonSource("room-data", new URI("asset://indoor-3d-map.geojson")));
56+
57+
style.addLayer(new FillExtrusionLayer(
58+
"room-extrusion", "room-data").withProperties(
59+
fillExtrusionColor(get("color")),
60+
fillExtrusionHeight(get("height")),
61+
fillExtrusionBase(get("base_height")),
62+
fillExtrusionOpacity(0.5f)
63+
));
64+
} catch (URISyntaxException exception) {
65+
Timber.d(exception);
66+
}
6367
}
6468
});
6569
}
@@ -107,21 +111,4 @@ protected void onSaveInstanceState(Bundle outState) {
107111
super.onSaveInstanceState(outState);
108112
mapView.onSaveInstanceState(outState);
109113
}
110-
111-
private String loadJsonFromAsset(String filename) {
112-
// Using this method to load in GeoJSON files from the assets folder.
113-
114-
try {
115-
InputStream is = getAssets().open(filename);
116-
int size = is.available();
117-
byte[] buffer = new byte[size];
118-
is.read(buffer);
119-
is.close();
120-
return new String(buffer, "UTF-8");
121-
122-
} catch (IOException ex) {
123-
ex.printStackTrace();
124-
return null;
125-
}
126-
}
127114
}

0 commit comments

Comments
 (0)