use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class OSMUnmapOpTest method testMappingAndUnmappingOfNodesWithAlias.
@Test
public void testMappingAndUnmappingOfNodesWithAlias() 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_alias", 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());
// unmap without having made any changes and check that the canonical folders are not
// modified
geogig.command(OSMUnmapOp.class).setPath("busstops").call();
WorkingTree workTree = geogig.getRepository().workingTree();
long unstaged = workTree.countUnstaged("way").count();
assertEquals(0, unstaged);
unstaged = workTree.countUnstaged("node").count();
assertEquals(0, unstaged);
// 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_alias", "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();
unstaged = workTree.countUnstaged("node").featureCount();
assertEquals(1, unstaged);
// 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());
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class ApplyPatchOp method applyPatch.
private void applyPatch(Patch patch) {
final WorkingTree workTree = workingTree();
final StagingDatabase indexDb = stagingDatabase();
if (reverse) {
patch = patch.reversed();
}
List<FeatureInfo> removed = patch.getRemovedFeatures();
for (FeatureInfo feature : removed) {
workTree.delete(NodeRef.parentPath(feature.getPath()), NodeRef.nodeFromPath(feature.getPath()));
}
List<FeatureInfo> added = patch.getAddedFeatures();
for (FeatureInfo feature : added) {
workTree.insert(NodeRef.parentPath(feature.getPath()), feature.getFeature());
}
List<FeatureDiff> diffs = patch.getModifiedFeatures();
for (FeatureDiff diff : diffs) {
String path = diff.getPath();
DepthSearch depthSearch = new DepthSearch(indexDb);
Optional<NodeRef> noderef = depthSearch.find(workTree.getTree(), path);
RevFeatureType oldRevFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
String refSpec = Ref.WORK_HEAD + ":" + path;
RevFeature feature = command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class).get();
RevFeatureType newRevFeatureType = getFeatureType(diff, feature, oldRevFeatureType);
ImmutableList<Optional<Object>> values = feature.getValues();
ImmutableList<PropertyDescriptor> oldDescriptors = oldRevFeatureType.sortedDescriptors();
ImmutableList<PropertyDescriptor> newDescriptors = newRevFeatureType.sortedDescriptors();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) newRevFeatureType.type());
Map<Name, Optional<?>> attrs = Maps.newHashMap();
for (int i = 0; i < oldDescriptors.size(); i++) {
PropertyDescriptor descriptor = oldDescriptors.get(i);
if (newDescriptors.contains(descriptor)) {
Optional<Object> value = values.get(i);
attrs.put(descriptor.getName(), value);
}
}
Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
if (!entry.getValue().getType().equals(TYPE.REMOVED)) {
Optional<?> oldValue = attrs.get(entry.getKey().getName());
attrs.put(entry.getKey().getName(), entry.getValue().applyOn(oldValue));
}
}
Set<Entry<Name, Optional<?>>> entries = attrs.entrySet();
for (Iterator<Entry<Name, Optional<?>>> iterator = entries.iterator(); iterator.hasNext(); ) {
Entry<Name, Optional<?>> entry = iterator.next();
featureBuilder.set(entry.getKey(), entry.getValue().orNull());
}
SimpleFeature featureToInsert = featureBuilder.buildFeature(NodeRef.nodeFromPath(path));
workTree.insert(NodeRef.parentPath(path), featureToInsert);
}
ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees();
for (FeatureTypeDiff diff : alteredTrees) {
Optional<RevFeatureType> featureType;
if (diff.getOldFeatureType().isNull()) {
featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
workTree.createTypeTree(diff.getPath(), featureType.get().type());
} else if (diff.getNewFeatureType().isNull()) {
workTree.delete(diff.getPath());
} else {
featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
workTree.updateTypeTree(diff.getPath(), featureType.get().type());
}
}
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project UVMS-ActivityModule-APP by UnionVMS.
the class FishingTripToGeoJsonMapper method toFeature.
// convert Geometry to Feature
public static SimpleFeature toFeature(Geometry geometry) {
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TRIP_FEATURE);
featureBuilder.set(GEOMETRY, geometry);
SimpleFeature feature = featureBuilder.buildFeature(null);
feature.setDefaultGeometry(geometry);
log.debug("SimpleFeature:" + feature.toString());
return feature;
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project sldeditor by robward-scisys.
the class CreateSampleData method create.
/**
* Creates the sample data from the supplied schema.
*
* @param schema the schema
* @param fieldList the field list
*/
public void create(FeatureType schema, List<DataSourceAttributeData> fieldList) {
if (schema == null) {
return;
}
// Put fields into map for speed
Map<String, DataSourceAttributeData> fieldMap = new HashMap<String, DataSourceAttributeData>();
if (fieldList != null) {
for (DataSourceAttributeData attributeData : fieldList) {
fieldMap.put(attributeData.getName(), attributeData);
}
}
SimpleFeatureType featureType = (SimpleFeatureType) schema;
memory = new MemoryDataStore();
try {
memory.createSchema(featureType);
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
memory = null;
return;
}
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
SimpleFeature feature = DataUtilities.template(featureType);
builder.init((SimpleFeature) feature);
int index = 0;
for (AttributeDescriptor descriptor : featureType.getAttributeDescriptors()) {
AttributeType attributeType = descriptor.getType();
Object value = null;
Class<?> fieldType = attributeType.getBinding();
if (attributeType instanceof GeometryTypeImpl) {
geometryType = GeometryTypeMapping.getGeometryType(fieldType);
switch(geometryType) {
case POLYGON:
ExamplePolygonInterface examplePolygon = DataSourceFactory.createExamplePolygon(null);
value = examplePolygon.getPolygon();
break;
case LINE:
ExampleLineInterface exampleLine = DataSourceFactory.createExampleLine(null);
value = exampleLine.getLine();
break;
case POINT:
default:
ExamplePointInterface examplePoint = DataSourceFactory.createExamplePoint(null);
value = examplePoint.getPoint();
break;
}
} else {
if ((fieldList != null) && (index < fieldList.size())) {
DataSourceAttributeData attrData = fieldMap.get(descriptor.getLocalName());
if (attrData != null) {
value = attrData.getValue();
}
}
value = getFieldTypeValue(index, attributeType.getName().getLocalPart(), fieldType, value);
}
builder.add(value);
index++;
}
SimpleFeature newFeature = builder.buildFeature("1234");
memory.addFeature(newFeature);
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project sldeditor by robward-scisys.
the class InLineFeatureModel method removeFeature.
/**
* Removes the feature.
*
* @param selectedRow the selected row
*/
public void removeFeature(int selectedRow) {
if ((selectedRow < 0) || (selectedRow >= getRowCount())) {
return;
}
SimpleFeatureType featureType = userLayer.getInlineFeatureType();
String typeName = userLayer.getInlineFeatureType().getTypeName();
try {
SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType);
ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
SimpleFeatureIterator it = featureSource.getFeatures().features();
try {
int index = 0;
while (it.hasNext()) {
SimpleFeature sf = it.next();
if (index != selectedRow) {
List<Object> attributeValueList = sf.getAttributes();
sfb.addAll(attributeValueList);
featureList.add(sfb.buildFeature(null));
}
index++;
}
} finally {
it.close();
}
SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList);
featureCollection = collection;
cachedFeature = null;
lastRow = -1;
DataStore dataStore = DataUtilities.dataStore(collection);
userLayer.setInlineFeatureDatastore(dataStore);
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
}
this.fireTableStructureChanged();
this.fireTableDataChanged();
if (parentObj != null) {
parentObj.inlineFeatureUpdated();
}
}
Aggregations