Commit 698af9f9 authored by Andrea Aime's avatar Andrea Aime
Browse files

[GEOS-9161] Complex attributes with date simple content are not encoded...

[GEOS-9161] Complex attributes with date simple content are not encoded properly by the ComplexJSONBuilder, [GEOS-9160] JSON encoding of 1-d linear geometries fails
parent 5039a709
......@@ -8,7 +8,9 @@ import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.util.Objects;
import net.sf.json.JSON;
......@@ -58,6 +60,13 @@ public final class GeoJsonOutputFormatWfsTest extends AbstractAppSchemaTestSuppo
"/test-data/stations/geoJson/stations.xsd",
"/test-data/stations/geoJson/stations.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");
}
}
......@@ -86,7 +95,7 @@ public final class GeoJsonOutputFormatWfsTest extends AbstractAppSchemaTestSuppo
/** Helper method that station 1 exists and was correctly encoded in the GeoJSON response. */
private void checkStation1Exists(JSON geoJson) {
// get the station from the response
JSONObject station = getStationPropertiesById(geoJson, "st.1");
JSONObject station = getFeaturePropertiesById(geoJson, "st.1");
assertThat(station, notNullValue());
// validate the station name
JSONObject name = station.getJSONObject("name");
......@@ -116,7 +125,7 @@ public final class GeoJsonOutputFormatWfsTest extends AbstractAppSchemaTestSuppo
* Helper method that just extracts \ looks for a station in the provided GeoJSON response based
* on its ID.
*/
private JSONObject getStationPropertiesById(JSON geoJson, String id) {
private JSONObject getFeaturePropertiesById(JSON geoJson, String id) {
assertThat(geoJson, instanceOf(JSONObject.class));
JSONObject json = (JSONObject) geoJson;
JSONArray features = json.getJSONArray("features");
......@@ -130,4 +139,61 @@ public final class GeoJsonOutputFormatWfsTest extends AbstractAppSchemaTestSuppo
// feature matching the provided ID not found
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;
}
}
_=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
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
\ No newline at end of file
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|2015-07-02T00:00:00Z|core diamond|85.3|89.6|purpose|||geochronology|29921|Arthurton Granite|91.55
\ No newline at end of file
......@@ -119,10 +119,10 @@
<value>'rock'</value>
</ClientProperty>
</AttributeMapping>
<AttributeMapping>
<!--AttributeMapping>
<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>
</AttributeMapping>
</AttributeMapping-->
<AttributeMapping>
<targetAttribute>sa:relatedSamplingFeature/sa:SamplingFeatureComplex/sa:relatedSamplingFeature/spec:SF_Specimen/spec:samplingTime/gml:TimeInstant/gml:timePosition</targetAttribute>
<sourceExpression><OCQL>SAMPLING_DATE</OCQL></sourceExpression>
......
......@@ -7,6 +7,7 @@ package org.geoserver.wfs.json;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
......@@ -416,7 +417,10 @@ class ComplexGeoJsonWriter {
return null;
}
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
return value;
}
......
......@@ -179,12 +179,16 @@ public class GeoJSONBuilder extends JSONBuilder {
// adjust the order of X and Y ordinates if needed
if (axisOrder == CRS.AxisOrder.NORTH_EAST) {
// encode latitude first and then longitude
roundedValue(y);
if (!Double.isNaN(y)) { // for 1d linear referencing cases
roundedValue(y);
}
roundedValue(x);
} else {
// encode longitude first and then latitude
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
z = Double.isNaN(z) && !Double.isNaN(m) ? 0 : z;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment