Search in sources :

Example 21 with LineString

use of com.vividsolutions.jts.geom.LineString in project GeoGig by boundlessgeo.

the class GeoGigDataStoreTest method testFeatureWriterAppend.

@Test
public void testFeatureWriterAppend() throws Exception {
    dataStore.createSchema(linesType);
    Transaction tx = new DefaultTransaction();
    FeatureWriter<SimpleFeatureType, SimpleFeature> fw = dataStore.getFeatureWriterAppend(linesTypeName.getLocalPart(), tx);
    LineString line = new GeometryBuilder().lineString(0, 0, 1, 1);
    SimpleFeature f = (SimpleFeature) fw.next();
    f.setAttribute("sp", "foo");
    f.setAttribute("ip", 10);
    f.setAttribute("pp", line);
    fw.write();
    fw.close();
    tx.commit();
    FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(linesTypeName);
    assertEquals(1, source.getCount(null));
    FeatureReader<SimpleFeatureType, SimpleFeature> r = dataStore.getFeatureReader(new Query(linesTypeName.getLocalPart()), Transaction.AUTO_COMMIT);
    assertTrue(r.hasNext());
    f = r.next();
    assertEquals("foo", f.getAttribute("sp"));
    assertEquals(10, f.getAttribute("ip"));
    assertTrue(line.equals((Geometry) f.getAttribute("pp")));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Query(org.geotools.data.Query) LineString(com.vividsolutions.jts.geom.LineString) GeometryBuilder(org.geotools.geometry.jts.GeometryBuilder) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 22 with LineString

use of com.vividsolutions.jts.geom.LineString in project GeoGig by boundlessgeo.

the class OSMUnmapOp method unmapWay.

private void unmapWay(SimpleFeature feature, FeatureMapFlusher flusher) {
    boolean modified = false;
    String id = feature.getID();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.wayType());
    Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.WAY_TYPE_NAME + "/" + id).call(RevFeature.class);
    Map<String, String> tagsMap = Maps.newHashMap();
    long timestamp = System.currentTimeMillis();
    int version = 1;
    long changeset = -1;
    String user = UNKNOWN_USER;
    Collection<Tag> tags = Lists.newArrayList();
    if (rawFeature.isPresent()) {
        ImmutableList<Optional<Object>> values = rawFeature.get().getValues();
        tags = OSMUtils.buildTagsCollectionFromString(values.get(WAY_TAGS_FIELD_INDEX).get().toString());
        for (Tag tag : tags) {
            tagsMap.put(tag.getKey(), tag.getValue());
        }
        Optional<Object> timestampOpt = values.get(WAY_TIMESTAMP_FIELD_INDEX);
        if (timestampOpt.isPresent()) {
            timestamp = ((Long) timestampOpt.get()).longValue();
        }
        Optional<Object> versionOpt = values.get(WAY_VERSION_FIELD_INDEX);
        if (versionOpt.isPresent()) {
            version = ((Integer) versionOpt.get()).intValue();
        }
        Optional<Object> changesetOpt = values.get(WAY_CHANGESET_FIELD_INDEX);
        if (changesetOpt.isPresent()) {
            changeset = ((Long) changesetOpt.get()).longValue();
        }
        Optional<Object> userOpt = values.get(WAY_USER_FIELD_INDEX);
        if (userOpt.isPresent()) {
            user = (String) userOpt.get();
        }
    }
    Map<String, String> unaliased = Maps.newHashMap();
    Collection<Property> properties = feature.getProperties();
    for (Property property : properties) {
        String name = property.getName().getLocalPart();
        if (name.equals("id") || name.equals("nodes") || Geometry.class.isAssignableFrom(property.getDescriptor().getType().getBinding())) {
            continue;
        }
        Object value = property.getValue();
        if (value != null) {
            String tagName = name;
            if (mapping != null) {
                if (unaliased.containsKey(name)) {
                    tagName = unaliased.get(name);
                } else {
                    tagName = mapping.getTagNameFromAlias(path, tagName);
                    unaliased.put(name, tagName);
                }
            }
            if (!DefaultField.isDefaultField(tagName)) {
                if (tagsMap.containsKey(tagName)) {
                    if (!modified) {
                        String oldValue = tagsMap.get(tagName);
                        modified = !value.equals(oldValue);
                    }
                } else {
                    modified = true;
                }
                tagsMap.put(tagName, value.toString());
            }
        }
    }
    if (!modified && rawFeature.isPresent()) {
        // no changes after unmapping tags, so there's nothing else to do
        return;
    }
    tags.clear();
    Set<Entry<String, String>> entries = tagsMap.entrySet();
    for (Entry<String, String> entry : entries) {
        tags.add(new Tag(entry.getKey(), entry.getValue()));
    }
    Geometry geom = (Geometry) feature.getDefaultGeometry();
    LineString line;
    if (geom instanceof LineString) {
        line = (LineString) geom;
    } else {
        line = gf.createLineString(geom.getCoordinates());
    }
    featureBuilder.set("visible", true);
    featureBuilder.set("tags", OSMUtils.buildTagsString(tags));
    featureBuilder.set("way", line);
    featureBuilder.set("changeset", changeset);
    featureBuilder.set("timestamp", timestamp);
    featureBuilder.set("version", version);
    featureBuilder.set("user", user);
    featureBuilder.set("nodes", getNodeStringFromWay(feature, flusher));
    if (rawFeature.isPresent()) {
        // the feature has changed, so we cannot reuse some attributes
        featureBuilder.set("timestamp", System.currentTimeMillis());
        // temporary negative changeset ID
        featureBuilder.set("changeset", -changeset);
        // featureBuilder.set("version", version);
        flusher.put("way", featureBuilder.buildFeature(id));
    } else {
        flusher.put("way", featureBuilder.buildFeature(id));
    }
}
Also used : Optional(com.google.common.base.Optional) LineString(com.vividsolutions.jts.geom.LineString) Point(com.vividsolutions.jts.geom.Point) Geometry(com.vividsolutions.jts.geom.Geometry) ReadOSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.ReadOSMMappingLogEntry) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) OSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry) LineString(com.vividsolutions.jts.geom.LineString) RevFeature(org.locationtech.geogig.api.RevFeature) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Property(org.opengis.feature.Property) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 23 with LineString

use of com.vividsolutions.jts.geom.LineString in project GeoGig by boundlessgeo.

the class OSMUnmapOp method getNodeStringFromWay.

/**
     * This method takes a way and generates the corresponding string with ids of nodes than compose
     * that line. If those nodes are declared in the "nodes" attribute of the feature, they will be
     * referenced and no new node will be added. If a coordinate in the linestring doesn't have a
     * corresponding node declared in the "nodes" attribute, a new node at that coordinate will be
     * added to the "node" tree. This way, the returned linestring is guaranteed to refer to nodes
     * that already exist in the repository, and as such can be safely used to add a new way that
     * uses those nodes.
     * 
     * @param line
     * @return
     */
private String getNodeStringFromWay(SimpleFeature way, FeatureMapFlusher flusher) {
    Map<Coordinate, Long> nodeCoords = Maps.newHashMap();
    Object nodesAttribute = way.getAttribute("nodes");
    if (nodesAttribute != null) {
        String[] nodeIds = nodesAttribute.toString().split(";");
        for (String nodeId : nodeIds) {
            Optional<RevFeature> revFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.NODE_TYPE_NAME + "/" + nodeId).call(RevFeature.class);
            if (revFeature.isPresent()) {
                Optional<Object> location = revFeature.get().getValues().get(NODE_LOCATION_FIELD_INDEX);
                if (location.isPresent()) {
                    Coordinate coord = ((Geometry) location.get()).getCoordinate();
                    nodeCoords.put(coord, Long.parseLong(nodeId));
                }
            }
        }
    }
    List<Long> nodes = Lists.newArrayList();
    Coordinate[] coords = ((Geometry) way.getDefaultGeometryProperty().getValue()).getCoordinates();
    for (Coordinate coord : coords) {
        if (nodeCoords.containsKey(coord)) {
            nodes.add(nodeCoords.get(coord));
        } else {
            nodes.add(createNodeForCoord(coord, flusher));
        }
    }
    return Joiner.on(';').join(nodes);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevFeature(org.locationtech.geogig.api.RevFeature) LineString(com.vividsolutions.jts.geom.LineString)

Example 24 with LineString

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

the class KMLTransformerImpl method createKmlGeo.

private Geometry createKmlGeo(com.vividsolutions.jts.geom.Geometry geo) throws CatalogTransformerException {
    Geometry kmlGeo = null;
    if (Point.class.getSimpleName().equals(geo.getGeometryType())) {
        Point jtsPoint = (Point) geo;
        kmlGeo = KmlFactory.createPoint().addToCoordinates(jtsPoint.getX(), jtsPoint.getY());
    } else if (LineString.class.getSimpleName().equals(geo.getGeometryType())) {
        LineString jtsLS = (LineString) geo;
        de.micromata.opengis.kml.v_2_2_0.LineString kmlLS = KmlFactory.createLineString();
        List<Coordinate> kmlCoords = kmlLS.createAndSetCoordinates();
        for (com.vividsolutions.jts.geom.Coordinate coord : jtsLS.getCoordinates()) {
            kmlCoords.add(new Coordinate(coord.x, coord.y));
        }
        kmlGeo = kmlLS;
    } else if (Polygon.class.getSimpleName().equals(geo.getGeometryType())) {
        Polygon jtsPoly = (Polygon) geo;
        de.micromata.opengis.kml.v_2_2_0.Polygon kmlPoly = KmlFactory.createPolygon();
        List<Coordinate> kmlCoords = kmlPoly.createAndSetOuterBoundaryIs().createAndSetLinearRing().createAndSetCoordinates();
        for (com.vividsolutions.jts.geom.Coordinate coord : jtsPoly.getCoordinates()) {
            kmlCoords.add(new Coordinate(coord.x, coord.y));
        }
        kmlGeo = kmlPoly;
    } else if (geo instanceof GeometryCollection) {
        List<Geometry> geos = new ArrayList<Geometry>();
        for (int xx = 0; xx < geo.getNumGeometries(); xx++) {
            geos.add(createKmlGeo(geo.getGeometryN(xx)));
        }
        kmlGeo = KmlFactory.createMultiGeometry().withGeometry(geos);
    } else {
        throw new CatalogTransformerException("Unknown / Unsupported Geometry Type '" + geo.getGeometryType() + "'. Unale to preform KML Transform.");
    }
    return kmlGeo;
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Point(com.vividsolutions.jts.geom.Point) Geometry(de.micromata.opengis.kml.v_2_2_0.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) LineString(com.vividsolutions.jts.geom.LineString) Coordinate(de.micromata.opengis.kml.v_2_2_0.Coordinate) List(java.util.List) ArrayList(java.util.ArrayList) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 25 with LineString

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

the class TestWfs10JTStoGML200Converter method testLineStringTypeToJAXB.

@Test
public void testLineStringTypeToJAXB() throws JAXBException, SAXException, IOException, ParseException, NullPointerException {
    LineString lineString = (LineString) getGeometryFromWkt(LINESTRING);
    assertThat(lineString == null, is(Boolean.FALSE));
    String lineStringGML = Wfs10JTStoGML200Converter.convertGeometryToGML(lineString);
    LineStringType lineStringType = (LineStringType) Wfs10JTStoGML200Converter.convertGMLToGeometryType(lineStringGML, Wfs10Constants.LINESTRING);
    JAXBElement<LineStringType> lineStringTypeJAXBElement = (JAXBElement<LineStringType>) Wfs10JTStoGML200Converter.convertGeometryTypeToJAXB(lineStringType);
    XMLUnit.setNormalizeWhitespace(Boolean.TRUE);
    JAXB.marshal(lineStringTypeJAXBElement, writer);
    String xml = writer.toString();
    Diff diff = XMLUnit.compareXML(xml, LINESTRING_GML);
    assertTrue(XMLUNIT_SIMILAR, diff.similar());
    assertThat(diff.similar(), is(Boolean.TRUE));
    assertThat(diff.identical(), is(Boolean.FALSE));
}
Also used : LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Diff(org.custommonkey.xmlunit.Diff) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) JAXBElement(javax.xml.bind.JAXBElement) MultiLineStringType(ogc.schema.opengis.gml.v_2_1_2.MultiLineStringType) LineStringType(ogc.schema.opengis.gml.v_2_1_2.LineStringType) Test(org.junit.Test)

Aggregations

LineString (com.vividsolutions.jts.geom.LineString)26 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)15 Point (com.vividsolutions.jts.geom.Point)12 Coordinate (com.vividsolutions.jts.geom.Coordinate)9 Geometry (com.vividsolutions.jts.geom.Geometry)7 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)7 Polygon (com.vividsolutions.jts.geom.Polygon)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)3 LineStringType (ogc.schema.opengis.gml.v_2_1_2.LineStringType)3 MultiLineStringType (ogc.schema.opengis.gml.v_2_1_2.MultiLineStringType)3 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 ElasticsearchGeoAssertions.assertMultiLineString (org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiLineString)3 ElasticsearchGeoAssertions.assertMultiPolygon (org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiPolygon)3 ElasticsearchGeoAssertions.assertPolygon (org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertPolygon)3 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)2 ParseException (com.vividsolutions.jts.io.ParseException)2 MultiCurveType (net.opengis.gml.v_3_2_1.MultiCurveType)2