Search in sources :

Example 1 with GeogigSimpleFeature

use of org.locationtech.geogig.api.GeogigSimpleFeature 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 2 with GeogigSimpleFeature

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

the class ResponseWriter method writeGeometryChanges.

/**
     * Writes the response for a set of diffs while also supplying the geometry.
     * 
     * @param geogig - a CommandLocator to call commands from
     * @param diff - a DiffEntry iterator to build the response from
     * @throws XMLStreamException
     */
public void writeGeometryChanges(final Context geogig, Iterator<DiffEntry> diff, int page, int elementsPerPage) throws XMLStreamException {
    Iterators.advance(diff, page * elementsPerPage);
    int counter = 0;
    Iterator<GeometryChange> changeIterator = Iterators.transform(diff, new Function<DiffEntry, GeometryChange>() {

        @Override
        public GeometryChange apply(DiffEntry input) {
            Optional<RevObject> feature = Optional.absent();
            Optional<RevObject> type = Optional.absent();
            String path = null;
            String crsCode = null;
            GeometryChange change = null;
            if (input.changeType() == ChangeType.ADDED || input.changeType() == ChangeType.MODIFIED) {
                feature = geogig.command(RevObjectParse.class).setObjectId(input.newObjectId()).call();
                type = geogig.command(RevObjectParse.class).setObjectId(input.getNewObject().getMetadataId()).call();
                path = input.getNewObject().path();
            } else if (input.changeType() == ChangeType.REMOVED) {
                feature = geogig.command(RevObjectParse.class).setObjectId(input.oldObjectId()).call();
                type = geogig.command(RevObjectParse.class).setObjectId(input.getOldObject().getMetadataId()).call();
                path = input.getOldObject().path();
            }
            if (feature.isPresent() && feature.get() instanceof RevFeature && type.isPresent() && type.get() instanceof RevFeatureType) {
                RevFeatureType featureType = (RevFeatureType) type.get();
                Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
                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;
                    }
                }
                RevFeature revFeature = (RevFeature) feature.get();
                FeatureBuilder builder = new FeatureBuilder(featureType);
                GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(revFeature.getId().toString(), revFeature);
                change = new GeometryChange(simpleFeature, input.changeType(), path, crsCode);
            }
            return change;
        }
    });
    while (changeIterator.hasNext() && (elementsPerPage == 0 || counter < elementsPerPage)) {
        GeometryChange next = changeIterator.next();
        if (next != null) {
            GeogigSimpleFeature feature = next.getFeature();
            ChangeType change = next.getChangeType();
            out.writeStartElement("Feature");
            writeElement("change", change.toString());
            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();
            counter++;
        }
    }
    if (changeIterator.hasNext()) {
        writeElement("nextPage", "true");
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) FactoryException(org.opengis.referencing.FactoryException) PropertyType(org.opengis.feature.type.PropertyType) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryType(org.opengis.feature.type.GeometryType) ChangeType(org.locationtech.geogig.api.plumbing.diff.DiffEntry.ChangeType) RevFeature(org.locationtech.geogig.api.RevFeature) Collection(java.util.Collection) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeogigSimpleFeature(org.locationtech.geogig.api.GeogigSimpleFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 3 with GeogigSimpleFeature

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

the class ResponseWriter method writeConflicts.

/**
     * Writes the response for a set of conflicts while also supplying the geometry.
     * 
     * @param geogig - a CommandLocator to call commands from
     * @param conflicts - a Conflict iterator to build the response from
     * @throws XMLStreamException
     */
public void writeConflicts(final Context geogig, Iterator<Conflict> conflicts, final ObjectId ours, final ObjectId theirs) throws XMLStreamException {
    Iterator<GeometryConflict> conflictIterator = Iterators.transform(conflicts, new Function<Conflict, GeometryConflict>() {

        @Override
        public GeometryConflict apply(Conflict input) {
            ObjectId commitId = ours;
            if (input.getOurs().equals(ObjectId.NULL)) {
                commitId = theirs;
            }
            Optional<RevObject> object = geogig.command(RevObjectParse.class).setObjectId(commitId).call();
            RevCommit commit = null;
            if (object.isPresent() && object.get() instanceof RevCommit) {
                commit = (RevCommit) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve id: " + commitId.toString() + " to a commit");
            }
            object = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call();
            Optional<NodeRef> node = Optional.absent();
            if (object.isPresent()) {
                RevTree tree = (RevTree) object.get();
                node = geogig.command(FindTreeChild.class).setParent(tree).setChildPath(input.getPath()).call();
            } else {
                throw new CommandSpecException("Couldn't resolve commit's treeId");
            }
            RevFeatureType type = null;
            RevFeature feature = null;
            if (node.isPresent()) {
                object = geogig.command(RevObjectParse.class).setObjectId(node.get().getMetadataId()).call();
                if (object.isPresent() && object.get() instanceof RevFeatureType) {
                    type = (RevFeatureType) object.get();
                } else {
                    throw new CommandSpecException("Couldn't resolve newCommit's featureType");
                }
                object = geogig.command(RevObjectParse.class).setObjectId(node.get().objectId()).call();
                if (object.isPresent() && object.get() instanceof RevFeature) {
                    feature = (RevFeature) object.get();
                } else {
                    throw new CommandSpecException("Couldn't resolve newCommit's feature");
                }
            }
            GeometryConflict conflict = null;
            if (feature != null && type != null) {
                String crsCode = null;
                Collection<PropertyDescriptor> attribs = type.type().getDescriptors();
                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(type);
                GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(feature.getId().toString(), feature);
                Geometry geom = null;
                List<Object> attributes = simpleFeature.getAttributes();
                for (Object attribute : attributes) {
                    if (attribute instanceof Geometry) {
                        geom = (Geometry) attribute;
                        break;
                    }
                }
                conflict = new GeometryConflict(input, geom, crsCode);
            }
            return conflict;
        }
    });
    while (conflictIterator.hasNext()) {
        GeometryConflict next = conflictIterator.next();
        if (next != null) {
            out.writeStartElement("Feature");
            writeElement("change", "CONFLICT");
            writeElement("id", next.getConflict().getPath());
            writeElement("ourvalue", next.getConflict().getOurs().toString());
            writeElement("theirvalue", next.getConflict().getTheirs().toString());
            writeElement("geometry", next.getGeometry().toText());
            if (next.getCRS() != null) {
                writeElement("crs", next.getCRS());
            }
            out.writeEndElement();
        }
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) FactoryException(org.opengis.referencing.FactoryException) PropertyType(org.opengis.feature.type.PropertyType) GeometryType(org.opengis.feature.type.GeometryType) RevFeature(org.locationtech.geogig.api.RevFeature) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeogigSimpleFeature(org.locationtech.geogig.api.GeogigSimpleFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) Geometry(com.vividsolutions.jts.geom.Geometry) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) Collection(java.util.Collection) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) RevTree(org.locationtech.geogig.api.RevTree)

Aggregations

Geometry (com.vividsolutions.jts.geom.Geometry)3 Collection (java.util.Collection)3 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)3 GeogigSimpleFeature (org.locationtech.geogig.api.GeogigSimpleFeature)3 RevFeature (org.locationtech.geogig.api.RevFeature)3 RevFeatureBuilder (org.locationtech.geogig.api.RevFeatureBuilder)3 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)3 RevObject (org.locationtech.geogig.api.RevObject)3 GeometryType (org.opengis.feature.type.GeometryType)3 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)3 PropertyType (org.opengis.feature.type.PropertyType)3 FactoryException (org.opengis.referencing.FactoryException)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 Optional (com.google.common.base.Optional)2 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 FeatureInfo (org.locationtech.geogig.api.FeatureInfo)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 RevCommit (org.locationtech.geogig.api.RevCommit)1