use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class CreateOSMChangesetOp method _call.
/**
* Executes the diff operation.
*
* @return an iterator to a set of differences between the two trees
* @see DiffEntry
*/
@Override
protected Iterator<ChangeContainer> _call() {
Iterator<DiffEntry> nodeIterator = command(DiffOp.class).setFilter(OSMUtils.NODE_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
Iterator<DiffEntry> wayIterator = command(DiffOp.class).setFilter(OSMUtils.WAY_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
Iterator<DiffEntry> iterator = Iterators.concat(nodeIterator, wayIterator);
final EntityConverter converter = new EntityConverter();
Function<DiffEntry, ChangeContainer> function = new Function<DiffEntry, ChangeContainer>() {
@Override
@Nullable
public ChangeContainer apply(@Nullable DiffEntry diff) {
NodeRef ref = diff.changeType().equals(ChangeType.REMOVED) ? diff.getOldObject() : diff.getNewObject();
RevFeature revFeature = command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(ref.getMetadataId()).call(RevFeatureType.class).get();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
ImmutableList<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, id);
EntityContainer container;
if (entity instanceof Node) {
container = new NodeContainer((Node) entity);
} else {
container = new WayContainer((Way) entity);
}
ChangeAction action = diff.changeType().equals(ChangeType.ADDED) ? ChangeAction.Create : diff.changeType().equals(ChangeType.MODIFIED) ? ChangeAction.Modify : ChangeAction.Delete;
return new ChangeContainer(container, action);
}
};
return Iterators.transform(iterator, function);
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class MappingRule method getFeatureType.
/**
* Returns the feature type defined by this rule. This is the feature type that features
* transformed by this rule will have
*
* @return
*/
public SimpleFeatureType getFeatureType() {
if (featureType == null) {
SimpleFeatureTypeBuilder fb = new SimpleFeatureTypeBuilder();
fb.setName(name);
fb.add("id", Long.class);
if (defaultFields != null) {
for (DefaultField df : defaultFields) {
fb.add(df.name().toLowerCase(), df.getFieldClass());
}
}
Set<String> keys = this.fields.keySet();
for (String key : keys) {
AttributeDefinition field = fields.get(key);
Class<?> clazz = field.getType().getBinding();
if (Geometry.class.isAssignableFrom(clazz)) {
Preconditions.checkArgument(geometryType == null, "The mapping has more than one geometry attribute");
CoordinateReferenceSystem epsg4326;
try {
epsg4326 = CRS.decode("EPSG:4326", true);
fb.add(field.getName(), clazz, epsg4326);
} catch (NoSuchAuthorityCodeException e) {
} catch (FactoryException e) {
}
geometryType = clazz;
} else {
fb.add(field.getName(), clazz);
}
}
Preconditions.checkNotNull(geometryType, "The mapping rule does not define a geometry field");
if (!geometryType.equals(Point.class)) {
fb.add("nodes", String.class);
}
featureType = fb.buildFeatureType();
featureBuilder = new SimpleFeatureBuilder(featureType);
}
return featureType;
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class OSMHistoryImport method toFeature.
private static SimpleFeature toFeature(Primitive feature, Geometry geom) {
SimpleFeatureType ft = feature instanceof Node ? nodeType() : wayType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft);
// "visible:Boolean,version:Int,timestamp:long,[location:Point | way:LineString];
builder.set("visible", Boolean.valueOf(feature.isVisible()));
builder.set("version", Integer.valueOf(feature.getVersion()));
builder.set("timestamp", Long.valueOf(feature.getTimestamp()));
builder.set("changeset", Long.valueOf(feature.getChangesetId()));
String tags = buildTagsString(feature.getTags());
builder.set("tags", tags);
String user = feature.getUserName() + ":" + feature.getUserId();
builder.set("user", user);
if (feature instanceof Node) {
builder.set("location", geom);
} else if (feature instanceof Way) {
builder.set("way", geom);
String nodes = buildNodesString(((Way) feature).getNodes());
builder.set("nodes", nodes);
} else {
throw new IllegalArgumentException();
}
String fid = String.valueOf(feature.getId());
SimpleFeature simpleFeature = builder.buildFeature(fid);
return simpleFeature;
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class EntityConverter method toFeature.
public SimpleFeature toFeature(Entity entity, Geometry geom) {
SimpleFeatureType ft = entity instanceof Node ? OSMUtils.nodeType() : OSMUtils.wayType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft, FEATURE_FACTORY);
// TODO: Check this!
builder.set("visible", Boolean.TRUE);
builder.set("version", Integer.valueOf(entity.getVersion()));
builder.set("timestamp", Long.valueOf(entity.getTimestamp().getTime()));
builder.set("changeset", Long.valueOf(entity.getChangesetId()));
String tags = OSMUtils.buildTagsString(entity.getTags());
builder.set("tags", tags);
String user = entity.getUser().getName() + ":" + Integer.toString(entity.getUser().getId());
builder.set("user", user);
if (entity instanceof Node) {
builder.set("location", geom);
} else if (entity instanceof Way) {
builder.set("way", geom);
String nodes = buildNodesString(((Way) entity).getWayNodes());
builder.set("nodes", nodes);
} else {
throw new IllegalArgumentException();
}
String fid = String.valueOf(entity.getId());
SimpleFeature simpleFeature = builder.buildFeature(fid);
return simpleFeature;
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project sldeditor by robward-scisys.
the class InLineFeatureModel method removeColumn.
/**
* Removes the column.
*
* @param columnName the column name
*/
public void removeColumn(String columnName) {
if (featureCollection != null) {
if (columnList.contains(columnName)) {
columnList.remove(columnName);
// Find field name to remote
SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
featureTypeBuilder.init(featureCollection.getSchema());
featureTypeBuilder.remove(columnName);
SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType();
int attributeToRemoveIndex = 0;
for (AttributeDescriptor descriptor : newFeatureType.getAttributeDescriptors()) {
if (descriptor.getLocalName().compareTo(columnName) == 0) {
break;
}
attributeToRemoveIndex++;
}
String typeName = userLayer.getInlineFeatureType().getTypeName();
try {
SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
SimpleFeatureIterator it = featureSource.getFeatures().features();
try {
while (it.hasNext()) {
SimpleFeature sf = it.next();
List<Object> attributes = sf.getAttributes();
attributes.remove(attributeToRemoveIndex);
sfb.addAll(attributes);
featureList.add(sfb.buildFeature(null));
}
} finally {
it.close();
}
SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList);
featureCollection = collection;
cachedFeature = null;
lastRow = -1;
DataStore dataStore = DataUtilities.dataStore(collection);
userLayer.setInlineFeatureDatastore(dataStore);
userLayer.setInlineFeatureType(newFeatureType);
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
}
this.fireTableStructureChanged();
this.fireTableDataChanged();
if (parentObj != null) {
parentObj.inlineFeatureUpdated();
}
}
}
}
Aggregations