Search in sources :

Example 6 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class OSMUnmapOp method unmapNode.

private void unmapNode(SimpleFeature feature, FeatureMapFlusher mapFlusher) {
    boolean modified = false;
    String id = feature.getID();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.nodeType());
    Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.NODE_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(NODE_TAGS_FIELD_INDEX).get().toString());
        for (Tag tag : tags) {
            tagsMap.put(tag.getKey(), tag.getValue());
        }
        Optional<Object> timestampOpt = values.get(NODE_TIMESTAMP_FIELD_INDEX);
        if (timestampOpt.isPresent()) {
            timestamp = ((Long) timestampOpt.get()).longValue();
        }
        Optional<Object> versionOpt = values.get(NODE_VERSION_FIELD_INDEX);
        if (versionOpt.isPresent()) {
            version = ((Integer) versionOpt.get()).intValue();
        }
        Optional<Object> changesetOpt = values.get(NODE_CHANGESET_FIELD_INDEX);
        if (changesetOpt.isPresent()) {
            changeset = ((Long) changesetOpt.get()).longValue();
        }
        Optional<Object> userOpt = values.get(NODE_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") || 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;
    }
    Collection<Tag> newTags = Lists.newArrayList();
    Set<Entry<String, String>> entries = tagsMap.entrySet();
    for (Entry<String, String> entry : entries) {
        newTags.add(new Tag(entry.getKey(), entry.getValue()));
    }
    featureBuilder.set("tags", OSMUtils.buildTagsString(newTags));
    featureBuilder.set("location", feature.getDefaultGeometry());
    featureBuilder.set("changeset", changeset);
    featureBuilder.set("timestamp", timestamp);
    featureBuilder.set("version", version);
    featureBuilder.set("user", user);
    featureBuilder.set("visible", true);
    if (rawFeature.isPresent()) {
        // the feature has changed, so we cannot reuse some attributes.
        // We reconstruct the feature and insert it
        featureBuilder.set("timestamp", System.currentTimeMillis());
        featureBuilder.set("changeset", null);
        featureBuilder.set("version", null);
        featureBuilder.set("visible", true);
        mapFlusher.put("node", featureBuilder.buildFeature(id));
    } else {
        // The feature didn't exist, so we have to add it
        mapFlusher.put("node", 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) 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 7 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class OSMUnmapOp method createNodeForCoord.

/**
     * Creates a new node at a given a given coordinate and inserts it into the working tree
     * 
     * @param coord
     * @return the id of the created node
     */
private Long createNodeForCoord(Coordinate coord, FeatureMapFlusher flusher) {
    // TODO: This has to be changed!!!
    long id = -1 * System.currentTimeMillis();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.nodeType());
    featureBuilder.set("tags", null);
    featureBuilder.set("location", gf.createPoint(coord));
    featureBuilder.set("changeset", null);
    featureBuilder.set("timestamp", System.currentTimeMillis());
    featureBuilder.set("version", 1);
    featureBuilder.set("user", null);
    featureBuilder.set("visible", true);
    flusher.put("node", featureBuilder.buildFeature(Long.toString(id)));
    return id;
}
Also used : SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 8 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class RevFeatureSerializationTest method feature.

protected Feature feature(SimpleFeatureType type, String id, Object... values) throws ParseException {
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        if (type.getDescriptor(i) instanceof GeometryDescriptor) {
            if (value instanceof String) {
                value = new WKTReader2().read((String) value);
            }
        }
        builder.set(i, value);
    }
    return builder.buildFeature(id);
}
Also used : GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) WKTReader2(org.geotools.geometry.jts.WKTReader2) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 9 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class OSMExport method getFeatures.

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
    if (bbox != null) {
        final Envelope env;
        try {
            env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)), Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Wrong bbox definition");
        }
        Predicate<Bounded> filter = new Predicate<Bounded>() {

            @Override
            public boolean apply(final Bounded bounded) {
                boolean intersects = bounded.intersects(env);
                return intersects;
            }
        };
        op.setBoundsFilter(filter);
    }
    Iterator<NodeRef> iterator = op.call();
    final EntityConverter converter = new EntityConverter();
    Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {

        @Override
        @Nullable
        public EntityContainer apply(@Nullable NodeRef ref) {
            RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
            SimpleFeatureType featureType;
            if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
                featureType = OSMUtils.nodeType();
            } else {
                featureType = OSMUtils.wayType();
            }
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
            RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
            List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < descriptors.size(); i++) {
                PropertyDescriptor descriptor = descriptors.get(i);
                Optional<Object> value = values.get(i);
                featureBuilder.set(descriptor.getName(), value.orNull());
            }
            SimpleFeature feature = featureBuilder.buildFeature(ref.name());
            Entity entity = converter.toEntity(feature, null);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }
            return container;
        }
    };
    return Iterators.transform(iterator, function);
}
Also used : EntityConverter(org.locationtech.geogig.osm.internal.EntityConverter) Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) Envelope(com.vividsolutions.jts.geom.Envelope) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Predicate(com.google.common.base.Predicate) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) Bounded(org.locationtech.geogig.api.Bounded) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 10 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class OSMUnmapOpTest method testMappingAndUnmappingOfNodes.

@Test
public void testMappingAndUnmappingOfNodes() throws Exception {
    // Import
    String filename = OSMImportOp.class.getResource("nodes.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").call();
    // Map
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> mappings = Maps.newHashMap();
    mappings.put("highway", Lists.newArrayList("bus_stop"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.POINT));
    fields.put("name", new AttributeDefinition("name", FieldType.STRING));
    Map<String, List<String>> filterExclude = Maps.newHashMap();
    MappingRule mappingRule = new MappingRule("busstops", mappings, filterExclude, fields, null);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    geogig.command(OSMMapOp.class).setMapping(mapping).call();
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:busstops/507464799").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class).setRefSpec("HEAD:busstops/507464799").call();
    assertTrue(featureType.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    assertEquals(3, values.size());
    String wkt = "POINT (7.1959361 50.739397)";
    assertEquals(wkt, values.get(2).get().toString());
    assertEquals(507464799l, values.get(0).get());
    // Modify a node
    GeometryFactory gf = new GeometryFactory();
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder((SimpleFeatureType) featureType.get().type());
    fb.set("geom", gf.createPoint(new Coordinate(0, 1)));
    fb.set("name", "newname");
    fb.set("id", 507464799l);
    SimpleFeature newFeature = fb.buildFeature("507464799");
    geogig.getRepository().workingTree().insert("busstops", newFeature);
    // check that it was correctly inserted in the working tree
    Optional<RevFeature> mapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:busstops/507464799").call(RevFeature.class);
    assertTrue(mapped.isPresent());
    values = mapped.get().getValues();
    assertEquals("POINT (0 1)", values.get(2).get().toString());
    assertEquals(507464799l, ((Long) values.get(0).get()).longValue());
    assertEquals("newname", values.get(1).get().toString());
    // unmap
    geogig.command(OSMUnmapOp.class).setPath("busstops").call();
    // check that the unmapped node has the changes we introduced
    Optional<RevFeature> unmapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/507464799").call(RevFeature.class);
    assertTrue(unmapped.isPresent());
    values = unmapped.get().getValues();
    assertEquals("POINT (0 1)", values.get(6).get().toString());
    assertEquals("bus:yes|public_transport:platform|highway:bus_stop|VRS:ortsteil:Hoholz|name:newname|VRS:ref:68566|VRS:gemeinde:BONN", values.get(3).get().toString());
    // check that unchanged nodes keep their attributes
    Optional<RevFeature> unchanged = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/1633594723").call(RevFeature.class);
    values = unchanged.get().getValues();
    assertEquals("14220478", values.get(4).get().toString());
    assertEquals("1355097351000", values.get(2).get().toString());
    assertEquals("2", values.get(1).get().toString());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Aggregations

SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)56 SimpleFeature (org.opengis.feature.simple.SimpleFeature)34 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)25 Optional (com.google.common.base.Optional)17 ArrayList (java.util.ArrayList)15 RevFeature (org.locationtech.geogig.api.RevFeature)14 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)13 Test (org.junit.Test)11 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)10 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)10 File (java.io.File)9 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)9 Coordinate (com.vividsolutions.jts.geom.Coordinate)8 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)8 Point (com.vividsolutions.jts.geom.Point)8 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)8 Geometry (com.vividsolutions.jts.geom.Geometry)7 List (java.util.List)7