Ignore:
Timestamp:
2011-09-16T22:47:09+02:00 (13 years ago)
Author:
stoecker
Message:

fix WMS projection checking, use Marcator when WMS supports it, drop old style imagery source reader

Location:
trunk/src/org/openstreetmap/josm/io/imagery
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java

    r4423 r4430  
    4141        ENTRY_ATTRIBUTE,    // note we are inside an entry attribute to collect the character data
    4242        SUPPORTED_PROJECTIONS,
    43         PR,
     43        SRS,
    4444        BOUNDS,
    4545        SHAPE,
     
    5252
    5353    public List<ImageryInfo> parse() throws SAXException, IOException {
    54         if (isXml(source)) {
    55             Parser parser = new Parser();
    56             try {
    57                 SAXParserFactory factory = SAXParserFactory.newInstance();
    58                 factory.setNamespaceAware(true);
    59                 InputStream in = new MirroredInputStream(source);
    60                 InputSource is = new InputSource(UTFInputStreamReader.create(in, "UTF-8"));
    61                 factory.newSAXParser().parse(is, parser);
    62                 return parser.entries;
    63             } catch (SAXException e) {
    64                 throw e;
    65             } catch (ParserConfigurationException e) {
    66                 e.printStackTrace(); // broken SAXException chaining
    67                 throw new SAXException(e);
    68             }
    69         } else {
    70             return readCSV(source);
    71         }
    72     }
    73 
    74     /**
    75      * Probe the file to see if it is xml or the traditional csv format.
    76      *
    77      * If the first non-whitespace character is a '<', decide for
    78      * xml, otherwise csv.
    79      */
    80     private boolean isXml(String source) {
    81         MirroredInputStream in = null;
     54        Parser parser = new Parser();
    8255        try {
    83             in = new MirroredInputStream(source);
    84             InputStreamReader reader = UTFInputStreamReader.create(in, null);
    85             WHILE: while (true) {
    86                 int c = reader.read();
    87                 switch (c) {
    88                     case -1:
    89                         break WHILE;
    90                     case ' ':
    91                     case '\t':
    92                     case '\n':
    93                     case '\r':
    94                         continue;
    95                     case '<':
    96                         return true;
    97                     default:
    98                         return false;
    99                 }
    100             }
    101         } catch (IOException ex) {
    102             ex.printStackTrace();
    103         } finally {
    104             Utils.close(in);
    105         }
    106         Main.warn(tr("Warning: Could not detect type of imagery source ''{0}''. Using default (xml).", source));
    107         return true;
    108     }
    109 
    110     private List<ImageryInfo> readCSV(String source) {
    111         List<ImageryInfo> entries = new ArrayList<ImageryInfo>();
    112         MirroredInputStream s = null;
    113         try {
    114             s = new MirroredInputStream(source);
    115             try {
    116                 InputStreamReader r;
    117                 try
    118                 {
    119                     r = new InputStreamReader(s, "UTF-8");
    120                 }
    121                 catch (UnsupportedEncodingException e)
    122                 {
    123                     r = new InputStreamReader(s);
    124                 }
    125                 BufferedReader reader = new BufferedReader(r);
    126                 String line;
    127                 while((line = reader.readLine()) != null)
    128                 {
    129                     String val[] = line.split(";");
    130                     if(!line.startsWith("#") && val.length >= 3) {
    131                         boolean defaultEntry = "true".equals(val[0]);
    132                         String name = tr(val[1]);
    133                         String url = val[2];
    134                         String eulaAcceptanceRequired = null;
    135 
    136                         if (val.length >= 4 && !val[3].isEmpty()) {
    137                             // 4th parameter optional for license agreement (EULA)
    138                             eulaAcceptanceRequired = val[3];
    139                         }
    140 
    141                         ImageryInfo info = new ImageryInfo(name, url, eulaAcceptanceRequired);
    142                        
    143                         info.setDefaultEntry(defaultEntry);
    144 
    145                         if (val.length >= 5 && !val[4].isEmpty()) {
    146                             // 5th parameter optional for bounds
    147                             try {
    148                                 info.setBounds(new ImageryBounds(val[4], ","));
    149                             } catch (IllegalArgumentException e) {
    150                                 Main.warn(e.toString());
    151                             }
    152                         }
    153                         if (val.length >= 6 && !val[5].isEmpty()) {
    154                             info.setAttributionText(val[5]);
    155                         }
    156                         if (val.length >= 7 && !val[6].isEmpty()) {
    157                             info.setAttributionLinkURL(val[6]);
    158                         }
    159                         if (val.length >= 8 && !val[7].isEmpty()) {
    160                             info.setTermsOfUseURL(val[7]);
    161                         }
    162                         if (val.length >= 9 && !val[8].isEmpty()) {
    163                             info.setAttributionImage(val[8]);
    164                         }
    165 
    166                         entries.add(info);
    167                     }
    168                 }
    169             } finally {
    170                 Utils.close(s);
    171             }
    172             return entries;
    173         } catch (IOException ex) {
    174             ex.printStackTrace();
    175         } finally {
    176             Utils.close(s);
    177         }
    178         return entries;
     56            SAXParserFactory factory = SAXParserFactory.newInstance();
     57            factory.setNamespaceAware(true);
     58            InputStream in = new MirroredInputStream(source);
     59            InputSource is = new InputSource(UTFInputStreamReader.create(in, "UTF-8"));
     60            factory.newSAXParser().parse(is, parser);
     61            return parser.entries;
     62        } catch (SAXException e) {
     63            throw e;
     64        } catch (ParserConfigurationException e) {
     65            e.printStackTrace(); // broken SAXException chaining
     66            throw new SAXException(e);
     67        }
    17968    }
    18069
     
    275164                    break;
    276165                case SUPPORTED_PROJECTIONS:
    277                     if (qName.equals("pr")) {
    278                         newState = State.PR;
     166                    if (qName.equals("srs")) {
     167                        newState = State.SRS;
    279168                    }
    280169                    break;
     
    383272                    shape = null;
    384273                    break;
    385                 case PR:
     274                case SRS:
    386275                    supported_srs.add(accumulator.toString());
    387276                    break;
  • trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java

    r4253 r4430  
    9696        String srs = "";
    9797        boolean useepsg = false;
     98        // FIXME: Non-Pattern format should be dropped in future
    9899        try
    99100        {
     
    103104                if(m.group(1).equals("EPSG:4326") && Main.getProjection() instanceof Mercator)
    104105                    useepsg = true;
    105             } else if(Main.getProjection() instanceof Mercator) {
     106            } else if((Main.getProjection() instanceof Mercator) && !serverProjections.contains(myProj)) {
    106107                useepsg = true;
    107108                srs ="&srs=EPSG:4326";
     
    156157    {
    157158        ArrayList<String> serverProjections = new ArrayList<String>();
     159        boolean hasepsg = false;
     160       
     161        // FIXME: Non-Pattern format should be dropped in future
    158162        try
    159163        {
     
    161165            if(m.matches())
    162166            {
    163                 boolean hasepsg = false;
    164167                for(String p : m.group(1).split(","))
    165168                {
     
    168171                        hasepsg = true;
    169172                }
    170                 if(hasepsg && !serverProjections.contains(new Mercator().toCode()))
    171                     serverProjections.add(new Mercator().toCode());
    172173            }
    173174            else
     
    178179                    serverProjections.add(m.group(1));
    179180                    if(m.group(1).equals("EPSG:4326"))
    180                         serverProjections.add(new Mercator().toCode());
     181                        hasepsg = true;
    181182                }
    182183                /* TODO: here should be an "else" code checking server capabilities */
     
    191192        {
    192193            String myProj = Main.getProjection().toCode().toUpperCase();
    193             if(!serverProjections.contains(myProj))
     194            if(!serverProjections.contains(myProj) &&
     195            !(Main.getProjection() instanceof Mercator && serverProjections.contains("EPSG:4326")))
    194196            {
    195197                JOptionPane.showMessageDialog(Main.parent,
Note: See TracChangeset for help on using the changeset viewer.