Search in sources :

Example 26 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature 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());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) WorkingTree(org.locationtech.geogig.repository.WorkingTree) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Example 27 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature 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());
        }
    }
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) Name(org.opengis.feature.type.Name) WorkingTree(org.locationtech.geogig.repository.WorkingTree) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DepthSearch(org.locationtech.geogig.repository.DepthSearch) FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 28 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class ExportDiffOp method _call.

/**
     * Executes the export operation using the parameters that have been specified.
     * 
     * @return a FeatureCollection with the specified features
     */
@Override
protected SimpleFeatureStore _call() {
    final SimpleFeatureStore targetStore = getTargetStore();
    final String refspec = old ? oldRef : newRef;
    final RevTree rootTree = resolveRootTree(refspec);
    final NodeRef typeTreeRef = resolTypeTreeRef(refspec, path, rootTree);
    final ObjectId defaultMetadataId = typeTreeRef.getMetadataId();
    final ProgressListener progressListener = getProgressListener();
    progressListener.started();
    progressListener.setDescription("Exporting diffs for path '" + path + "'... ");
    FeatureCollection<SimpleFeatureType, SimpleFeature> asFeatureCollection = new BaseFeatureCollection<SimpleFeatureType, SimpleFeature>() {

        @Override
        public FeatureIterator<SimpleFeature> features() {
            Iterator<DiffEntry> diffs = command(DiffOp.class).setOldVersion(oldRef).setNewVersion(newRef).setFilter(path).call();
            final Iterator<SimpleFeature> plainFeatures = getFeatures(diffs, old, stagingDatabase(), defaultMetadataId, progressListener);
            Iterator<Optional<Feature>> transformed = Iterators.transform(plainFeatures, ExportDiffOp.this.function);
            Iterator<SimpleFeature> filtered = Iterators.filter(Iterators.transform(transformed, new Function<Optional<Feature>, SimpleFeature>() {

                @Override
                public SimpleFeature apply(Optional<Feature> input) {
                    return (SimpleFeature) (input.isPresent() ? input.get() : null);
                }
            }), Predicates.notNull());
            return new DelegateFeatureIterator<SimpleFeature>(filtered);
        }
    };
    // add the feature collection to the feature store
    final Transaction transaction;
    if (transactional) {
        transaction = new DefaultTransaction("create");
    } else {
        transaction = Transaction.AUTO_COMMIT;
    }
    try {
        targetStore.setTransaction(transaction);
        try {
            targetStore.addFeatures(asFeatureCollection);
            transaction.commit();
        } catch (final Exception e) {
            if (transactional) {
                transaction.rollback();
            }
            Throwables.propagateIfInstanceOf(e, GeoToolsOpException.class);
            throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
        } finally {
            transaction.close();
        }
    } catch (IOException e) {
        throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
    }
    progressListener.complete();
    return targetStore;
}
Also used : DelegateFeatureIterator(org.geotools.feature.collection.DelegateFeatureIterator) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) IOException(java.io.IOException) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) BaseFeatureCollection(org.geotools.feature.collection.BaseFeatureCollection) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 29 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class ExportOp method adaptToArguments.

private Iterator<SimpleFeature> adaptToArguments(final Iterator<SimpleFeature> plainFeatures, final ObjectId defaultMetadataId) {
    Iterator<SimpleFeature> features = plainFeatures;
    if (alter) {
        ObjectId featureTypeId = this.filterFeatureTypeId == null ? defaultMetadataId : this.filterFeatureTypeId;
        features = alter(features, featureTypeId);
    } else if (forceExportDefaultFeatureType) {
        features = filter(features, defaultMetadataId);
    } else if (this.filterFeatureTypeId != null) {
        features = filter(features, filterFeatureTypeId);
    } else {
        features = force(features, defaultMetadataId);
    }
    return features;
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 30 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class ExportOp method getFeatures.

private static Iterator<SimpleFeature> getFeatures(final RevTree typeTree, final ObjectDatabase database, final ObjectId defaultMetadataId, final ProgressListener progressListener) {
    Iterator<NodeRef> nodes = new DepthTreeIterator("", defaultMetadataId, typeTree, database, Strategy.FEATURES_ONLY);
    // progress reporting
    nodes = Iterators.transform(nodes, new Function<NodeRef, NodeRef>() {

        private AtomicInteger count = new AtomicInteger();

        @Override
        public NodeRef apply(NodeRef input) {
            progressListener.setProgress((count.incrementAndGet() * 100.f) / typeTree.size());
            return input;
        }
    });
    Function<NodeRef, SimpleFeature> asFeature = new Function<NodeRef, SimpleFeature>() {

        private Map<ObjectId, FeatureBuilder> ftCache = Maps.newHashMap();

        @Override
        @Nullable
        public SimpleFeature apply(final NodeRef input) {
            final ObjectId metadataId = input.getMetadataId();
            final RevFeature revFeature = database.getFeature(input.objectId());
            FeatureBuilder featureBuilder = getBuilderFor(metadataId);
            Feature feature = featureBuilder.build(input.name(), revFeature);
            feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
            feature.getUserData().put(RevFeature.class, revFeature);
            feature.getUserData().put(RevFeatureType.class, featureBuilder.getType());
            if (feature instanceof SimpleFeature) {
                return (SimpleFeature) feature;
            }
            return null;
        }

        private FeatureBuilder getBuilderFor(final ObjectId metadataId) {
            FeatureBuilder featureBuilder = ftCache.get(metadataId);
            if (featureBuilder == null) {
                RevFeatureType revFtype = database.getFeatureType(metadataId);
                featureBuilder = new FeatureBuilder(revFtype);
                ftCache.put(metadataId, featureBuilder);
            }
            return featureBuilder;
        }
    };
    Iterator<SimpleFeature> asFeatures = Iterators.transform(nodes, asFeature);
    UnmodifiableIterator<SimpleFeature> filterNulls = Iterators.filter(asFeatures, Predicates.notNull());
    return filterNulls;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RevFeature(org.locationtech.geogig.api.RevFeature) DepthTreeIterator(org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator) Map(java.util.Map) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Aggregations

SimpleFeature (org.opengis.feature.simple.SimpleFeature)66 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)32 Test (org.junit.Test)27 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)21 RevFeature (org.locationtech.geogig.api.RevFeature)17 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)16 Optional (com.google.common.base.Optional)15 Coordinate (com.vividsolutions.jts.geom.Coordinate)15 NodeRef (org.locationtech.geogig.api.NodeRef)13 ObjectId (org.locationtech.geogig.api.ObjectId)13 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)12 File (java.io.File)10 ArrayList (java.util.ArrayList)10 DefaultTransaction (org.geotools.data.DefaultTransaction)10 WorkingTree (org.locationtech.geogig.repository.WorkingTree)10 IOException (java.io.IOException)9 List (java.util.List)9 Feature (org.opengis.feature.Feature)9 Function (com.google.common.base.Function)8 Transaction (org.geotools.data.Transaction)8