Class MBTilesFile

  • All Implemented Interfaces:
    AutoCloseable

    public class MBTilesFile
    extends Object
    implements AutoCloseable
    • Constructor Detail

      • MBTilesFile

        public MBTilesFile()
                    throws IOException
        Creates a new empty MbTilesFile, generating a new file.
        Throws:
        IOException
      • MBTilesFile

        public MBTilesFile​(boolean disableJournal)
                    throws IOException
        Creates a new empty MbTilesFile, generating a new file, also deciding if journal must be disabled or not.

        This constructor assumes no credentials are required to connect to the database.

        Throws:
        IOException
      • MBTilesFile

        public MBTilesFile​(File file)
                    throws IOException
        Creates a MbTilesFile from an existing file.

        This constructor assumes no credentials are required to connect to the database.

        Throws:
        IOException
      • MBTilesFile

        public MBTilesFile​(File file,
                           boolean disableJournal)
                    throws IOException
        Creates a MbTilesFile from an existing file, also deciding if journal must be disabled or not.

        This constructor assumes no credentials are required to connect to the database.

        Throws:
        IOException
      • MBTilesFile

        public MBTilesFile​(File file,
                           String user,
                           String passwd,
                           boolean disableJournal)
                    throws IOException
        Creates a MbTilesFile from an existing file specifying database credentials.
        Throws:
        IOException
      • MBTilesFile

        public MBTilesFile​(DataSource dataSource)
        Create an MBTilesFile from an SQL DataSource connected to an MBTiles file. Behaviour is undefined if the DataSource is any other form of database.
    • Method Detail

      • saveMetaData

        public void saveMetaData​(MBTilesMetadata metaData)
                          throws IOException
        Store MetaData in file
        Throws:
        IOException
      • saveMinMaxZoomMetadata

        public void saveMinMaxZoomMetadata​(int min,
                                           int max)
                                    throws IOException
        Save the minimum and maximum zoom level as metadata items. GDAL and QGIS expect these items.
        Parameters:
        min - The minimum zoom level
        max - The maximum zoom level
        Throws:
        IOException
      • saveTile

        public void saveTile​(MBTilesTile entry)
                      throws IOException
        Store a tile
        Throws:
        IOException
      • saveGrid

        public void saveGrid​(MBTilesGrid entry)
                      throws IOException
        Store a grid
        Throws:
        IOException
      • loadMetaData

        public MBTilesMetadata loadMetaData()
                                     throws IOException
        Throws:
        IOException
      • loadTile

        public MBTilesTile loadTile​(long zoomLevel,
                                    long column,
                                    long row)
                             throws IOException
        Throws:
        IOException
      • loadGrid

        public MBTilesGrid loadGrid​(long zoomLevel,
                                    long column,
                                    long row)
                             throws IOException
        Throws:
        IOException
      • tiles

        public MBTilesFile.TileIterator tiles​(long zoomLevel,
                                              long leftTile,
                                              long bottomTile,
                                              long rightTile,
                                              long topTile)
                                       throws SQLException
        Throws:
        SQLException
      • numberOfTiles

        public int numberOfTiles()
                          throws SQLException
        Throws:
        SQLException
      • numberOfTiles

        public int numberOfTiles​(long zoomLevel)
                          throws SQLException
        Throws:
        SQLException
      • closestZoom

        public long closestZoom​(long zoomLevel)
                         throws SQLException
        Throws:
        SQLException
      • minZoom

        public long minZoom()
                     throws SQLException
        Throws:
        SQLException
      • maxZoom

        public long maxZoom()
                     throws SQLException
        Throws:
        SQLException
      • minColumn

        public long minColumn​(long zoomLevel)
                       throws SQLException
        Throws:
        SQLException
      • maxColumn

        public long maxColumn​(long zoomLevel)
                       throws SQLException
        Throws:
        SQLException
      • minRow

        public long minRow​(long zoomLevel)
                    throws SQLException
        Throws:
        SQLException
      • maxRow

        public long maxRow​(long zoomLevel)
                    throws SQLException
        Throws:
        SQLException
      • close

        public void close()
        Closes the mbtiles database connection.

        The application should always call this method when done with a mbtiles to prevent connection leakage.

        Specified by:
        close in interface AutoCloseable
      • getFile

        public File getFile()
        The underlying database file.

        Note: this value may be null depending on how the geopackage was initialized.

      • saveMetaDataEntry

        protected void saveMetaDataEntry​(String name,
                                         String value,
                                         Connection cx)
                                  throws SQLException
        Throws:
        SQLException
      • loadMetaDataEntry

        protected String loadMetaDataEntry​(String name,
                                           Connection cx)
                                    throws SQLException
        Throws:
        SQLException
      • init

        public void init()
                  throws IOException
        Initializes the mbtiles database.

        This method creates all the necessary tables.

        Throws:
        IOException
      • init

        protected void init​(Connection cx)
                     throws SQLException
        Initializes a mbtiles connection.

        This method creates all the necessary tables.

        Throws:
        SQLException
      • runScript

        protected void runScript​(String filename,
                                 Connection cx)
                          throws SQLException
        Throws:
        SQLException
      • toTilesRectangle

        protected RectangleLong toTilesRectangle​(Envelope envelope,
                                                 long zoomLevel)
                                          throws SQLException
        Converts the envelope into a tiles rectangle containing it, at the requested zoom level. X tiles start from west and increase towards east, Y tiles start from north and increase towards south
        Throws:
        SQLException
      • tilesForZoom

        protected static long tilesForZoom​(long zoomLevel)
      • getTileBounds

        protected RectangleLong getTileBounds​(long zoomLevel,
                                              boolean exact)
                                       throws SQLException
        Returns the actual tile bounds for the given zoom level,
        Throws:
        SQLException
      • getZoomLevel

        protected long getZoomLevel​(double distance)
                             throws SQLException
        Returns the zoom level for the given simplification distance
        Throws:
        SQLException
      • toEnvelope

        protected ReferencedEnvelope toEnvelope​(RectangleLong rect,
                                                long zoom)
        Converts the tile locations into a real world one
        Parameters:
        rect - The tile rectangle, in tile space
        zoom - The zoom level
      • toEnvelope

        protected static ReferencedEnvelope toEnvelope​(MBTilesTileLocation tile)