Search in sources :

Example 1 with TileSourceTemplate

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

the class NewTileSourceDialog method okPressed.

public boolean okPressed() {
    if (Algorithms.isEmpty(templateName.getText())) {
        // $NON-NLS-1$ //$NON-NLS-2$
        JOptionPane.showMessageDialog(this, Messages.getString("NewTileSourceDialog.SPECIFY.TEMPLATE.NAME"), Messages.getString("NewTileSourceDialog.ERROR.CREATING.NEW.TILE.SRC"), JOptionPane.ERROR_MESSAGE);
        return false;
    }
    if (Algorithms.isEmpty(templateUrl.getText())) {
        // $NON-NLS-1$ //$NON-NLS-2$
        JOptionPane.showMessageDialog(this, Messages.getString("NewTileSourceDialog.SPECIFY.TEMPLATE.URL"), Messages.getString("NewTileSourceDialog.ERROR.CREATING.NEW.TILE.SRC"), JOptionPane.ERROR_MESSAGE);
        return false;
    }
    String url = templateUrl.getText();
    if (!url.contains("{$x}") || !url.contains("{$y}") || !url.contains("{$z}")) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        // $NON-NLS-1$ //$NON-NLS-2$
        JOptionPane.showMessageDialog(this, Messages.getString("NewTileSourceDialog.SPECIFY.ALL.PLACEHLDRS"), Messages.getString("NewTileSourceDialog.ERROR.CREATING.NEW.TILE.SRC"), JOptionPane.ERROR_MESSAGE);
        return false;
    }
    File tilesDirectory = DataExtractionSettings.getSettings().getTilesDirectory();
    if (tilesDirectory != null) {
        File dir = new File(tilesDirectory, templateName.getText());
        if (dir.mkdirs()) {
            try {
                url = url.replace("{$x}", "{1}").replace("{$y}", "{2}").replace("{$z}", "{0}");
                tileSourceTemplate = new TileSourceManager.TileSourceTemplate(templateName.getText(), url, ".jpg", 18, 1, 256, 16, 20000);
                TileSourceManager.createMetaInfoFile(dir, tileSourceTemplate, true);
            } catch (IOException e) {
                // $NON-NLS-1$
                log.error(Messages.getString("NewTileSourceDialog.ERROR.CREATING.NEW.TILE.SRC") + " " + url, e);
            }
        }
    }
    return true;
}
Also used : TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) TileSourceManager(net.osmand.map.TileSourceManager) IOException(java.io.IOException) File(java.io.File)

Example 2 with TileSourceTemplate

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

the class MapPanel method getCommonTemplates.

private static Map<String, TileSourceTemplate> getCommonTemplates(File dir) {
    final List<TileSourceTemplate> list = TileSourceManager.getKnownSourceTemplates();
    Map<String, TileSourceTemplate> map = new LinkedHashMap<String, TileSourceTemplate>();
    for (TileSourceTemplate t : list) {
        map.put(t.getName(), t);
    }
    if (!dir.isDirectory()) {
        return map;
    }
    for (File f : dir.listFiles()) {
        if (f.isDirectory()) {
            if (map.containsKey(f.getName())) {
                if (TileSourceManager.isTileSourceMetaInfoExist(f)) {
                    map.put(f.getName(), TileSourceManager.createTileSourceTemplate(f));
                } else {
                    try {
                        TileSourceManager.createMetaInfoFile(f, map.get(f.getName()), false);
                    } catch (IOException e) {
                    }
                }
            } else {
                map.put(f.getName(), TileSourceManager.createTileSourceTemplate(f));
            }
        }
    }
    return map;
}
Also used : TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with TileSourceTemplate

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

the class MapPanel method getMenuToChooseSource.

public static JMenu getMenuToChooseSource(final MapPanel panel) {
    // $NON-NLS-1$
    final JMenu tiles = new JMenu(Messages.getString("MapPanel.SOURCE.OF.TILES"));
    // $NON-NLS-1$
    final JMenu downloadedMenu = new JMenu("Additional");
    final File tilesDirectory = DataExtractionSettings.getSettings().getTilesDirectory();
    Map<String, TileSourceTemplate> udf = getCommonTemplates(tilesDirectory);
    final List<TileSourceTemplate> downloaded = TileSourceManager.downloadTileSourceTemplates(MapCreatorVersion.APP_VERSION);
    final Map<TileSourceTemplate, JCheckBoxMenuItem> items = new IdentityHashMap<TileSourceTemplate, JCheckBoxMenuItem>();
    tiles.add(downloadedMenu);
    for (final TileSourceTemplate l : udf.values()) {
        if (l == null) {
            continue;
        }
        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()));
                }
                File dir = new File(tilesDirectory, l.getName());
                try {
                    dir.mkdirs();
                    TileSourceManager.createMetaInfoFile(dir, l, false);
                } catch (IOException e1) {
                }
                panel.setMapName(l);
            }
        });
    }
    if (downloaded != null) {
        for (final TileSourceTemplate l : downloaded) {
            if (l == null) {
                continue;
            }
            JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(l.getName());
            downloadedMenu.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()));
                    }
                    File dir = new File(tilesDirectory, l.getName());
                    try {
                        dir.mkdirs();
                        TileSourceManager.createMetaInfoFile(dir, l, true);
                    } catch (IOException e1) {
                    }
                    panel.setMapName(l);
                }
            });
        }
    }
    for (final Map.Entry<TileSourceTemplate, JCheckBoxMenuItem> em : items.entrySet()) {
        if (Algorithms.objectEquals(panel.getMap(), em.getKey())) {
            em.getValue().setSelected(true);
        }
    }
    tiles.addSeparator();
    tiles.add(createNewTileSourceAction(panel, tiles, items));
    return tiles;
}
Also used : TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) ActionEvent(java.awt.event.ActionEvent) IdentityHashMap(java.util.IdentityHashMap) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) ActionListener(java.awt.event.ActionListener) File(java.io.File) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) JMenu(javax.swing.JMenu)

Example 4 with TileSourceTemplate

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

the class SQLiteBigPlanetIndex method createSQLiteDatabase.

public static void createSQLiteDatabase(File dirWithTiles, String regionName, ITileSource template) throws SQLException, IOException {
    long now = System.currentTimeMillis();
    try {
        // $NON-NLS-1$
        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException e) {
        // $NON-NLS-1$
        log.error("Illegal configuration", e);
        throw new IllegalStateException(e);
    }
    File fileToWrite = new File(dirWithTiles, regionName + "." + template.getName() + ".sqlitedb");
    fileToWrite.delete();
    // $NON-NLS-1$
    Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileToWrite.getAbsolutePath());
    Statement statement = conn.createStatement();
    statement.execute("CREATE TABLE tiles (x int, y int, z int, s int, image blob, time long, PRIMARY KEY (x,y,z,s))");
    statement.execute("CREATE INDEX IND on tiles (x,y,z,s)");
    statement.execute("CREATE TABLE info(tilenumbering,minzoom,maxzoom,timecolumn,url,rule,referer)");
    statement.execute("CREATE TABLE android_metadata (locale TEXT)");
    statement.close();
    PreparedStatement pStatement = conn.prepareStatement("INSERT INTO INFO VALUES(?,?,?,?,?,?,?)");
    String tileNumbering = bigPlanet ? "BigPlanet" : "simple";
    pStatement.setString(1, tileNumbering);
    int minNormalZoom = bigPlanet ? 17 - template.getMaximumZoomSupported() : template.getMinimumZoomSupported();
    int maxNormalZoom = bigPlanet ? 17 - template.getMinimumZoomSupported() : template.getMaximumZoomSupported();
    pStatement.setInt(2, minNormalZoom);
    pStatement.setInt(3, maxNormalZoom);
    pStatement.setString(4, "yes");
    pStatement.setString(5, ((TileSourceTemplate) template).getUrlTemplate());
    pStatement.setString(6, ((TileSourceTemplate) template).getRule());
    pStatement.setString(7, ((TileSourceTemplate) template).getReferer());
    pStatement.execute();
    log.info("Info table" + tileNumbering + "maxzoom = " + maxNormalZoom + " minzoom = " + minNormalZoom + " timecolumn = yes" + " url = " + ((TileSourceTemplate) template).getUrlTemplate());
    pStatement.close();
    conn.setAutoCommit(false);
    pStatement = conn.prepareStatement("INSERT INTO tiles VALUES (?, ?, ?, ?, ?, ?)");
    int ch = 0;
    // be attentive to create buf enough for image
    byte[] buf;
    int maxZoom = 17;
    int minZoom = 1;
    File rootDir = new File(dirWithTiles, template.getName());
    for (File z : rootDir.listFiles()) {
        try {
            int zoom = Integer.parseInt(z.getName());
            for (File xDir : z.listFiles()) {
                try {
                    int x = Integer.parseInt(xDir.getName());
                    for (File f : xDir.listFiles()) {
                        if (!f.isFile()) {
                            continue;
                        }
                        try {
                            int i = f.getName().indexOf('.');
                            int y = Integer.parseInt(f.getName().substring(0, i));
                            buf = new byte[(int) f.length()];
                            if (zoom > maxZoom) {
                                maxZoom = zoom;
                            }
                            if (zoom < minZoom) {
                                minZoom = zoom;
                            }
                            FileInputStream is = new FileInputStream(f);
                            int l = 0;
                            try {
                                l = is.read(buf);
                            } finally {
                                is.close();
                            }
                            if (l > 0) {
                                pStatement.setInt(1, x);
                                pStatement.setInt(2, y);
                                pStatement.setInt(3, bigPlanet ? 17 - zoom : zoom);
                                pStatement.setInt(4, 0);
                                pStatement.setBytes(5, buf);
                                pStatement.setLong(6, f.lastModified());
                                pStatement.addBatch();
                                ch++;
                                if (ch >= BATCH_SIZE) {
                                    pStatement.executeBatch();
                                    ch = 0;
                                }
                            }
                        } catch (NumberFormatException e) {
                        }
                    }
                } catch (NumberFormatException e) {
                }
            }
        } catch (NumberFormatException e) {
        }
    }
    if (ch > 0) {
        pStatement.executeBatch();
        ch = 0;
    }
    pStatement.close();
    conn.commit();
    conn.close();
    log.info("Index created " + fileToWrite.getName() + " " + (System.currentTimeMillis() - now) + " ms");
}
Also used : BeanShellTileSourceTemplate(net.osmand.map.TileSourceManager.BeanShellTileSourceTemplate) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) FileInputStream(java.io.FileInputStream) File(java.io.File)

Example 5 with TileSourceTemplate

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

the class OsmandRasterMapsPlugin method selectMapOverlayLayer.

public void selectMapOverlayLayer(@NonNull final OsmandMapTileView mapView, @NonNull final CommonPreference<String> mapPref, @NonNull final CommonPreference<String> exMapPref, boolean force, @NonNull final MapActivity activity, @Nullable final OnMapSelectedCallback callback) {
    final MapActivityLayers layers = activity.getMapLayers();
    if (!force && exMapPref.get() != null) {
        mapPref.set(exMapPref.get());
        if (callback != null) {
            callback.onMapSelected(false);
        }
        updateMapLayers(mapView, mapPref, layers);
        return;
    }
    final OsmandSettings settings = app.getSettings();
    Map<String, String> entriesMap = settings.getTileSourceEntries();
    final ArrayList<String> keys = new ArrayList<>(entriesMap.keySet());
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    final String[] items = new String[entriesMap.size() + 1];
    int i = 0;
    for (String it : entriesMap.values()) {
        items[i++] = it;
    }
    items[i] = app.getString(R.string.install_more);
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == items.length - 1) {
                installMapLayers(activity, new ResultMatcher<TileSourceTemplate>() {

                    TileSourceTemplate template = null;

                    int count = 0;

                    @Override
                    public boolean publish(TileSourceTemplate object) {
                        if (object == null) {
                            if (count == 1) {
                                mapPref.set(template.getName());
                                exMapPref.set(template.getName());
                                if (callback != null) {
                                    callback.onMapSelected(false);
                                }
                                updateMapLayers(mapView, mapPref, layers);
                            } else {
                                selectMapOverlayLayer(mapView, mapPref, exMapPref, false, activity, null);
                            }
                        } else {
                            count++;
                            template = object;
                        }
                        return false;
                    }

                    @Override
                    public boolean isCancelled() {
                        return false;
                    }
                });
            } else {
                mapPref.set(keys.get(which));
                exMapPref.set(keys.get(which));
                if (callback != null) {
                    callback.onMapSelected(false);
                }
                updateMapLayers(mapView, mapPref, layers);
            }
            dialog.dismiss();
        }
    }).setNegativeButton(R.string.shared_string_cancel, null).setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            if (callback != null) {
                callback.onMapSelected(true);
            }
        }
    });
    builder.show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) MapActivityLayers(net.osmand.plus.activities.MapActivityLayers) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) ResultMatcher(net.osmand.ResultMatcher) OsmandSettings(net.osmand.plus.OsmandSettings)

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