Search in sources :

Example 1 with SQLiteTileSource

use of net.osmand.plus.SQLiteTileSource in project Osmand by osmandapp.

the class BitmapTilesCache method getTileObject.

@Override
protected Bitmap getTileObject(TileLoadDownloadRequest req) {
    Bitmap bmp = null;
    if (req.tileSource instanceof SQLiteTileSource) {
        try {
            long[] tm = new long[1];
            bmp = ((SQLiteTileSource) req.tileSource).getImage(req.xTile, req.yTile, req.zoom, tm);
            if (tm[0] != 0) {
                downloadIfExpired(req, tm[0]);
            }
        } catch (OutOfMemoryError e) {
            // $NON-NLS-1$
            log.error("Out of memory error", e);
            clearTiles();
        }
    } else {
        File en = new File(req.dirWithTiles, req.tileId);
        if (en.exists()) {
            try {
                bmp = BitmapFactory.decodeFile(en.getAbsolutePath());
                downloadIfExpired(req, en.lastModified());
            } catch (OutOfMemoryError e) {
                // $NON-NLS-1$
                log.error("Out of memory error", e);
                clearTiles();
            }
        }
    }
    return bmp;
}
Also used : Bitmap(android.graphics.Bitmap) File(java.io.File) SQLiteTileSource(net.osmand.plus.SQLiteTileSource)

Example 2 with SQLiteTileSource

use of net.osmand.plus.SQLiteTileSource in project Osmand by osmandapp.

the class HillshadeLayer method createTileSource.

private SQLiteTileSource createTileSource(MapActivity activity) {
    return new SQLiteTileSource(activity.getMyApplication(), null, new ArrayList<TileSourceTemplate>()) {

        @Override
        protected SQLiteConnection getDatabase() {
            throw new UnsupportedOperationException();
        }

        public boolean isLocked() {
            return false;
        }

        List<String> getTileSource(int x, int y, int zoom) {
            ArrayList<String> ls = new ArrayList<String>();
            int z = (zoom - ZOOM_BOUNDARY);
            if (z > 0) {
                indexedResources.queryInBox(new QuadRect(x >> z, y >> z, (x >> z), (y >> z)), ls);
            } else {
                indexedResources.queryInBox(new QuadRect(x << -z, y << -z, (x + 1) << -z, (y + 1) << -z), ls);
            }
            return ls;
        }

        @Override
        public boolean exists(int x, int y, int zoom) {
            List<String> ts = getTileSource(x, y, zoom);
            for (String t : ts) {
                SQLiteTileSource sqLiteTileSource = resources.get(t);
                if (sqLiteTileSource != null && sqLiteTileSource.exists(x, y, zoom)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public Bitmap getImage(int x, int y, int zoom, long[] timeHolder) {
            List<String> ts = getTileSource(x, y, zoom);
            for (String t : ts) {
                SQLiteTileSource sqLiteTileSource = resources.get(t);
                if (sqLiteTileSource != null) {
                    Bitmap bmp = sqLiteTileSource.getImage(x, y, zoom, timeHolder);
                    if (bmp != null) {
                        return sqLiteTileSource.getImage(x, y, zoom, timeHolder);
                    }
                }
            }
            return null;
        }

        @Override
        public int getBitDensity() {
            return 32;
        }

        @Override
        public int getMinimumZoomSupported() {
            return 5;
        }

        @Override
        public int getMaximumZoomSupported() {
            return 11;
        }

        @Override
        public int getTileSize() {
            return 256;
        }

        @Override
        public boolean couldBeDownloadedFromInternet() {
            return false;
        }

        @Override
        public String getName() {
            return "Hillshade";
        }

        @Override
        public String getTileFormat() {
            return "jpg";
        }
    };
}
Also used : Bitmap(android.graphics.Bitmap) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) ArrayList(java.util.ArrayList) QuadRect(net.osmand.data.QuadRect) SQLiteTileSource(net.osmand.plus.SQLiteTileSource)

Example 3 with SQLiteTileSource

use of net.osmand.plus.SQLiteTileSource in project Osmand by osmandapp.

the class LocalIndexHelper method updateDescription.

public void updateDescription(LocalIndexInfo info) {
    File f = new File(info.getPathToData());
    if (info.getType() == LocalIndexType.MAP_DATA) {
        Map<String, String> ifns = app.getResourceManager().getIndexFileNames();
        if (ifns.containsKey(info.getFileName())) {
            try {
                Date dt = app.getResourceManager().getDateFormat().parse(ifns.get(info.getFileName()));
                info.setDescription(getInstalledDate(dt.getTime(), null));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            info.setDescription(getInstalledDate(f));
        }
    } else if (info.getType() == LocalIndexType.TILES_DATA) {
        ITileSource template;
        if (f.isDirectory() && TileSourceManager.isTileSourceMetaInfoExist(f)) {
            template = TileSourceManager.createTileSourceTemplate(new File(info.getPathToData()));
        } else if (f.isFile() && f.getName().endsWith(SQLiteTileSource.EXT)) {
            template = new SQLiteTileSource(app, f, TileSourceManager.getKnownSourceTemplates());
        } else {
            return;
        }
        String descr = "";
        descr += app.getString(R.string.local_index_tile_data_name, template.getName());
        if (template.getExpirationTimeMinutes() >= 0) {
            descr += "\n" + app.getString(R.string.local_index_tile_data_expire, template.getExpirationTimeMinutes());
        }
        info.setAttachedObject(template);
        info.setDescription(descr);
    } else if (info.getType() == LocalIndexType.SRTM_DATA) {
        info.setDescription(app.getString(R.string.download_srtm_maps));
    } else if (info.getType() == LocalIndexType.WIKI_DATA) {
        info.setDescription(getInstalledDate(f));
    } else if (info.getType() == LocalIndexType.TTS_VOICE_DATA) {
        info.setDescription(getInstalledDate(f));
    } else if (info.getType() == LocalIndexType.DEACTIVATED) {
        info.setDescription(getInstalledDate(f));
    } else if (info.getType() == LocalIndexType.VOICE_DATA) {
        info.setDescription(getInstalledDate(f));
    } else if (info.getType() == LocalIndexType.FONT_DATA) {
        info.setDescription(getInstalledDate(f));
    }
}
Also used : ITileSource(net.osmand.map.ITileSource) ParseException(java.text.ParseException) File(java.io.File) Date(java.util.Date) SQLiteTileSource(net.osmand.plus.SQLiteTileSource)

Example 4 with SQLiteTileSource

use of net.osmand.plus.SQLiteTileSource in project Osmand by osmandapp.

the class MapActivityLayers method updateMapSource.

public void updateMapSource(OsmandMapTileView mapView, CommonPreference<String> settingsToWarnAboutMap) {
    OsmandSettings settings = getApplication().getSettings();
    // update transparency
    int mapTransparency = settings.MAP_UNDERLAY.get() == null ? 255 : settings.MAP_TRANSPARENCY.get();
    mapTileLayer.setAlpha(mapTransparency);
    mapVectorLayer.setAlpha(mapTransparency);
    ITileSource newSource = settings.getMapTileSource(settings.MAP_TILE_SOURCES == settingsToWarnAboutMap);
    ITileSource oldMap = mapTileLayer.getMap();
    if (newSource != oldMap) {
        if (oldMap instanceof SQLiteTileSource) {
            ((SQLiteTileSource) oldMap).closeDB();
        }
        mapTileLayer.setMap(newSource);
    }
    boolean vectorData = !settings.MAP_ONLINE_DATA.get();
    mapTileLayer.setVisible(!vectorData);
    mapVectorLayer.setVisible(vectorData);
    if (vectorData) {
        mapView.setMainLayer(mapVectorLayer);
    } else {
        mapView.setMainLayer(mapTileLayer);
    }
}
Also used : ITileSource(net.osmand.map.ITileSource) OsmandSettings(net.osmand.plus.OsmandSettings) SQLiteTileSource(net.osmand.plus.SQLiteTileSource)

Example 5 with SQLiteTileSource

use of net.osmand.plus.SQLiteTileSource in project Osmand by osmandapp.

the class HillshadeLayer method indexHillshadeFiles.

private void indexHillshadeFiles(final OsmandApplication app) {
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

        private SQLiteDatabase sqliteDb;

        @Override
        protected Void doInBackground(Void... params) {
            File tilesDir = app.getAppPath(IndexConstants.TILES_INDEX_DIR);
            File cacheDir = app.getCacheDir();
            // fix http://stackoverflow.com/questions/26937152/workaround-for-nexus-9-sqlite-file-write-operations-on-external-dirs
            sqliteDb = SQLiteDatabase.openDatabase(new File(cacheDir, HILLSHADE_CACHE).getPath(), null, SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING | SQLiteDatabase.CREATE_IF_NECESSARY);
            if (sqliteDb.getVersion() == 0) {
                sqliteDb.setVersion(1);
                sqliteDb.execSQL("CREATE TABLE TILE_SOURCES(filename varchar2(256), date_modified int, left int, right int, top int, bottom int)");
            }
            Map<String, Long> fileModified = new HashMap<String, Long>();
            Map<String, SQLiteTileSource> rs = readFiles(app, tilesDir, fileModified);
            indexCachedResources(fileModified, rs);
            indexNonCachedResources(fileModified, rs);
            sqliteDb.close();
            resources = rs;
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            app.getResourceManager().reloadTilesFromFS();
        }

        private void indexNonCachedResources(Map<String, Long> fileModified, Map<String, SQLiteTileSource> rs) {
            for (String filename : fileModified.keySet()) {
                try {
                    log.info("Indexing hillshade file " + filename);
                    ContentValues cv = new ContentValues();
                    cv.put("filename", filename);
                    cv.put("date_modified", fileModified.get(filename));
                    SQLiteTileSource ts = rs.get(filename);
                    QuadRect rt = ts.getRectBoundary(ZOOM_BOUNDARY, 1);
                    if (rt != null) {
                        indexedResources.insert(filename, rt);
                        cv.put("left", (int) rt.left);
                        cv.put("right", (int) rt.right);
                        cv.put("top", (int) rt.top);
                        cv.put("bottom", (int) rt.bottom);
                        sqliteDb.insert("TILE_SOURCES", null, cv);
                    }
                } catch (RuntimeException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }

        private void indexCachedResources(Map<String, Long> fileModified, Map<String, SQLiteTileSource> rs) {
            Cursor cursor = sqliteDb.rawQuery("SELECT filename, date_modified, left, right, top, bottom FROM TILE_SOURCES", new String[0]);
            if (cursor.moveToFirst()) {
                do {
                    String filename = cursor.getString(0);
                    long lastModified = cursor.getLong(1);
                    Long read = fileModified.get(filename);
                    if (rs.containsKey(filename) && read != null && lastModified == read) {
                        int left = cursor.getInt(2);
                        int right = cursor.getInt(3);
                        int top = cursor.getInt(4);
                        float bottom = cursor.getInt(5);
                        indexedResources.insert(filename, new QuadRect(left, top, right, bottom));
                        fileModified.remove(filename);
                    }
                } while (cursor.moveToNext());
            }
            cursor.close();
        }

        private Map<String, SQLiteTileSource> readFiles(final OsmandApplication app, File tilesDir, Map<String, Long> fileModified) {
            Map<String, SQLiteTileSource> rs = new LinkedHashMap<String, SQLiteTileSource>();
            File[] files = tilesDir.listFiles();
            if (files != null) {
                for (File f : files) {
                    if (f != null && f.getName().endsWith(IndexConstants.SQLITE_EXT) && f.getName().toLowerCase().startsWith("hillshade")) {
                        SQLiteTileSource ts = new SQLiteTileSource(app, f, new ArrayList<TileSourceTemplate>());
                        rs.put(f.getName(), ts);
                        fileModified.put(f.getName(), f.lastModified());
                    }
                }
            }
            return rs;
        }
    };
    executeTaskInBackground(task);
}
Also used : ContentValues(android.content.ContentValues) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) OsmandApplication(net.osmand.plus.OsmandApplication) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AsyncTask(android.os.AsyncTask) Cursor(android.database.Cursor) QuadRect(net.osmand.data.QuadRect) SQLiteTileSource(net.osmand.plus.SQLiteTileSource) LinkedHashMap(java.util.LinkedHashMap) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

SQLiteTileSource (net.osmand.plus.SQLiteTileSource)6 File (java.io.File)4 Bitmap (android.graphics.Bitmap)2 QuadRect (net.osmand.data.QuadRect)2 ITileSource (net.osmand.map.ITileSource)2 TileSourceTemplate (net.osmand.map.TileSourceManager.TileSourceTemplate)2 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 AsyncTask (android.os.AsyncTask)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 OsmandSettings (net.osmand.plus.OsmandSettings)1 TileLoadDownloadRequest (net.osmand.plus.resources.AsyncLoadingThread.TileLoadDownloadRequest)1