Ignore:
Timestamp:
2012-03-11T17:50:51+01:00 (13 years ago)
Author:
donvip
Message:

opendata: allow to download Paris and Toulouse data

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

Legend:

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

    r28031 r28044  
    1616package org.openstreetmap.josm.plugins.opendata;
    1717
     18import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
     19import static org.openstreetmap.josm.tools.I18n.marktr;
     20
     21import java.awt.event.KeyEvent;
    1822import java.io.File;
    1923import java.util.Arrays;
     24import java.util.HashMap;
    2025import java.util.List;
     26import java.util.Map;
     27
     28import javax.swing.JMenu;
    2129
    2230import org.openstreetmap.josm.Main;
    2331import org.openstreetmap.josm.actions.ExtensionFileFilter;
     32import org.openstreetmap.josm.gui.MainMenu;
    2433import org.openstreetmap.josm.gui.MapFrame;
    2534import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
     
    2736import org.openstreetmap.josm.plugins.PluginInformation;
    2837import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
     38import org.openstreetmap.josm.plugins.opendata.core.actions.DownloadDataAction;
     39import org.openstreetmap.josm.plugins.opendata.core.actions.DownloadDataTask;
     40import org.openstreetmap.josm.plugins.opendata.core.actions.OpenPreferencesActions;
     41import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler;
     42import org.openstreetmap.josm.plugins.opendata.core.datasets.DataSetCategory;
    2943import org.openstreetmap.josm.plugins.opendata.core.gui.OdDialog;
    3044import org.openstreetmap.josm.plugins.opendata.core.gui.OdPreferenceSetting;
     
    3852import org.openstreetmap.josm.plugins.opendata.core.io.tabular.OdsImporter;
    3953import org.openstreetmap.josm.plugins.opendata.core.io.tabular.XlsImporter;
     54import org.openstreetmap.josm.plugins.opendata.core.modules.Module;
    4055import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleHandler;
    4156import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleInformation;
     
    4661       
    4762        public final XmlImporter xmlImporter;
     63       
     64        private final JMenu menu;
    4865       
    4966        public OdPlugin(PluginInformation info) { // NO_UCD
     
    6582        // Load modules
    6683        loadModules();
     84        // Add menu
     85        menu = Main.main.menu.addMenu(marktr("Open Data"), KeyEvent.VK_O, Main.main.menu.defaultMenuPos, ht("/Plugin/OpenData"));
     86        buildMenu();
     87        // Add download task
     88        Main.main.menu.openLocation.addDownloadTaskClass(DownloadDataTask.class);
    6789        }
    6890       
    6991        public static final OdPlugin getInstance() {
    7092                return instance;
     93        }
     94       
     95        private void buildMenu() {
     96        for (Module module : ModuleHandler.moduleList) {
     97                Map<DataSetCategory, JMenu> catMenus = new HashMap<DataSetCategory, JMenu>();
     98                JMenu moduleMenu = null;
     99                for (AbstractDataSetHandler handler: module.getHandlers()) {
     100                        if (handler.getDataURL() != null) {
     101                                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());
     108                                }
     109                                DataSetCategory cat = handler.getCategory();
     110                                JMenu endMenu = null;
     111                                if (cat != null) {
     112                                        if ((endMenu = catMenus.get(cat)) == null) {
     113                                                catMenus.put(cat, endMenu = new JMenu(cat.getName()));
     114                                                moduleMenu.add(endMenu);
     115                                        }
     116                                }
     117                                if (endMenu == null) {
     118                                        endMenu = moduleMenu;
     119                                }
     120                                endMenu.add(new DownloadDataAction(handler));
     121                        }
     122                }
     123                if (moduleMenu != null) {
     124                        menu.add(moduleMenu);
     125                }
     126        }
     127        menu.addSeparator();
     128        MainMenu.add(menu, new OpenPreferencesActions());
    71129        }
    72130
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/AbstractDataSetHandler.java

    r28022 r28044  
    5353import org.openstreetmap.josm.data.projection.proj.LambertConformalConic.Parameters2SP;
    5454import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
     55import org.openstreetmap.josm.io.AbstractReader;
    5556import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
    5657import org.openstreetmap.josm.plugins.opendata.core.util.NamesFrUtils;
     
    9293        }
    9394
     95        private String name;
     96        private DataSetCategory category;
    9497        private String sourceDate;
    9598        private File associatedFile;
     
    170173        public URL getWikiURL() {return null;}
    171174       
    172         public abstract URL getLocalPortalURL();
    173         public abstract URL getNationalPortalURL();
     175        public URL getLocalPortalURL() {return null;}
     176       
     177        public URL getNationalPortalURL() {return null;}
    174178
    175179        public URL getLicenseURL() {return null;}
     180
     181        public URL getDataURL() {return null;}
     182       
     183        public AbstractReader getReaderForUrl(String url) {return null;}
     184
     185        public final DataSetCategory getCategory() {
     186                return category;
     187        }
     188
     189        public final void setCategory(DataSetCategory category) {
     190                this.category = category;
     191        }
    176192
    177193        public final Collection<String> getOsmXapiRequests(Bounds bounds) {
     
    340356        }
    341357
    342         /**
    343          * @return the source
    344          */
    345358        public String getSource() {
    346359                return null;
    347360        }
    348361               
    349         /**
    350          * @return the sourceDate
    351          */
    352362        public final String getSourceDate() {
    353363                return sourceDate;
    354364        }
    355365       
    356         /**
    357          * @param sourceDate the sourceDate to set
    358          */
    359366        public final void setSourceDate(String sourceDate) {
    360367                this.sourceDate = sourceDate;
    361368        }
    362        
     369
     370        public final String getName() {
     371                return name;
     372        }
     373       
     374        public final void setName(String name) {
     375                this.name = name;
     376        }
     377
    363378        public String getLocalPortalIconName() {
    364379                return ICON_CORE_24;
     
    503518                return null;
    504519        }
     520
     521        public boolean checkShpNodeProximity() {
     522                return false;
     523        }
    505524}
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/AbstractImporter.java

    r28018 r28044  
    1818import java.io.File;
    1919import java.io.IOException;
    20 import java.text.SimpleDateFormat;
    21 import java.util.Date;
    2220
    23 import org.openstreetmap.josm.Main;
    2421import org.openstreetmap.josm.actions.ExtensionFileFilter;
    2522import org.openstreetmap.josm.data.osm.DataSet;
     
    3027import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
    3128import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler;
     29import org.openstreetmap.josm.plugins.opendata.core.datasets.DataSetUpdater;
    3230import org.openstreetmap.josm.plugins.opendata.core.layers.OdDataLayer;
    3331import org.openstreetmap.josm.plugins.opendata.core.modules.Module;
     
    7371        @Override
    7472        protected OsmDataLayer createLayer(DataSet dataSet, File associatedFile, String layerName) {
    75                 if (handler != null) {
    76                         handler.setAssociatedFile(associatedFile);
    77                         handler.setSourceDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date(associatedFile.lastModified())));
    78                         if (!Main.pref.getBoolean(PREF_RAWDATA)) {
    79                                 handler.updateDataSet(dataSet);
    80                         }
    81                         handler.checkDataSetSource(dataSet);
    82                         handler.checkNames(dataSet);
    83                 }
     73                DataSetUpdater.updateDataSet(dataSet, handler, associatedFile);
    8474                return new OdDataLayer(dataSet, layerName, associatedFile, handler);
    8575        }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java

    r28031 r28044  
    5050        private final ZipInputStream zis;
    5151        private final AbstractDataSetHandler handler;
     52       
     53        private File file;
    5254   
    53     public ZipReader(ZipInputStream zis, AbstractDataSetHandler handler) {
    54         this.zis = zis;
     55    public ZipReader(InputStream in, AbstractDataSetHandler handler) {
     56        this.zis = in instanceof ZipInputStream ? (ZipInputStream) in : new ZipInputStream(in);
    5557        this.handler = handler;
    5658    }
    5759
    5860        public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {
    59                 return new ZipReader(new ZipInputStream(in), handler).parseDoc(instance);
     61                return new ZipReader(in, handler).parseDoc(instance);
     62        }
     63       
     64        public final File getReadFile() {
     65                return file;
    6066        }
    6167       
     
    8187        }
    8288
    83         private DataSet parseDoc(ProgressMonitor instance) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException  {
     89        public DataSet parseDoc(ProgressMonitor instance) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException  {
    8490               
    8591            final File temp = createTempDir();
     
    9197                                File file = new File(temp + File.separator + entry.getName());
    9298                            if (file.exists() && !file.delete()) {
    93                                 throw new IOException("Could not delete temp file: " + file.getAbsolutePath());
     99                                throw new IOException("Could not delete temp file/dir: " + file.getAbsolutePath());
    94100                            }
    95                                 if (!file.createNewFile()) {
    96                                         throw new IOException("Could not create temp file: " + file.getAbsolutePath());
    97                                 }
    98                                 FileOutputStream fos = new FileOutputStream(file);
    99                                 byte[] buffer = new byte[8192];
    100                                 int count = 0;
    101                                 while ((count = zis.read(buffer, 0, buffer.length)) > 0) {
    102                                         fos.write(buffer, 0, count);
    103                                 }
    104                                 fos.close();
    105                                 for (String ext : new String[] {
    106                                                 CSV_EXT, KML_EXT, KMZ_EXT, XLS_EXT, ODS_EXT, SHP_EXT, MIF_EXT, TAB_EXT, XML_EXT
    107                                 }) {
    108                                         if (entry.getName().toLowerCase().endsWith("."+ext)) {
    109                                                 candidates.add(file);
    110                                                 System.out.println(entry.getName());
    111                                                 break;
     101                            if (!entry.isDirectory()) {
     102                                if (!file.createNewFile()) {
     103                                        throw new IOException("Could not create temp file: " + file.getAbsolutePath());
     104                                }
     105                                        FileOutputStream fos = new FileOutputStream(file);
     106                                        byte[] buffer = new byte[8192];
     107                                        int count = 0;
     108                                        while ((count = zis.read(buffer, 0, buffer.length)) > 0) {
     109                                                fos.write(buffer, 0, count);
    112110                                        }
     111                                        fos.close();
     112                                        long time = entry.getTime();
     113                                        if (time > -1) {
     114                                                file.setLastModified(time);
     115                                        }
     116                                        for (String ext : new String[] {
     117                                                        CSV_EXT, KML_EXT, KMZ_EXT, XLS_EXT, ODS_EXT, SHP_EXT, MIF_EXT, TAB_EXT, XML_EXT
     118                                        }) {
     119                                                if (entry.getName().toLowerCase().endsWith("."+ext)) {
     120                                                        candidates.add(file);
     121                                                        System.out.println(entry.getName());
     122                                                        break;
     123                                                }
     124                                        }
     125                                } else if (!file.mkdir()) {
     126                                        throw new IOException("Could not create temp dir: " + file.getAbsolutePath());
    113127                                }
    114128                        }
    115129                       
    116                         File file = null;
     130                        file = null;
    117131                       
    118132                        if (candidates.size() > 1) {
     
    159173                                }
    160174                        }
    161                        
     175            } catch (IllegalArgumentException e) {
     176                System.err.println(e.getMessage());
    162177            } finally {
    163178                deleteDir(temp);
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/ShpReader.java

    r28000 r28044  
    371371        }
    372372       
     373        private Node getNode(Point p, String key) {
     374                Node n = nodes.get(key);
     375                if (n == null && handler != null && handler.checkShpNodeProximity()) {
     376                        LatLon ll = new LatLon(p.getY(), p.getX());
     377                        for (Node node : nodes.values()) {
     378                                if (node.getCoor().equalsEpsilon(ll)) {
     379                                        return node;
     380                                }
     381                        }
     382                }
     383                return n;
     384        }
     385       
    373386        private Node createOrGetNode(Point p) throws MismatchedDimensionException, TransformException {
    374387                if (transform != null) {
    375388                        Point p2 = (Point) JTS.transform(p, transform);
    376389                        String key = p2.getX()+"/"+p2.getY();
    377                         Node n = nodes.get(key);
     390                        //String key = LatLon.roundToOsmPrecisionStrict(p2.getX())+"/"+LatLon.roundToOsmPrecisionStrict(p2.getY());
     391                        Node n = getNode(p2, key);
    378392                        if (n == null) {
    379393                                nodes.put(key, n = new Node(new LatLon(p2.getY(), p2.getX())));
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/AbstractModule.java

    r28000 r28044  
    5555        public List<AbstractDataSetHandler> getHandlers() {
    5656                return handlers;
     57        }
     58
     59        /* (non-Javadoc)
     60         * @see org.openstreetmap.josm.plugins.opendata.core.modules.Module#getDisplayedName()
     61         */
     62        @Override
     63        public String getDisplayedName() {
     64                return info.name;
    5765        }
    5866
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/Module.java

    r28000 r28044  
    2323public interface Module {
    2424
     25        public String getDisplayedName();
     26
    2527        public List<AbstractDataSetHandler> getHandlers();
    2628       
Note: See TracChangeset for help on using the changeset viewer.