Search in sources :

Example 61 with Geometry

use of com.vividsolutions.jts.geom.Geometry 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 62 with Geometry

use of com.vividsolutions.jts.geom.Geometry in project series-rest-api by 52North.

the class TransformationService method transformInline.

/**
     * @param feature the feature to transform.
     * @param query the query containing CRS and how to handle axes order.
     */
protected void transformInline(GeoJSONFeature feature, IoParameters query) {
    String crs = query.getCrs();
    if (CRSUtils.DEFAULT_CRS.equals(crs)) {
        // no need to transform
        return;
    }
    Geometry geometry = transform(feature.getGeometry(), query);
    if (geometry != null) {
        feature.setGeometry(geometry);
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry)

Example 63 with Geometry

use of com.vividsolutions.jts.geom.Geometry in project series-rest-api by 52North.

the class GeoJSONTest method readWriteTest.

protected void readWriteTest(final Geometry geom) {
    try {
        JsonNode json = enc.encodeGeometry(geom);
        Geometry parsed = dec.decodeGeometry(json);
        JsonNode json2 = enc.encodeGeometry(parsed);
        errors.checkThat(geom, is(equalTo(parsed)));
        //            errors.checkThat(json, is(instanceOf(JSONConstants.GEOMETRY)));
        //            errors.checkThat(json2, is(instanceOf(JSONConstants.GEOMETRY)));
        errors.checkThat(json, is(equalTo(json2)));
    } catch (GeoJSONException ex) {
        errors.addError(ex);
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 64 with Geometry

use of com.vividsolutions.jts.geom.Geometry in project series-rest-api by 52North.

the class IoParametersTest method when_jsonBbox_then_parsingSpatialFilter.

@Test
public void when_jsonBbox_then_parsingSpatialFilter() throws ParseException {
    Map<String, String> map = Collections.singletonMap("bbox", "{\"ll\":{\"type\":\"Point\",\"coordinates\":[6.7,51.7]},\"ur\":{\"type\":\"Point\",\"coordinates\":[7.9,51.9]}}");
    IoParameters parameters = createFromSingleValueMap(map);
    BoundingBox actual = parameters.getSpatialFilter();
    WKTReader wktReader = new WKTReader();
    Geometry ll = wktReader.read("POINT (6.7 51.7)");
    Geometry ur = wktReader.read("POINT(7.9 51.9)");
    Assert.assertTrue(actual.getLowerLeft().equals(ll));
    Assert.assertTrue(actual.getUpperRight().equals(ur));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) BoundingBox(org.n52.io.crs.BoundingBox) WKTReader(com.vividsolutions.jts.io.WKTReader) Test(org.junit.Test)

Example 65 with Geometry

use of com.vividsolutions.jts.geom.Geometry 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)

Aggregations

Geometry (com.vividsolutions.jts.geom.Geometry)125 WKTReader (com.vividsolutions.jts.io.WKTReader)35 Test (org.junit.Test)31 Coordinate (com.vividsolutions.jts.geom.Coordinate)24 Point (com.vividsolutions.jts.geom.Point)21 ParseException (com.vividsolutions.jts.io.ParseException)19 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)14 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)14 Envelope (com.vividsolutions.jts.geom.Envelope)13 WKTWriter (com.vividsolutions.jts.io.WKTWriter)13 ArrayList (java.util.ArrayList)13 LineString (com.vividsolutions.jts.geom.LineString)10 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)9 Metacard (ddf.catalog.data.Metacard)9 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 Optional (com.google.common.base.Optional)6 IOException (java.io.IOException)6 RevFeature (org.locationtech.geogig.api.RevFeature)6 JtsGeometry (org.locationtech.spatial4j.shape.jts.JtsGeometry)6 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)5