[1637] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.File;
|
---|
| 7 | import java.io.FileNotFoundException;
|
---|
| 8 | import java.io.IOException;
|
---|
[1653] | 9 | import java.io.InputStream;
|
---|
[9537] | 10 | import java.util.Arrays;
|
---|
[1637] | 11 |
|
---|
[4393] | 12 | import javax.swing.JOptionPane;
|
---|
[2050] | 13 |
|
---|
[1637] | 14 | import org.openstreetmap.josm.Main;
|
---|
| 15 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[12636] | 17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[1637] | 18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[2851] | 19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[4695] | 20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[12620] | 21 | import org.openstreetmap.josm.tools.Logging;
|
---|
[1637] | 22 |
|
---|
[12470] | 23 | /**
|
---|
| 24 | * File importer that reads *.osm data files. (main storage format for OSM data
|
---|
| 25 | * in JOSM)
|
---|
| 26 | */
|
---|
[1637] | 27 | public class OsmImporter extends FileImporter {
|
---|
| 28 |
|
---|
[7392] | 29 | /**
|
---|
| 30 | * The OSM file filter (*.osm and *.xml files).
|
---|
| 31 | */
|
---|
[9537] | 32 | public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
|
---|
[9547] | 33 | "osm,xml", "osm", tr("OSM Server Files") + " (*.osm, *.osm.gz, *.osm.bz2, *.osm.zip, *.xml)",
|
---|
| 34 | ExtensionFileFilter.AddArchiveExtension.NONE, Arrays.asList("gz", "bz", "bz2", "zip"));
|
---|
[6070] | 35 |
|
---|
[7392] | 36 | /**
|
---|
| 37 | * Utility class containing imported OSM layer, and a task to run after it is added to MapView.
|
---|
| 38 | */
|
---|
[4815] | 39 | public static class OsmImporterData {
|
---|
| 40 |
|
---|
[7816] | 41 | private final OsmDataLayer layer;
|
---|
| 42 | private final Runnable postLayerTask;
|
---|
[4815] | 43 |
|
---|
| 44 | public OsmImporterData(OsmDataLayer layer, Runnable postLayerTask) {
|
---|
| 45 | this.layer = layer;
|
---|
| 46 | this.postLayerTask = postLayerTask;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public OsmDataLayer getLayer() {
|
---|
| 50 | return layer;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public Runnable getPostLayerTask() {
|
---|
| 54 | return postLayerTask;
|
---|
| 55 | }
|
---|
[4814] | 56 | }
|
---|
[4668] | 57 |
|
---|
[6798] | 58 | /**
|
---|
| 59 | * Constructs a new {@code OsmImporter}.
|
---|
| 60 | */
|
---|
[1637] | 61 | public OsmImporter() {
|
---|
[5361] | 62 | super(FILE_FILTER);
|
---|
[1637] | 63 | }
|
---|
| 64 |
|
---|
[7392] | 65 | /**
|
---|
| 66 | * Constructs a new {@code OsmImporter} with the given extension file filter.
|
---|
| 67 | * @param filter The extension file filter
|
---|
| 68 | */
|
---|
[1653] | 69 | public OsmImporter(ExtensionFileFilter filter) {
|
---|
| 70 | super(filter);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[5859] | 73 | /**
|
---|
[5863] | 74 | * Imports OSM data from file
|
---|
| 75 | * @param file file to read data from
|
---|
| 76 | * @param progressMonitor handler for progress monitoring and canceling
|
---|
[5859] | 77 | */
|
---|
[4668] | 78 | @Override
|
---|
| 79 | public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
|
---|
[7037] | 80 | try (InputStream in = Compression.getUncompressedFileInputStream(file)) {
|
---|
[5859] | 81 | importData(in, file, progressMonitor);
|
---|
[1637] | 82 | } catch (FileNotFoundException e) {
|
---|
[12620] | 83 | Logging.error(e);
|
---|
[6798] | 84 | throw new IOException(tr("File ''{0}'' does not exist.", file.getName()), e);
|
---|
[1637] | 85 | }
|
---|
| 86 | }
|
---|
[1653] | 87 |
|
---|
[5859] | 88 | /**
|
---|
[5863] | 89 | * Imports OSM data from stream
|
---|
| 90 | * @param in input stream
|
---|
| 91 | * @param associatedFile filename of data (layer name will be generated from name of file)
|
---|
| 92 | * @param pm handler for progress monitoring and canceling
|
---|
[8926] | 93 | * @throws IllegalDataException if an error was found while parsing the OSM data
|
---|
[5859] | 94 | */
|
---|
| 95 | protected void importData(InputStream in, final File associatedFile, ProgressMonitor pm) throws IllegalDataException {
|
---|
[4815] | 96 | final OsmImporterData data = loadLayer(in, associatedFile,
|
---|
[5859] | 97 | associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(), pm);
|
---|
[4755] | 98 |
|
---|
[2050] | 99 | // FIXME: remove UI stuff from IO subsystem
|
---|
[10615] | 100 | GuiHelper.runInEDT(() -> {
|
---|
| 101 | OsmDataLayer layer = data.getLayer();
|
---|
[12636] | 102 | MainApplication.getLayerManager().addLayer(layer);
|
---|
[10615] | 103 | data.getPostLayerTask().run();
|
---|
| 104 | data.getLayer().onPostLoadFromFile();
|
---|
[4695] | 105 | });
|
---|
[1653] | 106 | }
|
---|
[4668] | 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Load osm data layer from InputStream.
|
---|
[5863] | 110 | * @param in input stream
|
---|
| 111 | * @param associatedFile filename of data (can be <code>null</code> if the stream does not come from a file)
|
---|
| 112 | * @param layerName name of generated layer
|
---|
| 113 | * @param progressMonitor handler for progress monitoring and canceling
|
---|
[8929] | 114 | * @return Utility class containing imported OSM layer, and a task to run after it is added to MapView
|
---|
[8926] | 115 | * @throws IllegalDataException if an error was found while parsing the OSM data
|
---|
[4668] | 116 | */
|
---|
[8509] | 117 | public OsmImporterData loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor)
|
---|
| 118 | throws IllegalDataException {
|
---|
[4687] | 119 | final DataSet dataSet = parseDataSet(in, progressMonitor);
|
---|
[4800] | 120 | if (dataSet == null) {
|
---|
| 121 | throw new IllegalDataException(tr("Invalid dataset"));
|
---|
| 122 | }
|
---|
[4815] | 123 | OsmDataLayer layer = createLayer(dataSet, associatedFile, layerName);
|
---|
| 124 | Runnable postLayerTask = createPostLayerTask(dataSet, associatedFile, layerName, layer);
|
---|
| 125 | return new OsmImporterData(layer, postLayerTask);
|
---|
[4686] | 126 | }
|
---|
[4815] | 127 |
|
---|
[4687] | 128 | protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
| 129 | return OsmReader.parseDataSet(in, progressMonitor);
|
---|
| 130 | }
|
---|
[4815] | 131 |
|
---|
[4686] | 132 | protected OsmDataLayer createLayer(final DataSet dataSet, final File associatedFile, final String layerName) {
|
---|
| 133 | return new OsmDataLayer(dataSet, layerName, associatedFile);
|
---|
| 134 | }
|
---|
[4815] | 135 |
|
---|
[4814] | 136 | protected Runnable createPostLayerTask(final DataSet dataSet, final File associatedFile, final String layerName, final OsmDataLayer layer) {
|
---|
[10615] | 137 | return () -> {
|
---|
| 138 | if (dataSet.allPrimitives().isEmpty()) {
|
---|
| 139 | String msg;
|
---|
| 140 | if (associatedFile == null) {
|
---|
| 141 | msg = tr("No data found for layer ''{0}''.", layerName);
|
---|
| 142 | } else {
|
---|
| 143 | msg = tr("No data found in file ''{0}''.", associatedFile.getPath());
|
---|
[4668] | 144 | }
|
---|
[10615] | 145 | JOptionPane.showMessageDialog(
|
---|
| 146 | Main.parent,
|
---|
| 147 | msg,
|
---|
| 148 | tr("Open OSM file"),
|
---|
| 149 | JOptionPane.INFORMATION_MESSAGE);
|
---|
[4668] | 150 | }
|
---|
[10615] | 151 | layer.onPostLoadFromFile();
|
---|
[4668] | 152 | };
|
---|
| 153 | }
|
---|
[1637] | 154 | }
|
---|