Changeset 24521 in osm for applications/editors


Ignore:
Timestamp:
2010-12-02T17:11:07+01:00 (14 years ago)
Author:
upliner
Message:

add TMS settings

Location:
applications/editors/josm/plugins/imagery/src/org/openstreetmap/josm/plugins/imagery
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery/src/org/openstreetmap/josm/plugins/imagery/ImageryPreferenceEditor.java

    r24509 r24521  
    77import java.awt.Dimension;
    88import java.awt.FlowLayout;
     9import java.awt.Font;
    910import java.awt.GridBagConstraints;
    1011import java.awt.GridBagLayout;
     
    2728import javax.swing.JPanel;
    2829import javax.swing.JScrollPane;
     30import javax.swing.JSeparator;
     31import javax.swing.JSlider;
    2932import javax.swing.JSpinner;
    3033import javax.swing.JTabbedPane;
     
    3740import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    3841import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
     42import org.openstreetmap.josm.plugins.imagery.tms.TMSPreferences;
    3943import org.openstreetmap.josm.plugins.imagery.wms.AddWMSLayerPanel;
    4044import org.openstreetmap.josm.plugins.imagery.wms.WMSAdapter;
     
    5458    ImageryPlugin plugin = ImageryPlugin.instance;
    5559
    56     public JPanel buildImageryProvidersPanel(final PreferenceTabbedPane gui) {
     60    //TMS settings controls
     61    private JCheckBox autozoomActive = new JCheckBox();
     62    private JCheckBox autoloadTiles = new JCheckBox();
     63    private JSpinner maxZoomLvl;
     64    private JSpinner minZoomLvl = new JSpinner();
     65    private JSlider fadeBackground = new JSlider(0, 100);
     66
     67
     68    private JPanel buildImageryProvidersPanel(final PreferenceTabbedPane gui) {
    5769        final JPanel p = new JPanel(new GridBagLayout());
    5870        model = new ImageryLayerTableModel();
     
    173185    }
    174186
    175     public Component buildSettingsPanel() {
    176         final JPanel p = new JPanel(new GridBagLayout());
    177         p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    178 
     187    private JPanel buildWMSSettingsPanel() {
     188        final JPanel pnlW = new JPanel(new GridBagLayout());
    179189        browser = new JComboBox(new String[] {
    180190                "webkit-image {0}",
     
    184194        browser.setEditable(true);
    185195        browser.setSelectedItem(Main.pref.get("wmsplugin.browser", "webkit-image {0}"));
    186         p.add(new JLabel(tr("Downloader:")), GBC.eol().fill(GBC.HORIZONTAL));
    187         p.add(browser);
     196        pnlW.add(new JLabel(tr("Downloader:")), GBC.eol().fill(GBC.HORIZONTAL));
     197        pnlW.add(browser);
    188198
    189199        // Overlap
    190         p.add(Box.createHorizontalGlue(), GBC.eol().fill(GBC.HORIZONTAL));
     200        pnlW.add(Box.createHorizontalGlue(), GBC.eol().fill(GBC.HORIZONTAL));
    191201
    192202        overlapCheckBox = new JCheckBox(tr("Overlap tiles"), wmsAdapter.PROP_OVERLAP.get());
     
    203213        overlapPanel.add(spinNorth);
    204214
    205         p.add(overlapPanel);
     215        pnlW.add(overlapPanel);
    206216
    207217        // Simultaneous connections
    208         p.add(Box.createHorizontalGlue(), GBC.eol().fill(GBC.HORIZONTAL));
     218        pnlW.add(Box.createHorizontalGlue(), GBC.eol().fill(GBC.HORIZONTAL));
    209219        JLabel labelSimConn = new JLabel(tr("Simultaneous connections"));
    210220        spinSimConn = new JSpinner(new SpinnerNumberModel(wmsAdapter.PROP_SIMULTANEOUS_CONNECTIONS.get(), 1, 30, 1));
    211         JPanel overlapPanelSimConn = new JPanel(new FlowLayout());
     221        JPanel overlapPanelSimConn = new JPanel(new FlowLayout(FlowLayout.LEFT));
    212222        overlapPanelSimConn.add(labelSimConn);
    213223        overlapPanelSimConn.add(spinSimConn);
    214         p.add(overlapPanelSimConn, GBC.eol().fill(GBC.HORIZONTAL).anchor(GBC.NORTHWEST));
     224        pnlW.add(overlapPanelSimConn, GBC.eol().fill(GBC.HORIZONTAL));
    215225
    216226        allowRemoteControl = Main.pref.getBoolean("wmsplugin.remotecontrol", true);
    217227        remoteCheckBox = new JCheckBox(tr("Allow remote control (reqires remotecontrol plugin)"), allowRemoteControl);
    218         JPanel remotePanel = new JPanel(new FlowLayout());
     228        JPanel remotePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    219229        remotePanel.add(remoteCheckBox);
    220230
    221         p.add(remotePanel,GBC.eol().fill(GBC.HORIZONTAL).anchor(GBC.NORTHWEST));
     231        pnlW.add(remotePanel,GBC.eol().fill(GBC.HORIZONTAL));
     232        return pnlW;
     233    }
     234
     235    private JPanel buildTMSSettingsPanel() {
     236        JPanel tmsTab = new JPanel(new GridBagLayout());
     237        minZoomLvl = new JSpinner(new SpinnerNumberModel(TMSPreferences.DEFAULT_MIN_ZOOM, TMSPreferences.MIN_ZOOM, TMSPreferences.MAX_ZOOM, 1));
     238        maxZoomLvl = new JSpinner(new SpinnerNumberModel(TMSPreferences.DEFAULT_MAX_ZOOM, TMSPreferences.MIN_ZOOM, TMSPreferences.MAX_ZOOM, 1));
     239
     240        tmsTab.add(new JLabel(tr("Auto zoom by default: ")), GBC.std());
     241        tmsTab.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     242        tmsTab.add(autozoomActive, GBC.eol().fill(GBC.HORIZONTAL));
     243
     244        tmsTab.add(new JLabel(tr("Autoload tiles by default: ")), GBC.std());
     245        tmsTab.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     246        tmsTab.add(autoloadTiles, GBC.eol().fill(GBC.HORIZONTAL));
     247
     248        tmsTab.add(new JLabel(tr("Min zoom lvl: ")), GBC.std());
     249        tmsTab.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     250        tmsTab.add(this.minZoomLvl, GBC.eol().fill(GBC.HORIZONTAL));
     251
     252        tmsTab.add(new JLabel(tr("Max zoom lvl: ")), GBC.std());
     253        tmsTab.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     254        tmsTab.add(this.maxZoomLvl, GBC.eol().fill(GBC.HORIZONTAL));
     255
     256        tmsTab.add(new JLabel(tr("Fade background: ")), GBC.std());
     257        tmsTab.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     258        tmsTab.add(this.fadeBackground, GBC.eol().fill(GBC.HORIZONTAL));
     259
     260        tmsTab.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
     261
     262        this.autozoomActive.setSelected(TMSPreferences.PROP_DEFAULT_AUTOZOOM.get());
     263        this.autoloadTiles.setSelected(TMSPreferences.PROP_DEFAULT_AUTOLOAD.get());
     264        this.maxZoomLvl.setValue(TMSPreferences.getMaxZoomLvl(null));
     265        this.minZoomLvl.setValue(TMSPreferences.getMinZoomLvl(null));
     266        this.fadeBackground.setValue(TMSPreferences.PROP_FADE_BACKGROUND.get());
     267        return tmsTab;
     268    }
     269
     270    private Component buildSettingsPanel() {
     271        // TODO: make some settings common for WMS and TMS
     272        final JPanel p = new JPanel(new GridBagLayout());
     273        p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
     274
     275        final JLabel lblW = new JLabel(tr("WMS Settings"));
     276        lblW.setFont(lblW.getFont().deriveFont(Font.BOLD));
     277        p.add(lblW);
     278        p.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 0));
     279        p.add(buildWMSSettingsPanel(),GBC.eol().insets(20,5,0,0));
     280
     281        final JLabel lblT = new JLabel(tr("TMS Settings"));
     282        lblT.setFont(lblT.getFont().deriveFont(Font.BOLD));
     283        p.add(lblT);
     284        p.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 0));
     285        p.add(buildTMSSettingsPanel(),GBC.eol().insets(20,5,0,0));
    222286
    223287        p.add(new JPanel(),GBC.eol().fill(GBC.BOTH));
     
    250314
    251315        Main.pref.put("wmsplugin.remotecontrol", String.valueOf(allowRemoteControl));
     316
     317        TMSPreferences.PROP_DEFAULT_AUTOZOOM.put(this.autozoomActive.isSelected());
     318        TMSPreferences.PROP_DEFAULT_AUTOLOAD.put(this.autoloadTiles.isSelected());
     319        TMSPreferences.setMaxZoomLvl((Integer)this.maxZoomLvl.getValue());
     320        TMSPreferences.setMinZoomLvl((Integer)this.minZoomLvl.getValue());
     321        TMSPreferences.PROP_FADE_BACKGROUND.put(this.fadeBackground.getValue());
     322
    252323        return false;
    253324    }
  • applications/editors/josm/plugins/imagery/src/org/openstreetmap/josm/plugins/imagery/tms/TMSLayer.java

    r24501 r24521  
    109109    private JPopupMenu tileOptionMenu;
    110110    JCheckBoxMenuItem autoZoomPopup;
     111    JCheckBoxMenuItem autoLoadPopup;
    111112    Tile showMetadataTile;
    112113    private Image attrImage;
     
    116117    private static Font ATTR_LINK_FONT = Font.decode("Arial Underline 10");
    117118
    118     protected boolean autoZoom = true;
    119     protected boolean autoLoad = true;
     119    protected boolean autoZoom;
     120    protected boolean autoLoad;
    120121
    121122    void redraw()
     
    170171        this.setVisible(true);
    171172
    172         currentZoomLevel = 0; //FIXME: detect current zoom level
     173        currentZoomLevel = TMSPreferences.getMinZoomLvl(null); //FIXME: detect current zoom level
     174
    173175        if (info.getImageryType() == ImageryType.TMS) {
    174176            setTileStorage(new TMSTileSource(info.getName(),info.getURL()));
     
    179181        tileOptionMenu = new JPopupMenu();
    180182
     183
     184        autoZoom = TMSPreferences.PROP_DEFAULT_AUTOZOOM.get();
    181185        autoZoomPopup = new JCheckBoxMenuItem();
    182186        autoZoomPopup.setAction(new AbstractAction(tr("Auto Zoom")) {
     
    188192        autoZoomPopup.setSelected(autoZoom);
    189193        tileOptionMenu.add(autoZoomPopup);
     194
     195        autoLoad = TMSPreferences.PROP_DEFAULT_AUTOLOAD.get();
     196        autoLoadPopup = new JCheckBoxMenuItem();
     197        autoLoadPopup.setAction(new AbstractAction(tr("Auto load tiles")) {
     198            @Override
     199            public void actionPerformed(ActionEvent ae) {
     200                autoLoad= !autoLoad;
     201            }
     202        });
     203        autoLoadPopup.setSelected(autoLoad);
     204        tileOptionMenu.add(autoLoadPopup);
    190205
    191206        tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load Tile")) {
     
    348363    int getMaxZoomLvl()
    349364    {
    350         return tileSource.getMaxZoom();
     365        return TMSPreferences.getMaxZoomLvl(tileSource);
    351366    }
    352367
    353368    int getMinZoomLvl()
    354369    {
    355         return tileSource.getMinZoom();
     370        return TMSPreferences.getMinZoomLvl(tileSource);
    356371    }
    357372
     
    659674                        img_x_end, img_y_end,
    660675                        this);
     676        float fadeBackground = TMSPreferences.getFadeBackground();
     677        if (fadeBackground != 0f) {
     678            // dimm by painting opaque rect...
     679            // TODO: make fade color configurable and implement same feature for WMS layers
     680            g.setColor(new Color(1f, 1f, 1f, fadeBackground));
     681            g.fillRect(target.x, target.y,
     682                       target.width, target.height);
     683        }
    661684    }
    662685    Double lastImageScale = null;
     
    703726        int texty = p.y + 2 + fontHeight;
    704727
     728        if (TMSPreferences.PROP_DRAW_DEBUG.get()) {
     729            g.drawString("x=" + t.getXtile() + " y=" + t.getYtile() + " z=" + zoom + "", p.x + 2, texty);
     730            texty += 1 + fontHeight;
     731            if ((t.getXtile() % 32 == 0) && (t.getYtile() % 32 == 0)) {
     732                g.drawString("x=" + t.getXtile() / 32 + " y=" + t.getYtile() / 32 + " z=7", p.x + 2, texty);
     733                texty += 1 + fontHeight;
     734            }
     735        }// end of if draw debug
     736
    705737        if (tile == showMetadataTile) {
    706738            String md = tile.toString();
     
    715747            g.drawString(tr("image " + tileStatus), p.x + 2, texty);
    716748            texty += 1 + fontHeight;
     749        }
     750
     751        int xCursor = -1;
     752        int yCursor = -1;
     753        if (TMSPreferences.PROP_DRAW_DEBUG.get()) {
     754            if (yCursor < t.getYtile()) {
     755                if (t.getYtile() % 32 == 31) {
     756                    g.fillRect(0, p.y - 1, mv.getWidth(), 3);
     757                } else {
     758                    g.drawLine(0, p.y, mv.getWidth(), p.y);
     759                }
     760                yCursor = t.getYtile();
     761            }
     762            // This draws the vertical lines for the entire
     763            // column. Only draw them for the top tile in
     764            // the column.
     765            if (xCursor < t.getXtile()) {
     766                if (t.getXtile() % 32 == 0) {
     767                    // level 7 tile boundary
     768                    g.fillRect(p.x - 1, 0, 3, mv.getHeight());
     769                } else {
     770                    g.drawLine(p.x, 0, p.x, mv.getHeight());
     771                }
     772                xCursor = t.getXtile();
     773            }
    717774        }
    718775    }
  • applications/editors/josm/plugins/imagery/src/org/openstreetmap/josm/plugins/imagery/tms/TMSPreferences.java

    r24507 r24521  
    1 package org.openstreetmap.josm.plugins.slippymap;
     1package org.openstreetmap.josm.plugins.imagery.tms;
    22
    3 import static org.openstreetmap.josm.tools.I18n.tr;
    4 
    5 import java.awt.Image;
    6 import java.io.IOException;
    7 import java.io.InputStream;
    8 import java.net.URL;
    9 import java.util.ArrayList;
    10 import java.util.Collections;
    11 import java.util.List;
    12 import java.util.Map;
    13 
    14 import org.openstreetmap.gui.jmapviewer.OsmTileSource;
    15 import org.openstreetmap.gui.jmapviewer.OsmTileSource.AbstractOsmTileSource;
    163import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
    17 import org.openstreetmap.josm.Main;
    18 import org.openstreetmap.josm.data.Bounds;
    19 import org.openstreetmap.josm.data.coor.LatLon;
    20 import org.openstreetmap.josm.tools.ImageProvider;
    21 import org.xml.sax.Attributes;
    22 import org.xml.sax.InputSource;
    23 import org.xml.sax.SAXException;
    24 import org.xml.sax.XMLReader;
    25 import org.xml.sax.helpers.DefaultHandler;
    26 import org.xml.sax.helpers.XMLReaderFactory;
     4import org.openstreetmap.josm.data.preferences.BooleanProperty;
     5import org.openstreetmap.josm.data.preferences.IntegerProperty;
    276
    287/**
     
    3110 * @author Hakan Tandogan <hakan@gurkensalat.com>
    3211 * @author LuVar <lubomir.varga@freemap.sk>
     12 * @author Upliner <upliner@gmail.com>
    3313 *
    3414 */
    35 public class SlippyMapPreferences
     15public class TMSPreferences
    3616{
    37     public static final String NO_DEFAULT_TILE_SOURCE_NAME = "{%no_default%}";
    38     public static final String PREFERENCE_PREFIX   = "slippymap";
    39 
    40     public static final String PREFERENCE_TILE_CUSTOM_SOURCE = PREFERENCE_PREFIX + ".custom_tile_source_";
    41     public static final String PREFERENCE_TILE_SOURCE = PREFERENCE_PREFIX + ".tile_source";
    42     public static final String PREFERENCE_AUTOZOOM = PREFERENCE_PREFIX + ".autozoom";
    43     public static final String PREFERENCE_AUTOLOADTILES = PREFERENCE_PREFIX + ".autoload_tiles";
    44     public static final String PREFERENCE_MIN_ZOOM_LVL = PREFERENCE_PREFIX + ".min_zoom_lvl";
    45     public static final String PREFERENCE_MAX_ZOOM_LVL = PREFERENCE_PREFIX + ".max_zoom_lvl";
    46     public static final String PREFERENCE_LAST_ZOOM = PREFERENCE_PREFIX + ".last_zoom_lvl";
    47     public static final String PREFERENCE_FADE_BACKGROUND = PREFERENCE_PREFIX + ".fade_background";
    48     public static final String PREFERENCE_DRAW_DEBUG = PREFERENCE_PREFIX + ".draw_debug";
     17    public static final String PREFERENCE_PREFIX   = "imagery.tms";
    4918
    5019    public static final int MAX_ZOOM = 30;
     
    5322    public static final int DEFAULT_MIN_ZOOM = 2;
    5423
    55 
    56     public static TileSource getMapSource()
    57     {
    58         String name = Main.pref.get(PREFERENCE_TILE_SOURCE);
    59         return getMapSource(name);
    60     }
    61     public static TileSource getMapSource(String name)
    62     {
    63         if (NO_DEFAULT_TILE_SOURCE_NAME.equals(name)) {
    64             return NO_DEFAULT_TILE_SOURCE; // User don't want to load slippy layer on startup
    65         }
    66 
    67         List<TileSource> sources = SlippyMapPreferences.getAllMapSources();
    68 
    69         if (name == null || "".equals(name)) {
    70             Main.pref.put(PREFERENCE_TILE_SOURCE, sources.get(0).getName());
    71             return sources.get(0);
    72         }
    73 
    74         for (TileSource s : sources) {
    75             if (name.equals(s.getName()))
    76                 return s;
    77         }
    78 
    79         return sources.get(0);
    80     }
    81 
    82     public static void setMapSource(TileSource source) {
    83         Main.pref.put(SlippyMapPreferences.PREFERENCE_TILE_SOURCE, source == NO_DEFAULT_TILE_SOURCE?NO_DEFAULT_TILE_SOURCE_NAME:source.getName());
    84     }
    85 
    86     public static boolean getAutozoom()
    87     {
    88         String autozoom = Main.pref.get(PREFERENCE_AUTOZOOM);
    89 
    90         if (autozoom == null || "".equals(autozoom))
    91         {
    92             autozoom = "true";
    93             Main.pref.put(PREFERENCE_AUTOZOOM, autozoom);
    94         }
    95 
    96         return Boolean.parseBoolean(autozoom);
    97     }
    98 
    99     public static void setAutozoom(boolean autozoom) {
    100         Main.pref.put(SlippyMapPreferences.PREFERENCE_AUTOZOOM, autozoom);
    101     }
    102 
    103     public static void setDrawDebug(boolean drawDebug) {
    104         Main.pref.put(SlippyMapPreferences.PREFERENCE_DRAW_DEBUG, drawDebug);
    105     }
    106 
    107     public static void setLastZoom(int zoom) {
    108         Main.pref.put(SlippyMapPreferences.PREFERENCE_LAST_ZOOM, ""+zoom);
    109     }
    110     public static int getLastZoom() {
    111         int ret = -1;
    112         String pref = Main.pref.get(SlippyMapPreferences.PREFERENCE_LAST_ZOOM);
    113         try {
    114             ret = Integer.parseInt(pref);
    115         } catch (NumberFormatException e) {
    116         }
    117         return ret;
    118     }
    119 
    120     public static boolean getDrawDebug()
    121     {
    122         String drawDebug = Main.pref.get(PREFERENCE_DRAW_DEBUG);
    123 
    124         if (drawDebug == null || "".equals(drawDebug))
    125         {
    126             drawDebug = "false";
    127             Main.pref.put(PREFERENCE_DRAW_DEBUG, drawDebug);
    128         }
    129 
    130         return Boolean.parseBoolean(drawDebug);
    131     }
    132 
    133     public static boolean getAutoloadTiles()
    134     {
    135         String autoloadTiles = Main.pref.get(PREFERENCE_AUTOLOADTILES);
    136 
    137         if (autoloadTiles == null || "".equals(autoloadTiles))
    138         {
    139             autoloadTiles = "true";
    140             Main.pref.put(PREFERENCE_AUTOLOADTILES, autoloadTiles);
    141         }
    142 
    143         return Boolean.parseBoolean(autoloadTiles);
    144     }
     24    public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
     25    public static final BooleanProperty PROP_DEFAULT_AUTOLOAD = new BooleanProperty(PREFERENCE_PREFIX + ".default_autoload", true);
     26    public static final IntegerProperty PROP_MIN_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".min_zoom_lvl", DEFAULT_MIN_ZOOM);
     27    public static final IntegerProperty PROP_MAX_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".max_zoom_lvl", DEFAULT_MAX_ZOOM);
     28    public static final IntegerProperty PROP_FADE_BACKGROUND = new IntegerProperty(PREFERENCE_PREFIX + ".fade_background", 0);
     29    public static final BooleanProperty PROP_DRAW_DEBUG = new BooleanProperty(PREFERENCE_PREFIX + ".draw_debug", false);
    14530
    14631    public static void setFadeBackground(float fadeBackground) {
    147         Main.pref.put(SlippyMapPreferences.PREFERENCE_FADE_BACKGROUND, fadeBackground + "");
     32        PROP_FADE_BACKGROUND.put(Math.round(fadeBackground*100));
    14833    }
    14934
     
    15338     */
    15439    public static float getFadeBackground() {
    155         String fadeBackground = Main.pref.get(PREFERENCE_FADE_BACKGROUND);
    156 
    157         if (fadeBackground == null || "".equals(fadeBackground))
    158         {
    159             fadeBackground = "0.0";
    160             Main.pref.put(PREFERENCE_FADE_BACKGROUND, fadeBackground);
    161         }
    162 
    163         float parsed;
    164         try {
    165             parsed = Float.parseFloat(fadeBackground);
    166         } catch (Exception ex) {
    167             setFadeBackground(0.1f);
    168             System.out.println("Error while parsing setting fade background to float! returning 0.1, because of error:");
    169             ex.printStackTrace(System.out);
    170             return 0.1f;
    171         }
     40        float parsed = (PROP_FADE_BACKGROUND.get())/100.0f;
    17241        if(parsed < 0f) {
    17342            parsed = 0f;
     
    18049    }
    18150
    182     public static void setAutoloadTiles(boolean autoloadTiles) {
    183         Main.pref.put(SlippyMapPreferences.PREFERENCE_AUTOLOADTILES, autoloadTiles);
    184     }
    185 
    186     private static int getIntPref(String prefName, int def)
    187     {
    188         int pref;
    189         try {
    190             //Should we use Main.pref.getInteger(str)?
    191             pref = Main.pref.getInteger(prefName, def);
    192         } catch (Exception ex) {
    193             String str = Main.pref.get(prefName);
    194             Main.pref.put(prefName, null);
    195             throw new RuntimeException("Problem while converting string to int. "
    196                                        + "Converting value of preferences "
    197                                        + prefName + ". Value=\"" + str
    198                                        + "\". Should be an integer. Error: "
    199                                        + ex.getMessage(), ex);
    200         }
    201         return pref;
    202     }
    203 
    204     static int checkMaxZoomLvl(int maxZoomLvl)
     51    static int checkMaxZoomLvl(int maxZoomLvl, TileSource ts)
    20552    {
    20653        if(maxZoomLvl > MAX_ZOOM) {
     
    20855            maxZoomLvl = MAX_ZOOM;
    20956        }
    210         if(maxZoomLvl < SlippyMapPreferences.__getMinZoomLvl()) {
     57        if(maxZoomLvl < PROP_MIN_ZOOM_LVL.get()) {
    21158            System.err.println("maxZoomLvl shouldnt be more than minZoomLvl! Setting to minZoomLvl.");
    212             maxZoomLvl = SlippyMapPreferences.__getMinZoomLvl();
     59            maxZoomLvl = PROP_MIN_ZOOM_LVL.get();
    21360        }
    214         TileSource ts = getMapSource();
    215         if (ts != null && ts.getMaxZoom() < SlippyMapPreferences.__getMinZoomLvl()) {
     61        if (ts != null && ts.getMaxZoom() < PROP_MIN_ZOOM_LVL.get()) {
    21662            System.err.println("decreasing maxZoomLvl to match new tile source");
    21763            maxZoomLvl = ts.getMaxZoom();
     
    22066    }
    22167
    222     public static int getMaxZoomLvl()
     68    public static int getMaxZoomLvl(TileSource ts)
    22369    {
    224         int maxZoomLvl = getIntPref(PREFERENCE_MAX_ZOOM_LVL, DEFAULT_MAX_ZOOM);
    225         return checkMaxZoomLvl(maxZoomLvl);
     70        return checkMaxZoomLvl(PROP_MAX_ZOOM_LVL.get(), ts);
    22671    }
    22772
    22873    public static void setMaxZoomLvl(int maxZoomLvl) {
    229         maxZoomLvl = checkMaxZoomLvl(maxZoomLvl);
    230         Main.pref.put(SlippyMapPreferences.PREFERENCE_MAX_ZOOM_LVL, "" + maxZoomLvl);
     74        maxZoomLvl = checkMaxZoomLvl(maxZoomLvl, null);
     75        PROP_MAX_ZOOM_LVL.put(maxZoomLvl);
    23176    }
    23277
    233     static int checkMinZoomLvl(int minZoomLvl)
     78    static int checkMinZoomLvl(int minZoomLvl, TileSource ts)
    23479    {
    23580        if(minZoomLvl < MIN_ZOOM) {
     
    23782            minZoomLvl = MIN_ZOOM;
    23883        }
    239         if(minZoomLvl > SlippyMapPreferences.getMaxZoomLvl()) {
     84        if(minZoomLvl > PROP_MAX_ZOOM_LVL.get()) {
    24085            System.err.println("minZoomLvl shouldnt be more than maxZoomLvl! Setting to maxZoomLvl.");
    241             minZoomLvl = SlippyMapPreferences.getMaxZoomLvl();
     86            minZoomLvl = getMaxZoomLvl(ts);
     87        }
     88        if (ts != null && ts.getMinZoom() > PROP_MIN_ZOOM_LVL.get()) {
     89            System.err.println("increasomg minZoomLvl to match new tile source");
     90            minZoomLvl = ts.getMinZoom();
    24291        }
    24392        return minZoomLvl;
    24493    }
    24594
    246     private static int __getMinZoomLvl()
     95    public static int getMinZoomLvl(TileSource ts)
    24796    {
    248         // We can use this internally
    249         return getIntPref(PREFERENCE_MIN_ZOOM_LVL, DEFAULT_MIN_ZOOM);
    250     }
    251     public static int getMinZoomLvl()
    252     {
    253         return checkMinZoomLvl(__getMinZoomLvl());
     97        return checkMinZoomLvl(PROP_MIN_ZOOM_LVL.get(), ts);
    25498    }
    25599
    256100    public static void setMinZoomLvl(int minZoomLvl) {
    257         minZoomLvl = checkMinZoomLvl(minZoomLvl);
    258         Main.pref.put(SlippyMapPreferences.PREFERENCE_MIN_ZOOM_LVL, "" + minZoomLvl);
    259     }
    260 
    261     public static TileSource NO_DEFAULT_TILE_SOURCE = new AbstractOsmTileSource(tr("(none)"), "") {
    262         public TileUpdate getTileUpdate() {
    263             return null;
    264         }
    265     };
    266 
    267     public static class Coastline extends OsmTileSource.AbstractOsmTileSource {
    268         public Coastline() {
    269             super("Coastline", "http://hypercube.telascience.org/tiles/1.0.0/coastline");
    270         }
    271         public TileUpdate getTileUpdate() {
    272             return TileUpdate.IfNoneMatch;
    273         }
    274     }
    275     public static class FreeMapySk extends OsmTileSource.AbstractOsmTileSource {
    276         public FreeMapySk() {
    277             super("freemapy.sk", "http://www.freemap.sk/layers/allinone/?");
    278         }
    279         public TileUpdate getTileUpdate() {
    280             return TileUpdate.IfNoneMatch;
    281         }
    282     }
    283     public static class FreeMapySkPokus extends OsmTileSource.AbstractOsmTileSource {
    284         public FreeMapySkPokus() {
    285             super("freemapy.sk pokus 2", "http://www.freemap.sk/layers/tiles/?");
    286         }
    287         public TileUpdate getTileUpdate() {
    288             return TileUpdate.IfNoneMatch;
    289         }
    290     }
    291 
    292     public static class BingAerial extends OsmTileSource.AbstractOsmTileSource {
    293         private static String API_KEY = "Arzdiw4nlOJzRwOz__qailc8NiR31Tt51dN2D7cm57NrnceZnCpgOkmJhNpGoppU";
    294         private static List<Attribution> attributions;
    295        
    296         public BingAerial() {
    297             super("Bing Aerial Maps", "http://ecn.t2.tiles.virtualearth.net/tiles/");
    298            
    299             attributions = loadAttributionText();
    300             System.err.println("Added " + attributions.size() + " attributions.");
    301         }
    302        
    303         class Attribution {
    304             String attribution;
    305             int minZoom;
    306             int maxZoom;
    307             Bounds bounds;
    308         }
    309        
    310         class AttrHandler extends DefaultHandler {
    311 
    312             private String string;
    313             private Attribution curr;
    314             private List<Attribution> attributions = new ArrayList<Attribution>();
    315             private double southLat;
    316             private double northLat;
    317             private double eastLon;
    318             private double westLon;
    319             private boolean inCoverage = false;
    320 
    321             public void startElement(String uri, String stripped, String tagName,
    322                     Attributes attrs) throws SAXException {
    323                 if("ImageryProvider".equals(tagName)) {
    324                     curr = new Attribution();
    325                 } else if("CoverageArea".equals(tagName)) {
    326                     inCoverage = true;
    327                 }
    328             }
    329 
    330             public void characters(char[] ch, int start, int length)
    331                     throws SAXException {
    332                 string = new String(ch, start, length);
    333             }
    334 
    335             public void endElement(String uri, String stripped, String tagName)
    336                     throws SAXException {
    337                 if("ImageryProvider".equals(tagName)) {
    338                     attributions.add(curr);
    339                 } else if("Attribution".equals(tagName)) {
    340                     curr.attribution = string;
    341                 } else if(inCoverage && "ZoomMin".equals(tagName)) {
    342                     curr.minZoom = Integer.parseInt(string);
    343                 } else if(inCoverage && "ZoomMax".equals(tagName)) {
    344                     curr.maxZoom = Integer.parseInt(string);
    345                 } else if(inCoverage && "SouthLatitude".equals(tagName)) {
    346                     southLat = Double.parseDouble(string);
    347                 } else if(inCoverage && "NorthLatitude".equals(tagName)) {
    348                     northLat = Double.parseDouble(string);
    349                 } else if(inCoverage && "EastLongitude".equals(tagName)) {
    350                     eastLon = Double.parseDouble(string);
    351                 } else if(inCoverage && "WestLongitude".equals(tagName)) {
    352                     westLon = Double.parseDouble(string);
    353                 } else if("BoundingBox".equals(tagName)) {
    354                     curr.bounds = new Bounds(northLat, westLon, southLat, eastLon);
    355                 } else if("CoverageArea".equals(tagName)) {
    356                     inCoverage = false;
    357                 }
    358                 string = "";
    359             }
    360         }
    361 
    362         private List<Attribution> loadAttributionText() {
    363             try {
    364                 URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial/0,0?zl=1&mapVersion=v1&key="+API_KEY+"&include=ImageryProviders&output=xml");
    365                 InputStream stream = u.openStream();
    366                 XMLReader parser = XMLReaderFactory.createXMLReader();
    367                 AttrHandler handler = new AttrHandler();
    368                 parser.setContentHandler(handler);
    369                 parser.parse(new InputSource(stream));
    370                 return handler.attributions;
    371             } catch (IOException e) {
    372                 System.err.println("Could not open Bing aerials attribution metadata.");
    373             } catch (SAXException e) {
    374                 System.err.println("Could not parse Bing aerials attribution metadata.");
    375                 e.printStackTrace();
    376             }
    377             return Collections.emptyList();
    378         }
    379 
    380         @Override
    381         public int getMaxZoom() {
    382             return 22;
    383         }
    384        
    385         @Override
    386         public String getExtension() {
    387             return("jpeg");
    388         }
    389 
    390         @Override
    391         public String getTilePath(int zoom, int tilex, int tiley) {
    392             String quadtree = computeQuadTree(zoom, tilex, tiley);
    393             return "/tiles/a" + quadtree + "." + getExtension() + "?g=587";
    394         }
    395 
    396         public TileUpdate getTileUpdate() {
    397             return TileUpdate.IfNoneMatch;
    398         }
    399        
    400         public boolean requiresAttribution() {
    401             return true;
    402         }
    403 
    404         public Image getAttributionImage() {
    405             return ImageProvider.get("bing_maps").getImage();
    406         }
    407        
    408         public String getAttributionLinkURL() {
    409             //return "http://bing.com/maps"
    410             // FIXME: I've set attributionLinkURL temporarily to ToU URL to comply with bing ToU
    411             // (the requirement is that we have such a link at the bottom of the window)
    412             return "http://opengeodata.org/microsoft-imagery-details";
    413         }
    414        
    415         public String getTermsOfUseURL() {
    416             return "http://opengeodata.org/microsoft-imagery-details";
    417         }
    418        
    419         public String getAttributionText(int zoom, LatLon topLeft, LatLon botRight) {
    420             Bounds windowBounds = new Bounds(topLeft, botRight);
    421             StringBuilder a = new StringBuilder();
    422             for (Attribution attr : attributions) {
    423                 Bounds attrBounds = attr.bounds;
    424                 if(zoom <= attr.maxZoom && zoom >= attr.minZoom) {
    425                     if(windowBounds.getMin().lon() < attrBounds.getMax().lon()
    426                             && windowBounds.getMax().lon() > attrBounds.getMin().lon()
    427                             && windowBounds.getMax().lat() < attrBounds.getMin().lat()
    428                             && windowBounds.getMin().lat() > attrBounds.getMax().lat()) {
    429                         a.append(attr.attribution);
    430                         a.append(" ");
    431                     }
    432                 }
    433             }
    434             return a.toString();
    435         }
    436     }
    437 
    438     private static String computeQuadTree(int zoom, int tilex, int tiley) {
    439         StringBuilder k = new StringBuilder();
    440         for(int i = zoom; i > 0; i--) {
    441             char digit = 48;
    442             int mask = 1 << (i - 1);
    443             if ((tilex & mask) != 0) {
    444                 digit += 1;
    445             }
    446             if ((tiley & mask) != 0) {
    447                 digit += 2;
    448             }
    449             k.append(digit);
    450         }
    451         return k.toString();
    452     }
    453    
    454     public static class HaitiImagery extends OsmTileSource.AbstractOsmTileSource {
    455         public HaitiImagery() {
    456             super("HaitiImagery", "http://gravitystorm.dev.openstreetmap.org/imagery/haiti");
    457         }
    458 
    459         @Override
    460         public int getMaxZoom() {
    461             return 21;
    462         }
    463 
    464         @Override
    465         public String getTilePath(int zoom, int tilex, int tiley) {
    466             return "/" + zoom + "/" + tilex + "/" + tiley + ".png";
    467         }
    468 
    469         public TileUpdate getTileUpdate() {
    470             return TileUpdate.IfNoneMatch;
    471         }
    472     }
    473 
    474     public static class Custom extends OsmTileSource.AbstractOsmTileSource {
    475 
    476         String extension;
    477         String path;
    478 
    479         public Custom(String name, String url) {
    480             super(name, url);
    481         }
    482         public Custom(String name, String url, String extension) {
    483             super(name, url);
    484             this.extension = extension;
    485         }
    486         public Custom(String name, String url, String extension, String path) {
    487             super(name, url);
    488             this.extension = extension;
    489             this.path = path;
    490         }
    491 
    492         @Override
    493         public String getExtension() {
    494             if (extension == null)
    495                 return super.getExtension();
    496             return extension;
    497         }
    498 
    499         @Override
    500         public int getMaxZoom() {
    501             return 21;
    502         }
    503 
    504         @Override
    505         public String getTilePath(int zoom, int tilex, int tiley) {
    506             if (path == null)
    507                 return super.getTilePath(zoom,tilex,tiley);
    508             String tilepath = path;
    509             tilepath=tilepath.replaceAll("%z",String.valueOf(zoom));
    510             tilepath=tilepath.replaceAll("%x",String.valueOf(tilex));
    511             tilepath=tilepath.replaceAll("%y",String.valueOf(tiley));
    512             return tilepath;
    513         }
    514 
    515         public TileUpdate getTileUpdate() {
    516             return TileUpdate.IfNoneMatch;
    517         }
    518     }
    519 
    520     public static List<TileSource> getCustomSources()
    521     {
    522         List<TileSource> ret = new ArrayList<TileSource>();
    523         Map<String, String> customSources = Main.pref.getAllPrefix(PREFERENCE_TILE_CUSTOM_SOURCE);
    524         for (String key : customSources.keySet()) {
    525             String short_key = key.replaceFirst(PREFERENCE_TILE_CUSTOM_SOURCE, "");
    526             // slippymap.custom_tile_source_1.name=OOC layer
    527             // slippymap.custom_tile_source_1.url=http://a.ooc.openstreetmap.org/npe
    528             // slippymap.custom_tile_source_1.ext=png
    529             // slippymap.custom_tile_source_1.path=/%z/%x/%y
    530 
    531             if (!(short_key.endsWith("name")))
    532                 continue;
    533             String url_key = short_key.replaceFirst("name","url");
    534             String ext_key = short_key.replaceFirst("name","ext");
    535             String path_key = short_key.replaceFirst("name","path");
    536             String name = customSources.get(key);
    537             String url = customSources.get(PREFERENCE_TILE_CUSTOM_SOURCE + url_key);
    538             String ext = customSources.get(PREFERENCE_TILE_CUSTOM_SOURCE + ext_key);
    539             String path = customSources.get(PREFERENCE_TILE_CUSTOM_SOURCE + path_key);
    540             // ext and path may be null, but that's OK
    541             System.out.println("found new tile source: '" +name+"' url:'"+url+"'"+"' ext:'"+ext+"' path:'"+path+"'");
    542             ret.add(new Custom(name, url, ext, path));
    543         }
    544         return ret;
    545     }
    546 
    547     public static ArrayList<TileSource> sources = null;
    548     public static List<TileSource> getAllMapSources()
    549     {
    550         if (sources != null)
    551             return sources;
    552         sources = new ArrayList<TileSource>();
    553         // first here is the default if the user does not set one
    554         sources.add(new OsmTileSource.Mapnik());
    555         sources.add(new OsmTileSource.CycleMap());
    556         sources.add(new OsmTileSource.TilesAtHome());
    557         // *PLEASE* do not enable BingAerial until we have legal approval.
    558         sources.add(new BingAerial());
    559         sources.add(new Coastline());
    560         sources.add(new FreeMapySkPokus());
    561         sources.add(new FreeMapySk());
    562         sources.add(new HaitiImagery());
    563         sources.addAll(getCustomSources());
    564         // Probably need to either add these or let users add them somehow
    565         //      "http://hypercube.telascience.org/tiles/1.0.0/coastline", // coastline
    566         //      "http://www.freemap.sk/layers/allinone/?", //freemapy.sk
    567         //      "http://www.freemap.sk/layers/tiles/?", //freemapy.sk pokus 2
    568         return sources;
    569     }
    570 
    571     public static TileSource getSourceNamed(String name)
    572     {
    573         for (TileSource s : getAllMapSources())
    574             if (s.getName().equals(name))
    575                 return s;
    576         return null;
     101        minZoomLvl = checkMinZoomLvl(minZoomLvl, null);
     102        PROP_MIN_ZOOM_LVL.put(minZoomLvl);
    577103    }
    578104}
Note: See TracChangeset for help on using the changeset viewer.