public abstract class GeoJson extends Object
To create a GeoJSON feature collection string, use a builder and add features per the example below:
String geojson = GeoJson.builder()
.add(Feature.point(Location.create(34, -117))
.id("featureId")
.properties(ImmutableMap.of(
"title", "Feature Title",
"color", "#ff0080"))
.build())
.add(Feature.polygon(
LocationList.create(
Location.create(34, -117),
Location.create(35, -118),
Location.create(37, -116),
Location.create(38, -117)))
.id(3)
.build())
.toJson();
Where the GeoJSON string created is:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "featureId",
"geometry": {
"type": "Point",
"coordinates": [-117.0, 34.0]
},
"properties": {
"title": "Feature Title",
"color": "#ff0080"
}
},
{
"type": "Feature",
"id": 3,
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-117.0, 34.0],
[-118.0, 35.0],
[-116.0, 37.0],
[-117.0, 38.0]
]
]
},
"properties": {}
}
]
}
A builder can also write directly to a specified
path.
Note in the example above that features have their own geometry-specific
builders. Feature builders also supply toJson() and write(Path) methods to
output single-feature GeoJSON directly.
Parse GeoJSON to a feature or feature collection using static
from* methods as follows:
Feature f = GeoJson.from(stringOrPathOrUrl).toFeature(); FeatureCollection fc = GeoJson.from(stringOrPathOrUrl).toFeatureCollection();
Once parsed, the feature geometry may be accessed as objects in the
geo package (e.g. Location, LocationList, etc...).
This requires some prior knowledge of the contents of the parsed GeoJSON.
This class does not allow the creation of empty feature collections or features with null geometry.
| Modifier and Type | Class and Description |
|---|---|
static class |
GeoJson.Builder
A GeoJSON builder.
|
static class |
GeoJson.Type
GeoJSON type identifier.
|
| Modifier and Type | Method and Description |
|---|---|
static GeoJson.Builder |
builder()
A reusable GeoJSON builder.
|
static GeoJson |
from(Path json)
Create a GeoJSON from a file path.
|
static GeoJson |
from(String json)
Create a GeoJSON from a string.
|
static GeoJson |
from(URL json)
Create a GeoJSON from a URL.
|
abstract Feature |
toFeature()
Return this GeoJson as a
Feature. |
abstract FeatureCollection |
toFeatureCollection()
Return this GeoJson as a
FeatureCollection. |
public abstract Feature toFeature()
Feature.public abstract FeatureCollection toFeatureCollection()
FeatureCollection.public static GeoJson from(String json)
json - string to readpublic static GeoJson from(Path json)
json - file path to readpublic static GeoJson from(URL json)
json - URL to readpublic static GeoJson.Builder builder()