Search in sources :

Example 1 with EntityContainer

use of org.openstreetmap.osmosis.core.container.v0_6.EntityContainer in project GeoGig by boundlessgeo.

the class OSMExport method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args.size() < 1 || args.size() > 2) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    checkParameter(bbox == null || bbox.size() == 4, "The specified bounding box is not correct");
    geogig = cli.getGeogig();
    String osmfile = args.get(0);
    String ref = "WORK_HEAD";
    if (args.size() == 2) {
        ref = args.get(1);
        Optional<ObjectId> tree = geogig.command(ResolveTreeish.class).setTreeish(ref).call();
        checkParameter(tree.isPresent(), "Invalid commit or reference: %s", ref);
    }
    File file = new File(osmfile);
    checkParameter(!file.exists() || overwrite, "The selected file already exists. Use -o to overwrite");
    Iterator<EntityContainer> nodes = getFeatures(ref + ":node");
    Iterator<EntityContainer> ways = getFeatures(ref + ":way");
    Iterator<EntityContainer> iterator = Iterators.concat(nodes, ways);
    if (file.getName().endsWith(".pbf")) {
        BlockOutputStream output = new BlockOutputStream(new FileOutputStream(file));
        OsmosisSerializer serializer = new OsmosisSerializer(output);
        while (iterator.hasNext()) {
            EntityContainer entity = iterator.next();
            serializer.process(entity);
        }
        serializer.complete();
    } else {
        XmlWriter writer = new XmlWriter(file, CompressionMethod.None);
        while (iterator.hasNext()) {
            EntityContainer entity = iterator.next();
            writer.process(entity);
        }
        writer.complete();
    }
}
Also used : OsmosisSerializer(crosby.binary.osmosis.OsmosisSerializer) ObjectId(org.locationtech.geogig.api.ObjectId) FileOutputStream(java.io.FileOutputStream) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) BlockOutputStream(org.openstreetmap.osmosis.osmbinary.file.BlockOutputStream) File(java.io.File) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) XmlWriter(org.openstreetmap.osmosis.xml.v0_6.XmlWriter)

Example 2 with EntityContainer

use of org.openstreetmap.osmosis.core.container.v0_6.EntityContainer 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 3 with EntityContainer

use of org.openstreetmap.osmosis.core.container.v0_6.EntityContainer in project GeoGig by boundlessgeo.

the class CreateOSMChangesetOp method _call.

/**
     * Executes the diff operation.
     * 
     * @return an iterator to a set of differences between the two trees
     * @see DiffEntry
     */
@Override
protected Iterator<ChangeContainer> _call() {
    Iterator<DiffEntry> nodeIterator = command(DiffOp.class).setFilter(OSMUtils.NODE_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
    Iterator<DiffEntry> wayIterator = command(DiffOp.class).setFilter(OSMUtils.WAY_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
    Iterator<DiffEntry> iterator = Iterators.concat(nodeIterator, wayIterator);
    final EntityConverter converter = new EntityConverter();
    Function<DiffEntry, ChangeContainer> function = new Function<DiffEntry, ChangeContainer>() {

        @Override
        @Nullable
        public ChangeContainer apply(@Nullable DiffEntry diff) {
            NodeRef ref = diff.changeType().equals(ChangeType.REMOVED) ? diff.getOldObject() : diff.getNewObject();
            RevFeature revFeature = command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
            RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(ref.getMetadataId()).call(RevFeatureType.class).get();
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
            ImmutableList<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, id);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }
            ChangeAction action = diff.changeType().equals(ChangeType.ADDED) ? ChangeAction.Create : diff.changeType().equals(ChangeType.MODIFIED) ? ChangeAction.Modify : ChangeAction.Delete;
            return new ChangeContainer(container, action);
        }
    };
    return Iterators.transform(iterator, function);
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) ChangeAction(org.openstreetmap.osmosis.core.task.common.ChangeAction) 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) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Function(com.google.common.base.Function) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) ChangeContainer(org.openstreetmap.osmosis.core.container.v0_6.ChangeContainer) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) SimpleFeature(org.opengis.feature.simple.SimpleFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

EntityContainer (org.openstreetmap.osmosis.core.container.v0_6.EntityContainer)3 Function (com.google.common.base.Function)2 Optional (com.google.common.base.Optional)2 Nullable (javax.annotation.Nullable)2 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)2 NodeRef (org.locationtech.geogig.api.NodeRef)2 ObjectId (org.locationtech.geogig.api.ObjectId)2 RevFeature (org.locationtech.geogig.api.RevFeature)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)2 NodeContainer (org.openstreetmap.osmosis.core.container.v0_6.NodeContainer)2 WayContainer (org.openstreetmap.osmosis.core.container.v0_6.WayContainer)2 Entity (org.openstreetmap.osmosis.core.domain.v0_6.Entity)2 Node (org.openstreetmap.osmosis.core.domain.v0_6.Node)2 Way (org.openstreetmap.osmosis.core.domain.v0_6.Way)2 Predicate (com.google.common.base.Predicate)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 OsmosisSerializer (crosby.binary.osmosis.OsmosisSerializer)1