Skip to content

Commit

Permalink
[GEOS-9161] Complex attributes with date simple content are not encod…
Browse files Browse the repository at this point in the history
…ed properly by the ComplexJSONBuilder, [GEOS-9160] JSON encoding of 1-d linear geometries fails
  • Loading branch information
aaime committed Mar 25, 2019
1 parent bd8d293 commit d5fc101
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;


import java.util.Objects; import java.util.Objects;
import net.sf.json.JSON; import net.sf.json.JSON;
Expand Down Expand Up @@ -58,6 +60,13 @@ public void addContent() {
"/test-data/stations/geoJson/stations.xsd", "/test-data/stations/geoJson/stations.xsd",
"/test-data/stations/geoJson/stations.properties", "/test-data/stations/geoJson/stations.properties",
"/test-data/stations/geoJson/measurements.properties"); "/test-data/stations/geoJson/measurements.properties");
// add borehole
new Gsml32BoreholeMockData().getNamespaces().forEach((k, v) -> putNamespace(k, v));
addFeatureType(
Gsml32BoreholeMockData.GSMLBH_PREFIX,
"Borehole",
"Gsml32Borehole.xml",
"Gsml32Borehole.properties");
} }
} }


Expand Down Expand Up @@ -86,7 +95,7 @@ public void testGetGeoJsonResponseWfs20() throws Exception {
/** Helper method that station 1 exists and was correctly encoded in the GeoJSON response. */ /** Helper method that station 1 exists and was correctly encoded in the GeoJSON response. */
private void checkStation1Exists(JSON geoJson) { private void checkStation1Exists(JSON geoJson) {
// get the station from the response // get the station from the response
JSONObject station = getStationPropertiesById(geoJson, "st.1"); JSONObject station = getFeaturePropertiesById(geoJson, "st.1");
assertThat(station, notNullValue()); assertThat(station, notNullValue());
// validate the station name // validate the station name
JSONObject name = station.getJSONObject("name"); JSONObject name = station.getJSONObject("name");
Expand Down Expand Up @@ -116,7 +125,7 @@ private void checkStation1Exists(JSON geoJson) {
* Helper method that just extracts \ looks for a station in the provided GeoJSON response based * Helper method that just extracts \ looks for a station in the provided GeoJSON response based
* on its ID. * on its ID.
*/ */
private JSONObject getStationPropertiesById(JSON geoJson, String id) { private JSONObject getFeaturePropertiesById(JSON geoJson, String id) {
assertThat(geoJson, instanceOf(JSONObject.class)); assertThat(geoJson, instanceOf(JSONObject.class));
JSONObject json = (JSONObject) geoJson; JSONObject json = (JSONObject) geoJson;
JSONArray features = json.getJSONArray("features"); JSONArray features = json.getJSONArray("features");
Expand All @@ -130,4 +139,61 @@ private JSONObject getStationPropertiesById(JSON geoJson, String id) {
// feature matching the provided ID not found // feature matching the provided ID not found
return null; return null;
} }

@Test
public void testSimpleContentTimeEncoding() throws Exception {
String path = "wfs?request=GetFeature&typename=gsmlbh:Borehole&outputFormat=json";
JSON json = getAsJSON(path);
print(json);
JSONObject properties = getFeaturePropertiesById(json, "borehole.GA.17322");
assertThat(properties, is(notNullValue()));
JSONObject timeInstant =
getNestedObject(
properties,
"relatedSamplingFeature",
"SamplingFeatureComplex",
"relatedSamplingFeature",
"SF_Specimen",
"samplingTime",
"TimeInstant");
assertThat(timeInstant.getString("timePosition"), is("2014-07-02T00:00:00Z"));
}

@Test
public void testOneDimensionalEncoding() throws Exception {
String path = "wfs?request=GetFeature&typename=gsmlbh:Borehole&outputFormat=json";
JSON json = getAsJSON(path);
print(json);
JSONObject properties = getFeaturePropertiesById(json, "borehole.GA.17322");
assertThat(properties, is(notNullValue()));
JSONObject samplingLocation =
getNestedObject(
properties,
"relatedSamplingFeature",
"SamplingFeatureComplex",
"relatedSamplingFeature",
"SF_Specimen",
"samplingLocation",
"value");
JSONArray coordinates = samplingLocation.getJSONArray("coordinates");
assertThat(coordinates.size(), is(2));
JSONArray c1 = coordinates.getJSONArray(0);
assertThat(c1.size(), is(1));
assertEquals(57.9, c1.getDouble(0), 0.1);
JSONArray c2 = coordinates.getJSONArray(1);
assertThat(c2.size(), is(1));
assertEquals(66.5, c2.getDouble(0), 0.1);
}

/** Drills into nested JSON objects (won't traverse arrays though) */
private JSONObject getNestedObject(JSONObject root, String... keys) {
JSONObject curr = root;
for (String key : keys) {
if (!curr.has(key)) {
fail("Could not find property " + key + " in " + curr);
}
curr = curr.getJSONObject(key);
}
return curr;
}
} }
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,3 @@
_=ID:String,BOREHOLE_ID:String,GEOM:Geometry,SAMPLE_ID:String,SAMPLE_DATA_ID:String,SAMPLING_DATE:Date,SAMPLE_TYPE:String,INTERVAL_BEGIN:Double,INTERVAL_END:Double,PROPERTY:String,NUMERIC_VALUE:Double,UNIT_OF_MEASURE:String,TEXT_VALUE:String,STRATNO:String,STRATNAME:String,TOTAL_DEPTH:double _=ID:String,BOREHOLE_ID:String,GEOM:Geometry,SAMPLE_ID:String,SAMPLE_DATA_ID:String,SAMPLING_DATE:Date,SAMPLE_TYPE:String,INTERVAL_BEGIN:Double,INTERVAL_END:Double,PROPERTY:String,NUMERIC_VALUE:Double,UNIT_OF_MEASURE:String,TEXT_VALUE:String,STRATNO:String,STRATNAME:String,TOTAL_DEPTH:double
17322=17322|borehole.GA.17322|POINT(-28.4139 121.142)|100|2251225|14-Jul-02|core diamond|57.9|66.4|magnetic susceptibility|940|10-5 SI||29921|Arthurton Granite|153.92 17322=17322|borehole.GA.17322|POINT(-28.4139 121.142)|100|2251225|2014-07-02T00:00:00Z|core diamond|57.9|66.4|magnetic susceptibility|940|10-5 SI||29921|Arthurton Granite|153.92
17338=17338|borehole.GA.17338|POINT(-12.15 100.142)|102|128|15-Jul-02|core diamond|85.3|89.6|purpose|||geochronology|29921|Arthurton Granite|91.55 17338=17338|borehole.GA.17338|POINT(-12.15 100.142)|102|128|2015-07-02T00:00:00Z|core diamond|85.3|89.6|purpose|||geochronology|29921|Arthurton Granite|91.55
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
<value>'rock'</value> <value>'rock'</value>
</ClientProperty> </ClientProperty>
</AttributeMapping> </AttributeMapping>
<AttributeMapping> <!--AttributeMapping>
<targetAttribute>sa:relatedSamplingFeature/sa:SamplingFeatureComplex/sa:relatedSamplingFeature/spec:SF_Specimen/spec:samplingTime/gml:TimeInstant</targetAttribute> <targetAttribute>sa:relatedSamplingFeature/sa:SamplingFeatureComplex/sa:relatedSamplingFeature/spec:SF_Specimen/spec:samplingTime/gml:TimeInstant</targetAttribute>
<idExpression><OCQL>strConcat('borehole.specimen.samplingTime.GA.', SAMPLE_ID)</OCQL></idExpression> <idExpression><OCQL>strConcat('borehole.specimen.samplingTime.GA.', SAMPLE_ID)</OCQL></idExpression>
</AttributeMapping> </AttributeMapping-->
<AttributeMapping> <AttributeMapping>
<targetAttribute>sa:relatedSamplingFeature/sa:SamplingFeatureComplex/sa:relatedSamplingFeature/spec:SF_Specimen/spec:samplingTime/gml:TimeInstant/gml:timePosition</targetAttribute> <targetAttribute>sa:relatedSamplingFeature/sa:SamplingFeatureComplex/sa:relatedSamplingFeature/spec:SF_Specimen/spec:samplingTime/gml:TimeInstant/gml:timePosition</targetAttribute>
<sourceExpression><OCQL>SAMPLING_DATE</OCQL></sourceExpression> <sourceExpression><OCQL>SAMPLING_DATE</OCQL></sourceExpression>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -416,7 +417,10 @@ private Object getSimpleContent(ComplexAttribute property) {
return null; return null;
} }
Object value = simpleContent.getValue(); Object value = simpleContent.getValue();
if (value instanceof Number || value instanceof String || value instanceof Character) { if (value instanceof Number
|| value instanceof String
|| value instanceof Character
|| value instanceof Date) {
// the extract value is a simple Java type // the extract value is a simple Java type
return value; return value;
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -179,12 +179,16 @@ private JSONBuilder writeCoordinate(double x, double y, double z, double m) {
// adjust the order of X and Y ordinates if needed // adjust the order of X and Y ordinates if needed
if (axisOrder == CRS.AxisOrder.NORTH_EAST) { if (axisOrder == CRS.AxisOrder.NORTH_EAST) {
// encode latitude first and then longitude // encode latitude first and then longitude
roundedValue(y); if (!Double.isNaN(y)) { // for 1d linear referencing cases
roundedValue(y);
}
roundedValue(x); roundedValue(x);
} else { } else {
// encode longitude first and then latitude // encode longitude first and then latitude
roundedValue(x); roundedValue(x);
roundedValue(y); if (!Double.isNaN(y)) { // for 1d linear referencing cases
roundedValue(y);
}
} }
// if Z value is not available but we have a measure, we set Z value to zero // if Z value is not available but we have a measure, we set Z value to zero
z = Double.isNaN(z) && !Double.isNaN(m) ? 0 : z; z = Double.isNaN(z) && !Double.isNaN(m) ? 0 : z;
Expand Down

0 comments on commit d5fc101

Please sign in to comment.