Search in sources :

Example 11 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project ddf by codice.

the class JTSGeometryWrapperTest method testComputeJTSPeerWithGeometry.

@Test
public void testComputeJTSPeerWithGeometry() {
    GeometryFactory fac = new GeometryFactory();
    Geometry toWrap = fac.createPoint(new Coordinate(0, 0));
    toTest = new JTSGeometryWrapper(toWrap);
    assertEquals(toWrap, toTest.computeJTSPeer());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Test(org.junit.Test)

Example 12 with GeometryFactory

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

use of com.vividsolutions.jts.geom.GeometryFactory in project ddf by codice.

the class TestCswRecordMapperFilterVisitor method testVisitBeyond.

@Test
public void testVisitBeyond() {
    GeometryFactory geoFactory = new GeometryFactory();
    double val = 30;
    Expression pt1 = factory.literal(geoFactory.createPoint(new Coordinate(4, 5)));
    Expression pt2 = factory.literal(geoFactory.createPoint(new Coordinate(6, 7)));
    Beyond filter = factory.beyond(pt1, pt2, val, "kilometers");
    Beyond duplicate = (Beyond) visitor.visit(filter, null);
    assertThat(duplicate.getExpression1(), is(pt1));
    assertThat(duplicate.getExpression2(), is(pt2));
    assertThat(duplicate.getDistanceUnits(), is(UomOgcMapping.METRE.name()));
    assertThat(duplicate.getDistance(), is(1000 * val));
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Expression(org.opengis.filter.expression.Expression) Coordinate(com.vividsolutions.jts.geom.Coordinate) Beyond(org.opengis.filter.spatial.Beyond) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 14 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project ddf by codice.

the class TestCswRecordMapperFilterVisitor method testIntersectsUtm.

@Test
public void testIntersectsUtm() throws FactoryException, TransformException {
    double lon = 33.45;
    double lat = 25.22;
    double easting = 545328.48;
    double northing = 2789384.24;
    CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32636");
    GeometryFactory geoFactory = new GeometryFactory();
    Geometry utmPoint = geoFactory.createPoint(new Coordinate(easting, northing));
    utmPoint.setUserData(sourceCRS);
    Expression pt1 = factory.literal(geoFactory.createPoint(new Coordinate(1, 2)));
    Expression pt2 = factory.literal(utmPoint);
    Intersects filter = factory.intersects(pt1, pt2);
    visitor.visit(filter, null);
    assertThat(pt2, instanceOf(Literal.class));
    Literal literalExpression = (Literal) pt2;
    assertThat(literalExpression.getValue(), instanceOf(Geometry.class));
    Geometry geometry = (Geometry) literalExpression.getValue();
    assertThat(geometry.getCoordinates()[0].x, closeTo(lon, .00001));
    assertThat(geometry.getCoordinates()[0].y, closeTo(lat, .00001));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Expression(org.opengis.filter.expression.Expression) Intersects(org.opengis.filter.spatial.Intersects) Literal(org.opengis.filter.expression.Literal) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 15 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project ddf by codice.

the class FeatureCollectionConverterWfs10 method getBounds.

private Geometry getBounds(List<Metacard> metacards) {
    if (metacards != null) {
        List<Geometry> geometries = new ArrayList<Geometry>();
        for (Metacard card : metacards) {
            if (null != card.getLocation()) {
                Geometry geo = XmlNode.readGeometry(card.getLocation());
                if (null != geo) {
                    geometries.add(geo);
                }
            }
        }
        Geometry allGeometry = new GeometryCollection(geometries.toArray(new Geometry[0]), new GeometryFactory());
        return allGeometry;
    } else {
        LOGGER.debug("List of metacards was null.");
        return null;
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) Metacard(ddf.catalog.data.Metacard) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) ArrayList(java.util.ArrayList)

Aggregations

GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)33 Coordinate (com.vividsolutions.jts.geom.Coordinate)27 Test (org.junit.Test)21 Geometry (com.vividsolutions.jts.geom.Geometry)13 SimpleFeature (org.opengis.feature.simple.SimpleFeature)12 ArrayList (java.util.ArrayList)10 Point (com.vividsolutions.jts.geom.Point)9 File (java.io.File)9 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)8 Optional (com.google.common.base.Optional)7 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)7 RevFeature (org.locationtech.geogig.api.RevFeature)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 AddOp (org.locationtech.geogig.api.porcelain.AddOp)6 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)5 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)5 Envelope (com.vividsolutions.jts.geom.Envelope)4 IOException (java.io.IOException)4 Serializable (java.io.Serializable)4