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