Search in sources :

Example 1 with FeatureBuilder

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

the class GeoGigAPI method getFeatureFromWorkingTree.

/**
     * Returns a feature from the working tree of the repository, given its full path
     * 
     * Returns null if the given path doesn't resolve to a feature
     * 
     * @param path the path to the feature to return
     */
public Feature getFeatureFromWorkingTree(String path) {
    String name = NodeRef.nodeFromPath(path);
    String refSpec = "WORK_HEAD:" + path;
    Optional<RevFeature> revFeature = repository.command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class);
    if (revFeature.isPresent()) {
        RevFeatureType revFeatureType = repository.command(ResolveFeatureType.class).setRefSpec(refSpec).call().get();
        FeatureBuilder builder = new FeatureBuilder(revFeatureType);
        return builder.build(name, revFeature.get());
    } else {
        return null;
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ResolveFeatureType(org.locationtech.geogig.api.plumbing.ResolveFeatureType) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 2 with FeatureBuilder

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

the class GeoGigAPI method getUnstagedFeatures.

public Feature[] getUnstagedFeatures(String path, boolean noDeletions) {
    Iterator<DiffEntry> diffs = repository.workingTree().getUnstaged(path);
    List<Feature> list = Lists.newArrayList();
    while (diffs.hasNext()) {
        DiffEntry diff = diffs.next();
        if (!diff.changeType().equals(ChangeType.REMOVED) || !noDeletions) {
            RevFeature revFeature = repository.command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
            RevFeatureType revFeatureType = repository.command(RevObjectParse.class).setObjectId(diff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
            FeatureBuilder builder = new FeatureBuilder(revFeatureType);
            list.add(builder.build(diff.getNewObject().name(), revFeature));
        }
    }
    return list.toArray(new Feature[0]);
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 3 with FeatureBuilder

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

the class ResponseWriter method writeMerged.

/**
     * Writes the response for a set of merged features while also supplying the geometry.
     * 
     * @param geogig - a CommandLocator to call commands from
     * @param features - a FeatureInfo iterator to build the response from
     * @throws XMLStreamException
     */
public void writeMerged(final Context geogig, Iterator<FeatureInfo> features) throws XMLStreamException {
    Iterator<GeometryChange> changeIterator = Iterators.transform(features, new Function<FeatureInfo, GeometryChange>() {

        @Override
        public GeometryChange apply(FeatureInfo input) {
            GeometryChange change = null;
            RevFeature revFeature = RevFeatureBuilder.build(input.getFeature());
            RevFeatureType featureType = input.getFeatureType();
            Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
            String crsCode = null;
            for (PropertyDescriptor attrib : attribs) {
                PropertyType attrType = attrib.getType();
                if (attrType instanceof GeometryType) {
                    GeometryType gt = (GeometryType) attrType;
                    CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
                    if (crs != null) {
                        try {
                            crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                        } catch (FactoryException e) {
                            crsCode = null;
                        }
                        if (crsCode != null) {
                            crsCode = "EPSG:" + crsCode;
                        }
                    }
                    break;
                }
            }
            FeatureBuilder builder = new FeatureBuilder(featureType);
            GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(revFeature.getId().toString(), revFeature);
            change = new GeometryChange(simpleFeature, ChangeType.MODIFIED, input.getPath(), crsCode);
            return change;
        }
    });
    while (changeIterator.hasNext()) {
        GeometryChange next = changeIterator.next();
        if (next != null) {
            GeogigSimpleFeature feature = next.getFeature();
            out.writeStartElement("Feature");
            writeElement("change", "MERGED");
            writeElement("id", next.getPath());
            List<Object> attributes = feature.getAttributes();
            for (Object attribute : attributes) {
                if (attribute instanceof Geometry) {
                    writeElement("geometry", ((Geometry) attribute).toText());
                    break;
                }
            }
            if (next.getCRS() != null) {
                writeElement("crs", next.getCRS());
            }
            out.writeEndElement();
        }
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) FactoryException(org.opengis.referencing.FactoryException) FeatureInfo(org.locationtech.geogig.api.FeatureInfo) PropertyType(org.opengis.feature.type.PropertyType) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryType(org.opengis.feature.type.GeometryType) RevFeature(org.locationtech.geogig.api.RevFeature) Collection(java.util.Collection) RevObject(org.locationtech.geogig.api.RevObject) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeogigSimpleFeature(org.locationtech.geogig.api.GeogigSimpleFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 4 with FeatureBuilder

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

the class OSMMapOp method getFeatures.

private Iterator<Feature> getFeatures(String ref) {
    Optional<ObjectId> id = command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    LsTreeOp op = command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
    Iterator<NodeRef> iterator = op.call();
    Function<NodeRef, Feature> nodeRefToFeature = new Function<NodeRef, Feature>() {

        private final //
        Map<String, FeatureBuilder> builders = //
        ImmutableMap.<//
        String, //
        FeatureBuilder>of(//
        OSMUtils.NODE_TYPE_NAME, //
        new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.nodeType())), //
        OSMUtils.WAY_TYPE_NAME, new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.wayType())));

        private final RevObjectParse parseCommand = command(RevObjectParse.class);

        @Override
        @Nullable
        public Feature apply(@Nullable NodeRef ref) {
            RevFeature revFeature = parseCommand.setObjectId(ref.objectId()).call(RevFeature.class).get();
            final String parentPath = ref.getParentPath();
            FeatureBuilder featureBuilder = builders.get(parentPath);
            String fid = ref.name();
            Feature feature = featureBuilder.build(fid, revFeature);
            return feature;
        }
    };
    return Iterators.transform(iterator, nodeRefToFeature);
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 5 with FeatureBuilder

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

the class PatchSerializer method addElement.

private static void addElement(List<String> lines, Patch patch, Map<String, RevFeatureType> featureTypes) {
    String[] headerTokens = lines.get(0).split("\t");
    if (headerTokens.length == 4 || headerTokens.length == 3) {
        // modified // modification
        if (lines.size() == 1) {
            // feature type
            FeatureTypeDiff diff = new FeatureTypeDiff(headerTokens[0], ObjectId.valueOf(headerTokens[1]), ObjectId.valueOf(headerTokens[2]));
            patch.addAlteredTree(diff);
        } else {
            // feature
            String element = Joiner.on("\n").join(lines.subList(1, lines.size()));
            ByteArrayInputStream stream;
            stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
            String operation = headerTokens[0].trim();
            if (operation.equals("M")) {
                String fullPath = headerTokens[1].trim();
                String oldMetadataId = headerTokens[2].trim();
                String newMetadataId = headerTokens[3].trim();
                RevFeatureType newRevFeatureType = featureTypes.get(newMetadataId);
                RevFeatureType oldRevFeatureType = featureTypes.get(oldMetadataId);
                Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
                for (int i = 1; i < lines.size(); i++) {
                    addDifference(lines.get(i), map, oldRevFeatureType, newRevFeatureType);
                }
                FeatureDiff featureDiff = new FeatureDiff(fullPath, map, oldRevFeatureType, newRevFeatureType);
                patch.addModifiedFeature(featureDiff);
            } else if (operation.equals("A") || operation.equals("R")) {
                String fullPath = headerTokens[1].trim();
                String featureTypeId = headerTokens[2].trim();
                RevFeatureType revFeatureType;
                revFeatureType = featureTypes.get(featureTypeId);
                FeatureBuilder featureBuilder = new FeatureBuilder(revFeatureType);
                ObjectReader<RevFeature> reader = factory.createFeatureReader();
                RevFeature revFeature = reader.read(null, stream);
                Feature feature = featureBuilder.build(NodeRef.nodeFromPath(fullPath), revFeature);
                if (operation.equals("R")) {
                    patch.addRemovedFeature(fullPath, feature, revFeatureType);
                } else {
                    patch.addAddedFeature(fullPath, feature, revFeatureType);
                }
            } else {
                throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
            }
        }
    } else if (headerTokens.length == 1) {
        // feature type definition
        String element = Joiner.on("\n").join(lines);
        ByteArrayInputStream stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
        String[] tokens = lines.get(1).split("\t");
        ObjectReader<RevFeatureType> reader = factory.createFeatureTypeReader();
        RevFeatureType featureType = reader.read(null, stream);
        featureTypes.put(featureType.getId().toString(), featureType);
    } else {
        throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) ByteArrayInputStream(java.io.ByteArrayInputStream) RevFeature(org.locationtech.geogig.api.RevFeature) ObjectReader(org.locationtech.geogig.storage.ObjectReader) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Aggregations

FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)15 RevFeature (org.locationtech.geogig.api.RevFeature)15 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)13 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)9 Feature (org.opengis.feature.Feature)8 ObjectId (org.locationtech.geogig.api.ObjectId)7 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)6 NodeRef (org.locationtech.geogig.api.NodeRef)5 RevFeatureBuilder (org.locationtech.geogig.api.RevFeatureBuilder)5 RevObject (org.locationtech.geogig.api.RevObject)5 Optional (com.google.common.base.Optional)4 RevTree (org.locationtech.geogig.api.RevTree)4 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)4 SimpleFeature (org.opengis.feature.simple.SimpleFeature)4 Function (com.google.common.base.Function)3 Geometry (com.vividsolutions.jts.geom.Geometry)3 Collection (java.util.Collection)3 GeogigSimpleFeature (org.locationtech.geogig.api.GeogigSimpleFeature)3 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)3 GeometryType (org.opengis.feature.type.GeometryType)3