Search in sources :

Example 11 with TileSourceTemplate

use of net.osmand.map.TileSourceManager.TileSourceTemplate in project OsmAnd-tools by osmandapp.

the class MapPanel method createNewTileSourceAction.

private static AbstractAction createNewTileSourceAction(final MapPanel panel, final JMenu tiles, final Map<TileSourceTemplate, JCheckBoxMenuItem> items) {
    return new // $NON-NLS-1$
    AbstractAction(// $NON-NLS-1$
    Messages.getString("MapPanel.NEW.TILE.SRC")) {

        private static final long serialVersionUID = -8286622335859339130L;

        @Override
        public void actionPerformed(ActionEvent e) {
            NewTileSourceDialog dlg = new NewTileSourceDialog(panel);
            dlg.showDialog();
            final TileSourceTemplate l = dlg.getTileSourceTemplate();
            if (l != null) {
                JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(l.getName());
                tiles.add(menuItem);
                items.put(l, menuItem);
                menuItem.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        for (final Map.Entry<TileSourceTemplate, JCheckBoxMenuItem> es : items.entrySet()) {
                            es.getValue().setSelected(l.equals(es.getKey()));
                        }
                        panel.setMapName(l);
                    }
                });
                for (final Map.Entry<TileSourceTemplate, JCheckBoxMenuItem> es : items.entrySet()) {
                    es.getValue().setSelected(l.equals(es.getKey()));
                }
                panel.setMapName(l);
            }
        }
    };
}
Also used : TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) AbstractAction(javax.swing.AbstractAction) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem)

Example 12 with TileSourceTemplate

use of net.osmand.map.TileSourceManager.TileSourceTemplate in project Osmand by osmandapp.

the class MapActivityLayers method selectMapLayer.

public void selectMapLayer(final OsmandMapTileView mapView, final ContextMenuItem it, final ArrayAdapter<ContextMenuItem> adapter) {
    if (OsmandPlugin.getEnabledPlugin(OsmandRasterMapsPlugin.class) == null) {
        Toast.makeText(activity, R.string.map_online_plugin_is_not_installed, Toast.LENGTH_LONG).show();
        return;
    }
    final OsmandSettings settings = getApplication().getSettings();
    final LinkedHashMap<String, String> entriesMap = new LinkedHashMap<>();
    final String layerOsmVector = "LAYER_OSM_VECTOR";
    final String layerInstallMore = "LAYER_INSTALL_MORE";
    final String layerEditInstall = "LAYER_EDIT";
    entriesMap.put(layerOsmVector, getString(R.string.vector_data));
    entriesMap.putAll(settings.getTileSourceEntries());
    entriesMap.put(layerInstallMore, getString(R.string.install_more));
    entriesMap.put(layerEditInstall, getString(R.string.maps_define_edit));
    final List<Entry<String, String>> entriesMapList = new ArrayList<>(entriesMap.entrySet());
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    String selectedTileSourceKey = settings.MAP_TILE_SOURCES.get();
    int selectedItem = -1;
    if (!settings.MAP_ONLINE_DATA.get()) {
        selectedItem = 0;
    } else {
        Entry<String, String> selectedEntry = null;
        for (Entry<String, String> entry : entriesMap.entrySet()) {
            if (entry.getKey().equals(selectedTileSourceKey)) {
                selectedEntry = entry;
                break;
            }
        }
        if (selectedEntry != null) {
            selectedItem = 0;
            entriesMapList.remove(selectedEntry);
            entriesMapList.add(0, selectedEntry);
        }
    }
    final String[] items = new String[entriesMapList.size()];
    int i = 0;
    for (Entry<String, String> entry : entriesMapList) {
        items[i++] = entry.getValue();
    }
    builder.setSingleChoiceItems(items, selectedItem, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String layerKey = entriesMapList.get(which).getKey();
            switch(layerKey) {
                case layerOsmVector:
                    settings.MAP_ONLINE_DATA.set(false);
                    updateMapSource(mapView, null);
                    it.setDescription(null);
                    adapter.notifyDataSetChanged();
                    break;
                case layerEditInstall:
                    OsmandRasterMapsPlugin.defineNewEditLayer(activity, new ResultMatcher<TileSourceTemplate>() {

                        @Override
                        public boolean publish(TileSourceTemplate object) {
                            settings.MAP_TILE_SOURCES.set(object.getName());
                            settings.MAP_ONLINE_DATA.set(true);
                            if (it != null) {
                                it.setDescription(object.getName());
                            }
                            updateMapSource(mapView, settings.MAP_TILE_SOURCES);
                            return true;
                        }

                        @Override
                        public boolean isCancelled() {
                            return false;
                        }
                    });
                    break;
                case layerInstallMore:
                    OsmandRasterMapsPlugin.installMapLayers(activity, new ResultMatcher<TileSourceTemplate>() {

                        TileSourceTemplate template = null;

                        int count = 0;

                        @Override
                        public boolean publish(TileSourceTemplate object) {
                            if (object == null) {
                                if (count == 1) {
                                    settings.MAP_TILE_SOURCES.set(template.getName());
                                    settings.MAP_ONLINE_DATA.set(true);
                                    it.setDescription(template.getName());
                                    adapter.notifyDataSetChanged();
                                    updateMapSource(mapView, settings.MAP_TILE_SOURCES);
                                } else {
                                    selectMapLayer(mapView, it, adapter);
                                }
                            } else {
                                count++;
                                template = object;
                            }
                            return false;
                        }

                        @Override
                        public boolean isCancelled() {
                            return false;
                        }
                    });
                    break;
                default:
                    settings.MAP_TILE_SOURCES.set(layerKey);
                    settings.MAP_ONLINE_DATA.set(true);
                    it.setDescription(layerKey);
                    adapter.notifyDataSetChanged();
                    updateMapSource(mapView, settings.MAP_TILE_SOURCES);
                    break;
            }
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(R.string.shared_string_dismiss, null);
    builder.show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) ResultMatcher(net.osmand.ResultMatcher) OsmandSettings(net.osmand.plus.OsmandSettings) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) OsmandRasterMapsPlugin(net.osmand.plus.rastermaps.OsmandRasterMapsPlugin)

Example 13 with TileSourceTemplate

use of net.osmand.map.TileSourceManager.TileSourceTemplate in project Osmand by osmandapp.

the class OsmandSettings method getTileSourceEntries.

public Map<String, String> getTileSourceEntries(boolean sqlite) {
    Map<String, String> map = new LinkedHashMap<String, String>();
    File dir = ctx.getAppPath(IndexConstants.TILES_INDEX_DIR);
    if (dir != null && dir.canRead()) {
        File[] files = dir.listFiles();
        Arrays.sort(files, new Comparator<File>() {

            @Override
            public int compare(File object1, File object2) {
                if (object1.lastModified() > object2.lastModified()) {
                    return -1;
                } else if (object1.lastModified() == object2.lastModified()) {
                    return 0;
                }
                return 1;
            }
        });
        if (files != null) {
            for (File f : files) {
                if (f.getName().endsWith(IndexConstants.SQLITE_EXT)) {
                    if (sqlite) {
                        String n = f.getName();
                        map.put(f.getName(), n.substring(0, n.lastIndexOf('.')));
                    }
                } else if (f.isDirectory() && !f.getName().equals(IndexConstants.TEMP_SOURCE_TO_LOAD) && !f.getName().startsWith(".")) {
                    map.put(f.getName(), f.getName());
                }
            }
        }
    }
    for (TileSourceTemplate l : TileSourceManager.getKnownSourceTemplates()) {
        if (!l.isHidden()) {
            map.put(l.getName(), l.getName());
        } else {
            map.remove(l.getName());
        }
    }
    return map;
}
Also used : TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with TileSourceTemplate

use of net.osmand.map.TileSourceManager.TileSourceTemplate in project Osmand by osmandapp.

the class OsmandRasterMapsPlugin method installMapLayers.

public static void installMapLayers(final Activity activity, final ResultMatcher<TileSourceTemplate> result) {
    final OsmandApplication app = (OsmandApplication) activity.getApplication();
    final OsmandSettings settings = app.getSettings();
    final Map<String, String> entriesMap = settings.getTileSourceEntries();
    if (!settings.isInternetConnectionAvailable(true)) {
        Toast.makeText(activity, R.string.internet_not_available, Toast.LENGTH_LONG).show();
        return;
    }
    AsyncTask<Void, Void, List<TileSourceTemplate>> t = new AsyncTask<Void, Void, List<TileSourceTemplate>>() {

        @Override
        protected List<TileSourceTemplate> doInBackground(Void... params) {
            return TileSourceManager.downloadTileSourceTemplates(Version.getVersionAsURLParam(app));
        }

        protected void onPostExecute(final java.util.List<TileSourceTemplate> downloaded) {
            if (downloaded == null || downloaded.isEmpty()) {
                Toast.makeText(activity, R.string.shared_string_io_error, Toast.LENGTH_SHORT).show();
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            String[] names = new String[downloaded.size()];
            for (int i = 0; i < names.length; i++) {
                names[i] = downloaded.get(i).getName();
            }
            final boolean[] selected = new boolean[downloaded.size()];
            builder.setMultiChoiceItems(names, selected, new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    selected[which] = isChecked;
                    if (entriesMap.containsKey(downloaded.get(which).getName()) && isChecked) {
                        Toast.makeText(activity, R.string.tile_source_already_installed, Toast.LENGTH_SHORT).show();
                    }
                }
            });
            builder.setNegativeButton(R.string.shared_string_cancel, null);
            builder.setTitle(R.string.select_tile_source_to_install);
            builder.setPositiveButton(R.string.shared_string_apply, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    List<TileSourceTemplate> toInstall = new ArrayList<>();
                    for (int i = 0; i < selected.length; i++) {
                        if (selected[i]) {
                            toInstall.add(downloaded.get(i));
                        }
                    }
                    for (TileSourceTemplate ts : toInstall) {
                        if (settings.installTileSource(ts)) {
                            if (result != null) {
                                result.publish(ts);
                            }
                        }
                    }
                    // at the end publish null to show end of process
                    if (!toInstall.isEmpty() && result != null) {
                        result.publish(null);
                    }
                }
            });
            builder.show();
        }
    };
    t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) OsmandApplication(net.osmand.plus.OsmandApplication) DialogInterface(android.content.DialogInterface) AsyncTask(android.os.AsyncTask) OsmandSettings(net.osmand.plus.OsmandSettings) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with TileSourceTemplate

use of net.osmand.map.TileSourceManager.TileSourceTemplate 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

TileSourceTemplate (net.osmand.map.TileSourceManager.TileSourceTemplate)15 File (java.io.File)8 LinkedHashMap (java.util.LinkedHashMap)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 DialogInterface (android.content.DialogInterface)4 AlertDialog (android.support.v7.app.AlertDialog)4 OsmandSettings (net.osmand.plus.OsmandSettings)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 OsmandApplication (net.osmand.plus.OsmandApplication)3 AsyncTask (android.os.AsyncTask)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 IdentityHashMap (java.util.IdentityHashMap)2 TreeMap (java.util.TreeMap)2 IIOException (javax.imageio.IIOException)2 JCheckBoxMenuItem (javax.swing.JCheckBoxMenuItem)2 ResultMatcher (net.osmand.ResultMatcher)2 QuadRect (net.osmand.data.QuadRect)2