use of org.geotools.xml.Parser in project ddf by codice.
the class GeospatialEvaluator method buildGeometry.
public static Geometry buildGeometry(String gmlText) throws IOException, SAXException, ParserConfigurationException {
String methodName = "buildGeometry";
LOGGER.trace(ENTERING_STR, methodName);
Geometry geometry = null;
gmlText = supportSRSName(gmlText);
try {
LOGGER.debug("Creating geoTools Configuration ...");
Configuration config = new org.geotools.gml3.GMLConfiguration();
LOGGER.debug("Parsing geoTools configuration");
Parser parser = new Parser(config);
LOGGER.debug("Parsing gmlText");
geometry = (Geometry) (parser.parse(new StringReader(gmlText)));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("geometry (before conversion): {}", geometry.toText());
}
// The metadata schema states that <gml:pos> elements specify points in
// LAT,LON order. But WKT specifies points in LON,LAT order. When the geoTools
// libraries return the geometry data, it's WKT is in LAT,LON order (which is
// incorrect).
// As a workaround here, for Polygons and Points (which are currently the only spatial
// criteria supported) we must swap the x,y of each coordinate so that they are
// specified in LON,LAT order and then use the swapped coordinates to create a new
// Polygon or Point to be returned to the caller.
GeometryFactory geometryFactory = new GeometryFactory();
if (geometry instanceof Polygon) {
// Build new array of coordinates using the swapped coordinates
ArrayList<Coordinate> newCoords = new ArrayList<Coordinate>();
// Swap each coordinate's x,y so that they specify LON,LAT order
for (Coordinate coord : geometry.getCoordinates()) {
newCoords.add(new Coordinate(coord.y, coord.x));
}
// Create a new polygon using the swapped coordinates
Polygon polygon = new Polygon(geometryFactory.createLinearRing(newCoords.toArray(new Coordinate[newCoords.size()])), null, geometryFactory);
if (LOGGER.isDebugEnabled()) {
// this logs the transformed WKT
LOGGER.debug("Translates to {}", polygon.toText());
// with LON,LAT ordered points
LOGGER.trace(EXITING_STR, methodName);
}
return polygon;
}
if (geometry instanceof Point) {
// Create a new point using the swapped coordinates that specify LON,LAT order
Point point = geometryFactory.createPoint(new Coordinate(geometry.getCoordinate().y, geometry.getCoordinate().x));
if (LOGGER.isDebugEnabled()) {
// this logs the transformed WKT
LOGGER.debug("Translates to {}", point.toText());
// with a LON,LAT ordered point
LOGGER.trace(EXITING_STR, methodName);
}
return point;
}
} catch (Exception e) {
LOGGER.debug("Exception using geotools", e);
}
LOGGER.debug("No translation done for geometry - probably not good ...");
LOGGER.trace(EXITING_STR, methodName);
return geometry;
}
use of org.geotools.xml.Parser in project ddf by codice.
the class CswQueryFactory method parseJaxB.
private Object parseJaxB(JAXBElement<?> element) throws CswException {
Parser parser = new Parser(PARSER_CONFIG);
InputStream inputStream;
try {
inputStream = marshalJaxB(element);
return parser.parse(inputStream);
} catch (JAXBException | IOException | SAXException | ParserConfigurationException | RuntimeException e) {
throw new CswException(String.format("Failed to parse Element: (%s): %s", e.getClass().getSimpleName(), e.getMessage()), CswConstants.INVALID_PARAMETER_VALUE, null);
}
}
use of org.geotools.xml.Parser in project ddf by codice.
the class AbstractFeatureConverterWfs11 method readGml.
private Geometry readGml(String xml) {
LOGGER.debug("readGml() input XML: {}", xml);
// Add namespace into XML for processing
DocumentBuilder dBuilder;
Document doc = null;
Object gml = null;
InputStream xmlIs;
// Check if GML 3.1.1 namespace exist on XML chunk
try {
dBuilder = XML_UTILS.getSecureDocumentBuilder(false);
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = dBuilder.parse(is);
} catch (ParserConfigurationException | SAXException | IOException e) {
LOGGER.debug(XML_PARSE_FAILURE, e);
}
if (null != doc) {
String[] namePrefix = doc.getDocumentElement().getNodeName().split(":");
String prefix = "";
if (namePrefix.length < 2) {
LOGGER.debug("Incoming XML has no GML prefix");
} else {
prefix = ":" + namePrefix[0];
}
String xmlNs = doc.getDocumentElement().getAttribute("xmlns" + prefix);
if (Wfs11Constants.GML_3_1_1_NAMESPACE.equals(xmlNs)) {
LOGGER.debug("Namespace already exists.");
} else {
doc.createElementNS(Wfs11Constants.GML_3_1_1_NAMESPACE, doc.getDocumentElement().getNodeName());
}
// Convert DOM to InputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
try {
XML_UTILS.getXmlTransformer(false).transform(xmlSource, outputTarget);
} catch (TransformerException | TransformerFactoryConfigurationError e) {
LOGGER.debug(CREATE_TRANSFORMER_FAILURE, e);
}
xmlIs = new ByteArrayInputStream(outputStream.toByteArray());
// Parse XML into a Geometry object
Configuration configurationG = new org.geotools.gml3.GMLConfiguration();
Parser parser = new Parser(configurationG);
parser.setStrict(false);
parser.setValidating(false);
parser.setFailOnValidationError(false);
parser.setForceParserDelegate(false);
try {
gml = parser.parse(xmlIs);
} catch (IOException | SAXException | ParserConfigurationException e) {
LOGGER.debug("{} {}", GML_FAILURE, xml, e);
}
}
return gml instanceof Geometry ? (Geometry) gml : null;
}
use of org.geotools.xml.Parser in project ddf by codice.
the class CswCqlTextFilter method getCqlText.
public String getCqlText(FilterType filterType) throws UnsupportedQueryException {
Parser parser = new Parser(new OGCConfiguration());
try {
StringReader reader = new StringReader(marshalFilterType(filterType));
Object parsedFilter = parser.parse(reader);
if (parsedFilter instanceof Filter) {
String cql = ECQL.toCQL((Filter) parsedFilter);
LOGGER.debug("Generated CQL from Filter => {}", cql);
return cql;
} else {
throw new UnsupportedQueryException("Query did not produce a valid filter.");
}
} catch (IOException | SAXException | ParserConfigurationException | JAXBException e) {
throw new UnsupportedQueryException("Unable to create CQL Filter.", e);
}
}
use of org.geotools.xml.Parser in project ddf by codice.
the class AbstractFeatureConverterWfs20 method readGml.
protected Object readGml(String xml) {
LOGGER.debug("readGml() input XML: {}", xml);
// Add namespace into XML for processing
DocumentBuilder dBuilder = null;
Document doc = null;
Object gml = null;
InputStream xmlIs = null;
// Check if GML 3.2.1 namespace exist on XML chunk
try {
dBuilder = XML_UTILS.getSecureDocumentBuilder(false);
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = dBuilder.parse(is);
} catch (ParserConfigurationException | SAXException | IOException e) {
LOGGER.debug(XML_PARSE_FAILURE);
}
if (null != doc) {
String[] namePrefix = doc.getDocumentElement().getNodeName().split(":");
String prefix = "";
if (namePrefix.length < 2) {
LOGGER.debug("Incoming XML has no GML prefix");
} else {
prefix = ":" + namePrefix[0];
}
String xmlNs = doc.getDocumentElement().getAttribute("xmlns" + prefix);
if (xmlNs.equals(Wfs20Constants.GML_3_2_NAMESPACE)) {
LOGGER.debug("Namespace already exists.");
} else {
doc.createElementNS(Wfs20Constants.GML_3_2_NAMESPACE, doc.getDocumentElement().getNodeName());
}
// Convert DOM to InputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
try {
XML_UTILS.getXmlTransformer(false).transform(xmlSource, outputTarget);
} catch (TransformerException | TransformerFactoryConfigurationError e) {
LOGGER.debug(CREATE_TRANSFORMER_FAILURE);
}
xmlIs = new ByteArrayInputStream(outputStream.toByteArray());
// Parse XML into a Geometry object
Configuration configurationG = new org.geotools.gml3.v3_2.GMLConfiguration();
Parser parser = new Parser(configurationG);
parser.setStrict(false);
parser.setValidating(false);
parser.setFailOnValidationError(false);
parser.setForceParserDelegate(false);
try {
gml = parser.parse(xmlIs);
} catch (IOException | SAXException | ParserConfigurationException e) {
LOGGER.debug("{} {}", GML_FAILURE, xml);
}
}
return gml;
}
Aggregations