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