Search in sources :

Example 6 with WKTWriter

use of com.vividsolutions.jts.io.WKTWriter in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method newWkt.

/**
     * Restrict to an area.
     * <p/>
     * If an area already exists the additional area is applied.
     *
     * @param wkt
     * @return new BiocacheQuery with the additional wkt area applied.
     */
@Override
public BiocacheQuery newWkt(String wkt, boolean forMapping) {
    if (wkt == null || wkt.equals(CommonData.WORLD_WKT) || wkt.equals(this.wkt)) {
        if (this.forMapping || !forMapping) {
            return this;
        } else {
            return new BiocacheQuery(lsids, rawNames, wkt, extraParams, facets, forMapping, null, biocacheServer, biocacheWebServer, this.supportsDynamicFacets);
        }
    }
    BiocacheQuery sq = null;
    try {
        String newWkt = wkt;
        if (this.wkt != null) {
            Geometry newGeom = new WKTReader().read(wkt);
            Geometry thisGeom = new WKTReader().read(this.wkt);
            Geometry intersectionGeom = thisGeom.intersection(newGeom);
            newWkt = (new WKTWriter()).write(intersectionGeom).replace(" (", "(").replace(", ", ",").replace(") ", ")");
        }
        sq = new BiocacheQuery(lsids, rawNames, newWkt, extraParams, facets, forMapping, null, biocacheServer, biocacheWebServer, this.supportsDynamicFacets);
    } catch (Exception e) {
        LOGGER.error("error getting new WKT from an intersection", e);
    }
    return sq;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) WKTWriter(com.vividsolutions.jts.io.WKTWriter) WKTReader(com.vividsolutions.jts.io.WKTReader)

Example 7 with WKTWriter

use of com.vividsolutions.jts.io.WKTWriter in project spatial-portal by AtlasOfLivingAustralia.

the class UserDataQuery method newWkt.

/**
     * Restrict to an area.
     * <p/>
     * If an area already exists the additional area is applied.
     *
     * @param wkt
     * @return new BiocacheQuery with the additional wkt area applied.
     */
@Override
public UserDataQuery newWkt(String wkt, boolean forMapping) {
    if (wkt == null || wkt.equals(CommonData.WORLD_WKT) || wkt.equals(this.wkt)) {
        return this;
    }
    UserDataQuery sq = null;
    try {
        String newWkt = wkt;
        if (this.wkt != null) {
            Geometry newGeom = new WKTReader().read(wkt);
            Geometry thisGeom = new WKTReader().read(this.wkt);
            Geometry intersectionGeom = thisGeom.intersection(newGeom);
            newWkt = (new WKTWriter()).write(intersectionGeom).replace(" (", "(").replace(", ", ",").replace(") ", ")");
        }
        sq = new UserDataQuery(udHeaderId, newWkt, facets);
    } catch (Exception e) {
        LOGGER.error("failed to filter user uploaded points with WKT: " + udHeaderId + ", " + wkt);
    }
    return sq;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) WKTWriter(com.vividsolutions.jts.io.WKTWriter) WKTReader(com.vividsolutions.jts.io.WKTReader) IOException(java.io.IOException)

Example 8 with WKTWriter

use of com.vividsolutions.jts.io.WKTWriter in project spatial-portal by AtlasOfLivingAustralia.

the class AreaUploadShapefile method getKMLPolygonAsWKT.

/**
     * parse a KML containing a single placemark, or a placemark in a folder, into WKT.
     *
     * @param kmldata
     * @return WKT if valid, null on error, empty string "" when placemark not matched.
     */
private static String getKMLPolygonAsWKT(String kmldata) {
    try {
        Parser parser = new Parser(new org.geotools.kml.v22.KMLConfiguration());
        SimpleFeature f = (SimpleFeature) parser.parse(new StringReader(kmldata));
        Collection placemarks = (Collection) f.getAttribute(StringConstants.FEATURE);
        Geometry g = null;
        SimpleFeature sf = null;
        //for <Placemark>
        if (!placemarks.isEmpty() && !placemarks.isEmpty()) {
            sf = (SimpleFeature) placemarks.iterator().next();
            g = (Geometry) sf.getAttribute("Geometry");
        }
        //for <Folder><Placemark>
        if (g == null && sf != null) {
            placemarks = (Collection) sf.getAttribute(StringConstants.FEATURE);
            if (placemarks != null && !placemarks.isEmpty()) {
                g = (Geometry) ((SimpleFeature) placemarks.iterator().next()).getAttribute("Geometry");
            } else {
                placemarks = (Collection) sf.getAttribute("Folder");
                if (placemarks != null && !placemarks.isEmpty()) {
                    g = (Geometry) ((SimpleFeature) placemarks.iterator().next()).getAttribute("Geometry");
                }
            }
        }
        if (g != null) {
            WKTWriter wr = new WKTWriter();
            String wkt = wr.write(g);
            return wkt.replace(" (", "(").replace(", ", ",").replace(") ", ")");
        } else {
            return "";
        }
    } catch (SAXException e) {
        LOGGER.error("KML spec parse error", e);
    } catch (ParserConfigurationException e) {
        LOGGER.error("error converting KML to WKT", e);
    } catch (Exception e) {
        LOGGER.error("error reading KML", e);
    }
    return null;
}
Also used : WKTWriter(com.vividsolutions.jts.io.WKTWriter) SimpleFeature(org.opengis.feature.simple.SimpleFeature) ParseException(com.vividsolutions.jts.io.ParseException) SuspendNotAllowedException(org.zkoss.zk.ui.SuspendNotAllowedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Parser(org.geotools.xml.Parser) SAXException(org.xml.sax.SAXException) Geometry(com.vividsolutions.jts.geom.Geometry) Collection(java.util.Collection) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 9 with WKTWriter

use of com.vividsolutions.jts.io.WKTWriter in project ddf by codice.

the class GmlGeometryConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    XmlNode gmlNode = new XmlNode(reader);
    GMLReader gmlReader = new GMLReader();
    Geometry geo = null;
    try {
        geo = gmlReader.read(gmlNode.toString(), null);
    } catch (SAXException | IOException | ParserConfigurationException e) {
        LOGGER.debug(ERROR_PARSING_MSG, e);
    }
    if (geo != null) {
        WKTWriter wktWriter = new WKTWriter();
        return wktWriter.write(geo);
    }
    return null;
}
Also used : GMLReader(com.vividsolutions.jts.io.gml2.GMLReader) Geometry(com.vividsolutions.jts.geom.Geometry) WKTWriter(com.vividsolutions.jts.io.WKTWriter) XmlNode(org.codice.ddf.spatial.ogc.catalog.common.converter.XmlNode) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 10 with WKTWriter

use of com.vividsolutions.jts.io.WKTWriter in project ddf by codice.

the class GeometryAdapter method unmarshalFrom.

public static Attribute unmarshalFrom(GeometryElement element) throws ConversionFailedException {
    AttributeImpl attribute = null;
    GML311ToJTSGeometryConverter converter = new GML311ToJTSGeometryConverter();
    WKTWriter wktWriter = new WKTWriter();
    for (Value xmlValue : element.getValue()) {
        JAXBElement<AbstractGeometryType> xmlGeometry = xmlValue.getGeometry();
        Geometry geometry = converter.createGeometry(new DefaultRootObjectLocator(xmlValue), xmlGeometry.getValue());
        String wkt = wktWriter.write(geometry);
        if (attribute == null) {
            attribute = new AttributeImpl(element.getName(), wkt);
        } else {
            attribute.addValue(wkt);
        }
    }
    return attribute;
}
Also used : GML311ToJTSGeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.GML311ToJTSGeometryConverter) Geometry(com.vividsolutions.jts.geom.Geometry) WKTWriter(com.vividsolutions.jts.io.WKTWriter) DefaultRootObjectLocator(org.jvnet.jaxb2_commons.locator.DefaultRootObjectLocator) AbstractGeometryType(net.opengis.gml.v_3_1_1.AbstractGeometryType) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) Value(ddf.catalog.transformer.xml.binding.GeometryElement.Value)

Aggregations

WKTWriter (com.vividsolutions.jts.io.WKTWriter)14 Geometry (com.vividsolutions.jts.geom.Geometry)13 ParseException (com.vividsolutions.jts.io.ParseException)4 IOException (java.io.IOException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 XmlNode (org.codice.ddf.spatial.ogc.catalog.common.converter.XmlNode)3 DefaultRootObjectLocator (org.jvnet.jaxb2_commons.locator.DefaultRootObjectLocator)3 GML311ToJTSGeometryConverter (org.jvnet.ogc.gml.v_3_1_1.jts.GML311ToJTSGeometryConverter)3 SAXException (org.xml.sax.SAXException)3 LineString (com.vividsolutions.jts.geom.LineString)2 WKTReader (com.vividsolutions.jts.io.WKTReader)2 GMLReader (com.vividsolutions.jts.io.gml2.GMLReader)2 Serializable (java.io.Serializable)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AbstractGeometryType (net.opengis.gml.v_3_1_1.AbstractGeometryType)2 ConversionFailedException (org.jvnet.ogc.gml.v_3_1_1.jts.ConversionFailedException)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 Value (ddf.catalog.transformer.xml.binding.GeometryElement.Value)1