use of org.geotools.xml.Configuration 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
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
try {
dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (ParserConfigurationException e) {
LOGGER.debug("Unable to configure features on document builder.", e);
}
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 = dbFactory.newDocumentBuilder();
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 {
TransformerFactory.newInstance().newTransformer().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 org.geotools.xml.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;
}
use of org.geotools.xml.Configuration in project ddf by codice.
the class GeospatialEvaluator method buildGeometry.
public static Geometry buildGeometry(String gmlText) throws IOException, SAXException, ParserConfigurationException {
String methodName = "buildGeometry";
LOGGER.debug("ENTERING: {}", 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)));
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);
// this logs the transformed WKT
LOGGER.debug("Translates to {}", polygon.toText());
// with LON,LAT ordered points
LOGGER.debug("EXITING: {}", 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));
// this logs the transformed WKT
LOGGER.debug("Translates to {}", point.toText());
// with a LON,LAT ordered point
LOGGER.debug("EXITING: {}", methodName);
return point;
}
} catch (Exception e) {
LOGGER.debug("Exception using geotools", e);
}
LOGGER.debug("No translation done for geometry - probably not good ...");
LOGGER.debug("EXITING: {}", methodName);
return geometry;
}
use of org.geotools.xml.Configuration in project coastal-hazards by USGS-CIDA.
the class GMLUtil method generateGMLConfiguration.
public static Configuration generateGMLConfiguration(InputStream inputStream) {
QName featureTypeSchema = GMLUtil.extractFeatureTypeSchema(inputStream);
if (featureTypeSchema == null) {
throw new RuntimeException("featureTypeSchema null for inputStream");
}
String schemaLocation = featureTypeSchema.getLocalPart();
Configuration configuration = null;
if (schemaLocation != null && featureTypeSchema.getNamespaceURI() != null) {
SchemaRepository.registerSchemaLocation(featureTypeSchema.getNamespaceURI(), schemaLocation);
configuration = new ApplicationSchemaConfiguration(featureTypeSchema.getNamespaceURI(), schemaLocation);
} else {
configuration = new GMLConfiguration();
configuration.getProperties().add(Parser.Properties.IGNORE_SCHEMA_LOCATION);
configuration.getProperties().add(Parser.Properties.PARSE_UNKNOWN_ELEMENTS);
}
return configuration;
}
use of org.geotools.xml.Configuration in project coastal-hazards by USGS-CIDA.
the class GMLUtil method generateGMLConfiguration.
public static Configuration generateGMLConfiguration(InputStream inputStream) {
QName featureTypeSchema = GMLUtil.extractFeatureTypeSchema(inputStream);
if (featureTypeSchema == null) {
throw new RuntimeException("featureTypeSchema null for inputStream");
}
String schemaLocation = featureTypeSchema.getLocalPart();
Configuration configuration = null;
if (schemaLocation != null && featureTypeSchema.getNamespaceURI() != null) {
configuration = new ApplicationSchemaConfiguration(featureTypeSchema.getNamespaceURI(), schemaLocation);
} else {
configuration = new GMLConfiguration();
configuration.getProperties().add(Parser.Properties.IGNORE_SCHEMA_LOCATION);
configuration.getProperties().add(Parser.Properties.PARSE_UNKNOWN_ELEMENTS);
}
return configuration;
}
use of org.geotools.xml.Configuration in project coastal-hazards by USGS-CIDA.
the class GMLUtil method generateGMLConfiguration.
public static Configuration generateGMLConfiguration(File file) {
Configuration configuration = null;
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(file), 16 << 10);
configuration = generateGMLConfiguration(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
}
}
return configuration;
}
Aggregations