Examples of SimpleFeatureBuilder


Examples of org.geotools.feature.simple.SimpleFeatureBuilder

                ((SimpleFeature) points3).getAttribute(2));

        Feature[] simplifiedPoints = new Feature[] { simplifiedPoints1, simplifiedPoints2,
                simplifiedPoints3 };

        final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(simplifiedPointsType);
        Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {
            @Override
            @Nullable
            public Optional<Feature> apply(@Nullable Feature feature) {
                SimpleFeature simpleFeature = (SimpleFeature) feature;
                featureBuilder.add(simpleFeature.getAttribute(0));
                featureBuilder.add(simpleFeature.getAttribute(2));
                return Optional.of((Feature) featureBuilder.buildFeature(null));
            }
        };

        Feature[] points = new Feature[] { points1, points2, points3 };
        for (Feature feature : points) {
            insert(feature);
        }
        MemoryDataStore dataStore = new MemoryDataStore(simplifiedPointsType);
        final String typeName = dataStore.getTypeNames()[0];
        SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
        SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
        geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName)
                .setFeatureTypeConversionFunction(function).call();
        featureSource = dataStore.getFeatureSource(typeName);
        featureStore = (SimpleFeatureStore) featureSource;
        SimpleFeatureCollection featureCollection = featureStore.getFeatures();
        assertEquals(featureCollection.size(), points.length);
        SimpleFeatureIterator features = featureCollection.features();
        assertTrue(collectionsAreEqual(features, simplifiedPoints));

        // check for exceptions when using a function that returns features with a wrong featuretype
        try {
            String wrongFeaturesName = "wrongFeatures";
            String wrongFeaturesTypeSpec = "sp:String";
            SimpleFeatureType wrongFeaturesType = DataUtilities.createType(pointsNs,
                    wrongFeaturesName, wrongFeaturesTypeSpec);
            final SimpleFeatureBuilder wrongFeatureBuilder = new SimpleFeatureBuilder(
                    wrongFeaturesType);
            Function<Feature, Optional<Feature>> wrongFunction = new Function<Feature, Optional<Feature>>() {
                @Override
                @Nullable
                public Optional<Feature> apply(@Nullable Feature feature) {
                    SimpleFeature simpleFeature = (SimpleFeature) feature;
                    wrongFeatureBuilder.add(simpleFeature.getAttribute(0));
                    return Optional.of((Feature) wrongFeatureBuilder.buildFeature(null));
                }
            };
            geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName)
                    .setFeatureTypeConversionFunction(wrongFunction).call();
            fail();
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

        String featureId = NodeRef.nodeFromPath(path);
        if (!featureTypes.containsKey(tree)) {
            Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class)
                    .setRefSpec("WORK_HEAD:" + tree).call();
            checkParameter(opt.isPresent(), "The parent tree does not exist: " + tree);
            SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) opt.get()
                    .type());
            featureTypes.put(tree, builder);
        }
        SimpleFeatureBuilder ftb = featureTypes.get(tree);
        SimpleFeatureType ft = ftb.getFeatureType();
        for (int i = 1; i < featureChanges.size(); i++) {
            String[] tokens = featureChanges.get(i).split("\t");
            Preconditions.checkArgument(tokens.length == 2, "Wrong attribute definition: "
                    + featureChanges.get(i));
            String fieldName = tokens[0];
            AttributeDescriptor desc = ft.getDescriptor(fieldName);
            Preconditions.checkNotNull(desc, "Wrong attribute in feature description");
            FieldType type = FieldType.forBinding(desc.getType().getBinding());
            Object value = TextValueSerializer.fromString(type, tokens[1]);
            ftb.set(tokens[0], value);
        }
        return ftb.buildFeature(featureId);
    }
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

        return geogig;
    }

    protected 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);
    }
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

    @SuppressWarnings("unchecked")
    private Feature merge(RevFeature featureA, RevFeature featureB, RevFeature ancestor,
            RevFeatureType featureType) {

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(
                (SimpleFeatureType) featureType.type());
        ImmutableList<Optional<Object>> valuesA = featureA.getValues();
        ImmutableList<Optional<Object>> valuesB = featureB.getValues();
        ImmutableList<Optional<Object>> valuesAncestor = ancestor.getValues();
        ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
        for (int i = 0; i < descriptors.size(); i++) {
            PropertyDescriptor descriptor = descriptors.get(i);
            boolean isGeom = Geometry.class.isAssignableFrom(descriptor.getType().getBinding());
            Name name = descriptor.getName();
            Optional<Object> valueAncestor = valuesAncestor.get(i);
            Optional<Object> valueA = valuesA.get(i);
            Optional<Object> valueB = valuesB.get(i);
            if (!valueA.equals(valueAncestor)) {
                Optional<Object> merged = valueA;
                if (isGeom && !valueB.equals(valueAncestor)) { // true merge is only done with
                                                               // geometries
                    GeometryAttributeDiff diffB = new GeometryAttributeDiff(
                            Optional.fromNullable((Geometry) valueAncestor.orNull()),
                            Optional.fromNullable((Geometry) valueB.orNull()));
                    merged = (Optional<Object>) diffB.applyOn(valueA);
                }
                featureBuilder.set(name, merged.orNull());
            } else {
                featureBuilder.set(name, valueB.orNull());
            }
        }
        return featureBuilder.buildFeature(nodeRefA.name());

    }
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

        assertEquals(wkt, values.get(2).get().toString());
        assertEquals(507464799l, values.get(0).get());

        // 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", "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);
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

        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);
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

        assertEquals(wkt, values.get(4).get().toString());
        assertEquals(507464799l, values.get(0).get());

        // 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);
        fb.set("tags",
                "VRS:gemeinde:BONN|VRS:ortsteil:Hoholz|VRS:ref:68566|bus:yes|highway:bus_stop|name:Gielgen|public_transport:platform");
        fb.set("timestamp", 1355097351000l);
        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);
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

        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();
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

        assertEquals(wkt, values.get(2).get().toString());
        assertEquals(507464799l, values.get(0).get());

        // 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", "newname");
        fb.set("id", 507464799l);
        SimpleFeature newFeature = fb.buildFeature("507464799");
        geogig.getRepository().workingTree().insert("busstops", newFeature);
        geogig.command(AddOp.class).call();
        geogig.command(CommitOp.class).setMessage("msg").call(); // this should trigger the hook

        // check that the unmapping has been triggered and the unmapped node has the changes we
View Full Code Here

Examples of org.geotools.feature.simple.SimpleFeatureBuilder

                }
                Preconditions.checkState(merges != null);

                Preconditions.checkState(ourFeatureType != null || theirFeatureType != null);

                SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(
                        (SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type()
                                : theirFeatureType.type()));

                ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType
                        : ourFeatureType).sortedDescriptors();

                for (Entry<String, JsonElement> entry : merges.entrySet()) {
                    int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors);
                    if (descriptorIndex != -1 && entry.getValue().isJsonObject()) {
                        PropertyDescriptor descriptor = descriptors.get(descriptorIndex);
                        JsonObject attributeObject = entry.getValue().getAsJsonObject();
                        if (attributeObject.has("ours")
                                && attributeObject.get("ours").isJsonPrimitive()
                                && attributeObject.get("ours").getAsBoolean()) {
                            featureBuilder.set(descriptor.getName(), ourFeature == null ? null
                                    : ourFeature.getValues().get(descriptorIndex).orNull());
                        } else if (attributeObject.has("theirs")
                                && attributeObject.get("theirs").isJsonPrimitive()
                                && attributeObject.get("theirs").getAsBoolean()) {
                            featureBuilder.set(descriptor.getName(), theirFeature == null ? null
                                    : theirFeature.getValues().get(descriptorIndex).orNull());
                        } else if (attributeObject.has("value")
                                && attributeObject.get("value").isJsonPrimitive()) {
                            JsonPrimitive primitive = attributeObject.get("value")
                                    .getAsJsonPrimitive();
                            if (primitive.isString()) {
                                try {
                                    Object object = valueFromString(
                                            FieldType.forBinding(descriptor.getType().getBinding()),
                                            primitive.getAsString());
                                    featureBuilder.set(descriptor.getName(), object);
                                } catch (Exception e) {
                                    throw new Exception("Unable to convert attribute ("
                                            + entry.getKey() + ") to required type: "
                                            + descriptor.getType().getBinding().toString());
                                }
                            } else if (primitive.isNumber()) {
                                try {
                                    Object value = valueFromNumber(
                                            FieldType.forBinding(descriptor.getType().getBinding()),
                                            primitive.getAsNumber());
                                    featureBuilder.set(descriptor.getName(), value);
                                } catch (Exception e) {
                                    throw new Exception("Unable to convert attribute ("
                                            + entry.getKey() + ") to required type: "
                                            + descriptor.getType().getBinding().toString());
                                }
                            } else if (primitive.isBoolean()) {
                                try {
                                    Object value = valueFromBoolean(
                                            FieldType.forBinding(descriptor.getType().getBinding()),
                                            primitive.getAsBoolean());
                                    featureBuilder.set(descriptor.getName(), value);
                                } catch (Exception e) {
                                    throw new Exception("Unable to convert attribute ("
                                            + entry.getKey() + ") to required type: "
                                            + descriptor.getType().getBinding().toString());
                                }
                            } else if (primitive.isJsonNull()) {
                                featureBuilder.set(descriptor.getName(), null);
                            } else {
                                throw new Exception("Unsupported JSON type for attribute value ("
                                        + entry.getKey() + ")");
                            }
                        }
                    }
                }
                SimpleFeature feature = featureBuilder
                        .buildFeature(NodeRef.nodeFromPath(featureId));
                RevFeature revFeature = RevFeatureBuilder.build(feature);
                ggit.getRepository().stagingDatabase().put(revFeature);

                getResponse().setEntity(
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.