|
| 1 | +package com.mapbox.geojson; |
| 2 | + |
| 3 | +import android.support.annotation.FloatRange; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | +import com.google.auto.value.AutoValue; |
| 6 | +import com.google.gson.GsonBuilder; |
| 7 | +import com.mapbox.geojson.gson.MapboxAdapterFactory; |
| 8 | +import com.mapbox.geojson.gson.PointSerializer; |
| 9 | + |
| 10 | +import java.io.Serializable; |
| 11 | + |
| 12 | +import static com.mapbox.services.Constants.MAX_LATITUDE; |
| 13 | +import static com.mapbox.services.Constants.MAX_LONGITUDE; |
| 14 | +import static com.mapbox.services.Constants.MIN_LATITUDE; |
| 15 | +import static com.mapbox.services.Constants.MIN_LONGITUDE; |
| 16 | + |
| 17 | +/** |
| 18 | + * A GeoJson object MAY have a member named "bbox" to include information on the coordinate range |
| 19 | + * for its Geometries, Features, or FeatureCollections. |
| 20 | + * <p> |
| 21 | + * This class simplifies the build process for creating a bounding box and working with them when |
| 22 | + * deserialized. specific parameter naming helps define which coordinates belong where when a |
| 23 | + * bounding box instance is being created. Note that since GeoJson objects only have the option of |
| 24 | + * including a bounding box JSON element, the {@code bbox} value returned by a GeoJson object might |
| 25 | + * be null. |
| 26 | + * <p> |
| 27 | + * At a minimum, a bounding box will have two {@link Point}s or four coordinates which define the |
| 28 | + * box. A 3rd dimensional bounding box can be produced if elevation or altitude is defined. |
| 29 | + * |
| 30 | + * @since 3.0.0 |
| 31 | + */ |
| 32 | +@AutoValue |
| 33 | +public abstract class BoundingBox implements Serializable { |
| 34 | + |
| 35 | + /** |
| 36 | + * Define a new instance of this class by passing in two {@link Point}s, representing both the |
| 37 | + * southwest and northwest corners of the bounding box. |
| 38 | + * |
| 39 | + * @param southwest represents the bottom left corner of the bounding box when the camera is |
| 40 | + * pointing due north |
| 41 | + * @param northeast represents the top right corner of the bounding box when the camera is |
| 42 | + * pointing due north |
| 43 | + * @return a new instance of this class defined by the provided points |
| 44 | + * @since 3.0.0 |
| 45 | + */ |
| 46 | + public static BoundingBox fromPoints(@NonNull Point southwest, @NonNull Point northeast) { |
| 47 | + return new AutoValue_BoundingBox(southwest, northeast); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Define a new instance of this class by passing in four coordinates in the same order they would |
| 52 | + * appear in the serialized GeoJson form. Limits are placed on the minimum and maximum coordinate |
| 53 | + * values which can exist and comply with the GeoJson spec. |
| 54 | + * |
| 55 | + * @param west the left side of the bounding box when the map is facing due north |
| 56 | + * @param south the bottom side of the bounding box when the map is facing due north |
| 57 | + * @param east the right side of the bounding box when the map is facing due north |
| 58 | + * @param north the top side of the bounding box when the map is facing due north |
| 59 | + * @return a new instance of this class defined by the provided coordinates |
| 60 | + * @since 3.0.0 |
| 61 | + */ |
| 62 | + public static BoundingBox fromCoordinates( |
| 63 | + @FloatRange(from = MIN_LONGITUDE, to = MAX_LONGITUDE) double west, |
| 64 | + @FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double south, |
| 65 | + @FloatRange(from = MIN_LONGITUDE, to = MAX_LONGITUDE) double east, |
| 66 | + @FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double north) { |
| 67 | + return new AutoValue_BoundingBox(Point.fromLngLat(west, south), Point.fromLngLat(east, north)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Define a new instance of this class by passing in four coordinates in the same order they would |
| 72 | + * appear in the serialized GeoJson form. Limits are placed on the minimum and maximum coordinate |
| 73 | + * values which can exist and comply with the GeoJson spec. |
| 74 | + * |
| 75 | + * @param west the left side of the bounding box when the map is facing due north |
| 76 | + * @param south the bottom side of the bounding box when the map is facing due north |
| 77 | + * @param southwestAltitude the southwest corner altitude or elevation when the map is facing due |
| 78 | + * north |
| 79 | + * @param east the right side of the bounding box when the map is facing due north |
| 80 | + * @param north the top side of the bounding box when the map is facing due north |
| 81 | + * @param northEastAltitude the northeast corner altitude or elevation when the map is facing due |
| 82 | + * north |
| 83 | + * @return a new instance of this class defined by the provided coordinates |
| 84 | + * @since 3.0.0 |
| 85 | + */ |
| 86 | + public static BoundingBox fromCoordinates( |
| 87 | + @FloatRange(from = MIN_LONGITUDE, to = MAX_LONGITUDE) double west, |
| 88 | + @FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double south, |
| 89 | + double southwestAltitude, |
| 90 | + @FloatRange(from = MIN_LONGITUDE, to = MAX_LONGITUDE) double east, |
| 91 | + @FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double north, |
| 92 | + double northEastAltitude) { |
| 93 | + return new AutoValue_BoundingBox( |
| 94 | + Point.fromLngLat(west, south, southwestAltitude), |
| 95 | + Point.fromLngLat(east, north, northEastAltitude)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Provides the {@link Point} which represents the southwest corner of this bounding box when the |
| 100 | + * map is facing due north. |
| 101 | + * |
| 102 | + * @return a {@link Point} which defines this bounding boxes southwest corner |
| 103 | + * @since 3.0.0 |
| 104 | + */ |
| 105 | + @NonNull |
| 106 | + public abstract Point southwest(); |
| 107 | + |
| 108 | + /** |
| 109 | + * Provides the {@link Point} which represents the northeast corner of this bounding box when the |
| 110 | + * map is facing due north. |
| 111 | + * |
| 112 | + * @return a {@link Point} which defines this bounding boxes northeast corner |
| 113 | + * @since 3.0.0 |
| 114 | + */ |
| 115 | + @NonNull |
| 116 | + public abstract Point northeast(); |
| 117 | + |
| 118 | + /** |
| 119 | + * Convenience method for getting the bounding box most westerly point (longitude) as a double |
| 120 | + * coordinate. |
| 121 | + * |
| 122 | + * @return the most westerly coordinate inside this bounding box |
| 123 | + * @since 3.0.0 |
| 124 | + */ |
| 125 | + public double west() { |
| 126 | + return southwest().longitude(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Convenience method for getting the bounding box most southerly point (latitude) as a double |
| 131 | + * coordinate. |
| 132 | + * |
| 133 | + * @return the most southerly coordinate inside this bounding box |
| 134 | + * @since 3.0.0 |
| 135 | + */ |
| 136 | + public double south() { |
| 137 | + return southwest().latitude(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Convenience method for getting the bounding box most easterly point (longitude) as a double |
| 142 | + * coordinate. |
| 143 | + * |
| 144 | + * @return the most easterly coordinate inside this bounding box |
| 145 | + * @since 3.0.0 |
| 146 | + */ |
| 147 | + public double east() { |
| 148 | + return northeast().longitude(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Convenience method for getting the bounding box most westerly point (longitude) as a double |
| 153 | + * coordinate. |
| 154 | + * |
| 155 | + * @return the most westerly coordinate inside this bounding box |
| 156 | + * @since 3.0.0 |
| 157 | + */ |
| 158 | + public double north() { |
| 159 | + return northeast().latitude(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * This takes the currently defined values found inside this instance and converts it to a GeoJson |
| 164 | + * string. |
| 165 | + * |
| 166 | + * @return a JSON string which represents this Bounding box |
| 167 | + * @since 3.0.0 |
| 168 | + */ |
| 169 | + public String toJson() { |
| 170 | + GsonBuilder gson = new GsonBuilder(); |
| 171 | + gson.registerTypeAdapterFactory(MapboxAdapterFactory.create()); |
| 172 | + gson.registerTypeAdapter(Point.class, new PointSerializer()); |
| 173 | + return gson.create().toJson(this); |
| 174 | + } |
| 175 | +} |
0 commit comments