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