use of com.mapbox.mapboxsdk.plugins.geojson.model.DataModel in project mapbox-plugins-android by mapbox.
the class GeoJsonPlugin method parseGeoJsonString.
/**
* @param geoJson String of the GeoJSON file
* @return DataModel list of polylines, polygons, and point with bounded
*/
private DataModel parseGeoJsonString(String geoJson) {
int pointCount = 0;
DataModel dataModel = new DataModel();
LatLngBounds.Builder builder = new LatLngBounds.Builder();
FeatureCollection featureCollection = FeatureCollection.fromJson(geoJson);
List<Feature> listFeature = featureCollection.features();
for (Feature feature : listFeature) {
String featureType = feature.geometry().type();
if (!TextUtils.isEmpty(featureType)) {
if (featureType.equalsIgnoreCase("LineString")) {
List<LatLng> latLngs = new ArrayList<>();
LineString lineString = (LineString) feature.geometry();
List<Point> coordinates = lineString.coordinates();
for (Point position : coordinates) {
LatLng latLng = new LatLng(position.latitude(), position.longitude());
latLngs.add(latLng);
pointCount++;
builder.include(latLng);
}
PolyData polylinePolyData = new PolyData();
polylinePolyData.setPoints(latLngs);
polylinePolyData.setType(featureType);
dataModel.addPolyline(polylinePolyData);
} else if (featureType.equalsIgnoreCase("Point")) {
Point point = (Point) feature.geometry();
if (point != null) {
LatLng latLng = new LatLng(point.latitude(), point.longitude());
MarkerData markerData = new MarkerData();
markerData.setPoint(latLng);
markerData.setProperties(feature.properties());
dataModel.addMarker(markerData);
pointCount++;
builder.include(latLng);
}
} else if (featureType.equalsIgnoreCase("Polygon")) {
List<LatLng> latLngs = new ArrayList<>();
Polygon polygon = (Polygon) feature.geometry();
List<Point> listPosition = polygon.coordinates().get(0);
for (Point position : listPosition) {
LatLng latLng = new LatLng(position.latitude(), position.longitude());
latLngs.add(latLng);
pointCount++;
builder.include(latLng);
}
PolyData polygonPolyData = new PolyData();
polygonPolyData.setPoints(latLngs);
polygonPolyData.setType(featureType);
dataModel.addPolygon(polygonPolyData);
} else {
// TODO
}
}
}
if (pointCount > 1) {
dataModel.setBounds(builder.build());
}
return dataModel;
}
use of com.mapbox.mapboxsdk.plugins.geojson.model.DataModel in project mapbox-plugins-android by mapbox.
the class GeoJsonPlugin method parseGeoJsonInputStream.
/**
* Converts a InputStream to a String and passes it to a parseGeoJsonString method
*
* @param inputStream the input stream of GeoJSON file
* @param listener the instance of onLoadingGeoJsonListener
* @return DataModel that generated by parseGeoJsonString function
*/
private DataModel parseGeoJsonInputStream(InputStream inputStream, OnLoadingGeoJsonListener listener) {
DataModel dataModel = null;
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
inputStream.close();
if (!isJSONValid(sb.toString())) {
triggerOnLoadFailed(listener, new IllegalStateException("GeoJSON string is not valid"));
} else {
dataModel = parseGeoJsonString(sb.toString());
}
} catch (IOException exception) {
Timber.e(exception, "Exception InputStream To String: ");
}
return dataModel;
}
Aggregations