Search in sources :

Example 1 with Parser

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

Example 2 with Parser

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Parser(org.geotools.xml.Parser) SAXException(org.xml.sax.SAXException)

Example 3 with Parser

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;
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) 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) Geometry(org.locationtech.jts.geom.Geometry) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 4 with Parser

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);
    }
}
Also used : Filter(org.opengis.filter.Filter) OGCConfiguration(org.geotools.filter.v1_1.OGCConfiguration) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Parser(org.geotools.xml.Parser) SAXException(org.xml.sax.SAXException)

Example 5 with Parser

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;
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) 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)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)6 Parser (org.geotools.xml.Parser)6 SAXException (org.xml.sax.SAXException)6 IOException (java.io.IOException)5 StringReader (java.io.StringReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Configuration (org.geotools.xml.Configuration)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 JAXBException (javax.xml.bind.JAXBException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 Result (javax.xml.transform.Result)2 Source (javax.xml.transform.Source)2 TransformerException (javax.xml.transform.TransformerException)2 TransformerFactoryConfigurationError (javax.xml.transform.TransformerFactoryConfigurationError)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2 Geometry (org.locationtech.jts.geom.Geometry)2 Document (org.w3c.dom.Document)2 InputSource (org.xml.sax.InputSource)2