use of org.activityinfo.geoadmin.source.GeometryConverter in project activityinfo by bedatadriven.
the class ImportView method updateGeometry.
private void updateGeometry(GeoAdminClient client, Map<ResourceId, ResourceId> idMap) throws IOException {
FeatureSourceStorageProvider catalog = new FeatureSourceStorageProvider();
FeatureSourceStorage formStorage = (FeatureSourceStorage) catalog.getForm(getModel().getSourceFormId().get()).get();
ResourceId targetFormId = getModel().getTargetFormId().get();
FieldProfile targetField = getTargetProfile().get().getGeometryField();
if (targetField == null) {
System.err.println("No geometry field to update.");
return;
}
int sourceIndex = formStorage.getGeometryAttributeIndex();
if (sourceIndex == -1) {
System.err.println("No source geometry field.");
return;
}
SimpleFeatureSource featureSource = formStorage.getFeatureSource();
GeometryType geometryType = (GeometryType) featureSource.getSchema().getDescriptor(sourceIndex).getType();
GeometryConverter converter = new GeometryConverter(geometryType);
SimpleFeatureIterator it = featureSource.getFeatures().features();
while (it.hasNext()) {
SimpleFeature feature = it.next();
ResourceId sourceId = ResourceId.valueOf(feature.getID());
ResourceId targetId = idMap.get(sourceId);
if (targetId != null) {
Geometry geometry = converter.toWgs84(feature.getAttribute(sourceIndex));
System.out.print("Updating geometry for " + targetId + " [" + geometry.getGeometryType() + "] ... ");
try {
client.updateGeometry(targetFormId, targetId, targetField.getId(), geometry);
System.out.println("OK");
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}
}
Aggregations