Search in sources :

Example 51 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMMapOpTest method testMappingNodesWithAlias.

@Test
public void testMappingNodesWithAlias() throws Exception {
    // import and check that we have nodes
    String filename = OSMImportOp.class.getResource("nodes.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    WorkingTree workTree = geogig.getRepository().workingTree();
    long unstaged = workTree.countUnstaged("node").count();
    assertTrue(unstaged > 0);
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").call();
    // Define mapping
    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("the_geometry", FieldType.POINT));
    fields.put("name", new AttributeDefinition("the_name", 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();
    // Check that mapping was correctly performed
    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();
    ImmutableList<PropertyDescriptor> descriptors = featureType.get().sortedDescriptors();
    assertEquals("the_name", descriptors.get(1).getName().getLocalPart());
    assertEquals("Gielgen", values.get(1).get());
    assertEquals("the_geometry", descriptors.get(2).getName().getLocalPart());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) WorkingTree(org.locationtech.geogig.repository.WorkingTree) 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) Test(org.junit.Test)

Example 52 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMMapOpTest method testMappingWays.

@Test
public void testMappingWays() throws Exception {
    // import and check that we have both ways and nodes
    String filename = OSMImportOp.class.getResource("ways.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    WorkingTree workTree = geogig.getRepository().workingTree();
    long unstaged = workTree.countUnstaged("way").count();
    assertTrue(unstaged > 0);
    unstaged = workTree.countUnstaged("node").count();
    assertTrue(unstaged > 0);
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").call();
    // Define mapping
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> filter = Maps.newHashMap();
    filter.put("oneway", Lists.newArrayList("yes"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.LINESTRING));
    fields.put("lit", new AttributeDefinition("lit", FieldType.STRING));
    Map<String, List<String>> filterExclude = Maps.newHashMap();
    MappingRule mappingRule = new MappingRule("onewaystreets", filter, filterExclude, fields, null);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    geogig.command(OSMMapOp.class).setMapping(mapping).call();
    // Check that mapping was correctly performed
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:onewaystreets/31045880").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    assertEquals(4, values.size());
    String wkt = "LINESTRING (7.1923367 50.7395887, 7.1923127 50.7396946, 7.1923444 50.7397419, 7.1924199 50.7397781)";
    assertEquals(wkt, values.get(2).get().toString());
    assertEquals("yes", values.get(1).get());
    // Check that the corresponding log files have been added
    File osmMapFolder = geogig.command(ResolveOSMMappingLogFolder.class).call();
    file = new File(osmMapFolder, "onewaystreets");
    assertTrue(file.exists());
    file = new File(osmMapFolder, geogig.getRepository().workingTree().getTree().getId().toString());
    assertTrue(file.exists());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) ResolveOSMMappingLogFolder(org.locationtech.geogig.osm.internal.log.ResolveOSMMappingLogFolder) Optional(com.google.common.base.Optional) WorkingTree(org.locationtech.geogig.repository.WorkingTree) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 53 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMMapOpTest method testMappingDefaultFields.

@Test
public void testMappingDefaultFields() throws Exception {
    // import and check that we have both ways and nodes
    String filename = OSMImportOp.class.getResource("ways.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    WorkingTree workTree = geogig.getRepository().workingTree();
    long unstaged = workTree.countUnstaged("way").count();
    assertTrue(unstaged > 0);
    unstaged = workTree.countUnstaged("node").count();
    assertTrue(unstaged > 0);
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").call();
    // Define mapping
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> filter = Maps.newHashMap();
    filter.put("oneway", Lists.newArrayList("yes"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.LINESTRING));
    fields.put("lit", new AttributeDefinition("lit", FieldType.STRING));
    ArrayList<DefaultField> defaultFields = Lists.newArrayList();
    defaultFields.add(DefaultField.timestamp);
    defaultFields.add(DefaultField.visible);
    MappingRule mappingRule = new MappingRule("onewaystreets", filter, null, fields, defaultFields);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    geogig.command(OSMMapOp.class).setMapping(mapping).call();
    // Check that mapping was correctly performed
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:onewaystreets/31045880").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    assertEquals(6, values.size());
    String wkt = "LINESTRING (7.1923367 50.7395887, 7.1923127 50.7396946, 7.1923444 50.7397419, 7.1924199 50.7397781)";
    assertEquals(wkt, values.get(4).get().toString());
    assertEquals("yes", values.get(3).get());
    assertEquals(true, values.get(2).get());
    assertEquals(1318750940000L, values.get(1).get());
    Optional<RevFeatureType> revFeatureType = geogig.command(ResolveFeatureType.class).setRefSpec("HEAD:onewaystreets/31045880").call();
    assertTrue(revFeatureType.isPresent());
    ImmutableList<PropertyDescriptor> descriptors = revFeatureType.get().sortedDescriptors();
    assertEquals("timestamp", descriptors.get(1).getName().toString());
    assertEquals("visible", descriptors.get(2).getName().toString());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) WorkingTree(org.locationtech.geogig.repository.WorkingTree) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DefaultField(org.locationtech.geogig.osm.internal.MappingRule.DefaultField) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Example 54 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class ExportDiffOp method getFeatures.

private static Iterator<SimpleFeature> getFeatures(Iterator<DiffEntry> diffs, final boolean old, final ObjectDatabase database, final ObjectId metadataId, final ProgressListener progressListener) {
    final SimpleFeatureType featureType = addFidAttribute(database.getFeatureType(metadataId));
    final RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
    final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
    Function<DiffEntry, SimpleFeature> asFeature = new Function<DiffEntry, SimpleFeature>() {

        @Override
        @Nullable
        public SimpleFeature apply(final DiffEntry input) {
            NodeRef nodeRef = old ? input.getOldObject() : input.getNewObject();
            if (nodeRef == null) {
                return null;
            }
            final RevFeature revFeature = database.getFeature(nodeRef.objectId());
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < values.size(); i++) {
                String name = featureType.getDescriptor(i + 1).getLocalName();
                Object value = values.get(i).orNull();
                featureBuilder.set(name, value);
            }
            featureBuilder.set("geogig_fid", nodeRef.name());
            Feature feature = featureBuilder.buildFeature(nodeRef.name());
            feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
            feature.getUserData().put(RevFeature.class, revFeature);
            feature.getUserData().put(RevFeatureType.class, revFeatureType);
            if (feature instanceof SimpleFeature) {
                return (SimpleFeature) feature;
            }
            return null;
        }
    };
    Iterator<SimpleFeature> asFeatures = Iterators.transform(diffs, asFeature);
    UnmodifiableIterator<SimpleFeature> filterNulls = Iterators.filter(asFeatures, Predicates.notNull());
    return filterNulls;
}
Also used : Optional(com.google.common.base.Optional) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Function(com.google.common.base.Function) NodeRef(org.locationtech.geogig.api.NodeRef) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 55 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class ImportOp method alter.

/**
     * Translates a feature pointed by a node from its original feature type to a given one, using
     * values from those attributes that exist in both original and destination feature type. New
     * attributes are populated with null values
     * 
     * @param node The node that points to the feature. No checking is performed to ensure the node
     *        points to a feature instead of other type
     * @param featureType the destination feature type
     * @return a feature with the passed feature type and data taken from the input feature
     */
private Feature alter(NodeRef node, RevFeatureType featureType) {
    RevFeature oldFeature = command(RevObjectParse.class).setObjectId(node.objectId()).call(RevFeature.class).get();
    RevFeatureType oldFeatureType;
    oldFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class).get();
    ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
    ImmutableList<PropertyDescriptor> newAttributes = featureType.sortedDescriptors();
    ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
    List<Optional<Object>> newValues = Lists.newArrayList();
    for (int i = 0; i < newAttributes.size(); i++) {
        int idx = oldAttributes.indexOf(newAttributes.get(i));
        if (idx != -1) {
            Optional<Object> oldValue = oldValues.get(idx);
            newValues.add(oldValue);
        } else {
            newValues.add(Optional.absent());
        }
    }
    RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
    FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
    Feature feature = featureBuilder.build(node.name(), newFeature);
    return feature;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DecoratingFeature(org.geotools.feature.DecoratingFeature) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Aggregations

RevFeature (org.locationtech.geogig.api.RevFeature)89 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)46 Test (org.junit.Test)45 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)43 Optional (com.google.common.base.Optional)40 NodeRef (org.locationtech.geogig.api.NodeRef)32 File (java.io.File)24 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)24 ObjectId (org.locationtech.geogig.api.ObjectId)21 AddOp (org.locationtech.geogig.api.porcelain.AddOp)21 ImmutableList (com.google.common.collect.ImmutableList)20 Feature (org.opengis.feature.Feature)20 List (java.util.List)19 RevObject (org.locationtech.geogig.api.RevObject)18 RevTree (org.locationtech.geogig.api.RevTree)16 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)16 SimpleFeature (org.opengis.feature.simple.SimpleFeature)16 ArrayList (java.util.ArrayList)15 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)15 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)15