Search in sources :

Example 76 with Geometry

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

the class GenericFeatureConverter method marshal.

/**
     * This method will convert a {@link Metacard} instance into xml that will validate against the
     * GML 2.1.2 AbstractFeatureType.
     *
     * @param value   the {@link Metacard} to convert
     * @param writer  the stream writer responsible for writing this xml doc
     * @param context a reference back to the Xstream marshalling context. Allows you to call
     *                "convertAnother" which will lookup other registered converters.
     */
@Override
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
    Metacard metacard = (Metacard) value;
    // TODO when we have a reference to the MCT we can get the namespace too
    QName qname = WfsQnameBuilder.buildQName(metacard.getMetacardType().getName(), metacard.getContentTypeName());
    writer.startNode(qname.getPrefix() + ":" + qname.getLocalPart());
    // Add the "fid" attribute if we have an ID
    String fid = (String) metacard.getAttribute(Metacard.ID).getValue();
    if (fid != null) {
        writer.addAttribute(FID, fid);
    }
    if (null != metacard.getLocation()) {
        Geometry geo = XmlNode.readGeometry(metacard.getLocation());
        if (geo != null && !geo.isEmpty()) {
            XmlNode.writeEnvelope(WfsConstants.GML_PREFIX + ":" + "boundedBy", context, writer, geo.getEnvelopeInternal());
        }
    }
    Set<AttributeDescriptor> descriptors = new TreeSet<AttributeDescriptor>(new AttributeDescriptorComparator());
    descriptors.addAll(metacard.getMetacardType().getAttributeDescriptors());
    for (AttributeDescriptor attributeDescriptor : descriptors) {
        Attribute attribute = metacard.getAttribute(attributeDescriptor.getName());
        if (attribute != null) {
            writeAttributeToXml(attribute, qname, attributeDescriptor.getType().getAttributeFormat(), context, writer);
        }
    }
    writer.endNode();
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) QName(javax.xml.namespace.QName) TreeSet(java.util.TreeSet) AttributeDescriptorComparator(org.codice.ddf.spatial.ogc.wfs.catalog.common.AttributeDescriptorComparator) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor)

Example 77 with Geometry

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

Example 78 with Geometry

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

the class GeoUtilTest method testTransformEpsg4326UTM.

@Test
public void testTransformEpsg4326UTM() throws FactoryException, TransformException, GeoFormatException {
    double lon = 33.45;
    double lat = 25.22;
    double easting = 545328.48;
    double northing = 2789384.24;
    CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32636");
    GeometryFactory geometryFactory = new GeometryFactory();
    Coordinate utmCoordinate = new Coordinate(easting, northing);
    Point utmPoint = geometryFactory.createPoint(utmCoordinate);
    Envelope envelope = JTS.toEnvelope(utmPoint);
    Geometry utmGeometry = JTS.toGeometry(envelope);
    Geometry lonLatGeom = GeospatialUtil.transformToEPSG4326LonLatFormat(utmGeometry, sourceCRS);
    assertThat(lonLatGeom.getCoordinates()[0].x, closeTo(lon, .00001));
    assertThat(lonLatGeom.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) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Point(com.vividsolutions.jts.geom.Point) Envelope(com.vividsolutions.jts.geom.Envelope) Test(org.junit.Test)

Example 79 with Geometry

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

the class GeoUtilTest method testTransformEpsg4326LonLatNullSrs.

@Test
public void testTransformEpsg4326LonLatNullSrs() throws GeoFormatException {
    GeometryFactory gf = new GeometryFactory();
    Coordinate coord = new Coordinate(25.22, 33.45);
    Point point = gf.createPoint(coord);
    Geometry geom = GeospatialUtil.transformToEPSG4326LonLatFormat(point, (String) null);
    assertThat(geom, is(point));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point(com.vividsolutions.jts.geom.Point) Test(org.junit.Test)

Example 80 with Geometry

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

the class GeoUtilTest method testTransformEpsg4326LonLatBadSrs.

@Test(expected = GeoFormatException.class)
public void testTransformEpsg4326LonLatBadSrs() throws GeoFormatException {
    GeometryFactory gf = new GeometryFactory();
    Coordinate coord = new Coordinate(25.22, 33.45);
    Point point = gf.createPoint(coord);
    Geometry geom = GeospatialUtil.transformToEPSG4326LonLatFormat(point, "ESPG:Bad");
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point(com.vividsolutions.jts.geom.Point) Test(org.junit.Test)

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