Ignore:
Timestamp:
2012-03-12T23:46:51+01:00 (13 years ago)
Author:
donvip
Message:

opendata: download improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/modules/fr.datagouvfr/src/org/openstreetmap/josm/plugins/opendata/modules/fr/datagouvfr/datasets/hydrologie/EauxDeSurfaceHandler.java

    r28054 r28055  
    1616package org.openstreetmap.josm.plugins.opendata.modules.fr.datagouvfr.datasets.hydrologie;
    1717
     18import java.io.BufferedReader;
     19import java.io.BufferedWriter;
     20import java.io.File;
     21import java.io.FileReader;
     22import java.io.FileWriter;
    1823import java.net.MalformedURLException;
    1924import java.net.URL;
     
    2934public class EauxDeSurfaceHandler extends DataGouvDataSetHandler {
    3035
    31         private static final String ZIP_PATTERN = "FR(I|J|K|L)_SW";
    32         private static final String SHP_PATTERN = "FR_(I|J|K|L)_SWB_.W_20......";
     36        private static final String ZIP_PATTERN = "FR(.*)_SW";
     37        private static final String SHP_PATTERN = "FR_(.*)_SWB_.W_20......";
    3338       
    34         private static final String[] letters = new String[]{"I","J","K","L"};
    35         private static final String[] names   = new String[]{"Guadeloupe","Martinique","Guyane","La Réunion"};
    36         private static final String[] urls    = new String[]{
    37                 "Couche-SIG-des-caractéristiques-des-bassins-2010-%3A-eaux-de-surface---Guadeloupe-30381899",
    38                 "Couche-SIG-des-caractéristiques-des-bassins-2010-%3A-eaux-de-surface---Martinique-30381935",
    39                 "Couche-SIG-des-caractéristiques-des-bassins-2010-%3A-eaux-de-surface---Guyane-30381988",
    40                 "Couche-SIG-des-caractéristiques-des-bassins-2010-%3A-eaux-de-surface---Réunion-30381991"
    41         };
     39        private static final class WaterAgency {
     40                public final String code;
     41                public final String name;
     42                public final String suffix;
     43                public WaterAgency(String code, String name, String suffix) {
     44                        this.code = code;
     45                        this.name = name;
     46                        this.suffix = suffix;
     47                }
     48        }
     49       
     50        private static final WaterAgency[] waterAgencies = new WaterAgency[]{
     51                new WaterAgency("A",  "Escaut Somme", "Escaut-Somme-30381967"),
     52                new WaterAgency("B1", "Meuse", "Meuse-30381855"),
     53                new WaterAgency("B2", "Sambre", "Sambre-30381857"),
     54                new WaterAgency("C", "Rhin", "Rhin-30381951"),
     55                new WaterAgency("D",  "Rhône Méditerranée", "Rhône-Méditerranée-30382014"),
     56                new WaterAgency("E",  "Corse", "Corse-30381905"),
     57                new WaterAgency("F",  "Adour Garonne", "Adour-Garonne-30381839"),
     58                new WaterAgency("G",  "Loire Bretagne", "Loire-Bretagne-30381904"),
     59                new WaterAgency("I",  "Guadeloupe", "Guadeloupe-30381899"),
     60                new WaterAgency("J",  "Martinique", "Martinique-30381935"),
     61                new WaterAgency("K",  "Guyane", "Guyane-30381988"),
     62                new WaterAgency("L",  "La Réunion", "Réunion-30381991"),
     63        };
    4264       
    4365        public EauxDeSurfaceHandler() {
     
    6789                        Matcher m = Pattern.compile(".*"+pattern+"\\....").matcher(filename);
    6890                        if (m.matches()) {
    69                                 for (int i =0; i<letters.length; i++) {
    70                                         if (letters[i].equals(m.group(1))) {
    71                                                 return urls[i];
     91                                for (int i =0; i<waterAgencies.length; i++) {
     92                                        if (waterAgencies[i].code.equals(m.group(1))) {
     93                                                return "Couche-SIG-des-caractéristiques-des-bassins-2010-%3A-eaux-de-surface---"+waterAgencies[i].suffix;
    7294                                        }
    7395                                }
     
    89111                List<Pair<String, URL>> result = new ArrayList<Pair<String,URL>>();
    90112                try {
    91                         for (int i =0; i<letters.length; i++) {
    92                                 result.add(getDownloadURL(i));
     113                        for (int i =0; i<waterAgencies.length; i++) {
     114                                result.add(getDownloadURL(waterAgencies[i]));
    93115                        }
    94116                } catch (MalformedURLException e) {
     
    98120        }
    99121
    100         private Pair<String, URL> getDownloadURL(int i) throws MalformedURLException {
    101                 return new Pair<String, URL>(names[i], new URL("http://www.rapportage.eaufrance.fr/sites/default/files/SIG/FR"+letters[i]+"_SW.zip"));
     122        private Pair<String, URL> getDownloadURL(WaterAgency a) throws MalformedURLException {
     123                return new Pair<String, URL>(a.name, new URL("http://www.rapportage.eaufrance.fr/sites/default/files/SIG/FR"+a.code+"_SW.zip"));
     124        }
     125
     126        /* (non-Javadoc)
     127         * @see org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler#notifyTempFileWritten(java.io.File)
     128         */
     129        @Override
     130        public void notifyTempFileWritten(File file) {
     131                if (file.getName().matches(SHP_PATTERN.replace("(.*)", "F")+"\\.prj")) { // Adour-Garonne .prj files cannot be parsed because they do not contain quotes...
     132                        try {
     133                                BufferedReader reader = new BufferedReader(new FileReader(file));
     134                                String line = reader.readLine();
     135                                reader.close();
     136                                if (!line.contains("\"")) {
     137                                        for (String term : new String[]{"GCS_ETRS_1989", "D_ETRS_1989", "GRS_1980", "Greenwich", "Degree"}) {
     138                                                line = line.replace(term, "\""+term+"\"");
     139                                        }
     140                                        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
     141                                        writer.write(line);
     142                                        writer.close();
     143                                }
     144                        } catch (Exception e) {
     145                                e.printStackTrace();
     146                        }
     147                }
    102148        }
    103149}
Note: See TracChangeset for help on using the changeset viewer.