All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openstreetmap.atlas.geography.index.PackedSpatialIndex Maven / Gradle / Ivy

There is a newer version: 7.0.8
Show newest version
package org.openstreetmap.atlas.geography.index;

import java.util.ArrayList;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.openstreetmap.atlas.exception.CoreException;
import org.openstreetmap.atlas.geography.Located;
import org.openstreetmap.atlas.geography.Rectangle;
import org.openstreetmap.atlas.geography.atlas.items.Edge;

/**
 * This {@link PackedSpatialIndex} accepts a {@link Located} object (e.g. an {@link Edge}), but only
 * stores the packed information (identifier) into the real index. Spatial index can be an R-Tree or
 * Quad-Tree.
 *
 * @param 
 *            The type of {@link Located} item
 * @param 
 *            The type of packed item
 * @author tony
 */
public abstract class PackedSpatialIndex implements SpatialIndex
{
    private static final long serialVersionUID = 1747435801359663115L;
    private final JtsSpatialIndex index;

    public PackedSpatialIndex(final JtsSpatialIndex index)
    {
        this.index = index;
    }

    @Override
    public void add(final L located)
    {
        final Rectangle bounds = located.bounds();
        if (bounds != null)
        {
            this.index.add(bounds, compress(located));
        }
        else
        {
            throw new CoreException(
                    "Unable to get bounds for located item when building spatial index: {}",
                    located);
        }
    }

    @Override
    public Rectangle bounds()
    {
        return this.index.bounds();
    }

    @Override
    public Iterable get(final Rectangle bound)
    {
        return ((ArrayList) this.index.get(bound)).stream().map(this::restore)
                .collect(Collectors.toList());
    }

    @Override
    public Iterable get(final Rectangle bound, final Predicate predicate)
    {
        return ((ArrayList) this.index.get(bound)).stream().map(this::restore)
                .filter(predicate).collect(Collectors.toList());
    }

    /**
     * Extract a packed object from the given located object
     *
     * @param located
     *            The {@link Located} item to extract
     * @return The packed object
     */
    protected abstract Packed compress(L located);

    protected abstract boolean isValid(L located, Rectangle bounds);

    /**
     * Restore the located object from packed one
     *
     * @param packed
     *            The packed object to restore
     * @return The restored object
     */
    protected abstract L restore(Packed packed);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy