Changeset 4240 in josm for trunk/src


Ignore:
Timestamp:
2011-07-14T12:48:27+02:00 (14 years ago)
Author:
bastiK
Message:

see #6532 - add support for xml imagery source format

(todo: the specified server projections aren't used properly in WMSGrabber)

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java

    r4198 r4240  
    44import java.util.ArrayList;
    55import java.util.Collection;
     6import java.util.Collections;
     7import java.util.List;
    68import java.util.regex.Matcher;
    79import java.util.regex.Pattern;
     
    4244    private String name;
    4345    private String url = null;
     46    private boolean defaultEntry = false;
    4447    private String cookies = null;
    4548    private String eulaAcceptanceRequired= null;
     
    5053    private int defaultMinZoom = 0;
    5154    private Bounds bounds = null;
     55    private List<String> serverProjections;
    5256    private String attributionText;
    5357    private String attributionImage;
     
    5559    private String termsOfUseURL;
    5660
     61    public ImageryInfo() {
     62    }
     63
    5764    public ImageryInfo(String name) {
    5865        this.name=name;
     
    6168    public ImageryInfo(String name, String url) {
    6269        this.name=name;
    63         setUrl(url);
     70        setExtendedUrl(url);
    6471    }
    6572
    6673    public ImageryInfo(String name, String url, String eulaAcceptanceRequired) {
    6774        this.name=name;
    68         setUrl(url);
     75        setExtendedUrl(url);
    6976        this.eulaAcceptanceRequired = eulaAcceptanceRequired;
    7077    }
     
    7279    public ImageryInfo(String name, String url, String eulaAcceptanceRequired, String cookies) {
    7380        this.name=name;
    74         setUrl(url);
     81        setExtendedUrl(url);
    7582        this.cookies=cookies;
    7683        this.eulaAcceptanceRequired = eulaAcceptanceRequired;
     
    7986    public ImageryInfo(String name, String url, String cookies, double pixelPerDegree) {
    8087        this.name=name;
    81         setUrl(url);
     88        setExtendedUrl(url);
    8289        this.cookies=cookies;
    8390        this.pixelPerDegree=pixelPerDegree;
     
    8794        ArrayList<String> res = new ArrayList<String>();
    8895        res.add(name);
    89         res.add((url != null && !url.isEmpty()) ? getFullUrl() : null);
     96        res.add((url != null && !url.isEmpty()) ? getExtendedUrl() : null);
    9097        res.add(cookies);
    9198        if(imageryType == ImageryType.WMS || imageryType == ImageryType.HTML) {
     
    106113        this.name=array.get(0);
    107114        if(array.size() >= 2 && !array.get(1).isEmpty()) {
    108             setUrl(array.get(1));
     115            setExtendedUrl(array.get(1));
    109116        }
    110117        if(array.size() >= 3 && !array.get(2).isEmpty()) {
     
    178185    }
    179186
     187    public void setDefaultMaxZoom(int defaultMaxZoom) {
     188        this.defaultMaxZoom = defaultMaxZoom;
     189    }
     190
     191    public void setDefaultMinZoom(int defaultMinZoom) {
     192        this.defaultMinZoom = defaultMinZoom;
     193    }
     194   
    180195    public void setMaxZoom(int maxZoom) {
    181196        this.maxZoom = maxZoom;
     
    206221    }
    207222
    208     public void setUrl(String url) {
     223    public void setExtendedUrl(String url) {
    209224        CheckParameterUtil.ensureParameterNotNull(url);
    210225       
     
    244259    }
    245260
     261    public void setUrl(String url) {
     262        this.url = url;
     263    }
     264
     265    public boolean isDefaultEntry() {
     266        return defaultEntry;
     267    }
     268
     269    public void setDefaultEntry(boolean defaultEntry) {
     270        this.defaultEntry = defaultEntry;
     271    }
     272
    246273    public String getCookies() {
    247274        return this.cookies;
     
    264291    }
    265292
    266     public String getFullUrl() {
     293    public void setEulaAcceptanceRequired(String eulaAcceptanceRequired) {
     294        this.eulaAcceptanceRequired = eulaAcceptanceRequired;
     295    }
     296
     297    /**
     298     * Get the projections supported by the server. Only relevant for
     299     * WMS-type ImageryInfo at the moment.
     300     * @return null, if no projections have been specified; the list
     301     * of supported projections otherwise.
     302     */
     303    public List<String> getServerProjections() {
     304        if (serverProjections == null) return null;
     305        return Collections.unmodifiableList(serverProjections);
     306    }
     307
     308    public void setServerProjections(Collection<String> serverProjections) {
     309        this.serverProjections = new ArrayList<String>(serverProjections);
     310    }
     311
     312    public String getExtendedUrl() {
    267313        return imageryType.getUrlString() + (defaultMaxZoom != 0
    268314            ? "["+(defaultMinZoom != 0 ? defaultMinZoom+",":"")+defaultMaxZoom+"]" : "") + ":" + url;
     
    331377    public ImageryType getImageryType() {
    332378        return imageryType;
     379    }
     380
     381    public void setImageryType(ImageryType imageryType) {
     382        this.imageryType = imageryType;
    333383    }
    334384
  • TabularUnified trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    r4216 r4240  
    22package org.openstreetmap.josm.data.imagery;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.io.BufferedReader;
    74import java.io.IOException;
    8 import java.io.InputStreamReader;
    9 import java.io.UnsupportedEncodingException;
    105import java.util.ArrayList;
    116import java.util.Arrays;
     
    1611
    1712import org.openstreetmap.josm.Main;
    18 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    19 import org.openstreetmap.josm.data.Bounds;
     13import org.openstreetmap.josm.io.imagery.ImageryReader;
    2014import org.openstreetmap.josm.io.MirroredInputStream;
     15import org.openstreetmap.josm.tools.Utils;
     16import org.xml.sax.SAXException;
    2117
    2218public class ImageryLayerInfo {
     
    6561    public void loadDefaults(boolean clearCache) {
    6662        defaultLayers.clear();
    67         Collection<String> defaults = Main.pref.getCollection(
    68                 "imagery.layers.default", Collections.<String>emptySet());
     63        for (String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES))) {
     64            if (clearCache) {
     65                MirroredInputStream.cleanup(source);
     66            }
     67            MirroredInputStream stream = null;
     68            try {
     69                ImageryReader reader = new ImageryReader(source);
     70                Collection<ImageryInfo> result = reader.parse();
     71                defaultLayers.addAll(result);
     72            } catch (IOException ex) {
     73                Utils.close(stream);
     74                ex.printStackTrace();
     75                continue;
     76            } catch (SAXException sex) {
     77                Utils.close(stream);
     78                sex.printStackTrace();
     79                continue;
     80            }
     81        }
     82        while (defaultLayers.remove(null)) {}
     83       
     84        Collection<String> defaults = Main.pref.getCollection("imagery.layers.default");
    6985        ArrayList<String> defaultsSave = new ArrayList<String>();
    70         for(String source : Main.pref.getCollection("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES)))
    71         {
    72             try
    73             {
    74                 if (clearCache) {
    75                     MirroredInputStream.cleanup(source);
     86        for (ImageryInfo def : defaultLayers) {
     87            if (def.isDefaultEntry()) {
     88                defaultsSave.add(def.getUrl());
     89               
     90                boolean isKnownDefault = false;
     91                for (String url : defaults) {
     92                    if (isSimilar(url, def.getUrl())) {
     93                        isKnownDefault = true;
     94                        break;
     95                    }
    7696                }
    77                 MirroredInputStream s = new MirroredInputStream(source, -1);
    78                 try {
    79                     InputStreamReader r;
    80                     try
    81                     {
    82                         r = new InputStreamReader(s, "UTF-8");
    83                     }
    84                     catch (UnsupportedEncodingException e)
    85                     {
    86                         r = new InputStreamReader(s);
    87                     }
    88                     BufferedReader reader = new BufferedReader(r);
    89                     String line;
    90                     while((line = reader.readLine()) != null)
    91                     {
    92                         String val[] = line.split(";");
    93                         if(!line.startsWith("#") && val.length >= 3) {
    94                             boolean force = "true".equals(val[0]);
    95                             String name = tr(val[1]);
    96                             String url = val[2];
    97                             String eulaAcceptanceRequired = null;
    98 
    99                             if (val.length >= 4 && !val[3].isEmpty()) {
    100                                 // 4th parameter optional for license agreement (EULA)
    101                                 eulaAcceptanceRequired = val[3];
    102                             }
    103 
    104                             ImageryInfo info = new ImageryInfo(name, url, eulaAcceptanceRequired);
    105 
    106                             if (val.length >= 5 && !val[4].isEmpty()) {
    107                                 // 5th parameter optional for bounds
    108                                 try {
    109                                     info.setBounds(new Bounds(val[4], ","));
    110                                 } catch (IllegalArgumentException e) {
    111                                     Main.warn(e.toString());
    112                                 }
    113                             }
    114                             if (val.length >= 6 && !val[5].isEmpty()) {
    115                                 info.setAttributionText(val[5]);
    116                             }
    117                             if (val.length >= 7 && !val[6].isEmpty()) {
    118                                 info.setAttributionLinkURL(val[6]);
    119                             }
    120                             if (val.length >= 8 && !val[7].isEmpty()) {
    121                                 info.setTermsOfUseURL(val[7]);
    122                             }
    123                             if (val.length >= 9 && !val[8].isEmpty()) {
    124                                 info.setAttributionImage(val[8]);
    125                             }
    126 
    127                             defaultLayers.add(info);
    128 
    129                             if (force) {
    130                                 defaultsSave.add(url);
    131                                 if (!defaults.contains(url)) {
    132                                     for (ImageryInfo i : layers) {
    133                                         if ((i.getImageryType() == ImageryType.WMS && url.equals(i.getUrl()))
    134                                                 || url.equals(i.getFullUrl())) {
    135                                             force = false;
    136                                         }
    137                                     }
    138                                     if (force) {
    139                                         add(new ImageryInfo(name, url));
    140                                     }
    141                                 }
    142                             }
     97                boolean isInUserList = false;
     98                if (!isKnownDefault) {
     99                    for (ImageryInfo i : layers) {
     100                        if (isSimilar(def.getUrl(), i.getUrl())) {
     101                            isInUserList = true;
     102                            break;
    143103                        }
    144104                    }
    145                 } finally {
    146                     s.close();
    147105                }
    148             }
    149             catch (IOException e)
    150             {
     106                if (!isKnownDefault && !isInUserList) {
     107                    add(new ImageryInfo(def));
     108                }
    151109            }
    152110        }
     
    155113        Main.pref.putCollection("imagery.layers.default", defaultsSave.size() > 0
    156114                ? defaultsSave : defaults);
     115    }
     116   
     117    // some additional checks to respect extended URLs in preferences (legacy workaround)
     118    private boolean isSimilar(String a, String b) {
     119        return Utils.equal(a, b) || (a != null && b != null && !"".equals(a) && !"".equals(b) && (a.contains(b) || b.contains(a)));
    157120    }
    158121
  • TabularUnified trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java

    r4184 r4240  
    108108    private int workingThreadCount;
    109109    private boolean canceled;
    110     private ArrayList<String> serverProjections = null;
     110    private List<String> serverProjections = null;
    111111
    112112    /** set to true if this layer uses an invalid base url */
     
    121121    public WMSLayer(ImageryInfo info) {
    122122        super(info);
     123        serverProjections = info.getServerProjections();
    123124        mv = Main.map.mapView;
    124125        setBackgroundLayer(true); /* set global background variable */
     
    143144
    144145        if(info.getUrl() != null) {
    145             serverProjections = WMSGrabber.getServerProjections(info.getUrl(), true);
     146            if (serverProjections == null) {
     147                serverProjections = WMSGrabber.getServerProjections(info.getUrl(), true);
     148            }
    146149            startGrabberThreads();
    147150            if(info.getImageryType() == ImageryType.WMS && !ImageryInfo.isUrlWithPatterns(info.getUrl())) {
     
    310313     */
    311314    public int getBaseImageWidth() {
    312         int overlap = (PROP_OVERLAP.get()?PROP_OVERLAP_EAST.get() * imageSize / 100:0);
     315        int overlap = PROP_OVERLAP.get() ? (PROP_OVERLAP_EAST.get() * imageSize / 100) : 0;
    313316        return imageSize + overlap;
    314317    }
     
    319322     */
    320323    public int getBaseImageHeight() {
    321         int overlap = (PROP_OVERLAP.get()?PROP_OVERLAP_NORTH.get() * imageSize / 100:0);
     324        int overlap = PROP_OVERLAP.get() ? (PROP_OVERLAP_NORTH.get() * imageSize / 100) : 0;
    322325        return imageSize + overlap;
    323326    }
     
    696699                    oos.writeDouble(info.getPixelPerDegree());
    697700                    oos.writeObject(info.getName());
    698                     oos.writeObject(info.getFullUrl());
     701                    oos.writeObject(info.getExtendedUrl());
    699702                    oos.writeObject(images);
    700703                    oos.close();
     
    735738                info.setPixelPerDegree(ois.readDouble());
    736739                doSetName((String)ois.readObject());
    737                 info.setUrl((String) ois.readObject());
     740                info.setExtendedUrl((String) ois.readObject());
    738741                images = (GeorefImage[][])ois.readObject();
    739742                ois.close();
     
    914917    }
    915918
     919    /**
     920     * Get the list of projections supported by the WMS server corresponding to this layer.
     921     * @return The list of projections, if known. An empty list otherwise.
     922     */
     923    public List<String> getServerProjections() {
     924        if (serverProjections == null)
     925            return Collections.emptyList();
     926        else
     927            return Collections.unmodifiableList(serverProjections);
     928    }
     929   
    916930    @Override
    917931    public boolean isProjectionSupported(Projection proj) {
  • TabularUnified trunk/src/org/openstreetmap/josm/gui/preferences/AddWMSLayerPanel.java

    r4188 r4240  
    323323        String incomingData;
    324324        try {
     325            System.out.println("GET "+getCapabilitiesUrl.toString());
    325326            URLConnection openConnection = getCapabilitiesUrl.openConnection();
    326327            InputStream inputStream = openConnection.getInputStream();
     
    341342        Document document;
    342343        try {
     344            //System.out.println("WMS capabilities:\n"+incomingData+"\n");
    343345            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    344346            builderFactory.setValidating(false);
  • TabularUnified trunk/src/org/openstreetmap/josm/gui/preferences/ImageryPreference.java

    r4216 r4240  
    593593                    return info.getName();
    594594                case 1:
    595                     return info.getFullUrl();
     595                    return info.getExtendedUrl();
    596596                case 2:
    597597                    return (info.getImageryType() == ImageryType.WMS || info.getImageryType() == ImageryType.HTML) ?
     
    611611                    break;
    612612                case 1:
    613                     info.setUrl((String)o);
     613                    info.setExtendedUrl((String)o);
    614614                    break;
    615615                case 2:
     
    660660                    return info.getName();
    661661                case 1:
    662                     return info.getFullUrl();
     662                    return info.getExtendedUrl();
    663663                }
    664664                return null;
  • TabularUnified trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r4172 r4240  
    2929    InputStream fs = null;
    3030    File file = null;
     31   
     32    public final static long DEFAULT_MAXTIME = -1l;
    3133
    3234    public MirroredInputStream(String name) throws IOException {
    33         this(name, null, -1L);
     35        this(name, null, DEFAULT_MAXTIME);
    3436    }
    3537
     
    3941
    4042    public MirroredInputStream(String name, String destDir) throws IOException {
    41         this(name, destDir, -1L);
     43        this(name, destDir, DEFAULT_MAXTIME);
    4244    }
    4345
     
    193195                file = null;
    194196            else {
    195                 if (maxTime <= 0) {
     197                if ( maxTime == DEFAULT_MAXTIME
     198                        || maxTime <= 0 // arbitrary value <= 0 is deprecated
     199                ) {
    196200                    maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
    197201                }
  • TabularUnified trunk/src/org/openstreetmap/josm/io/imagery/OsmosnimkiOffsetServer.java

    r4126 r4240  
    2626    public boolean isLayerSupported(ImageryInfo info) {
    2727        try {
    28             URL url = new URL(this.url + "action=CheckAvailability&id=" + URLEncoder.encode(info.getFullUrl(), "UTF-8"));
     28            URL url = new URL(this.url + "action=CheckAvailability&id=" + URLEncoder.encode(info.getUrl(), "UTF-8"));
    2929            System.out.println(tr("Querying offset availability: {0}", url));
    3030            final BufferedReader rdr = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"));
     
    4242        LatLon ll = Main.getProjection().eastNorth2latlon(en);
    4343        try {
    44             URL url = new URL(this.url + "action=GetOffsetForPoint&lat=" + ll.lat() + "&lon=" + ll.lon() + "&id=" + URLEncoder.encode(info.getFullUrl(), "UTF-8"));
     44            URL url = new URL(this.url + "action=GetOffsetForPoint&lat=" + ll.lat() + "&lon=" + ll.lon() + "&id=" + URLEncoder.encode(info.getUrl(), "UTF-8"));
    4545            System.out.println(tr("Querying offset: {0}", url.toString()));
    4646            final BufferedReader rdr = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"));
  • TabularUnified trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java

    r4228 r4240  
    1919import java.text.NumberFormat;
    2020import java.util.ArrayList;
     21import java.util.List;
    2122import java.util.Map.Entry;
    2223import java.util.Locale;
     
    4748    protected String baseURL;
    4849    private final boolean urlWithPatterns;
     50    private List<String> serverProjections;
    4951    private Map<String, String> props = new HashMap<String, String>();
    5052
     
    5254        super(mv, layer);
    5355        this.baseURL = layer.getInfo().getUrl();
     56        this.serverProjections = layer.getServerProjections();
    5457        /* URL containing placeholders? */
    5558        urlWithPatterns = ImageryInfo.isUrlWithPatterns(baseURL);
     
    139142        } else {
    140143            str += "bbox=" + bbox
    141             + srs
    142             + "&width=" + wi + "&height=" + ht;
     144                    + srs
     145                    + "&width=" + wi + "&height=" + ht;
    143146            if (!(baseURL.endsWith("&") || baseURL.endsWith("?"))) {
    144147                System.out.println(tr("Warning: The base URL ''{0}'' for a WMS service doesn't have a trailing '&' or a trailing '?'.", baseURL));
Note: See TracChangeset for help on using the changeset viewer.