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));
}
}
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;
}
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);
}
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);
}
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());
}
Aggregations