Search in sources :

Example 31 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class OSMUnmapOpTest method testMappingAndUnmappingOfWaysWithAlias.

@Test
public void testMappingAndUnmappingOfWaysWithAlias() throws Exception {
    // Import
    String filename = OSMImportOp.class.getResource("ways.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("residential"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.LINESTRING));
    fields.put("name", new AttributeDefinition("name_alias", FieldType.STRING));
    Map<String, List<String>> filterExclude = Maps.newHashMap();
    MappingRule mappingRule = new MappingRule("residential", 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:residential/31347480").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class).setRefSpec("WORK_HEAD:residential/31347480").call();
    assertTrue(featureType.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    assertEquals(4, values.size());
    // unmap without having made any changes and check that the canonical folders are not
    // modified
    WorkingTree workTree = geogig.getRepository().workingTree();
    geogig.command(OSMUnmapOp.class).setPath("residential").call();
    long unstaged = workTree.countUnstaged("way").count();
    assertEquals(0, unstaged);
    unstaged = workTree.countUnstaged("node").count();
    assertEquals(0, unstaged);
    // modify a mapped feature. We change the value of 'name_alias' tag to "newvalue"
    ArrayList<Coordinate> coords = Lists.newArrayList(((Geometry) values.get(2).get()).getCoordinates());
    coords.add(new Coordinate(0, 1));
    assertEquals(31347480l, values.get(0).get());
    GeometryFactory gf = new GeometryFactory();
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder((SimpleFeatureType) featureType.get().type());
    fb.set("geom", gf.createLineString(coords.toArray(new Coordinate[0])));
    fb.set("name_alias", "newname");
    fb.set("id", 31347480l);
    fb.set("nodes", values.get(3).get());
    SimpleFeature newFeature = fb.buildFeature("31347480");
    geogig.getRepository().workingTree().insert("residential", newFeature);
    Optional<RevFeature> mapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:residential/31347480").call(RevFeature.class);
    assertTrue(mapped.isPresent());
    values = mapped.get().getValues();
    assertEquals("LINESTRING (7.1960069 50.7399033, 7.195868 50.7399081, 7.1950788 50.739912, 7.1949262 50.7399053, " + "7.1942463 50.7398686, 7.1935778 50.7398262, 7.1931011 50.7398018, 7.1929987 50.7398009, 7.1925978 50.7397889, " + "7.1924199 50.7397781, 0 1)", values.get(2).get().toString());
    assertEquals(31347480l, ((Long) values.get(0).get()).longValue());
    assertEquals("newname", values.get(1).get().toString());
    // unmap
    geogig.command(OSMUnmapOp.class).setPath("residential").call();
    // Check that raw OSM data was updated
    // First, we check that the corresponding way has been modified
    Optional<RevFeature> unmapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:way/31347480").call(RevFeature.class);
    assertTrue(unmapped.isPresent());
    values = unmapped.get().getValues();
    assertEquals("LINESTRING (7.1960069 50.7399033, 7.195868 50.7399081, 7.1950788 50.739912, 7.1949262 50.7399053, " + "7.1942463 50.7398686, 7.1935778 50.7398262, 7.1931011 50.7398018, 7.1929987 50.7398009, 7.1925978 50.7397889, " + "7.1924199 50.7397781, 0 1)", values.get(7).get().toString());
    assertEquals("lit:no|highway:residential|name:newname|oneway:yes", values.get(3).get().toString());
    // now we get the 'nodes' field in the unmapped feature and check the id of its last
    // node, which refers to the node that we have added to the geometry
    int WAY_NODES_FIELD = 6;
    String nodes = values.get(WAY_NODES_FIELD).get().toString();
    String[] nodeIds = nodes.split(";");
    String newNodeId = nodeIds[nodeIds.length - 1];
    // and we check that the node has been added to the 'node' tree and has the right
    // coordinates.
    Optional<RevFeature> newNode = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/" + newNodeId).call(RevFeature.class);
    assertTrue(newNode.isPresent());
    values = newNode.get().getValues();
    int NODE_GEOM_FIELD = 6;
    assertEquals("POINT (0 1)", values.get(NODE_GEOM_FIELD).get().toString());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) 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) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Coordinate(com.vividsolutions.jts.geom.Coordinate) File(java.io.File) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Example 32 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder 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 33 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class TestFeatures method feature.

public static Feature feature(SimpleFeatureType type, String id, Object... values) throws ParseException {
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        if (type.getDescriptor(i) instanceof GeometryDescriptor) {
            if (value instanceof String) {
                value = new WKTReader2().read((String) value);
            }
        }
        builder.set(i, value);
    }
    return builder.buildFeature(id);
}
Also used : GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) WKTReader2(org.geotools.geometry.jts.WKTReader2) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 34 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class ShpExport method getTransformingFunction.

private Function<Feature, Optional<Feature>> getTransformingFunction(final SimpleFeatureType featureType) {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        @Nullable
        public Optional<Feature> apply(@Nullable Feature feature) {
            SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
            for (Property property : feature.getProperties()) {
                if (property instanceof GeometryAttribute) {
                    builder.set(featureType.getGeometryDescriptor().getName(), property.getValue());
                } else {
                    String name = property.getName().getLocalPart();
                    if (name.length() > 10) {
                        name = name.substring(0, 10);
                    }
                    builder.set(name, property.getValue());
                }
            }
            Feature modifiedFeature = builder.buildFeature(feature.getIdentifier().getID());
            return Optional.fromNullable(modifiedFeature);
        }
    };
    return function;
}
Also used : Function(com.google.common.base.Function) Optional(com.google.common.base.Optional) GeometryAttribute(org.opengis.feature.GeometryAttribute) Feature(org.opengis.feature.Feature) Property(org.opengis.feature.Property) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 35 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class ShpExportDiff method getTransformingFunction.

private Function<Feature, Optional<Feature>> getTransformingFunction(final SimpleFeatureType featureType) {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        @Nullable
        public Optional<Feature> apply(@Nullable Feature feature) {
            SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
            for (Property property : feature.getProperties()) {
                if (property instanceof GeometryAttribute) {
                    builder.set(featureType.getGeometryDescriptor().getName(), property.getValue());
                } else {
                    builder.set(property.getName(), property.getValue());
                }
            }
            Feature modifiedFeature = builder.buildFeature(feature.getIdentifier().getID());
            return Optional.fromNullable(modifiedFeature);
        }
    };
    return function;
}
Also used : Function(com.google.common.base.Function) Optional(com.google.common.base.Optional) GeometryAttribute(org.opengis.feature.GeometryAttribute) Feature(org.opengis.feature.Feature) Property(org.opengis.feature.Property) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)56 SimpleFeature (org.opengis.feature.simple.SimpleFeature)34 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)25 Optional (com.google.common.base.Optional)17 ArrayList (java.util.ArrayList)15 RevFeature (org.locationtech.geogig.api.RevFeature)14 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)13 Test (org.junit.Test)11 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)10 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)10 File (java.io.File)9 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)9 Coordinate (com.vividsolutions.jts.geom.Coordinate)8 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)8 Point (com.vividsolutions.jts.geom.Point)8 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)8 Geometry (com.vividsolutions.jts.geom.Geometry)7 List (java.util.List)7