-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b4dd89
commit 5b34936
Showing
8 changed files
with
239 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/javarosa/core/model/instance/GeoJsonExternalInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.javarosa.core.model.instance; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import org.javarosa.core.model.data.UncastData; | ||
import org.javarosa.core.util.GeoUtils; | ||
|
||
public class GeoJsonExternalInstance { | ||
public static TreeElement parse(String instanceId, String path) throws IOException { | ||
return parse(instanceId, new FileInputStream(path)); | ||
} | ||
|
||
protected static TreeElement parse(String instanceId, InputStream geojsonStream) throws IOException { | ||
final TreeElement root = new TreeElement("root", 0); | ||
root.setInstanceName(instanceId); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode geoJsonTree = objectMapper.readTree(geojsonStream); | ||
|
||
if (!geoJsonTree.get("type").asText().equals("FeatureCollection")) { | ||
throw new IOException("GeoJSON file must contain a top-level FeatureCollection"); | ||
} | ||
|
||
JsonNode features = geoJsonTree.get("features"); | ||
int multiplicity = 0; | ||
|
||
for (JsonNode feature : features) { | ||
root.addChild(toTreeElement(feature, multiplicity)); | ||
multiplicity++; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
private static TreeElement toTreeElement(JsonNode feature, int multiplicity) throws IOException { | ||
TreeElement item = new TreeElement("item", multiplicity); | ||
|
||
String geometry = GeoUtils.geojsonToOdkGeometry(feature.get("geometry")); | ||
TreeElement geoField = new TreeElement("geometry", 0); | ||
geoField.setValue(new UncastData(geometry)); | ||
item.addChild(geoField); | ||
|
||
JsonNode properties = feature.get("properties"); | ||
|
||
for (Iterator<Map.Entry<String, JsonNode>> i = properties.fields(); i.hasNext();) { | ||
Map.Entry<String, JsonNode> propertyEntry = i.next(); | ||
TreeElement field = new TreeElement(propertyEntry.getKey(), 0); | ||
field.setValue(new UncastData(propertyEntry.getValue().asText())); | ||
item.addChild(field); | ||
} | ||
|
||
return item; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/test/java/org/javarosa/core/model/instance/GeoJsonExternalInstanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.javarosa.core.model.instance; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.Test; | ||
|
||
public class GeoJsonExternalInstanceTest { | ||
@Test | ||
public void parse_throwsException_ifTopLevelElementNotFeatureCollection() { | ||
try { | ||
GeoJsonExternalInstance.parse("id", new ByteArrayInputStream(GEOMETRY_COLLECTION_GEOJSON.getBytes(StandardCharsets.UTF_8))); | ||
fail("Exception expected"); | ||
} catch (Exception e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void parse_parsesMultipleFeatures() throws IOException { | ||
TreeElement featureCollection = GeoJsonExternalInstance.parse("id", new ByteArrayInputStream(FEATURE_COLLECTION_GEOJSON.getBytes(StandardCharsets.UTF_8))); | ||
assertThat(featureCollection.getNumChildren(), is(2)); | ||
} | ||
|
||
@Test | ||
public void parse_addsGeometryAsChild() throws IOException { | ||
TreeElement featureCollection = GeoJsonExternalInstance.parse("id", new ByteArrayInputStream(FEATURE_COLLECTION_GEOJSON.getBytes(StandardCharsets.UTF_8))); | ||
assertThat(featureCollection.getChildAt(0).getChild("geometry", 0).getValue().getValue(), is("102 0.5 0 0")); | ||
assertThat(featureCollection.getChildAt(1).getChild("geometry", 0).getValue().getValue(), is("104 0.5 0 0")); | ||
} | ||
|
||
@Test | ||
public void parse_addsAllOtherPropertiesAsChildren() throws IOException { | ||
TreeElement featureCollection = GeoJsonExternalInstance.parse("id", new ByteArrayInputStream(FEATURE_COLLECTION_GEOJSON.getBytes(StandardCharsets.UTF_8))); | ||
assertThat(featureCollection.getChildAt(0).getNumChildren(), is(4)); | ||
assertThat(featureCollection.getChildAt(0).getChild("name", 0).getValue().getValue(), is("My cool point")); | ||
|
||
assertThat(featureCollection.getChildAt(1).getNumChildren(), is(5)); | ||
assertThat(featureCollection.getChildAt(1).getChild("special-property", 0).getValue().getValue(), is("special value")); | ||
} | ||
|
||
private static String GEOMETRY_COLLECTION_GEOJSON = "{\n" + | ||
" \"geometries\": [\n" + | ||
" {\n" + | ||
" \"coordinates\": [\n" + | ||
" [\n" + | ||
" 10.0,\n" + | ||
" 11.2\n" + | ||
" ],\n" + | ||
" [\n" + | ||
" 10.5,\n" + | ||
" 11.9\n" + | ||
" ]\n" + | ||
" ],\n" + | ||
" \"type\": \"Linestring\"\n" + | ||
" },\n" + | ||
" {\n" + | ||
" \"coordinates\": [\n" + | ||
" 10.0,\n" + | ||
" 20.0\n" + | ||
" ],\n" + | ||
" \"type\": \"Point\"\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"type\": \"GeometryCollection\"\n" + | ||
"}"; | ||
|
||
private static String FEATURE_COLLECTION_GEOJSON = "{\n" + | ||
" \"type\": \"FeatureCollection\",\n" + | ||
" \"features\": [\n" + | ||
" {\n" + | ||
" \"type\": \"Feature\",\n" + | ||
" \"geometry\": {\n" + | ||
" \"type\": \"Point\",\n" + | ||
" \"coordinates\": [\n" + | ||
" 102,\n" + | ||
" 0.5\n" + | ||
" ]\n" + | ||
" },\n" + | ||
" \"properties\": {\n" + | ||
" \"id\": \"fs87b\",\n" + | ||
" \"name\": \"My cool point\",\n" + | ||
" \"foo\": \"bar\"\n" + | ||
" }\n" + | ||
" },\n" + | ||
" {\n" + | ||
" \"type\": \"Feature\",\n" + | ||
" \"geometry\": {\n" + | ||
" \"type\": \"Point\",\n" + | ||
" \"coordinates\": [\n" + | ||
" 104,\n" + | ||
" 0.5\n" + | ||
" ]\n" + | ||
" },\n" + | ||
" \"properties\": {\n" + | ||
" \"id\": \"67abie\",\n" + | ||
" \"name\": \"Your cool point\",\n" + | ||
" \"foo\": \"quux\",\n" + | ||
" \"special-property\": \"special value\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" ]\n" + | ||
"}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters