Search in sources :

Example 1 with Configuration

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;
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Configuration(org.geotools.xml.Configuration) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Parser(org.geotools.xml.Parser) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 2 with Configuration

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;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Configuration(org.geotools.xml.Configuration) ArrayList(java.util.ArrayList) Point(com.vividsolutions.jts.geom.Point) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Parser(org.geotools.xml.Parser) Geometry(com.vividsolutions.jts.geom.Geometry) Coordinate(com.vividsolutions.jts.geom.Coordinate) StringReader(java.io.StringReader) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 3 with 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) {
        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;
}
Also used : GMLConfiguration(org.geotools.gml3.GMLConfiguration) Configuration(org.geotools.xml.Configuration) ApplicationSchemaConfiguration(org.geotools.gml3.ApplicationSchemaConfiguration) QName(javax.xml.namespace.QName) ApplicationSchemaConfiguration(org.geotools.gml3.ApplicationSchemaConfiguration) GMLConfiguration(org.geotools.gml3.GMLConfiguration)

Example 4 with 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;
}
Also used : GMLConfiguration(org.geotools.gml3.GMLConfiguration) Configuration(org.geotools.xml.Configuration) ApplicationSchemaConfiguration(org.geotools.gml3.ApplicationSchemaConfiguration) QName(javax.xml.namespace.QName) ApplicationSchemaConfiguration(org.geotools.gml3.ApplicationSchemaConfiguration) GMLConfiguration(org.geotools.gml3.GMLConfiguration)

Example 5 with 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;
}
Also used : GMLConfiguration(org.geotools.gml3.GMLConfiguration) Configuration(org.geotools.xml.Configuration) ApplicationSchemaConfiguration(org.geotools.gml3.ApplicationSchemaConfiguration) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

Configuration (org.geotools.xml.Configuration)6 IOException (java.io.IOException)4 ApplicationSchemaConfiguration (org.geotools.gml3.ApplicationSchemaConfiguration)4 GMLConfiguration (org.geotools.gml3.GMLConfiguration)4 InputStream (java.io.InputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 StringReader (java.io.StringReader)2 QName (javax.xml.namespace.QName)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Parser (org.geotools.xml.Parser)2 SAXException (org.xml.sax.SAXException)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 Point (com.vividsolutions.jts.geom.Point)1 Polygon (com.vividsolutions.jts.geom.Polygon)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1