From d48811dbc6e8322d75d22d73e097d2ebc2a61322 Mon Sep 17 00:00:00 2001 From: Benjamin Deininger Date: Fri, 6 Jan 2017 08:59:25 -0700 Subject: [PATCH] DDF-2669 Updated WFS 1.0.0 source to transform non WG84 projections to WG84 (#1523) --- .../spatial-wfs-v1_0_0-converter/pom.xml | 6 + .../impl/TestGenericFeatureConverter.java | 165 ++++++++++-------- ...ideo_data_set.xml => video_data_set_1.xml} | 0 .../src/test/resources/video_data_set_2.xml | 48 +++++ .../wfs/v1_0_0/catalog/source/WfsSource.java | 4 +- .../spatial/wfs/spatial-wfs-converter/pom.xml | 5 + .../impl/AbstractFeatureConverter.java | 39 +++++ .../impl/GenericFeatureConverter.java | 4 + 8 files changed, 201 insertions(+), 70 deletions(-) rename catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/{video_data_set.xml => video_data_set_1.xml} (100%) create mode 100644 catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set_2.xml diff --git a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/pom.xml b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/pom.xml index dfb9f612cb84..d40d461ede5b 100644 --- a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/pom.xml +++ b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/pom.xml @@ -103,6 +103,12 @@ org.codehaus.woodstox woodstox-core-asl + + org.geotools + gt-epsg-hsql + ${org.geotools.version} + test + diff --git a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/converter/impl/TestGenericFeatureConverter.java b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/converter/impl/TestGenericFeatureConverter.java index a389eedae253..3724d2111d97 100644 --- a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/converter/impl/TestGenericFeatureConverter.java +++ b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/converter/impl/TestGenericFeatureConverter.java @@ -13,8 +13,11 @@ **/ package org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.converter.impl; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; import java.io.InputStream; import java.util.ArrayList; @@ -100,62 +103,91 @@ public void testUnmarshalSingleFeatureXmlToObject() { xstream.alias(FEATURE_TYPE, MetacardImpl.class); InputStream is = - TestGenericFeatureConverter.class.getResourceAsStream("/video_data_set.xml"); + TestGenericFeatureConverter.class.getResourceAsStream("/video_data_set_1.xml"); Metacard mc = (Metacard) xstream.fromXML(is); - assertEquals("video_data_set.2", mc.getId()); - assertEquals(FEATURE_TYPE, mc.getContentTypeName()); - assertEquals(metacardType.getName(), - mc.getMetacardType() - .getName()); - assertEquals(SOURCE_ID, mc.getSourceId()); - assertEquals("video_data_set.2", mc.getAttribute(Core.TITLE).getValue()); - - assertEquals(2L, - mc.getAttribute(PROPERTY_PREFIX + ID_ELEMENT) - .getValue()); - assertEquals(Long.valueOf(1L), - mc.getAttribute(PROPERTY_PREFIX + VERSION_ELEMENT) - .getValue()); - assertEquals(DatatypeConverter.parseDateTime("2005-04-07T09:54:38.983") - .getTime(), - mc.getAttribute(PROPERTY_PREFIX + END_DATE_ELEMENT) - .getValue()); - assertEquals("/data/test_suite/video/video/videoFile.mpg", - mc.getAttribute(PROPERTY_PREFIX + FILENAME_ELEMENT) - .getValue()); - assertEquals(720L, - mc.getAttribute(PROPERTY_PREFIX + HEIGHT_ELEMENT) - .getValue()); - assertEquals("a8a55092f0afae881099637ef7746cd8d7066270d9af4cf0f52c41dab53c4005", - mc.getAttribute(PROPERTY_PREFIX + INDEX_ID_ELEMENT) - .getValue()); - assertEquals(getOtherTagsXml(), - mc.getAttribute(PROPERTY_PREFIX + OTHER_TAGS_XML_ELEMENT) - .getValue()); - assertEquals(26L, - mc.getAttribute(PROPERTY_PREFIX + REPOSITORY_ID_ELEMENT) - .getValue()); - assertEquals(DatatypeConverter.parseDateTime("2005-04-07T09:53:39.000") - .getTime(), - mc.getAttribute(PROPERTY_PREFIX + START_DATE_ELEMENT) - .getValue()); - assertEquals(1280L, - mc.getAttribute(PROPERTY_PREFIX + WIDTH_ELEMENT) - .getValue()); - - assertEquals(getLocation(), mc.getAttribute(Core.LOCATION).getValue()); - assertEquals(mc.getLocation(), - mc.getAttribute(PROPERTY_PREFIX + GROUND_GEOM_ELEMENT) - .getValue()); - - assertNotNull(mc.getEffectiveDate()); - assertNotNull(mc.getAttribute(Core.CREATED)); - assertNotNull(mc.getAttribute(Core.MODIFIED)); - - assertNotNull(mc.getContentTypeNamespace()); - assertEquals(mc.getContentTypeNamespace() - .toString(), WfsConstants.NAMESPACE_URN_ROOT + metacardType.getName()); + assertThat(mc.getId(), is("video_data_set.2")); + assertThat(mc.getContentTypeName(), is(FEATURE_TYPE)); + assertThat(mc.getMetacardType().getName(), is(metacardType.getName())); + assertThat(mc.getSourceId(), is(SOURCE_ID)); + assertThat(mc.getAttribute(Core.TITLE).getValue(), is("video_data_set.2")); + assertThat(mc.getAttribute(PROPERTY_PREFIX + ID_ELEMENT).getValue(), is(2L)); + assertThat(mc.getAttribute(PROPERTY_PREFIX + VERSION_ELEMENT).getValue(), is(1L)); + assertThat(mc.getAttribute(PROPERTY_PREFIX + END_DATE_ELEMENT).getValue(), is(DatatypeConverter.parseDateTime("2005-04-07T09:54:38.983").getTime())); + assertThat(mc.getAttribute(PROPERTY_PREFIX + FILENAME_ELEMENT).getValue(), is("/data/test_suite/video/video/videoFile.mpg")); + assertThat(mc.getAttribute(PROPERTY_PREFIX + HEIGHT_ELEMENT).getValue(), is(720L)); + assertThat(mc.getAttribute(PROPERTY_PREFIX + INDEX_ID_ELEMENT).getValue(), is("a8a55092f0afae881099637ef7746cd8d7066270d9af4cf0f52c41dab53c4005")); + assertThat(mc.getAttribute(PROPERTY_PREFIX + OTHER_TAGS_XML_ELEMENT).getValue(), is(getOtherTagsXml())); + assertThat(mc.getAttribute(PROPERTY_PREFIX + REPOSITORY_ID_ELEMENT).getValue(), is(26L)); + assertThat(mc.getAttribute(PROPERTY_PREFIX + START_DATE_ELEMENT).getValue(), is( + DatatypeConverter.parseDateTime("2005-04-07T09:53:39.000") + .getTime())); + assertThat(mc.getAttribute(PROPERTY_PREFIX + WIDTH_ELEMENT).getValue(), is(1280L)); + assertThat(mc.getAttribute(Core.LOCATION).getValue(), is(getLocation())); + assertThat(mc.getAttribute(PROPERTY_PREFIX + GROUND_GEOM_ELEMENT).getValue(), is(mc.getLocation())); + + assertThat(mc.getEffectiveDate(), notNullValue()); + assertThat(mc.getAttribute(Core.CREATED), notNullValue()); + assertThat(mc.getAttribute(Core.MODIFIED), notNullValue()); + + assertThat(mc.getContentTypeNamespace(), notNullValue()); + assertThat(mc.getContentTypeNamespace().toString(), is(WfsConstants.NAMESPACE_URN_ROOT + metacardType.getName())); + } + + @Test + public void testUnmarshalFeatureCollectionNonWGS84XmlToObject() { + XStream xstream = new XStream(new WstxDriver()); + FeatureCollectionConverterWfs10 fcConverter = new FeatureCollectionConverterWfs10(); + Map fcMap = new HashMap<>(); + + GenericFeatureConverter converter = new GenericFeatureConverter("EPSG:26713"); + + fcMap.put("video_data_set", converter); + fcConverter.setFeatureConverterMap(fcMap); + + xstream.registerConverter(fcConverter); + + converter.setMetacardType(buildMetacardType()); + xstream.registerConverter(converter); + xstream.registerConverter(new GmlGeometryConverter()); + xstream.alias("FeatureCollection", WfsFeatureCollection.class); + InputStream is = TestGenericFeatureConverter.class.getResourceAsStream( + "/video_data_set_2.xml"); + + WfsFeatureCollection wfc = (WfsFeatureCollection) xstream.fromXML(is); + assertThat(wfc.getFeatureMembers(), hasSize(1)); + Metacard mc = wfc.getFeatureMembers() + .get(0); + assertThat(mc.getId(), is("video_data_set.1")); + assertThat(mc.getLocation(), is("MULTILINESTRING ((-103.85275410013904 44.48520433037816, -103.85142707963864 44.48544013668279))")); + } + + @Test + public void testUnmarshalFeatureCollectionUnsupportedProjectionXmlToObject() { + XStream xstream = new XStream(new WstxDriver()); + FeatureCollectionConverterWfs10 fcConverter = new FeatureCollectionConverterWfs10(); + Map fcMap = new HashMap<>(); + + GenericFeatureConverter converter = new GenericFeatureConverter("CUSTOM UNSUPPORTED PROJECTION"); + + fcMap.put("video_data_set", converter); + fcConverter.setFeatureConverterMap(fcMap); + + xstream.registerConverter(fcConverter); + + converter.setMetacardType(buildMetacardType()); + xstream.registerConverter(converter); + xstream.registerConverter(new GmlGeometryConverter()); + xstream.alias("FeatureCollection", WfsFeatureCollection.class); + InputStream is = TestGenericFeatureConverter.class.getResourceAsStream( + "/video_data_set_2.xml"); + + WfsFeatureCollection wfc = (WfsFeatureCollection) xstream.fromXML(is); + assertThat(wfc.getFeatureMembers(), hasSize(1)); + Metacard mc = wfc.getFeatureMembers() + .get(0); + assertThat(mc.getId(), is("video_data_set.1")); + assertThat(mc.getLocation(), nullValue()); } @Test @@ -179,12 +211,9 @@ public void testUnmarshalFeatureCollectionXmlToObject() { "/video_data_set_collection.xml"); WfsFeatureCollection wfc = (WfsFeatureCollection) xstream.fromXML(is); - assertEquals(4, - wfc.getFeatureMembers() - .size()); - Metacard mc = wfc.getFeatureMembers() - .get(0); - assertEquals(mc.getId(), "video_data_set.1"); + assertThat(wfc.getFeatureMembers(), hasSize(4)); + Metacard mc = wfc.getFeatureMembers().get(0); + assertThat(mc.getId(), is("video_data_set.1")); } @@ -195,9 +224,9 @@ public void testUnmarshalNoMetacardTypeRegisteredInConverter() throws Throwable xstream.registerConverter(new GmlGeometryConverter()); xstream.alias(FEATURE_TYPE, Metacard.class); InputStream is = - TestGenericFeatureConverter.class.getResourceAsStream("/video_data_set.xml"); + TestGenericFeatureConverter.class.getResourceAsStream("/video_data_set_1.xml"); try { - WfsFeatureCollection wfs = (WfsFeatureCollection) xstream.fromXML(is); + xstream.fromXML(is); } catch (Exception e) { throw e.getCause(); } @@ -242,7 +271,7 @@ public AttributeDescriptor getAttributeDescriptor(String arg0) { wfc.getFeatureMembers() .add(mc2); - String xml = xstream.toXML(wfc); + xstream.toXML(wfc); } @Test @@ -253,7 +282,7 @@ public void testReadCdata() { String xml = ""; String results = (String) xstream.fromXML(xml); - assertEquals(contents, results); + assertThat(contents, is(results)); } private MetacardType buildMetacardType() { @@ -264,13 +293,13 @@ private MetacardType buildMetacardType() { return new FeatureMetacardType(schema, new QName(FEATURE_TYPE), - new ArrayList(), + new ArrayList<>(), Wfs10Constants.GML_NAMESPACE); } private Map buildElementMap(XmlSchema schema) { - Map elementMap = new HashMap(); + Map elementMap = new HashMap<>(); elementMap.put(new QName(ID_ELEMENT), buildSchemaElement(ID_ELEMENT, schema, Constants.XSD_LONG)); elementMap.put(new QName(VERSION_ELEMENT), diff --git a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set.xml b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set_1.xml similarity index 100% rename from catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set.xml rename to catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set_1.xml diff --git a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set_2.xml b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set_2.xml new file mode 100644 index 000000000000..b1200cc684ed --- /dev/null +++ b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-converter/src/test/resources/video_data_set_2.xml @@ -0,0 +1,48 @@ + + + + + unknown + + + + 2 + 1 + 2005-04-07T09:54:38.983 + /data/test_suite/video/video/videoFile.mpg + 720 + a8a55092f0afae881099637ef7746cd8d7066270d9af4cf0f52c41dab53c4005 + metadata goes here...]]> + 26 + 2005-04-07T09:53:39.000 + 1280 + + + + + 591259.19017609,4926203.43361179 591364.34430618,4926231.10769025 + + + + + + + \ No newline at end of file diff --git a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-source/src/main/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/source/WfsSource.java b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-source/src/main/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/source/WfsSource.java index 77f178196e23..202d42f1a979 100644 --- a/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-source/src/main/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/source/WfsSource.java +++ b/catalog/spatial/wfs/1.0.0/spatial-wfs-v1_0_0-source/src/main/java/org/codice/ddf/spatial/ogc/wfs/v1_0_0/catalog/source/WfsSource.java @@ -562,14 +562,14 @@ private void buildFeatureFilters(List featureTypes, List + + org.codice.thirdparty + geotools-suite + ${org.geotools.bundle.version} + commons-codec commons-codec diff --git a/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/AbstractFeatureConverter.java b/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/AbstractFeatureConverter.java index 0f243bea7b50..7dd35f3b8b6c 100644 --- a/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/AbstractFeatureConverter.java +++ b/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/AbstractFeatureConverter.java @@ -44,8 +44,18 @@ import org.apache.commons.lang.StringUtils; import org.codice.ddf.spatial.ogc.catalog.common.converter.XmlNode; import org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType; +import org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsConstants; import org.codice.ddf.spatial.ogc.wfs.catalog.converter.FeatureConverter; import org.codice.ddf.spatial.ogc.wfs.catalog.mapper.MetacardMapper; +import org.geotools.factory.Hints; +import org.geotools.geometry.jts.JTS; +import org.geotools.referencing.CRS; +import org.geotools.referencing.ReferencingFactoryFinder; +import org.opengis.referencing.FactoryException; +import org.opengis.referencing.crs.CRSAuthorityFactory; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.referencing.operation.MathTransform; +import org.opengis.referencing.operation.TransformException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; @@ -99,6 +109,8 @@ public abstract class AbstractFeatureConverter implements FeatureConverter { private MetacardMapper metacardMapper = null; + private String srs = WfsConstants.EPSG_4326; + public AbstractFeatureConverter() { } @@ -281,6 +293,9 @@ protected Serializable getValueForMetacardAttribute(AttributeFormat attributeFor Geometry geo = null; try { geo = gmlReader.read(xml, null); + if (StringUtils.isNotBlank(srs) && !srs.equals(WfsConstants.EPSG_4326)) { + geo = transformToEPSG4326(geo); + } } catch (SAXException | IOException | ParserConfigurationException e) { LOGGER.debug(ERROR_PARSING_MESSAGE, e); } @@ -307,6 +322,26 @@ protected Serializable getValueForMetacardAttribute(AttributeFormat attributeFor } + private Geometry transformToEPSG4326(Geometry geometry) { + Geometry transformedGeometry = null; + try { + Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE); + CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", + hints); + CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem( + WfsConstants.EPSG_4326); + + CoordinateReferenceSystem sourceCRS = CRS.decode(srs); + + MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS); + transformedGeometry = JTS.transform(geometry, transform); + LOGGER.debug("Converted CRS {} into {} : {}", srs, WfsConstants.EPSG_4326, geometry); + } catch (FactoryException | TransformException e) { + LOGGER.debug("Unable to convert {} into {}", srs, WfsConstants.EPSG_4326, e); + } + return transformedGeometry; + } + private String convertToBytes(HierarchicalStreamReader reader, String unit) { BigDecimal resourceSize = new BigDecimal(reader.getValue()); @@ -406,4 +441,8 @@ private boolean isBasicMetacardAttribute(String attrName) { return basicAttributeNames.contains(attrName); } + public void setSrs(String srs) { + this.srs = srs; + } + } diff --git a/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/GenericFeatureConverter.java b/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/GenericFeatureConverter.java index cafb364151be..4f7063179a7f 100644 --- a/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/GenericFeatureConverter.java +++ b/catalog/spatial/wfs/spatial-wfs-converter/src/main/java/org/codice/ddf/spatial/ogc/wfs/catalog/converter/impl/GenericFeatureConverter.java @@ -64,6 +64,10 @@ public GenericFeatureConverter() { } + public GenericFeatureConverter(String srs) { + this.setSrs(srs); + } + public GenericFeatureConverter(MetacardMapper metacardMapper) { super(metacardMapper); }