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