Ignore:
Timestamp:
2012-03-11T21:59:38+01:00 (13 years ago)
Author:
donvip
Message:

opendata: allow to download data.gouv.fr data

Location:
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/OdPlugin.java

    r28044 r28050  
    1919import static org.openstreetmap.josm.tools.I18n.marktr;
    2020
     21import java.awt.Toolkit;
    2122import java.awt.event.KeyEvent;
    2223import java.io.File;
     24import java.net.URL;
    2325import java.util.Arrays;
    2426import java.util.HashMap;
     
    2729
    2830import javax.swing.JMenu;
     31import javax.swing.JMenuItem;
    2932
    3033import org.openstreetmap.josm.Main;
     
    3235import org.openstreetmap.josm.gui.MainMenu;
    3336import org.openstreetmap.josm.gui.MapFrame;
     37import org.openstreetmap.josm.gui.MenuScroller;
    3438import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    3539import org.openstreetmap.josm.plugins.Plugin;
     
    5559import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleHandler;
    5660import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleInformation;
     61import org.openstreetmap.josm.tools.Pair;
    5762
    5863public final class OdPlugin extends Plugin implements OdConstants {
     
    9398        }
    9499       
     100        private JMenu getModuleMenu(Module module) {
     101                String moduleName = module.getDisplayedName();
     102                if (moduleName == null || moduleName.isEmpty()) {
     103                        moduleName = module.getModuleInformation().getName();
     104                }
     105                JMenu moduleMenu = new JMenu(moduleName);
     106                moduleMenu.setIcon(module.getModuleInformation().getScaledIcon());
     107                return moduleMenu;
     108        }
     109       
    95110        private void buildMenu() {
     111        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
    96112        for (Module module : ModuleHandler.moduleList) {
    97113                Map<DataSetCategory, JMenu> catMenus = new HashMap<DataSetCategory, JMenu>();
    98114                JMenu moduleMenu = null;
    99115                for (AbstractDataSetHandler handler: module.getHandlers()) {
    100                         if (handler.getDataURL() != null) {
     116                        if (handler.getDataURL() != null || (handler.getDataURLs() != null && !handler.getDataURLs().isEmpty())) {
    101117                                if (moduleMenu == null) {
    102                                         String moduleName = module.getDisplayedName();
    103                                         if (moduleName == null || moduleName.isEmpty()) {
    104                                                 moduleName = module.getModuleInformation().getName();
    105                                         }
    106                                         moduleMenu = new JMenu(moduleName);
    107                                         moduleMenu.setIcon(module.getModuleInformation().getScaledIcon());
     118                                        moduleMenu = getModuleMenu(module);
    108119                                }
    109120                                DataSetCategory cat = handler.getCategory();
     
    118129                                        endMenu = moduleMenu;
    119130                                }
    120                                 endMenu.add(new DownloadDataAction(handler));
     131                                String handlerName = handler.getName();
     132                                if (handlerName == null || handlerName.isEmpty()) {
     133                                        handlerName = handler.getClass().getName();
     134                                }
     135                                if (handler.getDataURL() != null) {
     136                                        endMenu.add(new DownloadDataAction(handlerName, handler.getDataURL()));
     137                                } else if (handler.getDataURLs() != null) {
     138                                        JMenu handlerMenu = new JMenu(handlerName);
     139                                        JMenuItem item = null;
     140                                        for (Pair<String, URL> pair : handler.getDataURLs()) {
     141                                                if (pair != null && pair.a != null && pair.b != null) {
     142                                                        item = handlerMenu.add(new DownloadDataAction(pair.a, pair.b));
     143                                                }
     144                                        }
     145                                        if (item != null) {
     146                                                MenuScroller.setScrollerFor(handlerMenu, (screenHeight / item.getPreferredSize().height)-3);
     147                                                endMenu.add(handlerMenu);
     148                                        }
     149                                }
    121150                        }
    122151                }
    123152                if (moduleMenu != null) {
     153                        //MenuScroller.setScrollerFor(moduleMenu, screenHeight / moduleMenu.getItem(0).getPreferredSize().height);
    124154                        menu.add(moduleMenu);
    125155                }
    126156        }
    127157        menu.addSeparator();
    128         MainMenu.add(menu, new OpenPreferencesActions());
     158        /*JMenuItem itemIcon =*/ MainMenu.add(menu, new OpenPreferencesActions());
     159        //MenuScroller.setScrollerFor(menu, screenHeight / itemIcon.getPreferredSize().height);
    129160        }
    130161
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/OdConstants.java

    r28033 r28050  
    126126    public static final String Y_STRING = "Y|LAT|LATI|LATITUDE|NORTHING";
    127127   
    128     // The list of all ProjectionPatterns (filled at each constrcutor call)
     128    // The list of all ProjectionPatterns (filled at each constructor call)
    129129    public static final Collection<ProjectionPatterns> PROJECTIONS = new ArrayList<ProjectionPatterns>();
    130130   
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/actions/DownloadDataAction.java

    r28044 r28050  
    1717
    1818import java.awt.event.ActionEvent;
     19import java.net.URL;
    1920
    2021import javax.swing.Action;
     
    2223import org.openstreetmap.josm.Main;
    2324import org.openstreetmap.josm.actions.JosmAction;
    24 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler;
    2525import org.openstreetmap.josm.tools.CheckParameterUtil;
    2626
    2727public class DownloadDataAction extends JosmAction {
    2828
    29         private final AbstractDataSetHandler handler;
     29        private final URL url;
    3030       
    31         public DownloadDataAction(AbstractDataSetHandler handler) {
    32                 CheckParameterUtil.ensureParameterNotNull(handler, "handler");
    33                 this.handler = handler;
    34                 String name = handler.getName();
    35                 if (name == null || name.isEmpty()) {
    36                         name = handler.getClass().getName();
    37                 }
     31        public DownloadDataAction(String name, URL url) {
     32                super(false);
     33                CheckParameterUtil.ensureParameterNotNull(name, "name");
     34                CheckParameterUtil.ensureParameterNotNull(url, "url");
    3835                putValue(Action.NAME, name);
     36                putValue("toolbar", "opendata_download_"+name.toLowerCase().replace(" ", "_"));
     37                this.url = url;
    3938        }
    4039       
    4140        @Override
    4241        public void actionPerformed(ActionEvent e) {
    43                 Main.main.menu.openLocation.openUrl(true, handler.getDataURL().toString());
     42                Main.main.menu.openLocation.openUrl(true, url.toString());
    4443        }
    4544}
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/actions/DownloadDataTask.java

    r28044 r28050  
    5656                for (Module module : ModuleHandler.moduleList) {
    5757                        for (AbstractDataSetHandler handler : module.getHandlers()) {
    58                                 if (handler != null && handler.getDataURL() != null && url.equals(handler.getDataURL().toString())) {
     58                                if (handler.acceptsUrl(url)) {
    5959                                        this.handler = handler;
    6060                                        return true;
     
    7777                protected OsmDataLayer createNewLayer(String layerName) {
    7878            File associatedFile = ((NetworkReader)reader).getReadFile();
     79            String filename = ((NetworkReader)reader).getReadFileName();
    7980            if (layerName == null || layerName.isEmpty()) {
    80                 layerName = associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName();
     81                if (associatedFile != null) {
     82                        layerName = associatedFile.getName();
     83                } else if (filename != null && !filename.isEmpty()) {
     84                        layerName = filename;
     85                } else {
     86                        layerName = OsmDataLayer.createNewName();
     87                }
    8188            }
    8289                DataSetUpdater.updateDataSet(dataSet, handler, associatedFile);
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/actions/OpenPreferencesActions.java

    r28044 r28050  
    3030       
    3131    public OpenPreferencesActions() {
     32        super(false);
    3233        putValue(NAME, tr("Preferences"));
    3334        putValue(SMALL_ICON, ImageProvider.get("dialogs", ICON_CORE_24));
     35                putValue("toolbar", "opendata_open_preferences");
    3436    }
    3537   
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/AbstractDataSetHandler.java

    r28044 r28050  
    1717
    1818import java.io.File;
     19import java.net.MalformedURLException;
    1920import java.net.URL;
    2021import java.nio.charset.Charset;
     
    9798        private String sourceDate;
    9899        private File associatedFile;
     100        private URL dataURL;
    99101
    100102        public AbstractDataSetHandler() {
     
    179181        public URL getLicenseURL() {return null;}
    180182
    181         public URL getDataURL() {return null;}
    182        
     183        public URL getDataURL() {return dataURL;}
     184       
     185        public final void setDataURL(String url) throws MalformedURLException {
     186                this.dataURL = new URL(url);
     187        }
     188       
     189        public final void setDataURL(URL url) {
     190                this.dataURL = url;
     191        }
     192
     193        public List<Pair<String,URL>> getDataURLs() {return null;}
     194
    183195        public AbstractReader getReaderForUrl(String url) {return null;}
    184196
     
    522534                return false;
    523535        }
     536       
     537        public boolean acceptsUrl(String url) {
     538                if (getDataURL() != null && url.equals(getDataURL().toString())) {
     539                        return true;
     540                }
     541                if (getDataURLs() != null) {
     542                        for (Pair<String, URL> pair : getDataURLs()) {
     543                                if (pair.b != null && url.equals(pair.b.toString())) {
     544                                        return true;
     545                                }
     546                        }
     547                }
     548                return false;
     549        }
    524550}
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/fr/FrenchConstants.java

    r28018 r28050  
    2323         * Portal
    2424         */
    25         public static final String FRENCH_PORTAL = "http://www.data.gouv.fr/donnees/view/";
     25        public static final String FRENCH_PORTAL = "http://www.data.gouv.fr/";
    2626       
    2727        /**
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/fr/FrenchDataSetHandler.java

    r28031 r28050  
    9898                try {
    9999                        if (nationalPortalPath != null && !nationalPortalPath.isEmpty()) {
    100                                 return new URL(FRENCH_PORTAL + nationalPortalPath);
     100                                return new URL(FRENCH_PORTAL + "donnees/view/" + nationalPortalPath);
    101101                        }
    102102                } catch (MalformedURLException e) {
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/NetworkReader.java

    r28044 r28050  
    2020import java.io.File;
    2121import java.io.InputStream;
     22import java.util.regex.Matcher;
     23import java.util.regex.Pattern;
    2224
    2325import org.openstreetmap.josm.data.osm.DataSet;
     
    2628import org.openstreetmap.josm.io.OsmServerReader;
    2729import org.openstreetmap.josm.io.OsmTransferException;
     30import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
    2831import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler;
    2932import org.openstreetmap.josm.plugins.opendata.core.io.archive.ZipReader;
     
    3841import org.openstreetmap.josm.tools.CheckParameterUtil;
    3942
    40 public class NetworkReader extends OsmServerReader {
     43public class NetworkReader extends OsmServerReader implements OdConstants {
    4144
    4245        private final String url;
     
    4548
    4649        private File file;
     50        private String filename;
    4751       
    4852    public NetworkReader(String url, AbstractDataSetHandler handler, Class<? extends AbstractReader> readerClass) {
     
    5862        }
    5963
     64        public final String getReadFileName() {
     65                return filename;
     66        }
     67
     68        private Class<? extends AbstractReader> findReaderByAttachment() {
     69                String cdisp = this.activeConnection.getHeaderField("Content-disposition");
     70                if (cdisp != null) {
     71                        Matcher m = Pattern.compile("attachment; filename=(.*)").matcher(cdisp);
     72                        if (m.matches()) {
     73                                filename = m.group(1);
     74                                return findReaderByExtension(filename.toLowerCase());
     75                        }
     76                }
     77                return null;
     78        }
     79
     80        private Class<? extends AbstractReader> findReaderByContentType() {
     81        String contentType = this.activeConnection.getContentType();
     82        if (contentType.startsWith("application/zip")) {
     83                return ZipReader.class;
     84        } else if (contentType.startsWith("application/vnd.ms-excel")) {
     85                return XlsReader.class;
     86        } else if (contentType.startsWith("application/octet-stream")) {
     87                //return OdsReader.class;//FIXME, can be anything
     88        } else if (contentType.startsWith("text/plain")) {//TODO: extract charset
     89                return CsvReader.class;
     90        } else if (contentType.startsWith("tdyn/html")) {
     91                //return CsvReader.class;//FIXME, can also be .tar.gz
     92        } else {
     93                System.err.println("Unsupported content type: "+contentType);
     94        }
     95        return null;
     96        }
     97
     98        private Class<? extends AbstractReader> findReaderByExtension(String filename) {
     99        if (filename.endsWith("."+XLS_EXT)) {
     100                return XlsReader.class;
     101        } else if (filename.endsWith("."+CSV_EXT)) {
     102                return CsvReader.class;
     103        } else if (filename.endsWith("."+ODS_EXT)) {
     104                return OdsReader.class;
     105        } else if (filename.endsWith("."+KML_EXT)) {
     106                return KmlReader.class;
     107        } else if (filename.endsWith("."+KMZ_EXT)) {
     108                return KmzReader.class;
     109        } else if (filename.endsWith("."+MIF_EXT)) {
     110                return MifReader.class;
     111        } else if (filename.endsWith("."+SHP_EXT)) {
     112                return ShpReader.class;
     113        } else if (filename.endsWith("."+TAB_EXT)) {
     114                return TabReader.class;
     115        } else if (filename.endsWith("."+ZIP_EXT)) {
     116                return ZipReader.class;
     117        } else {
     118                return null;
     119        }
     120        }
     121
    60122        @Override
    61123        public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
     
    69131            ProgressMonitor instance = progressMonitor.createSubTaskMonitor(1, false);
    70132            if (readerClass == null) {
    71                 String contentType = this.activeConnection.getContentType();
    72                 if (contentType.startsWith("application/zip")) {
    73                         readerClass = ZipReader.class;
    74                 } else {
    75                         throw new IllegalArgumentException("Unsupported content type: "+contentType);
     133                readerClass = findReaderByAttachment();
     134            }
     135            if (readerClass == null) {
     136                readerClass = findReaderByContentType();
     137            }
     138            if (readerClass == null) {
     139                readerClass = findReaderByExtension(url.toLowerCase());
     140                if (readerClass != null) {
     141                        filename = url.substring(url.lastIndexOf('/'));
    76142                }
     143            }
     144            if (readerClass == null) {
     145                        throw new OsmTransferException("Cannot find appropriate reader !");//TODO handler job ?
    77146            }
    78147            if (readerClass.equals(ZipReader.class)) {
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/ProjectionPatterns.java

    r28000 r28050  
    6363
    6464        public static final Pattern getCoordinatePattern(String coor, String proj) {
    65         return Pattern.compile("(?:.*(?:"+coor+").*(?:"+proj+").*)|(?:.*("+proj+").*(?:"+coor+").*)", Pattern.CASE_INSENSITIVE);
     65                if (proj != null && !proj.isEmpty()) {
     66                        return Pattern.compile("(?:.*(?:"+coor+").*(?:"+proj+").*)|(?:.*("+proj+").*(?:"+coor+").*)", Pattern.CASE_INSENSITIVE);
     67                } else {
     68                        return Pattern.compile(coor, Pattern.CASE_INSENSITIVE);
     69                }
    6670    }
    6771}
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java

    r28044 r28050  
    9696                        while ((entry = zis.getNextEntry()) != null) {
    9797                                File file = new File(temp + File.separator + entry.getName());
     98                        File parent = file.getParentFile();
     99                        if (parent != null && !parent.exists()) {
     100                                parent.mkdirs();
     101                        }
    98102                            if (file.exists() && !file.delete()) {
    99103                                throw new IOException("Could not delete temp file/dir: " + file.getAbsolutePath());
Note: See TracChangeset for help on using the changeset viewer.