source: josm/trunk/src/org/openstreetmap/josm/gui/io/importexport/GeoJSONImporter.java@ 15865

Last change on this file since 15865 was 15865, checked in by Don-vip, 4 years ago

fix #18722 - exclude json file extension from GeoJsonImporter + add robustness

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io.importexport;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.IOException;
8import java.io.InputStream;
9import java.util.Arrays;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.actions.ExtensionFileFilter;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.gui.MainApplication;
16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
17import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20import org.openstreetmap.josm.io.CachedFile;
21import org.openstreetmap.josm.io.Compression;
22import org.openstreetmap.josm.io.GeoJSONReader;
23import org.openstreetmap.josm.io.IllegalDataException;
24import org.openstreetmap.josm.tools.Logging;
25
26/**
27 * GeoJSON file importer.
28 * @author Ian Dees <ian.dees@gmail.com>
29 * @author matthieun <https://github.com/matthieun>
30 * @since 15424
31 */
32public class GeoJSONImporter extends FileImporter {
33
34 private static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
35 "geojson", "geojson", tr("GeoJSON file") + " (*.geojson, *.geojson.gz, *.geojson.bz2, *.geojson.xz, *.geojson.zip)",
36 ExtensionFileFilter.AddArchiveExtension.NONE, Arrays.asList("gz", "bz", "bz2", "xz", "zip"));
37
38 /**
39 * Constructs a new GeoJSON File importer with an extension filter for .json and .geojson
40 */
41 public GeoJSONImporter() {
42 super(FILE_FILTER);
43 }
44
45 @Override
46 public void importData(final File file, final ProgressMonitor progressMonitor) {
47 progressMonitor.beginTask(tr("Loading json file…"));
48 progressMonitor.setTicksCount(2);
49 Logging.info("Parsing GeoJSON: {0}", file.getAbsolutePath());
50 try (InputStream fileInputStream = Compression.getUncompressedFileInputStream(file)) {
51 DataSet data = GeoJSONReader.parseDataSet(fileInputStream, progressMonitor);
52 progressMonitor.worked(1);
53 MainApplication.getLayerManager().addLayer(new OsmDataLayer(data, file.getName(), file));
54 } catch (IOException | IllegalDataException e) {
55 Logging.error("Error while reading json file!");
56 Logging.error(e);
57 GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog(
58 null, tr("Error loading geojson file {0}", file.getAbsolutePath()), tr("Error"), JOptionPane.WARNING_MESSAGE));
59 } finally {
60 progressMonitor.finishTask();
61 }
62 }
63
64 /**
65 * Parse GeoJSON dataset.
66 * @param source geojson file
67 * @return GeoJSON dataset
68 * @throws IOException in case of I/O error
69 * @throws IllegalDataException if an error was found while parsing the data from the source
70 */
71 public DataSet parseDataSet(final String source) throws IOException, IllegalDataException {
72 try (CachedFile cf = new CachedFile(source)) {
73 InputStream fileInputStream = Compression.getUncompressedFileInputStream(cf.getFile()); // NOPMD
74 return GeoJSONReader.parseDataSet(fileInputStream, NullProgressMonitor.INSTANCE);
75 }
76 }
77}
Note: See TracBrowser for help on using the repository browser.