Skip to content

Commit d773b9a

Browse files
authored
Merge pull request #17 from otbutz/populate_id
Populate id field
2 parents 5ff1520 + 9df1b8d commit d773b9a

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

generator/src/main/java/de/westnordost/countryboundaries/GeoJsonWriter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class GeoJsonWriter
2020
{
2121
private static final String
2222
TYPE = "type",
23+
ID = "id",
2324
FEATURES = "features",
2425
COORDINATES = "coordinates",
2526
GEOMETRIES = "geometries",
@@ -69,6 +70,14 @@ private void writeFeature(JSONStringer b, Geometry g) throws JSONException
6970
{
7071
b.object();
7172
b.key(TYPE).value("Feature");
73+
if (g.getUserData() instanceof Map<?,?> && ((Map<?,?>) g.getUserData()).containsKey(ID))
74+
{
75+
Object id = ((Map<?, ?>) g.getUserData()).get(ID);
76+
if (id instanceof String || id instanceof Number)
77+
{
78+
b.key(ID).value(id);
79+
}
80+
}
7281
b.key(PROPERTIES);
7382
writeProperties(b, g.getUserData());
7483
b.key(GEOMETRY);

generator/src/test/java/de/westnordost/countryboundaries/GeoJsonWriterTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,34 @@ public class GeoJsonWriterTest
205205
"}]}", write(g));
206206
}
207207

208+
@Test public void writeFeatureWithStringId()
209+
{
210+
Polygon g = factory.createPolygon(
211+
factory.createLinearRing(new Coordinate[]{p(0,0), p(0,4), p(4,0), p(0,0)}),
212+
new LinearRing[]{factory.createLinearRing(new Coordinate[] {p(1,1), p(2,1), p(1,2), p(1,1)})}
213+
);
214+
g.setUserData(Collections.singletonMap("id", "DE"));
215+
assertEquals("{" +
216+
"\"type\":\"Feature\",\"id\":\"DE\",\"properties\":{\"id\":\"DE\"}," +
217+
"\"geometry\":{\"type\":\"Polygon\"," +
218+
"\"coordinates\":[[[0,0],[4,0],[0,4],[0,0]],[[1,1],[1,2],[2,1],[1,1]]]}" +
219+
"}", write(g));
220+
}
221+
222+
@Test public void writeFeatureWithNumericId()
223+
{
224+
Polygon g = factory.createPolygon(
225+
factory.createLinearRing(new Coordinate[]{p(0,0), p(0,4), p(4,0), p(0,0)}),
226+
new LinearRing[]{factory.createLinearRing(new Coordinate[] {p(1,1), p(2,1), p(1,2), p(1,1)})}
227+
);
228+
g.setUserData(Collections.singletonMap("id", 123));
229+
assertEquals("{" +
230+
"\"type\":\"Feature\",\"id\":123,\"properties\":{\"id\":123}," +
231+
"\"geometry\":{\"type\":\"Polygon\"," +
232+
"\"coordinates\":[[[0,0],[4,0],[0,4],[0,0]],[[1,1],[1,2],[2,1],[1,1]]]}" +
233+
"}", write(g));
234+
}
235+
208236
private static String write(Geometry g)
209237
{
210238
return new GeoJsonWriter().write(g);

0 commit comments

Comments
 (0)