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

org.geolatte.mapserver.tms.Tile Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2009-2010  Geovise BVBA, QMINO BVBA
 *
 * This file is part of GeoLatte Mapserver.
 *
 * GeoLatte Mapserver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GeoLatte Mapserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoLatte Mapserver.  If not, see .
 */

package org.geolatte.mapserver.tms;


import org.geolatte.geom.Envelope;
import org.geolatte.mapserver.img.Imaging;
import org.geolatte.mapserver.util.PixelRange;

import java.awt.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

public class Tile {

    final private TileCoordinate coordinate;
    final private TileImageSource source;
    final private TileSetCoordinateSpace tileSetCoordinateSpace;


    public Tile(TileImageSource source, TileCoordinate coordinate, TileSetCoordinateSpace tileSetCoordinateSpace) {
        this.source = source;
        this.coordinate = coordinate;
        this.tileSetCoordinateSpace = tileSetCoordinateSpace;
    }

    static PixelRange pixelBounds(Set tiles) {
        if (tiles.isEmpty()) return new PixelRange(0, 0, new Dimension(0, 0));
        PixelRange boundsTile = null;
        for (Tile tile : tiles) {
            PixelRange b = tile.getPixelBounds();
            boundsTile = PixelRange.union(boundsTile, b);
        }
        return boundsTile;
    }

    static Envelope boundingBox(Set tiles) {
        if (tiles.isEmpty()) return new Envelope(0, 0, 0, 0, null);
        Envelope bboxUnion = null;
        for (Tile tile : tiles) {
            Envelope bbox = tile.getBoundingBox();
            bboxUnion = Envelope.union(bboxUnion, bbox);
        }
        return bboxUnion;
    }

    /**
     * Returns the TileImage for this tile.
     *
     * @param imaging the Imaging implementation to use for decoding the image
     * @return
     */
    public TileImage getImage(Imaging imaging, boolean forceArgb) {
        InputStream is = null;
        PixelRange pb = getPixelBounds();
        try {
            is = source.open();
            return imaging.read(is, pb.getMinX(), pb.getMinY(), forceArgb);
        }
        catch (FileNotFoundException e) {
            // no tile found, return an empty tile
            // return img.empty(pb.getWidth(), pb.getHeight());
            throw new RuntimeException(e);
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // nothing to do;
                }
            }
        }
    }

    PixelRange getPixelBounds() {
        return tileSetCoordinateSpace.tilePixelRange(this.coordinate);
    }


    Envelope getBoundingBox() {
        return tileSetCoordinateSpace.boundingBox(this.coordinate);
    }

    @Override
    public String toString() {
        return source.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Tile tile = (Tile) o;

        if (source != null ? !source.equals(tile.source) : tile.source != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return source != null ? source.hashCode() : 0;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy