GeoJSON
The Mapbox Java SDK's GeoJSON module provides support for GeoJSON, an open standard file format for representing map data. Read the GeoJSON specification and the Mapbox Glossary's GeoJSON entry for more information about GeoJSON.
Geometries
The Java SDK provides classes for all GeoJSON geometry types. Point
, MultiPoint
, LineString
, MultiLineString
, Polygon
, MultiPolygon
, and GeometryCollection
all end up implementing the Geometry
interface. This shared interface gives you flexibility as you work with the Java SDK and potentially other Mapbox SDKs for Android.
The Point
object represents a single geographical coordinate. It contains two values, longitude and latitude, in that order. This order is the reverse of the LatLng
object in the Mapbox Maps SDK for Android. If you're using the Maps SDK and the Java SDK together, you'll need to convert between Point
and LatLng
objects.
Point point = Point.fromLngLat(LONGITUDE, LATITUDE); // Java SDK
LatLng latLng = new LatLng(point.latitude(), point.longitude()); // Maps SDK
Point point = Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude());
val point = Point.fromLngLat(LONGITUDE, LATITUDE) // Java SDK
val latLng = LatLng(point.latitude(), point.longitude()) // Maps SDK
val point = Point.fromLngLat(latLng.longitude, latLng.latitude)
All geometry classes can be created via several different static methods. Always use a static method to create a GeoJSON geometry class.
Creating a single Polygon
:
Polygon polygon = Polygon.fromLngLats(listOfPointList);
Polygon polygonFromOuterInner = Polygon.fromOuterInner(outerLineStringObject,innerLineStringObject);
val polygon: Polygon = Polygon.fromLngLats(listOfPointList)
val polygonFromOuterInner: Polygon = Polygon.fromOuterInner(outerLineStringObject,innerLineStringObject)
Creating a single MultiLineString
:
MultiLineString multiLineString = MultiLineString.fromLineString(singleLineString);
MultiLineString multiLineStringFromLngLat = MultiLineString.fromLngLats(listOfPointList);
MultiLineString multiLineStringFromJson = MultiLineString.fromJson(JSON_STRING);
val multiLineString = MultiLineString.fromLineString(singleLineString)
val multiLineStringFromLngLat = MultiLineString.fromLngLats(listOfPointList)
val multiLineStringFromJson = MultiLineString.fromJson(JSON_STRING)
Feature and FeatureCollection
In the GeoJSON specification, a feature is an individual (or in some cases, a group of) points, lines, or polygons. Read more about features in the Mapbox Glossary. A group of features is called a feature collection.
The Java SDK's GeoJSON module also provides Feature
and FeatureCollection
classes. These classes can be combined with the various GeoJSON geometry classes.
Feature pointFeature = Feature.fromGeometry(Point.fromLngLat(LONGITUDE, LATITUDE));
Feature multiPointFeature = Feature.fromGeometry(MultiPoint.fromLngLats(listOfPoints));
FeatureCollection featureCollectionFromSingleFeature = FeatureCollection.fromFeature(pointFeature);
FeatureCollection featureCollection = FeatureCollection.fromFeatures(listOfFeatures);
FeatureCollection featureCollectionFromJson = FeatureCollection.fromJson(jsonString);
val pointFeature = Feature.fromGeometry(Point.fromLngLat(LONGITUDE, LATITUDE))
val multiPointFeature = Feature.fromGeometry(MultiPoint.fromLngLats(listOfPoints))
val featureCollectionFromSingleFeature = FeatureCollection.fromFeature(pointFeature)
val featureCollection: FeatureCollection = FeatureCollection.fromFeatures(listOfFeatures)
val featureCollectionFromJson = FeatureCollection.fromJson(jsonString)
Cast to a specific geometry object
A Feature
's Geometry
can be cast to a specific geometry object.
In the code below, a Feature
is used to create a Polygon
:
String polygonFeatureJson = "{
" +
" "type": "Feature",
" +
" "properties": {},
" +
" "geometry": {
" +
" "type": "Polygon",
" +
" "coordinates": [
" +
" [
" +
" [
" +
" -20.7421875,
" +
" 38.8225909761771
" +
" ],
" +
" [
" +
" -22.8515625,
" +
" -36.03133177633187
" +
" ],
" +
" [
" +
" 51.328125,
" +
" -36.597889133070204
" +
" ],
" +
" [
" +
" 48.515625,
" +
" 39.90973623453719
" +
" ],
" +
" [
" +
" -20.7421875,
" +
" 38.8225909761771
" +
" ]
" +
" ]
" +
" ]
" +
" }
" +
" }";
Feature singleFeature = Feature.fromJson(polygonFeatureJson);
Polygon polygon = (Polygon) singleFeature.geometry();
val polygonFeatureJson =
"""
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-34.453125,
-45.08903556483102
],
[
71.71875,
-45.08903556483102
],
[
71.71875,
43.32517767999296
],
[
-34.453125,
43.32517767999296
],
[
-34.453125,
-45.08903556483102
]
]
]
}
}
"""
val singleFeature = Feature.fromJson(polygonFeatureJson)
val polygon = singleFeature.geometry() as Polygon?
Check a geometry type
You can check a Feature
's geometry type. In the code below, a log message would appear whenever a Feature
is a Point
:
FeatureCollection featureCollection = FeatureCollection.fromFeatures(featureList);
if (featureCollection.features() != null) {
for (Feature singleFeature : featureCollection.features()) {
if (singleFeature.geometry() instanceof Point) {
Log.d(TAG, "is a Point geometry");
}
}
}
val featureCollection = FeatureCollection.fromFeatures(featureList)
featureCollection.features()?.let { featureList ->
for (singleFeature in featureList) {
if (singleFeature.geometry() is Point) {
Log.d(TAG, "is a Point geometry")
}
}
}