Class Feature

java.lang.Object
com.mapbox.geojson.Feature
All Implemented Interfaces:
GeoJson, Serializable

public final class Feature extends Object implements GeoJson
This defines a GeoJson Feature object which represents a spatially bound thing. Every Feature object is a GeoJson object no matter where it occurs in a GeoJson text. A Feature object will always have a "TYPE" member with the value "Feature".

A Feature object has a member with the name "geometry". The value of the geometry member SHALL be either a Geometry object or, in the case that the Feature is unlocated, a JSON null value.

A Feature object has a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).

If a Feature has a commonly used identifier, that identifier SHOULD be included as a member of the Feature object through the id() method, and the value of this member is either a JSON string or number.

An example of a serialized feature is given below:

 {
   "TYPE": "Feature",
   "geometry": {
     "TYPE": "Point",
     "coordinates": [102.0, 0.5]
   },
   "properties": {
     "prop0": "value0"
   }
 
Since:
1.0.0
See Also:
  • Method Details

    • fromJson

      public static Feature fromJson(@NonNull String json)
      Create a new instance of this class by passing in a formatted valid JSON String. If you are creating a Feature object from scratch it is better to use one of the other provided static factory methods such as fromGeometry(Geometry).
      Parameters:
      json - a formatted valid JSON string defining a GeoJson Feature
      Returns:
      a new instance of this class defined by the values passed inside this static factory method
      Since:
      1.0.0
    • fromGeometry

      public static Feature fromGeometry(@Nullable Geometry geometry)
      Create a new instance of this class by giving the feature a Geometry.
      Parameters:
      geometry - a single geometry which makes up this feature object
      Returns:
      a new instance of this class defined by the values passed inside this static factory method
      Since:
      1.0.0
    • fromGeometry

      public static Feature fromGeometry(@Nullable Geometry geometry, @Nullable BoundingBox bbox)
      Create a new instance of this class by giving the feature a Geometry. You can also pass in a double array defining a bounding box.
      Parameters:
      geometry - a single geometry which makes up this feature object
      bbox - optionally include a bbox definition as a double array
      Returns:
      a new instance of this class defined by the values passed inside this static factory method
      Since:
      1.0.0
    • fromGeometry

      public static Feature fromGeometry(@Nullable Geometry geometry, @Nullable com.google.gson.JsonObject properties)
      Create a new instance of this class by giving the feature a Geometry and optionally a set of properties.
      Parameters:
      geometry - a single geometry which makes up this feature object
      properties - a JsonObject containing the feature properties
      Returns:
      a new instance of this class defined by the values passed inside this static factory method
      Since:
      1.0.0
    • fromGeometry

      public static Feature fromGeometry(@Nullable Geometry geometry, @Nullable com.google.gson.JsonObject properties, @Nullable BoundingBox bbox)
      Create a new instance of this class by giving the feature a Geometry, optionally a set of properties, and optionally pass in a bbox.
      Parameters:
      geometry - a single geometry which makes up this feature object
      bbox - optionally include a bbox definition as a double array
      properties - a JsonObject containing the feature properties
      Returns:
      a new instance of this class defined by the values passed inside this static factory method
      Since:
      1.0.0
    • fromGeometry

      public static Feature fromGeometry(@Nullable Geometry geometry, @Nullable com.google.gson.JsonObject properties, @Nullable String id)
      Create a new instance of this class by giving the feature a Geometry, optionally a set of properties, and a String which represents the objects id.
      Parameters:
      geometry - a single geometry which makes up this feature object
      properties - a JsonObject containing the feature properties
      id - common identifier of this feature
      Returns:
      Feature
      Since:
      1.0.0
    • fromGeometry

      public static Feature fromGeometry(@Nullable Geometry geometry, @Nullable com.google.gson.JsonObject properties, @Nullable String id, @Nullable BoundingBox bbox)
      Create a new instance of this class by giving the feature a Geometry, optionally a set of properties, and a String which represents the objects id.
      Parameters:
      geometry - a single geometry which makes up this feature object
      properties - a JsonObject containing the feature properties
      bbox - optionally include a bbox definition as a double array
      id - common identifier of this feature
      Returns:
      Feature
      Since:
      1.0.0
    • type

      @NonNull public String type()
      This describes the TYPE of GeoJson geometry this object is, thus this will always return Feature.
      Specified by:
      type in interface GeoJson
      Returns:
      a String which describes the TYPE of geometry, for this object it will always return Feature
      Since:
      1.0.0
    • bbox

      @Nullable public BoundingBox bbox()
      A Feature Collection might have a member named bbox to include information on the coordinate range for it's Features. The value of the bbox member MUST be a list of size 2*n where n is the number of dimensions represented in the contained feature geometries, with all axes of the most southwesterly point followed by all axes of the more northeasterly point. The axes order of a bbox follows the axes order of geometries.
      Specified by:
      bbox in interface GeoJson
      Returns:
      a list of double coordinate values describing a bounding box
      Since:
      3.0.0
    • id

      @Nullable public String id()
      A feature may have a commonly used identifier which is either a unique String or number.
      Returns:
      a String containing this features unique identification or null if one wasn't given during creation.
      Since:
      1.0.0
    • geometry

      @Nullable public Geometry geometry()
      The geometry which makes up this feature. A Geometry object represents points, curves, and surfaces in coordinate space. One of the seven geometries provided inside this library can be passed in through one of the static factory methods.
      Returns:
      a single defined Geometry which makes this feature spatially aware
      Since:
      1.0.0
    • properties

      @NonNull public com.google.gson.JsonObject properties()
      This contains the JSON object which holds the feature properties. The value of the properties member is a JsonObject and might be empty if no properties are provided.
      Returns:
      a JsonObject which holds this features current properties
      Since:
      1.0.0
    • toJson

      public String toJson()
      This takes the currently defined values found inside this instance and converts it to a GeoJson string.
      Specified by:
      toJson in interface GeoJson
      Returns:
      a JSON string which represents this Feature
      Since:
      1.0.0
    • typeAdapter

      public static com.google.gson.TypeAdapter<Feature> typeAdapter(com.google.gson.Gson gson)
      Gson TYPE adapter for parsing Gson to this class.
      Parameters:
      gson - the built Gson object
      Returns:
      the TYPE adapter for this class
      Since:
      3.0.0
    • addStringProperty

      public void addStringProperty(String key, String value)
      Convenience method to add a String member.
      Parameters:
      key - name of the member
      value - the String value associated with the member
      Since:
      1.0.0
    • addNumberProperty

      public void addNumberProperty(String key, Number value)
      Convenience method to add a Number member.
      Parameters:
      key - name of the member
      value - the Number value associated with the member
      Since:
      1.0.0
    • addBooleanProperty

      public void addBooleanProperty(String key, Boolean value)
      Convenience method to add a Boolean member.
      Parameters:
      key - name of the member
      value - the Boolean value associated with the member
      Since:
      1.0.0
    • addCharacterProperty

      public void addCharacterProperty(String key, Character value)
      Convenience method to add a Character member.
      Parameters:
      key - name of the member
      value - the Character value associated with the member
      Since:
      1.0.0
    • addProperty

      public void addProperty(String key, com.google.gson.JsonElement value)
      Convenience method to add a JsonElement member.
      Parameters:
      key - name of the member
      value - the JsonElement value associated with the member
      Since:
      1.0.0
    • getStringProperty

      public String getStringProperty(String key)
      Convenience method to get a String member.
      Parameters:
      key - name of the member
      Returns:
      the value of the member, null if it doesn't exist
      Since:
      1.0.0
    • getNumberProperty

      public Number getNumberProperty(String key)
      Convenience method to get a Number member.
      Parameters:
      key - name of the member
      Returns:
      the value of the member, null if it doesn't exist
      Since:
      1.0.0
    • getBooleanProperty

      public Boolean getBooleanProperty(String key)
      Convenience method to get a Boolean member.
      Parameters:
      key - name of the member
      Returns:
      the value of the member, null if it doesn't exist
      Since:
      1.0.0
    • getCharacterProperty

      @Deprecated @Nullable public Character getCharacterProperty(String key)
      Deprecated.
      This method was passing the call to JsonElement::getAsCharacter() which is in turn deprecated because of misleading nature, as it does not get this element as a char but rather as a string's first character.
      Convenience method to get a Character member.
      Parameters:
      key - name of the member
      Returns:
      the value of the member, null if it doesn't exist
      Since:
      1.0.0
    • getProperty

      public com.google.gson.JsonElement getProperty(String key)
      Convenience method to get a JsonElement member.
      Parameters:
      key - name of the member
      Returns:
      the value of the member, null if it doesn't exist
      Since:
      1.0.0
    • removeProperty

      public com.google.gson.JsonElement removeProperty(String key)
      Removes the property from the object properties.
      Parameters:
      key - name of the member
      Returns:
      Removed property from the key string passed in through the parameter.
      Since:
      1.0.0
    • hasProperty

      public boolean hasProperty(String key)
      Convenience method to check if a member with the specified name is present in this object.
      Parameters:
      key - name of the member
      Returns:
      true if there is the member has the specified name, false otherwise.
      Since:
      1.0.0
    • hasNonNullValueForProperty

      public boolean hasNonNullValueForProperty(String key)
      Convenience method to check for a member by name as well as non-null value.
      Parameters:
      key - name of the member
      Returns:
      true if member is present with non-null value, false otherwise.
      Since:
      1.3.0
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object