Search in sources :

Example 21 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class Show method printFeatureType.

private void printFeatureType(Ansi ansi, RevFeatureType ft, boolean useDefaultKeyword) {
    ImmutableList<PropertyDescriptor> attribs = ft.sortedDescriptors();
    ansi.fg(Color.YELLOW).a(useDefaultKeyword ? "DEFAULT " : "").a("FEATURE TYPE ID:  ").reset().a(ft.getId().toString()).newline().newline();
    ansi.a(useDefaultKeyword ? "DEFAULT " : "").a("FEATURE TYPE ATTRIBUTES").newline();
    for (PropertyDescriptor attrib : attribs) {
        ansi.fg(Color.YELLOW).a(attrib.getName() + ": ").reset().a("<" + FieldType.forBinding(attrib.getType().getBinding()) + ">").newline();
    }
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor)

Example 22 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class OSMExport method getFeatures.

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
    if (bbox != null) {
        final Envelope env;
        try {
            env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)), Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Wrong bbox definition");
        }
        Predicate<Bounded> filter = new Predicate<Bounded>() {

            @Override
            public boolean apply(final Bounded bounded) {
                boolean intersects = bounded.intersects(env);
                return intersects;
            }
        };
        op.setBoundsFilter(filter);
    }
    Iterator<NodeRef> iterator = op.call();
    final EntityConverter converter = new EntityConverter();
    Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {

        @Override
        @Nullable
        public EntityContainer apply(@Nullable NodeRef ref) {
            RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
            SimpleFeatureType featureType;
            if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
                featureType = OSMUtils.nodeType();
            } else {
                featureType = OSMUtils.wayType();
            }
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
            RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
            List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < descriptors.size(); i++) {
                PropertyDescriptor descriptor = descriptors.get(i);
                Optional<Object> value = values.get(i);
                featureBuilder.set(descriptor.getName(), value.orNull());
            }
            SimpleFeature feature = featureBuilder.buildFeature(ref.name());
            Entity entity = converter.toEntity(feature, null);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }
            return container;
        }
    };
    return Iterators.transform(iterator, function);
}
Also used : EntityConverter(org.locationtech.geogig.osm.internal.EntityConverter) Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) Envelope(com.vividsolutions.jts.geom.Envelope) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Predicate(com.google.common.base.Predicate) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) Bounded(org.locationtech.geogig.api.Bounded) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 23 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class Log method writeCSV.

private void writeCSV(GeoGIG geogig, Writer out, Iterator<RevCommit> log) throws Exception {
    String response = "ChangeType,FeatureId,CommitId,Parent CommitIds,Author Name,Author Email,Author Commit Time,Committer Name,Committer Email,Committer Commit Time,Commit Message";
    out.write(response);
    response = "";
    String path = paths.get(0);
    // This is the feature type object
    Optional<NodeRef> ref = geogig.command(FindTreeChild.class).setChildPath(path).setParent(geogig.getRepository().workingTree().getTree()).call();
    Optional<RevObject> type = Optional.absent();
    if (ref.isPresent()) {
        type = geogig.command(RevObjectParse.class).setRefSpec(ref.get().getMetadataId().toString()).call();
    } else {
        throw new CommandSpecException("Couldn't resolve the given path.");
    }
    if (type.isPresent() && type.get() instanceof RevFeatureType) {
        RevFeatureType featureType = (RevFeatureType) type.get();
        Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
        int attributeLength = attribs.size();
        for (PropertyDescriptor attrib : attribs) {
            response += "," + escapeCsv(attrib.getName().toString());
        }
        response += '\n';
        out.write(response);
        response = "";
        RevCommit commit = null;
        while (log.hasNext()) {
            commit = log.next();
            String parentId = commit.getParentIds().size() >= 1 ? commit.getParentIds().get(0).toString() : ObjectId.NULL.toString();
            Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).setNewVersion(commit.getId().toString()).setFilter(path).call();
            while (diff.hasNext()) {
                DiffEntry entry = diff.next();
                response += entry.changeType().toString() + ",";
                String fid = "";
                if (entry.newPath() != null) {
                    if (entry.oldPath() != null) {
                        fid = entry.oldPath() + " -> " + entry.newPath();
                    } else {
                        fid = entry.newPath();
                    }
                } else if (entry.oldPath() != null) {
                    fid = entry.oldPath();
                }
                response += fid + ",";
                response += commit.getId().toString() + ",";
                response += parentId;
                if (commit.getParentIds().size() > 1) {
                    for (int index = 1; index < commit.getParentIds().size(); index++) {
                        response += " " + commit.getParentIds().get(index).toString();
                    }
                }
                response += ",";
                if (commit.getAuthor().getName().isPresent()) {
                    response += escapeCsv(commit.getAuthor().getName().get());
                }
                response += ",";
                if (commit.getAuthor().getEmail().isPresent()) {
                    response += escapeCsv(commit.getAuthor().getEmail().get());
                }
                response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format(new Date(commit.getAuthor().getTimestamp())) + ",";
                if (commit.getCommitter().getName().isPresent()) {
                    response += escapeCsv(commit.getCommitter().getName().get());
                }
                response += ",";
                if (commit.getCommitter().getEmail().isPresent()) {
                    response += escapeCsv(commit.getCommitter().getEmail().get());
                }
                response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format(new Date(commit.getCommitter().getTimestamp())) + ",";
                String message = escapeCsv(commit.getMessage());
                response += message;
                if (entry.newObjectId() == ObjectId.NULL) {
                    // Feature was removed so we need to fill out blank attribute values
                    for (int index = 0; index < attributeLength; index++) {
                        response += ",";
                    }
                } else {
                    // Feature was added or modified so we need to write out the
                    // attribute
                    // values from the feature
                    Optional<RevObject> feature = geogig.command(RevObjectParse.class).setObjectId(entry.newObjectId()).call();
                    RevFeature revFeature = (RevFeature) feature.get();
                    List<Optional<Object>> values = revFeature.getValues();
                    for (int index = 0; index < values.size(); index++) {
                        Optional<Object> value = values.get(index);
                        PropertyDescriptor attrib = (PropertyDescriptor) attribs.toArray()[index];
                        String stringValue = "";
                        if (value.isPresent()) {
                            FieldType attributeType = FieldType.forBinding(attrib.getType().getBinding());
                            switch(attributeType) {
                                case DATE:
                                    stringValue = new SimpleDateFormat("MM/dd/yyyy z").format((java.sql.Date) value.get());
                                    break;
                                case DATETIME:
                                    stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format((Date) value.get());
                                    break;
                                case TIME:
                                    stringValue = new SimpleDateFormat("HH:mm:ss z").format((Time) value.get());
                                    break;
                                case TIMESTAMP:
                                    stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format((Timestamp) value.get());
                                    break;
                                default:
                                    stringValue = escapeCsv(value.get().toString());
                            }
                            response += "," + stringValue;
                        } else {
                            response += ",";
                        }
                    }
                }
                response += '\n';
                out.write(response);
                response = "";
            }
        }
    } else {
        // Couldn't resolve FeatureType
        throw new CommandSpecException("Couldn't resolve the given path to a feature type.");
    }
}
Also used : Time(java.sql.Time) Timestamp(java.sql.Timestamp) ParseTimestamp(org.locationtech.geogig.api.plumbing.ParseTimestamp) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) Date(java.util.Date) FieldType(org.locationtech.geogig.storage.FieldType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) SimpleDateFormat(java.text.SimpleDateFormat)

Example 24 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor 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 25 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class ResponseWriter method writeFeatureDiffResponse.

/**
     * Writes a set of feature diffs to the stream.
     * 
     * @param diffs a map of {@link PropertyDescriptor} to {@link AttributeDiffs} that specify the
     *        difference between two features
     * @throws XMLStreamException
     */
public void writeFeatureDiffResponse(Map<PropertyDescriptor, AttributeDiff> diffs) throws XMLStreamException {
    Set<Entry<PropertyDescriptor, AttributeDiff>> entries = diffs.entrySet();
    Iterator<Entry<PropertyDescriptor, AttributeDiff>> iter = entries.iterator();
    while (iter.hasNext()) {
        Entry<PropertyDescriptor, AttributeDiff> entry = iter.next();
        out.writeStartElement("diff");
        PropertyType attrType = entry.getKey().getType();
        if (attrType instanceof GeometryType) {
            writeElement("geometry", "true");
            GeometryType gt = (GeometryType) attrType;
            CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
            if (crs != null) {
                String crsCode = null;
                try {
                    crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                } catch (FactoryException e) {
                    crsCode = null;
                }
                if (crsCode != null) {
                    writeElement("crs", "EPSG:" + crsCode);
                }
            }
        }
        writeElement("attributename", entry.getKey().getName().toString());
        writeElement("changetype", entry.getValue().getType().toString());
        if (entry.getValue().getOldValue() != null && entry.getValue().getOldValue().isPresent()) {
            writeElement("oldvalue", entry.getValue().getOldValue().get().toString());
        }
        if (entry.getValue().getNewValue() != null && entry.getValue().getNewValue().isPresent() && !entry.getValue().getType().equals(TYPE.NO_CHANGE)) {
            writeElement("newvalue", entry.getValue().getNewValue().get().toString());
        }
        out.writeEndElement();
    }
}
Also used : GeometryType(org.opengis.feature.type.GeometryType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) FactoryException(org.opengis.referencing.FactoryException) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) PropertyType(org.opengis.feature.type.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)47 RevFeature (org.locationtech.geogig.api.RevFeature)24 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)23 Optional (com.google.common.base.Optional)18 AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)17 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)16 FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)15 Test (org.junit.Test)12 RevObject (org.locationtech.geogig.api.RevObject)12 GenericAttributeDiffImpl (org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl)11 NodeRef (org.locationtech.geogig.api.NodeRef)10 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)10 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)9 ObjectId (org.locationtech.geogig.api.ObjectId)8 Entry (java.util.Map.Entry)7 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)6 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)6 GeometryType (org.opengis.feature.type.GeometryType)6 PropertyType (org.opengis.feature.type.PropertyType)6