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