001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.io.importexport; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.File; 007import java.io.IOException; 008import java.io.InputStream; 009import java.util.Arrays; 010 011import javax.swing.JOptionPane; 012 013import org.openstreetmap.josm.actions.ExtensionFileFilter; 014import org.openstreetmap.josm.data.osm.DataSet; 015import org.openstreetmap.josm.gui.MainApplication; 016import org.openstreetmap.josm.gui.layer.OsmDataLayer; 017import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 018import org.openstreetmap.josm.gui.progress.ProgressMonitor; 019import org.openstreetmap.josm.gui.util.GuiHelper; 020import org.openstreetmap.josm.io.CachedFile; 021import org.openstreetmap.josm.io.Compression; 022import org.openstreetmap.josm.io.GeoJSONReader; 023import org.openstreetmap.josm.io.IllegalDataException; 024import org.openstreetmap.josm.tools.Logging; 025 026/** 027 * GeoJSON file importer. 028 * @author Ian Dees <ian.dees@gmail.com> 029 * @author matthieun <https://github.com/matthieun> 030 * @since 15424 031 */ 032public class GeoJSONImporter extends FileImporter { 033 034 private static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions( 035 "geojson,json", "geojson", tr("GeoJSON file") + " (*.geojson, *.geojson.gz, *.geojson.bz2, *.geojson.xz, *.geojson.zip, *.json)", 036 ExtensionFileFilter.AddArchiveExtension.NONE, Arrays.asList("gz", "bz", "bz2", "xz", "zip")); 037 038 /** 039 * Constructs a new GeoJSON File importer with an extension filter for .json and .geojson 040 */ 041 public GeoJSONImporter() { 042 super(FILE_FILTER); 043 } 044 045 @Override 046 public void importData(final File file, final ProgressMonitor progressMonitor) { 047 progressMonitor.beginTask(tr("Loading json fileā¦")); 048 progressMonitor.setTicksCount(2); 049 Logging.info("Parsing GeoJSON: {0}", file.getAbsolutePath()); 050 try (InputStream fileInputStream = Compression.getUncompressedFileInputStream(file)) { 051 DataSet data = GeoJSONReader.parseDataSet(fileInputStream, progressMonitor); 052 progressMonitor.worked(1); 053 MainApplication.getLayerManager().addLayer(new OsmDataLayer(data, file.getName(), file)); 054 } catch (IOException | IllegalDataException e) { 055 Logging.error("Error while reading json file!"); 056 Logging.error(e); 057 GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog( 058 null, tr("Error loading geojson file {0}", file.getAbsolutePath()), tr("Error"), JOptionPane.WARNING_MESSAGE)); 059 } finally { 060 progressMonitor.finishTask(); 061 } 062 } 063 064 /** 065 * Parse GeoJSON dataset. 066 * @param source geojson file 067 * @return GeoJSON dataset 068 * @throws IOException in case of I/O error 069 * @throws IllegalDataException if an error was found while parsing the data from the source 070 */ 071 public DataSet parseDataSet(final String source) throws IOException, IllegalDataException { 072 try (CachedFile cf = new CachedFile(source)) { 073 InputStream fileInputStream = Compression.getUncompressedFileInputStream(cf.getFile()); // NOPMD 074 return GeoJSONReader.parseDataSet(fileInputStream, NullProgressMonitor.INSTANCE); 075 } 076 } 077}