[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;
|
---|
[1637] | 10 |
|
---|
[4393] | 11 | import javax.swing.JOptionPane;
|
---|
[2050] | 12 |
|
---|
[1637] | 13 | import org.openstreetmap.josm.Main;
|
---|
| 14 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[7816] | 16 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
[1637] | 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[1811] | 18 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
[2851] | 19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[4695] | 20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[1637] | 21 |
|
---|
| 22 | public class OsmImporter extends FileImporter {
|
---|
| 23 |
|
---|
[7392] | 24 | /**
|
---|
| 25 | * The OSM file filter (*.osm and *.xml files).
|
---|
| 26 | */
|
---|
[5361] | 27 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
| 28 | "osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)");
|
---|
[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
|
---|
[5859] | 86 | */
|
---|
[4393] | 87 | protected void importData(InputStream in, final File associatedFile) throws IllegalDataException {
|
---|
[5859] | 88 | importData(in, associatedFile, NullProgressMonitor.INSTANCE);
|
---|
| 89 | }
|
---|
[6070] | 90 |
|
---|
[5859] | 91 | /**
|
---|
[5863] | 92 | * Imports OSM data from stream
|
---|
| 93 | * @param in input stream
|
---|
| 94 | * @param associatedFile filename of data (layer name will be generated from name of file)
|
---|
| 95 | * @param pm handler for progress monitoring and canceling
|
---|
[5859] | 96 | */
|
---|
| 97 | protected void importData(InputStream in, final File associatedFile, ProgressMonitor pm) throws IllegalDataException {
|
---|
[4815] | 98 | final OsmImporterData data = loadLayer(in, associatedFile,
|
---|
[5859] | 99 | associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(), pm);
|
---|
[4755] | 100 |
|
---|
[2050] | 101 | // FIXME: remove UI stuff from IO subsystem
|
---|
[4695] | 102 | GuiHelper.runInEDT(new Runnable() {
|
---|
[4393] | 103 | @Override
|
---|
[2050] | 104 | public void run() {
|
---|
[7816] | 105 | OsmDataLayer layer = data.getLayer();
|
---|
| 106 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
| 107 | v.visit(layer.data.getDataSourceBoundingBox());
|
---|
| 108 | if (!v.hasExtend()) {
|
---|
| 109 | v.computeBoundingBox(layer.data.getNodes());
|
---|
| 110 | }
|
---|
| 111 | Main.main.addLayer(layer, v.getBounds());
|
---|
| 112 | data.getPostLayerTask().run();
|
---|
| 113 | data.getLayer().onPostLoadFromFile();
|
---|
[2050] | 114 | }
|
---|
[4695] | 115 | });
|
---|
[1653] | 116 | }
|
---|
[4668] | 117 |
|
---|
| 118 | /**
|
---|
| 119 | * Load osm data layer from InputStream.
|
---|
[5863] | 120 | * @param in input stream
|
---|
| 121 | * @param associatedFile filename of data (can be <code>null</code> if the stream does not come from a file)
|
---|
| 122 | * @param layerName name of generated layer
|
---|
| 123 | * @param progressMonitor handler for progress monitoring and canceling
|
---|
[4668] | 124 | */
|
---|
[4814] | 125 | public OsmImporterData loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
[4687] | 126 | final DataSet dataSet = parseDataSet(in, progressMonitor);
|
---|
[4800] | 127 | if (dataSet == null) {
|
---|
| 128 | throw new IllegalDataException(tr("Invalid dataset"));
|
---|
| 129 | }
|
---|
[4815] | 130 | OsmDataLayer layer = createLayer(dataSet, associatedFile, layerName);
|
---|
| 131 | Runnable postLayerTask = createPostLayerTask(dataSet, associatedFile, layerName, layer);
|
---|
| 132 | return new OsmImporterData(layer, postLayerTask);
|
---|
[4686] | 133 | }
|
---|
[4815] | 134 |
|
---|
[4687] | 135 | protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
| 136 | return OsmReader.parseDataSet(in, progressMonitor);
|
---|
| 137 | }
|
---|
[4815] | 138 |
|
---|
[4686] | 139 | protected OsmDataLayer createLayer(final DataSet dataSet, final File associatedFile, final String layerName) {
|
---|
| 140 | return new OsmDataLayer(dataSet, layerName, associatedFile);
|
---|
| 141 | }
|
---|
[4815] | 142 |
|
---|
[4814] | 143 | protected Runnable createPostLayerTask(final DataSet dataSet, final File associatedFile, final String layerName, final OsmDataLayer layer) {
|
---|
[4686] | 144 | return new Runnable() {
|
---|
[4668] | 145 | @Override
|
---|
| 146 | public void run() {
|
---|
| 147 | if (dataSet.allPrimitives().isEmpty()) {
|
---|
| 148 | String msg;
|
---|
| 149 | if (associatedFile == null) {
|
---|
| 150 | msg = tr("No data found for layer ''{0}''.", layerName);
|
---|
| 151 | } else {
|
---|
| 152 | msg = tr("No data found in file ''{0}''.", associatedFile.getPath());
|
---|
| 153 | }
|
---|
| 154 | JOptionPane.showMessageDialog(
|
---|
| 155 | Main.parent,
|
---|
| 156 | msg,
|
---|
| 157 | tr("Open OSM file"),
|
---|
| 158 | JOptionPane.INFORMATION_MESSAGE);
|
---|
| 159 | }
|
---|
| 160 | layer.onPostLoadFromFile();
|
---|
| 161 | }
|
---|
| 162 | };
|
---|
| 163 | }
|
---|
[1637] | 164 | }
|
---|