Search in sources :

Example 21 with Bound

use of org.openstreetmap.osmosis.core.domain.v0_6.Bound in project osmosis by openstreetmap.

the class EntityMerger method run.

/**
 * {@inheritDoc}
 */
public void run() {
    try {
        EntityContainerComparator comparator;
        EntityContainer entityContainer0 = null;
        EntityContainer entityContainer1 = null;
        // Create a comparator for comparing two entities by type and identifier.
        comparator = new EntityContainerComparator(new EntityByTypeThenIdComparator());
        // We can't get meaningful data from the initialize data on the
        // input streams, so pass empty meta data to the sink and discard
        // the input meta data.
        postbox0.outputInitialize();
        postbox1.outputInitialize();
        sink.initialize(Collections.<String, Object>emptyMap());
        // BEGIN bound special handling
        // If there is a bound, it's going to be the first object
        // in a properly sorted stream
        entityContainer0 = nextOrNull(postbox0);
        entityContainer1 = nextOrNull(postbox1);
        // on both streams - no data implies no bound
        if (entityContainer0 != null && entityContainer1 != null) {
            Bound bound0 = null;
            Bound bound1 = null;
            // If there are any bounds upstream, eat them up
            if (entityContainer0.getEntity().getType() == EntityType.Bound) {
                bound0 = (Bound) entityContainer0.getEntity();
                entityContainer0 = nextOrNull(postbox0);
            }
            if (entityContainer1.getEntity().getType() == EntityType.Bound) {
                bound1 = (Bound) entityContainer1.getEntity();
                entityContainer1 = nextOrNull(postbox1);
            }
            // to be smaller than the actual data, which is bad)
            if (bound0 != null && bound1 != null) {
                sink.process(new BoundContainer(bound0.union(bound1)));
            } else if ((bound0 != null && bound1 == null) || (bound0 == null && bound1 != null)) {
                handleBoundRemoved(bound0 == null);
            }
        }
        // We continue in the comparison loop while both sources still have data.
        while ((entityContainer0 != null || postbox0.hasNext()) && (entityContainer1 != null || postbox1.hasNext())) {
            long comparisonResult;
            // Get the next input data where required.
            if (entityContainer0 == null) {
                entityContainer0 = postbox0.getNext();
            }
            if (entityContainer1 == null) {
                entityContainer1 = postbox1.getNext();
            }
            // Compare the two entities.
            comparisonResult = comparator.compare(entityContainer0, entityContainer1);
            if (comparisonResult < 0) {
                // Entity 0 doesn't exist on the other source and can be
                // sent straight through.
                sink.process(entityContainer0);
                entityContainer0 = null;
            } else if (comparisonResult > 0) {
                // Entity 1 doesn't exist on the other source and can be
                // sent straight through.
                sink.process(entityContainer1);
                entityContainer1 = null;
            } else {
                // The entity exists on both sources so we must resolve the conflict.
                if (conflictResolutionMethod.equals(ConflictResolutionMethod.Timestamp)) {
                    int timestampComparisonResult;
                    timestampComparisonResult = entityContainer0.getEntity().getTimestamp().compareTo(entityContainer1.getEntity().getTimestamp());
                    if (timestampComparisonResult < 0) {
                        sink.process(entityContainer1);
                    } else if (timestampComparisonResult > 0) {
                        sink.process(entityContainer0);
                    } else {
                        // If both have identical timestamps, use the second source.
                        sink.process(entityContainer1);
                    }
                } else if (conflictResolutionMethod.equals(ConflictResolutionMethod.LatestSource)) {
                    sink.process(entityContainer1);
                } else if (conflictResolutionMethod.equals(ConflictResolutionMethod.Version)) {
                    int version0 = entityContainer0.getEntity().getVersion();
                    int version1 = entityContainer1.getEntity().getVersion();
                    if (version0 < version1) {
                        sink.process(entityContainer1);
                    } else if (version0 > version1) {
                        sink.process(entityContainer0);
                    } else {
                        // If both have identical versions, use the second source.
                        sink.process(entityContainer1);
                    }
                } else {
                    throw new OsmosisRuntimeException("Conflict resolution method " + conflictResolutionMethod + " is not recognized.");
                }
                entityContainer0 = null;
                entityContainer1 = null;
            }
        }
        // Any remaining entities on either source can be sent straight through.
        while (entityContainer0 != null || postbox0.hasNext()) {
            if (entityContainer0 == null) {
                entityContainer0 = postbox0.getNext();
            }
            sink.process(entityContainer0);
            entityContainer0 = null;
        }
        while (entityContainer1 != null || postbox1.hasNext()) {
            if (entityContainer1 == null) {
                entityContainer1 = postbox1.getNext();
            }
            sink.process(entityContainer1);
            entityContainer1 = null;
        }
        sink.complete();
        postbox0.outputComplete();
        postbox1.outputComplete();
    } finally {
        sink.close();
        postbox0.outputRelease();
        postbox1.outputRelease();
    }
}
Also used : EntityByTypeThenIdComparator(org.openstreetmap.osmosis.core.sort.v0_6.EntityByTypeThenIdComparator) BoundContainer(org.openstreetmap.osmosis.core.container.v0_6.BoundContainer) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) Bound(org.openstreetmap.osmosis.core.domain.v0_6.Bound) EntityContainerComparator(org.openstreetmap.osmosis.core.sort.v0_6.EntityContainerComparator) OsmosisRuntimeException(org.openstreetmap.osmosis.core.OsmosisRuntimeException)

Example 22 with Bound

use of org.openstreetmap.osmosis.core.domain.v0_6.Bound in project osmosis by openstreetmap.

the class ApidbCurrentReader method runImpl.

/**
 * Runs the task implementation. This is called by the run method within a transaction.
 *
 * @param dbCtx
 *            Used to access the database.
 */
protected void runImpl(DatabaseContext2 dbCtx) {
    try {
        AllEntityDao entityDao;
        sink.initialize(Collections.<String, Object>emptyMap());
        new SchemaVersionValidator(loginCredentials, preferences).validateVersion(ApidbVersionConstants.SCHEMA_MIGRATIONS);
        entityDao = new AllEntityDao(dbCtx.getJdbcTemplate());
        sink.process(new BoundContainer(new Bound("Osmosis " + OsmosisConstants.VERSION)));
        try (ReleasableIterator<EntityContainer> reader = entityDao.getCurrent()) {
            while (reader.hasNext()) {
                sink.process(reader.next());
            }
        }
        sink.complete();
    } finally {
        sink.close();
    }
}
Also used : AllEntityDao(org.openstreetmap.osmosis.apidb.v0_6.impl.AllEntityDao) BoundContainer(org.openstreetmap.osmosis.core.container.v0_6.BoundContainer) Bound(org.openstreetmap.osmosis.core.domain.v0_6.Bound) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) SchemaVersionValidator(org.openstreetmap.osmosis.apidb.v0_6.impl.SchemaVersionValidator)

Example 23 with Bound

use of org.openstreetmap.osmosis.core.domain.v0_6.Bound in project osmosis by openstreetmap.

the class PostgreSqlDatasetContext method iterate.

/**
 * {@inheritDoc}
 */
@Override
public ReleasableIterator<EntityContainer> iterate() {
    List<Bound> bounds;
    List<ReleasableIterator<EntityContainer>> sources;
    if (!initialized) {
        initialize();
    }
    // Build the bounds list.
    bounds = new ArrayList<Bound>();
    bounds.add(new Bound("Osmosis " + OsmosisConstants.VERSION));
    sources = new ArrayList<ReleasableIterator<EntityContainer>>();
    sources.add(new UpcastIterator<EntityContainer, BoundContainer>(new BoundContainerIterator(new ReleasableAdaptorForIterator<Bound>(bounds.iterator()))));
    sources.add(new UpcastIterator<EntityContainer, NodeContainer>(new NodeContainerIterator(nodeDao.iterate())));
    sources.add(new UpcastIterator<EntityContainer, WayContainer>(new WayContainerIterator(wayDao.iterate())));
    sources.add(new UpcastIterator<EntityContainer, RelationContainer>(new RelationContainerIterator(relationDao.iterate())));
    return new MultipleSourceIterator<EntityContainer>(sources);
}
Also used : NodeContainerIterator(org.openstreetmap.osmosis.core.container.v0_6.NodeContainerIterator) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) MultipleSourceIterator(org.openstreetmap.osmosis.core.store.MultipleSourceIterator) Bound(org.openstreetmap.osmosis.core.domain.v0_6.Bound) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) RelationContainerIterator(org.openstreetmap.osmosis.core.container.v0_6.RelationContainerIterator) ReleasableIterator(org.openstreetmap.osmosis.core.lifecycle.ReleasableIterator) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) WayContainerIterator(org.openstreetmap.osmosis.core.container.v0_6.WayContainerIterator) BoundContainerIterator(org.openstreetmap.osmosis.core.container.v0_6.BoundContainerIterator) RelationContainer(org.openstreetmap.osmosis.core.container.v0_6.RelationContainer) BoundContainer(org.openstreetmap.osmosis.core.container.v0_6.BoundContainer)

Example 24 with Bound

use of org.openstreetmap.osmosis.core.domain.v0_6.Bound in project atlas by osmlab.

the class OsmPbfCounter method process.

@Override
public void process(final EntityContainer entityContainer) {
    final Entity rawEntity = entityContainer.getEntity();
    if (OsmPbfReader.shouldProcessEntity(this.loadingOption, rawEntity)) {
        // store all node locations for calculating way geometry
        if (rawEntity instanceof Node) {
            final Node node = (Node) rawEntity;
            final Location nodeLocation = new Location(Latitude.degrees(node.getLatitude()), Longitude.degrees(node.getLongitude()));
            this.nodeIdentifierToLocation.put(rawEntity.getId(), nodeLocation);
        }
        if (shouldLoadOsmNode(rawEntity)) {
            // This node is within the boundary, bring it in
            this.nodeIdentifiersToInclude.add(rawEntity.getId());
        } else if (shouldLoadOsmWay(rawEntity)) {
            final Way way = (Way) rawEntity;
            if (wayIntersectsBoundary(way)) {
                // This way contains at least one shape-point within the given bounding box.
                // Bring it and all of its nodes in to the atlas.
                addWayNodes(this.nodeIdentifiersBroughtInByWaysOrRelations, way);
                this.wayIdentifiersToInclude.add(way.getId());
            } else {
                // This way doesn't have any shape-points within the given boundary. Mark it as
                // a way to exclude so it can be revisited during relation processing
                this.waysToExclude.put(way.getId(), way);
            }
        } else if (shouldLoadOsmRelation(rawEntity)) {
            final Relation relation = (Relation) rawEntity;
            if (relationContainsMemberWithinBoundary(relation)) {
                // Shallow check showed that this relation has a member that is inside our
                // boundary, mark all members and relation as inside
                markRelationAndMembersInsideBoundary(relation);
            } else {
                // Stage the relation - it might be added later
                this.stagedRelations.add(relation);
            }
        } else if (rawEntity instanceof Bound) {
            logger.trace("Encountered PBF Bound {}, skipping over it.", rawEntity.getId());
        }
    }
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) Relation(org.openstreetmap.osmosis.core.domain.v0_6.Relation) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) Bound(org.openstreetmap.osmosis.core.domain.v0_6.Bound) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Location(org.openstreetmap.atlas.geography.Location)

Example 25 with Bound

use of org.openstreetmap.osmosis.core.domain.v0_6.Bound in project atlas by osmlab.

the class OsmPbfReader method constructRelationBean.

/**
 * Creates a {@link RelationBean} for the given {@link Relation}. Note: The returned bean can be
 * empty.
 *
 * @param relation
 *            The {@link Relation} for which to create the {@link RelationBean}
 * @return the created {@link RelationBean}
 */
private RelationBean constructRelationBean(final Relation relation) {
    final RelationBean bean = new RelationBean();
    for (final RelationMember member : relation.getMembers()) {
        final long memberIdentifier = padIdentifier(member.getMemberId());
        final EntityType memberType = member.getMemberType();
        final String role = member.getMemberRole();
        if (memberType == EntityType.Node) {
            if (this.builder.peek().point(memberIdentifier) != null) {
                bean.addItem(memberIdentifier, role, ItemType.POINT);
            } else {
                logger.trace(MISSING_MEMBER_MESSAGE, relation.getId(), EntityType.Node, memberIdentifier);
            }
        } else if (memberType == EntityType.Way) {
            if (this.builder.peek().line(memberIdentifier) != null) {
                bean.addItem(memberIdentifier, role, ItemType.LINE);
            } else {
                logger.trace(MISSING_MEMBER_MESSAGE, relation.getId(), EntityType.Way, memberIdentifier);
            }
        } else if (memberType == EntityType.Relation) {
            if (this.builder.peek().relation(memberIdentifier) != null) {
                bean.addItem(memberIdentifier, role, ItemType.RELATION);
            } else {
                logger.trace(MISSING_MEMBER_MESSAGE, relation.getId(), EntityType.Relation, memberIdentifier);
            }
        } else {
            // A Bound should never be a Relation member; if this is the case, log and continue
            logger.error("Invalid Bound member {} found for Relation {}", member.getMemberId(), relation.getId());
        }
    }
    return bean;
}
Also used : EntityType(org.openstreetmap.osmosis.core.domain.v0_6.EntityType) RelationMember(org.openstreetmap.osmosis.core.domain.v0_6.RelationMember) RelationBean(org.openstreetmap.atlas.geography.atlas.builder.RelationBean)

Aggregations

Bound (org.openstreetmap.osmosis.core.domain.v0_6.Bound)46 Test (org.junit.Test)25 BoundContainer (org.openstreetmap.osmosis.core.container.v0_6.BoundContainer)22 EntityContainer (org.openstreetmap.osmosis.core.container.v0_6.EntityContainer)19 CommonEntityData (org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData)14 SinkEntityInspector (org.openstreetmap.osmosis.testutil.v0_6.SinkEntityInspector)14 NodeContainer (org.openstreetmap.osmosis.core.container.v0_6.NodeContainer)13 Date (java.util.Date)11 Node (org.openstreetmap.osmosis.core.domain.v0_6.Node)11 OsmosisRuntimeException (org.openstreetmap.osmosis.core.OsmosisRuntimeException)7 RelationContainer (org.openstreetmap.osmosis.core.container.v0_6.RelationContainer)6 OsmUser (org.openstreetmap.osmosis.core.domain.v0_6.OsmUser)6 WayContainer (org.openstreetmap.osmosis.core.container.v0_6.WayContainer)5 Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)5 RunnableSource (org.openstreetmap.osmosis.core.task.v0_6.RunnableSource)5 Osmformat (crosby.binary.Osmformat)4 BoundContainerIterator (org.openstreetmap.osmosis.core.container.v0_6.BoundContainerIterator)4 NodeContainerIterator (org.openstreetmap.osmosis.core.container.v0_6.NodeContainerIterator)4 RelationContainerIterator (org.openstreetmap.osmosis.core.container.v0_6.RelationContainerIterator)4 WayContainerIterator (org.openstreetmap.osmosis.core.container.v0_6.WayContainerIterator)4