1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io.importexport;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.Arrays;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
20 | import org.openstreetmap.josm.io.CachedFile;
|
---|
21 | import org.openstreetmap.josm.io.Compression;
|
---|
22 | import org.openstreetmap.josm.io.GeoJSONReader;
|
---|
23 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
24 | import 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 | */
|
---|
32 | public 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 | }
|
---|