-
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
329143e
commit aac2a8a
Showing
18 changed files
with
449 additions
and
32 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
66 changes: 66 additions & 0 deletions
66
src/main/java/org/javarosa/core/model/instance/geojson/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,66 @@ | ||
/* | ||
* Copyright 2022 ODK | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.javarosa.core.model.instance.geojson; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import org.javarosa.core.model.instance.TreeElement; | ||
|
||
public class GeoJsonExternalInstance { | ||
public static TreeElement parse(String instanceId, String path) throws IOException { | ||
final TreeElement root = new TreeElement("root", 0); | ||
root.setInstanceName(instanceId); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try (JsonParser jsonParser = objectMapper.getFactory().createParser(new FileInputStream(path))) { | ||
validatePreamble(jsonParser); | ||
|
||
int multiplicity = 0; | ||
while (jsonParser.nextToken() != JsonToken.END_ARRAY) { | ||
GeojsonFeature feature = objectMapper.readValue(jsonParser, GeojsonFeature.class); | ||
|
||
root.addChild(feature.toTreeElement(multiplicity)); | ||
multiplicity++; | ||
} | ||
} | ||
|
||
return root; | ||
} | ||
|
||
private static void validatePreamble(JsonParser jsonParser) throws IOException { | ||
if (jsonParser.nextToken() != JsonToken.START_OBJECT) { | ||
throw new IOException("GeoJSON file must contain a top-level Object"); | ||
} | ||
|
||
jsonParser.nextToken(); | ||
String propertyName = jsonParser.getCurrentName(); | ||
jsonParser.nextToken(); | ||
if (!(propertyName.equals("type") && jsonParser.getValueAsString().equals("FeatureCollection"))) { | ||
throw new IOException("GeoJSON file must contain a top-level FeatureCollection"); | ||
} | ||
|
||
jsonParser.nextToken(); | ||
propertyName = jsonParser.getCurrentName(); | ||
|
||
if (!(propertyName.equals("features") && jsonParser.nextToken() == JsonToken.START_ARRAY)) { | ||
throw new IOException("GeoJSON FeatureCollection must contain an array of features"); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/javarosa/core/model/instance/geojson/GeojsonFeature.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,46 @@ | ||
/* | ||
* Copyright 2022 ODK | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.javarosa.core.model.instance.geojson; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.javarosa.core.model.data.UncastData; | ||
import org.javarosa.core.model.instance.TreeElement; | ||
|
||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) | ||
public class GeojsonFeature { | ||
private String type; | ||
private GeojsonGeometry geometry; | ||
private Map<String, String> properties; | ||
|
||
public TreeElement toTreeElement(int multiplicity) throws IOException { | ||
TreeElement item = new TreeElement("item", multiplicity); | ||
|
||
TreeElement geoField = new TreeElement("geometry", 0); | ||
geoField.setValue(new UncastData(geometry.getOdkCoordinates())); | ||
item.addChild(geoField); | ||
|
||
for (String property : properties.keySet()) { | ||
TreeElement field = new TreeElement(property, 0); | ||
field.setValue(new UncastData(properties.get(property))); | ||
item.addChild(field); | ||
} | ||
|
||
return item; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/javarosa/core/model/instance/geojson/GeojsonGeometry.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,39 @@ | ||
/* | ||
* Copyright 2022 ODK | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.javarosa.core.model.instance.geojson; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) | ||
public class GeojsonGeometry { | ||
private String type; | ||
private ArrayList<String> coordinates; | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public String getOdkCoordinates() throws IOException { | ||
if (!(getType().equals("Point") && coordinates.size() == 2)) { | ||
throw new IOException("Only Points are currently supported"); | ||
} | ||
|
||
return coordinates.get(0) + " " + coordinates.get(1) + " 0 0"; | ||
} | ||
} |
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
96 changes: 96 additions & 0 deletions
96
src/test/java/org/javarosa/core/model/instance/geojson/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,96 @@ | ||
/* | ||
* Copyright 2022 ODK | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.javarosa.core.model.instance.geojson; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.javarosa.test.utils.ResourcePathHelper.r; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.IOException; | ||
import org.javarosa.core.model.instance.TreeElement; | ||
import org.junit.Test; | ||
|
||
public class GeoJsonExternalInstanceTest { | ||
@Test | ||
public void parse_throwsException_ifNoTopLevelObject() { | ||
try { | ||
GeoJsonExternalInstance.parse("id", r("not-object.geojson").toString()); | ||
fail("Exception expected"); | ||
} catch (IOException e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void parse_throwsException_ifTopLevelObjectTypeNotFeatureCollection() { | ||
try { | ||
GeoJsonExternalInstance.parse("id", r("invalid-type.geojson").toString()); | ||
fail("Exception expected"); | ||
} catch (IOException e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void parse_throwsException_ifNoFeaturesArray() { | ||
try { | ||
GeoJsonExternalInstance.parse("id", r("bad-futures-collection.geojson").toString()); | ||
fail("Exception expected"); | ||
} catch (IOException e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void parse_throwsException_ifFeaturesNotArray() { | ||
try { | ||
GeoJsonExternalInstance.parse("id", r("bad-features-not-array.geojson").toString()); | ||
fail("Exception expected"); | ||
} catch (IOException e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void parse_addsGeometriesAsChildren_forMultipleFeatures() throws IOException { | ||
TreeElement featureCollection = GeoJsonExternalInstance.parse("id", r("feature-collection.geojson").toString()); | ||
assertThat(featureCollection.getNumChildren(), is(2)); | ||
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", r("feature-collection.geojson").toString()); | ||
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")); | ||
} | ||
|
||
@Test | ||
public void parse_throwsException_whenGeometryNotPoint() { | ||
try { | ||
GeoJsonExternalInstance.parse("id", r("feature-collection-with-line.geojson").toString()); | ||
fail("Exception expected"); | ||
} catch (IOException e) { | ||
// expected | ||
} | ||
} | ||
} |
Oops, something went wrong.