Changeset 28044 in osm for applications/editors/josm/plugins/opendata/src/org/openstreetmap
- Timestamp:
- 2012-03-11T17:50:51+01:00 (13 years ago)
- 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 16 16 package org.openstreetmap.josm.plugins.opendata; 17 17 18 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 19 import static org.openstreetmap.josm.tools.I18n.marktr; 20 21 import java.awt.event.KeyEvent; 18 22 import java.io.File; 19 23 import java.util.Arrays; 24 import java.util.HashMap; 20 25 import java.util.List; 26 import java.util.Map; 27 28 import javax.swing.JMenu; 21 29 22 30 import org.openstreetmap.josm.Main; 23 31 import org.openstreetmap.josm.actions.ExtensionFileFilter; 32 import org.openstreetmap.josm.gui.MainMenu; 24 33 import org.openstreetmap.josm.gui.MapFrame; 25 34 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; … … 27 36 import org.openstreetmap.josm.plugins.PluginInformation; 28 37 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 38 import org.openstreetmap.josm.plugins.opendata.core.actions.DownloadDataAction; 39 import org.openstreetmap.josm.plugins.opendata.core.actions.DownloadDataTask; 40 import org.openstreetmap.josm.plugins.opendata.core.actions.OpenPreferencesActions; 41 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler; 42 import org.openstreetmap.josm.plugins.opendata.core.datasets.DataSetCategory; 29 43 import org.openstreetmap.josm.plugins.opendata.core.gui.OdDialog; 30 44 import org.openstreetmap.josm.plugins.opendata.core.gui.OdPreferenceSetting; … … 38 52 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.OdsImporter; 39 53 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.XlsImporter; 54 import org.openstreetmap.josm.plugins.opendata.core.modules.Module; 40 55 import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleHandler; 41 56 import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleInformation; … … 46 61 47 62 public final XmlImporter xmlImporter; 63 64 private final JMenu menu; 48 65 49 66 public OdPlugin(PluginInformation info) { // NO_UCD … … 65 82 // Load modules 66 83 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); 67 89 } 68 90 69 91 public static final OdPlugin getInstance() { 70 92 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()); 71 129 } 72 130 -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/AbstractDataSetHandler.java
r28022 r28044 53 53 import org.openstreetmap.josm.data.projection.proj.LambertConformalConic.Parameters2SP; 54 54 import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry; 55 import org.openstreetmap.josm.io.AbstractReader; 55 56 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 56 57 import org.openstreetmap.josm.plugins.opendata.core.util.NamesFrUtils; … … 92 93 } 93 94 95 private String name; 96 private DataSetCategory category; 94 97 private String sourceDate; 95 98 private File associatedFile; … … 170 173 public URL getWikiURL() {return null;} 171 174 172 public abstract URL getLocalPortalURL(); 173 public abstract URL getNationalPortalURL(); 175 public URL getLocalPortalURL() {return null;} 176 177 public URL getNationalPortalURL() {return null;} 174 178 175 179 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 } 176 192 177 193 public final Collection<String> getOsmXapiRequests(Bounds bounds) { … … 340 356 } 341 357 342 /**343 * @return the source344 */345 358 public String getSource() { 346 359 return null; 347 360 } 348 361 349 /**350 * @return the sourceDate351 */352 362 public final String getSourceDate() { 353 363 return sourceDate; 354 364 } 355 365 356 /**357 * @param sourceDate the sourceDate to set358 */359 366 public final void setSourceDate(String sourceDate) { 360 367 this.sourceDate = sourceDate; 361 368 } 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 363 378 public String getLocalPortalIconName() { 364 379 return ICON_CORE_24; … … 503 518 return null; 504 519 } 520 521 public boolean checkShpNodeProximity() { 522 return false; 523 } 505 524 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/AbstractImporter.java
r28018 r28044 18 18 import java.io.File; 19 19 import java.io.IOException; 20 import java.text.SimpleDateFormat;21 import java.util.Date;22 20 23 import org.openstreetmap.josm.Main;24 21 import org.openstreetmap.josm.actions.ExtensionFileFilter; 25 22 import org.openstreetmap.josm.data.osm.DataSet; … … 30 27 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 31 28 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler; 29 import org.openstreetmap.josm.plugins.opendata.core.datasets.DataSetUpdater; 32 30 import org.openstreetmap.josm.plugins.opendata.core.layers.OdDataLayer; 33 31 import org.openstreetmap.josm.plugins.opendata.core.modules.Module; … … 73 71 @Override 74 72 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); 84 74 return new OdDataLayer(dataSet, layerName, associatedFile, handler); 85 75 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java
r28031 r28044 50 50 private final ZipInputStream zis; 51 51 private final AbstractDataSetHandler handler; 52 53 private File file; 52 54 53 public ZipReader( ZipInputStreamzis, AbstractDataSetHandler handler) {54 this.zis = zis;55 public ZipReader(InputStream in, AbstractDataSetHandler handler) { 56 this.zis = in instanceof ZipInputStream ? (ZipInputStream) in : new ZipInputStream(in); 55 57 this.handler = handler; 56 58 } 57 59 58 60 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; 60 66 } 61 67 … … 81 87 } 82 88 83 p rivateDataSet parseDoc(ProgressMonitor instance) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {89 public DataSet parseDoc(ProgressMonitor instance) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException { 84 90 85 91 final File temp = createTempDir(); … … 91 97 File file = new File(temp + File.separator + entry.getName()); 92 98 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()); 94 100 } 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); 112 110 } 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()); 113 127 } 114 128 } 115 129 116 Filefile = null;130 file = null; 117 131 118 132 if (candidates.size() > 1) { … … 159 173 } 160 174 } 161 175 } catch (IllegalArgumentException e) { 176 System.err.println(e.getMessage()); 162 177 } finally { 163 178 deleteDir(temp); -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/ShpReader.java
r28000 r28044 371 371 } 372 372 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 373 386 private Node createOrGetNode(Point p) throws MismatchedDimensionException, TransformException { 374 387 if (transform != null) { 375 388 Point p2 = (Point) JTS.transform(p, transform); 376 389 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); 378 392 if (n == null) { 379 393 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 55 55 public List<AbstractDataSetHandler> getHandlers() { 56 56 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; 57 65 } 58 66 -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/Module.java
r28000 r28044 23 23 public interface Module { 24 24 25 public String getDisplayedName(); 26 25 27 public List<AbstractDataSetHandler> getHandlers(); 26 28
Note:
See TracChangeset
for help on using the changeset viewer.