forked from criticalmaps/criticalmaps-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowGpxHandler.java
More file actions
114 lines (90 loc) · 4 KB
/
ShowGpxHandler.java
File metadata and controls
114 lines (90 loc) · 4 KB
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
package de.stephanlindauer.criticalmaps.handler;
import android.content.SharedPreferences;
import android.net.Uri;
import android.widget.Toast;
import com.google.gson.JsonObject;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.android.maps.Style;
import org.maplibre.android.style.sources.GeoJsonSource;
import org.maplibre.geojson.Feature;
import org.maplibre.geojson.FeatureCollection;
import org.maplibre.geojson.LineString;
import org.maplibre.geojson.Point;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.inject.Inject;
import javax.xml.parsers.ParserConfigurationException;
import de.stephanlindauer.criticalmaps.App;
import de.stephanlindauer.criticalmaps.R;
import de.stephanlindauer.criticalmaps.model.gpx.GpxModel;
import de.stephanlindauer.criticalmaps.model.gpx.GpxTrack;
import de.stephanlindauer.criticalmaps.prefs.SharedPrefsKeys;
import de.stephanlindauer.criticalmaps.utils.GpxReader;
import info.metadude.android.typedpreferences.BooleanPreference;
import info.metadude.android.typedpreferences.StringPreference;
public class ShowGpxHandler {
private final SharedPreferences sharedPreferences;
private final GpxModel gpxModel;
private final App app;
private final GpxReader gpxReader;
@Inject
public ShowGpxHandler(SharedPreferences sharedPreferences, GpxModel gpxModel, App app, GpxReader gpxReader) {
this.sharedPreferences = sharedPreferences;
this.gpxModel = gpxModel;
this.app = app;
this.gpxReader = gpxReader;
}
public void showGpx(Style mapStyle) {
boolean showTrack = new BooleanPreference(sharedPreferences, SharedPrefsKeys.SHOW_GPX).get();
if (!showTrack) {
return;
}
String gpxUri = new StringPreference(sharedPreferences, SharedPrefsKeys.GPX_FILE).get();
if (gpxModel.getUri() == null || !gpxModel.getUri().equals(gpxUri)) {
readFile(gpxUri);
}
showModelOnMap(mapStyle);
}
private void readFile(String gpxUri) {
try {
InputStream gpxInputStream = app.getContentResolver().openInputStream(Uri.parse(gpxUri));
gpxReader.readDataFromStream(gpxInputStream, gpxUri);
} catch (SecurityException | IOException | SAXException | ParserConfigurationException e) {
Toast.makeText(app, R.string.gpx_reading_error, Toast.LENGTH_SHORT).show();
}
}
private void showModelOnMap(Style mapStyle) {
addTracksToMap(mapStyle);
addPoisToMap(mapStyle);
}
private void addTracksToMap(Style mapStyle) {
ArrayList<Feature> features = new ArrayList<>();
for (GpxTrack track : gpxModel.getTracks()) {
ArrayList<Point> points = new ArrayList<>();
for (LatLng location : track.getWaypoints()) {
points.add(Point.fromLngLat(location.getLongitude(), location.getLatitude()));
}
JsonObject properties = new JsonObject();
properties.addProperty("label", track.getName());
features.add(Feature.fromGeometry(LineString.fromLngLats(points), properties));
}
GeoJsonSource gpxTrackSource =
(GeoJsonSource) mapStyle.getSource("gpxTrackSource");
gpxTrackSource.setGeoJson(FeatureCollection.fromFeatures(features));
}
private void addPoisToMap(Style mapStyle) {
ArrayList<Feature> features = new ArrayList<>();
gpxModel.getPoiList().forEach((gpxPoi) -> {
LatLng location = gpxPoi.getPosition();
JsonObject properties = new JsonObject();
properties.addProperty("label", gpxPoi.getName());
features.add(Feature.fromGeometry(
Point.fromLngLat(location.getLongitude(), location.getLatitude()), properties));
});
GeoJsonSource gpxPoiSource =
(GeoJsonSource) mapStyle.getSource("gpxPoiSource");
gpxPoiSource.setGeoJson(FeatureCollection.fromFeatures(features));
}
}