Skip to content

Commit c14dbeb

Browse files
author
Cameron Mace
authored
Adds new static initializers to geojson collection objects (#692)
1 parent dfd51b5 commit c14dbeb

8 files changed

Lines changed: 176 additions & 5 deletions

File tree

services-geojson/src/main/java/com/mapbox/geojson/FeatureCollection.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.mapbox.geojson.gson.PointSerializer;
1717
import com.mapbox.geojson.gson.BoundingBoxSerializer;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.List;
2122

@@ -92,6 +93,20 @@ public static FeatureCollection fromFeatures(@NonNull List<Feature> features) {
9293
return new AutoValue_FeatureCollection(null, features);
9394
}
9495

96+
/**
97+
* Create a new instance of this class by giving the feature collection a single {@link Feature}.
98+
*
99+
* @param feature a single feature
100+
* @return a new instance of this class defined by the values passed inside this static factory
101+
* method
102+
* @since 3.0.0
103+
*/
104+
public static FeatureCollection fromFeature(@NonNull Feature feature) {
105+
List<Feature> featureList = new ArrayList<>();
106+
featureList.add(feature);
107+
return new AutoValue_FeatureCollection(null, featureList);
108+
}
109+
95110
/**
96111
* Create a new instance of this class by giving the feature collection an array of
97112
* {@link Feature}s. The array of features itself isn't null but it can be empty and have a length
@@ -124,6 +139,22 @@ public static FeatureCollection fromFeatures(@NonNull List<Feature> features,
124139
return new AutoValue_FeatureCollection(bbox, features);
125140
}
126141

142+
/**
143+
* Create a new instance of this class by giving the feature collection a single {@link Feature}.
144+
*
145+
* @param feature a single feature
146+
* @param bbox optionally include a bbox definition as a double array
147+
* @return a new instance of this class defined by the values passed inside this static factory
148+
* method
149+
* @since 3.0.0
150+
*/
151+
public static FeatureCollection fromFeature(@NonNull Feature feature,
152+
@Nullable BoundingBox bbox) {
153+
List<Feature> featureList = new ArrayList<>();
154+
featureList.add(feature);
155+
return new AutoValue_FeatureCollection(bbox, featureList);
156+
}
157+
127158
/**
128159
* This describes the type of GeoJson this object is, thus this will always return
129160
* {@link FeatureCollection}.

services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.mapbox.geojson.gson.BoundingBoxSerializer;
1818

1919
import java.io.Serializable;
20+
import java.util.ArrayList;
2021
import java.util.List;
2122

2223
/**
@@ -99,6 +100,20 @@ public static GeometryCollection fromGeometries(@NonNull List<Geometry> geometri
99100
return new AutoValue_GeometryCollection(null, geometries);
100101
}
101102

103+
/**
104+
* Create a new instance of this class by giving the collection a single GeoJSON {@link Geometry}.
105+
*
106+
* @param geometry a non-null object of type geometry which makes up this collection
107+
* @return a new instance of this class defined by the values passed inside this static factory
108+
* method
109+
* @since 3.0.0
110+
*/
111+
public static GeometryCollection fromGeometry(@NonNull Geometry geometry) {
112+
List<Geometry> geometries = new ArrayList<>();
113+
geometries.add(geometry);
114+
return new AutoValue_GeometryCollection(null, geometries);
115+
}
116+
102117
/**
103118
* Create a new instance of this class by giving the collection a list of {@link Geometry}.
104119
*
@@ -113,6 +128,22 @@ public static GeometryCollection fromGeometries(@NonNull List<Geometry> geometri
113128
return new AutoValue_GeometryCollection(bbox, geometries);
114129
}
115130

131+
/**
132+
* Create a new instance of this class by giving the collection a single GeoJSON {@link Geometry}.
133+
*
134+
* @param geometry a non-null object of type geometry which makes up this collection
135+
* @param bbox optionally include a bbox definition as a double array
136+
* @return a new instance of this class defined by the values passed inside this static factory
137+
* method
138+
* @since 3.0.0
139+
*/
140+
public static GeometryCollection fromGeometry(@NonNull Geometry geometry,
141+
@Nullable BoundingBox bbox) {
142+
List<Geometry> geometries = new ArrayList<>();
143+
geometries.add(geometry);
144+
return new AutoValue_GeometryCollection(bbox, geometries);
145+
}
146+
116147
/**
117148
* This describes the TYPE of GeoJson this object is, thus this will always return
118149
* {@link GeometryCollection}.

services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,29 @@ public static MultiLineString fromLineStrings(@NonNull List<LineString> lineStri
9494
return new AutoValue_MultiLineString(null, coordinates);
9595
}
9696

97+
/**
98+
* Create a new instance of this class by passing in a single {@link LineString} object. The
99+
* LineStrings should comply with the GeoJson specifications described in the documentation.
100+
*
101+
* @param lineString a single LineString which make up this MultiLineString
102+
* @return a new instance of this class defined by the values passed inside this static factory
103+
* method
104+
* @since 3.0.0
105+
*/
106+
public static MultiLineString fromLineString(@NonNull LineString lineString) {
107+
List<List<Point>> coordinates = new ArrayList<>();
108+
coordinates.add(lineString.coordinates());
109+
return new AutoValue_MultiLineString(null, coordinates);
110+
}
111+
97112
/**
98113
* Create a new instance of this class by defining a list of {@link LineString} objects and
99114
* passing that list in as a parameter in this method. The LineStrings should comply with the
100115
* GeoJson specifications described in the documentation. Optionally, pass in an instance of a
101116
* {@link BoundingBox} which better describes this MultiLineString.
102117
*
103118
* @param lineStrings a list of LineStrings which make up this MultiLineString
104-
* @param bbox optionally include a bbox definition as a double array
119+
* @param bbox optionally include a bbox definition
105120
* @return a new instance of this class defined by the values passed inside this static factory
106121
* method
107122
* @since 3.0.0
@@ -115,6 +130,23 @@ public static MultiLineString fromLineStrings(@NonNull List<LineString> lineStri
115130
return new AutoValue_MultiLineString(bbox, coordinates);
116131
}
117132

133+
/**
134+
* Create a new instance of this class by passing in a single {@link LineString} object. The
135+
* LineStrings should comply with the GeoJson specifications described in the documentation.
136+
*
137+
* @param lineString a single LineString which make up this MultiLineString
138+
* @param bbox optionally include a bbox definition
139+
* @return a new instance of this class defined by the values passed inside this static factory
140+
* method
141+
* @since 3.0.0
142+
*/
143+
public static MultiLineString fromLineString(@NonNull LineString lineString,
144+
@Nullable BoundingBox bbox) {
145+
List<List<Point>> coordinates = new ArrayList<>();
146+
coordinates.add(lineString.coordinates());
147+
return new AutoValue_MultiLineString(bbox, coordinates);
148+
}
149+
118150
/**
119151
* Create a new instance of this class by defining a list of a list of {@link Point}s which follow
120152
* the correct specifications described in the Point documentation. Note that there should not be
@@ -137,7 +169,7 @@ public static MultiLineString fromLngLats(@NonNull List<List<Point>> points) {
137169
* distance greater than 0.
138170
*
139171
* @param points a list of {@link Point}s which make up the MultiLineString geometry
140-
* @param bbox optionally include a bbox definition as a double array
172+
* @param bbox optionally include a bbox definition
141173
* @return a new instance of this class defined by the values passed inside this static factory
142174
* method
143175
* @since 3.0.0

services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static MultiPolygon fromPolygons(@NonNull List<Polygon> polygons) {
120120
* {@link BoundingBox} which better describes this MultiPolygon.
121121
*
122122
* @param polygons a list of Polygons which make up this MultiPolygon
123-
* @param bbox optionally include a bbox definition as a double array
123+
* @param bbox optionally include a bbox definition
124124
* @return a new instance of this class defined by the values passed inside this static factory
125125
* method
126126
* @since 3.0.0
@@ -134,6 +134,39 @@ public static MultiPolygon fromPolygons(@NonNull List<Polygon> polygons,
134134
return new AutoValue_MultiPolygon(bbox, coordinates);
135135
}
136136

137+
/**
138+
* Create a new instance of this class by defining a single {@link Polygon} objects and passing
139+
* it in as a parameter in this method. The Polygon should comply with the GeoJson
140+
* specifications described in the documentation.
141+
*
142+
* @param polygon a single Polygon which make up this MultiPolygon
143+
* @return a new instance of this class defined by the values passed inside this static factory
144+
* method
145+
* @since 3.0.0
146+
*/
147+
public static MultiPolygon fromPolygon(@NonNull Polygon polygon) {
148+
List<List<List<Point>>> coordinates = new ArrayList<>();
149+
coordinates.add(polygon.coordinates());
150+
return new AutoValue_MultiPolygon(null, coordinates);
151+
}
152+
153+
/**
154+
* Create a new instance of this class by defining a single {@link Polygon} objects and passing
155+
* it in as a parameter in this method. The Polygon should comply with the GeoJson
156+
* specifications described in the documentation.
157+
*
158+
* @param polygon a single Polygon which make up this MultiPolygon
159+
* @param bbox optionally include a bbox definition
160+
* @return a new instance of this class defined by the values passed inside this static factory
161+
* method
162+
* @since 3.0.0
163+
*/
164+
public static MultiPolygon fromPolygon(@NonNull Polygon polygon, @Nullable BoundingBox bbox) {
165+
List<List<List<Point>>> coordinates = new ArrayList<>();
166+
coordinates.add(polygon.coordinates());
167+
return new AutoValue_MultiPolygon(bbox, coordinates);
168+
}
169+
137170
/**
138171
* Create a new instance of this class by defining a list of a list of a list of {@link Point}s
139172
* which follow the correct specifications described in the Point documentation.
@@ -152,14 +185,13 @@ public static MultiPolygon fromLngLats(@NonNull List<List<List<Point>>> points)
152185
* which follow the correct specifications described in the Point documentation.
153186
*
154187
* @param points a list of {@link Point}s which make up the MultiPolygon geometry
155-
* @param bbox optionally include a bbox definition as a double array
188+
* @param bbox optionally include a bbox definition
156189
* @return a new instance of this class defined by the values passed inside this static factory
157190
* method
158191
* @since 3.0.0
159192
*/
160193
public static MultiPolygon fromLngLats(@NonNull List<List<List<Point>>> points,
161194
@Nullable BoundingBox bbox) {
162-
163195
return new AutoValue_MultiPolygon(bbox, points);
164196
}
165197

services-geojson/src/test/java/com/mapbox/geojson/FeatureCollectionTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public void bbox_doesSerializeWhenPresent() throws Exception {
9090
+ "]}");
9191
}
9292

93+
@Test
94+
public void passingInSingleFeature_doesHandleCorrectly() throws Exception {
95+
Point geometry = Point.fromLngLat(1.0, 2.0);
96+
Feature feature = Feature.fromGeometry(geometry);
97+
FeatureCollection geo = FeatureCollection.fromFeature(feature);
98+
assertNotNull(geo.features());
99+
assertEquals(1, geo.features().size());
100+
assertEquals(2.0, ((Point) geo.features().get(0).geometry()).coordinates().get(1), DELTA);
101+
}
102+
93103
@Test
94104
public void fromJson() throws IOException {
95105
final String json = loadJsonFixture(SAMPLE_FEATURECOLLECTION);

services-geojson/src/test/java/com/mapbox/geojson/GeometryCollectionTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public void bbox_returnsCorrectBbox() throws Exception {
7979
assertEquals(4.0, geometryCollection.bbox().north(), DELTA);
8080
}
8181

82+
@Test
83+
public void passingInSingleGeometry_doesHandleCorrectly() throws Exception {
84+
Point geometry = Point.fromLngLat(1.0, 2.0);
85+
GeometryCollection collection = GeometryCollection.fromGeometry(geometry);
86+
assertNotNull(collection);
87+
assertEquals(1, collection.geometries().size());
88+
assertEquals(2.0, ((Point) collection.geometries().get(0)).latitude(), DELTA);
89+
}
90+
8291
@Test
8392
public void bbox_doesSerializeWhenPresent() throws Exception {
8493
List<Point> points = new ArrayList<>();

services-geojson/src/test/java/com/mapbox/geojson/MultiLineStringTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public void bbox_returnsCorrectBbox() throws Exception {
7474
assertEquals(4.0, multiLineString.bbox().north(), DELTA);
7575
}
7676

77+
@Test
78+
public void passingInSingleLineString_doesHandleCorrectly() throws Exception {
79+
List<Point> points = new ArrayList<>();
80+
points.add(Point.fromLngLat(1.0, 2.0));
81+
points.add(Point.fromLngLat(3.0, 4.0));
82+
LineString geometry = LineString.fromLngLats(points);
83+
MultiLineString multiLineString = MultiLineString.fromLineString(geometry);
84+
assertNotNull(multiLineString);
85+
assertEquals(1, multiLineString.lineStrings().size());
86+
assertEquals(2.0, multiLineString.lineStrings().get(0).coordinates().get(0).latitude(), DELTA);
87+
}
88+
7789
@Test
7890
public void bbox_doesSerializeWhenPresent() throws Exception {
7991
List<Point> points = new ArrayList<>();

services-geojson/src/test/java/com/mapbox/geojson/MultiPolygonTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public void bbox_returnsCorrectBbox() throws Exception {
8484
assertEquals(4.0, multiPolygon.bbox().north(), DELTA);
8585
}
8686

87+
@Test
88+
public void passingInSinglePolygon_doesHandleCorrectly() throws Exception {
89+
List<Point> points = new ArrayList<>();
90+
points.add(Point.fromLngLat(1.0, 2.0));
91+
points.add(Point.fromLngLat(3.0, 4.0));
92+
List<List<Point>> pointsList = new ArrayList<>();
93+
pointsList.add(points);
94+
Polygon geometry = Polygon.fromLngLats(pointsList);
95+
MultiPolygon multiPolygon = MultiPolygon.fromPolygon(geometry);
96+
assertNotNull(multiPolygon);
97+
assertEquals(1, multiPolygon.polygons().size());
98+
assertEquals(2.0, multiPolygon.polygons().get(0).coordinates().get(0).get(0).latitude(), DELTA);
99+
}
100+
87101
@Test
88102
public void bbox_doesSerializeWhenPresent() throws Exception {
89103
List<Point> points = new ArrayList<>();

0 commit comments

Comments
 (0)