Skip to content

Commit

Permalink
DDF-2669 Updated WFS 1.0.0 source to transform non WG84 projections t…
Browse files Browse the repository at this point in the history
…o WG84 (codice#1523)
  • Loading branch information
bdeining authored and kcwire committed Jan 6, 2017
1 parent b7518ef commit d48811d
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${org.geotools.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, FeatureConverter> 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<String, FeatureConverter> 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
Expand All @@ -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"));

}

Expand All @@ -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();
}
Expand Down Expand Up @@ -242,7 +271,7 @@ public AttributeDescriptor getAttributeDescriptor(String arg0) {
wfc.getFeatureMembers()
.add(mc2);

String xml = xstream.toXML(wfc);
xstream.toXML(wfc);
}

@Test
Expand All @@ -253,7 +282,7 @@ public void testReadCdata() {
String xml = "<string><![CDATA[" + contents + "]]></string>";
String results = (String) xstream.fromXML(xml);

assertEquals(contents, results);
assertThat(contents, is(results));
}

private MetacardType buildMetacardType() {
Expand All @@ -264,13 +293,13 @@ private MetacardType buildMetacardType() {

return new FeatureMetacardType(schema,
new QName(FEATURE_TYPE),
new ArrayList<String>(),
new ArrayList<>(),
Wfs10Constants.GML_NAMESPACE);

}

private Map<QName, XmlSchemaElement> buildElementMap(XmlSchema schema) {
Map<QName, XmlSchemaElement> elementMap = new HashMap<QName, XmlSchemaElement>();
Map<QName, XmlSchemaElement> elementMap = new HashMap<>();
elementMap.put(new QName(ID_ELEMENT),
buildSchemaElement(ID_ELEMENT, schema, Constants.XSD_LONG));
elementMap.put(new QName(VERSION_ELEMENT),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
/**
* Copyright (c) Codice Foundation
*
* This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either
* version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details. A copy of the GNU Lesser General Public License is distributed along with this program and can be found at
* <http://www.gnu.org/licenses/lgpl.html>.
*
**/
-->
<wfs:FeatureCollection
xmlns:vid='http://example.com'
xmlns:wfs='http://www.opengis.net/wfs'
xmlns:gml='http://www.opengis.net/gml'
xmlns='http://www.opengis.net/wfs'
>
<gml:boundedBy>
<gml:null>unknown</gml:null>
</gml:boundedBy>
<gml:featureMember>
<vid:video_data_set xmlns:vid='http://example.com' xmlns:gml='http://www.opengis.net/gml'
fid='video_data_set.1'>
<vid:id>2</vid:id>
<vid:version>1</vid:version>
<vid:end_date>2005-04-07T09:54:38.983</vid:end_date>
<vid:filename>/data/test_suite/video/video/videoFile.mpg</vid:filename>
<vid:height>720</vid:height>
<vid:index_id>a8a55092f0afae881099637ef7746cd8d7066270d9af4cf0f52c41dab53c4005</vid:index_id>
<vid:other_tags_xml><![CDATA[<metadata>metadata goes here...</metadata>]]></vid:other_tags_xml>
<vid:repository_id>26</vid:repository_id>
<vid:start_date>2005-04-07T09:53:39.000</vid:start_date>
<vid:width>1280</vid:width>
<vid:ground_geom>
<gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#26713">
<gml:lineStringMember>
<gml:LineString>
<gml:coordinates decimal="." cs="," ts=" ">591259.19017609,4926203.43361179 591364.34430618,4926231.10769025</gml:coordinates>
</gml:LineString>
</gml:lineStringMember>
</gml:MultiLineString>
</vid:ground_geom>
</vid:video_data_set>
</gml:featureMember>
</wfs:FeatureCollection>
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,14 @@ private void buildFeatureFilters(List<FeatureTypeType> featureTypes, List<String
"WfsSource {}: Unable to find a feature specific converter; {} will be converted using the GenericFeatureConverter",
getId(),
ftName);
featureConverter = new GenericFeatureConverter();
featureConverter = new GenericFeatureConverter(featureTypeType.getSRS());
}
} else {
LOGGER.debug(
"WfsSource {}: Unable to find a feature specific converter; {} will be converted using the GenericFeatureConverter",
getId(),
ftName);
featureConverter = new GenericFeatureConverter();
featureConverter = new GenericFeatureConverter(featureTypeType.getSRS());

}
featureConverter.setSourceId(getId());
Expand Down
5 changes: 5 additions & 0 deletions catalog/spatial/wfs/spatial-wfs-converter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.codice.thirdparty</groupId>
<artifactId>geotools-suite</artifactId>
<version>${org.geotools.bundle.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,6 +109,8 @@ public abstract class AbstractFeatureConverter implements FeatureConverter {

private MetacardMapper metacardMapper = null;

private String srs = WfsConstants.EPSG_4326;

public AbstractFeatureConverter() {

}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
Expand Down Expand Up @@ -406,4 +441,8 @@ private boolean isBasicMetacardAttribute(String attrName) {
return basicAttributeNames.contains(attrName);
}

public void setSrs(String srs) {
this.srs = srs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public GenericFeatureConverter() {

}

public GenericFeatureConverter(String srs) {
this.setSrs(srs);
}

public GenericFeatureConverter(MetacardMapper metacardMapper) {
super(metacardMapper);
}
Expand Down

0 comments on commit d48811d

Please sign in to comment.