Search in sources :

Example 1 with FeatureSourceStorageProvider

use of org.activityinfo.geoadmin.source.FeatureSourceStorageProvider 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());
            }
        }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) FeatureSourceStorage(org.activityinfo.geoadmin.source.FeatureSourceStorage) GeometryType(org.opengis.feature.type.GeometryType) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) ResourceId(org.activityinfo.model.resource.ResourceId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) FieldProfile(org.activityinfo.geoadmin.merge2.view.profile.FieldProfile) GeometryConverter(org.activityinfo.geoadmin.source.GeometryConverter) FeatureSourceStorageProvider(org.activityinfo.geoadmin.source.FeatureSourceStorageProvider) SimpleFeature(org.opengis.feature.simple.SimpleFeature) IOException(java.io.IOException)

Aggregations

Geometry (com.vividsolutions.jts.geom.Geometry)1 IOException (java.io.IOException)1 FieldProfile (org.activityinfo.geoadmin.merge2.view.profile.FieldProfile)1 FeatureSourceStorage (org.activityinfo.geoadmin.source.FeatureSourceStorage)1 FeatureSourceStorageProvider (org.activityinfo.geoadmin.source.FeatureSourceStorageProvider)1 GeometryConverter (org.activityinfo.geoadmin.source.GeometryConverter)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 GeometryType (org.opengis.feature.type.GeometryType)1