Changeset 18807 in josm


Ignore:
Timestamp:
2023-08-15T15:16:08+02:00 (10 months ago)
Author:
taylor.smock
Message:

Allow importing geojson files that were saved to a session file

  • GeoJSONImporter now extends OsmImporter instead of FileImporter and deprecates the parseDataSet(String) method.
Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/GeoJSONImporter.java

    r18747 r18807  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.File;
    76import java.io.IOException;
    87import java.io.InputStream;
    98import java.util.Arrays;
    10 import java.util.Locale;
    119
    12 import javax.swing.JOptionPane;
    13 
    14 import jakarta.json.JsonException;
    1510import org.openstreetmap.josm.actions.ExtensionFileFilter;
    1611import org.openstreetmap.josm.data.osm.DataSet;
    17 import org.openstreetmap.josm.gui.MainApplication;
    18 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1912import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2013import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    21 import org.openstreetmap.josm.gui.util.GuiHelper;
    2214import org.openstreetmap.josm.io.CachedFile;
    2315import org.openstreetmap.josm.io.Compression;
    2416import org.openstreetmap.josm.io.GeoJSONReader;
    2517import org.openstreetmap.josm.io.IllegalDataException;
    26 import org.openstreetmap.josm.tools.Logging;
    27 import org.openstreetmap.josm.tools.Utils;
    2818
    2919/**
    3020 * GeoJSON file importer.
    3121 * @author Ian Dees <ian.dees@gmail.com>
    32  * @author matthieun <https://github.com/matthieun>
    33  * @since 15424
     22 * @author <a href="https://github.com/matthieun">matthieun</a>
     23 * @since 15424, extends {@link OsmImporter} since 18807
    3424 */
    35 public class GeoJSONImporter extends FileImporter {
     25public class GeoJSONImporter extends OsmImporter {
    3626
    3727    private static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
     
    4636    }
    4737
    48     @Override
    49     public void importData(final File file, final ProgressMonitor progressMonitor) {
    50         progressMonitor.beginTask(tr("Loading json file…"));
    51         progressMonitor.setTicksCount(2);
    52         Logging.info("Parsing GeoJSON: {0}", file.getAbsolutePath());
    53         try (InputStream fileInputStream = Compression.getUncompressedFileInputStream(file)) {
    54             DataSet data = GeoJSONReader.parseDataSet(fileInputStream, progressMonitor);
    55             progressMonitor.worked(1);
    56             MainApplication.getLayerManager().addLayer(new OsmDataLayer(data, file.getName(), file));
    57         } catch (IOException | IllegalArgumentException | IllegalDataException | JsonException e) {
    58             Logging.error("Error while reading json file!");
    59             Logging.error(e);
    60             String message = tr("Error loading geojson file {0}", file.getAbsolutePath())
    61                     + tr(" ({0})", Utils.getSizeString(file.length(), Locale.getDefault()));
    62             GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog(null, message, tr("Error"), JOptionPane.WARNING_MESSAGE));
    63         } finally {
    64             progressMonitor.finishTask();
    65         }
    66     }
    67 
    6838    /**
    6939     * Parse GeoJSON dataset.
     
    7242     * @throws IOException in case of I/O error
    7343     * @throws IllegalDataException if an error was found while parsing the data from the source
     44     * @deprecated since 18807, use {@link #parseDataSet(InputStream, ProgressMonitor)} instead
    7445     */
     46    @Deprecated
    7547    public DataSet parseDataSet(final String source) throws IOException, IllegalDataException {
    7648        try (CachedFile cf = new CachedFile(source)) {
    7749            InputStream fileInputStream = Compression.getUncompressedFileInputStream(cf.getFile()); // NOPMD
    78             return GeoJSONReader.parseDataSet(fileInputStream, NullProgressMonitor.INSTANCE);
     50            return this.parseDataSet(fileInputStream, NullProgressMonitor.INSTANCE);
    7951        }
    8052    }
     53
     54    @Override
     55    protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException {
     56        return GeoJSONReader.parseDataSet(in, progressMonitor);
     57    }
    8158}
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionImporter.java

    r18208 r18807  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.File;
    67import java.io.IOException;
    78import java.io.InputStream;
     
    1314import javax.xml.xpath.XPathFactory;
    1415
     16import org.openstreetmap.josm.actions.ExtensionFileFilter;
     17import org.openstreetmap.josm.gui.io.importexport.FileImporter;
    1518import org.openstreetmap.josm.gui.io.importexport.OsmImporter;
    1619import org.openstreetmap.josm.gui.layer.Layer;
     
    3134    public Layer load(Element elem, ImportSupport support, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    3235        checkMetaVersion(elem);
    33         String fileStr = extractFileName(elem, support);
     36        final String fileStr = extractFileName(elem, support);
     37        final File file = new File(fileStr);
     38        for (FileImporter importer : ExtensionFileFilter.getImporters()) {
     39            if (importer instanceof OsmImporter && importer.acceptFile(file)) {
     40                return importData((OsmImporter) importer, support, fileStr, progressMonitor);
     41            }
     42        }
     43        // Fall back to the default OsmImporter, just in case. It will probably fail.
    3444        return importData(new OsmImporter(), support, fileStr, progressMonitor);
    3545    }
  • trunk/test/unit/org/openstreetmap/josm/actions/SessionLoadActionTest.java

    r17275 r18807  
    44import static org.junit.jupiter.api.Assertions.assertFalse;
    55
    6 import org.junit.jupiter.api.extension.RegisterExtension;
    76import org.junit.jupiter.api.Test;
    87import org.openstreetmap.josm.data.imagery.ImageryInfo;
    98import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    109import org.openstreetmap.josm.gui.layer.TMSLayer;
    11 import org.openstreetmap.josm.testutils.JOSMTestRules;
    12 
    13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     10import org.openstreetmap.josm.testutils.annotations.Main;
     11import org.openstreetmap.josm.testutils.annotations.Projection;
    1412
    1513/**
    1614 * Unit tests for class {@link SessionLoadAction}.
    1715 */
     16@Main
     17@Projection
    1818class SessionLoadActionTest {
    19 
    20     /**
    21      * Setup test.
    22      */
    23     @RegisterExtension
    24     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    25     public JOSMTestRules test = new JOSMTestRules().main().projection();
    26 
    2719    /**
    2820     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17702">Bug #17702</a>.
  • trunk/test/unit/org/openstreetmap/josm/io/session/SessionReaderTest.java

    r18690 r18807  
    1515import java.util.List;
    1616
    17 import org.junit.jupiter.api.extension.RegisterExtension;
    1817import org.junit.jupiter.api.Test;
    1918import org.openstreetmap.josm.TestUtils;
    2019import org.openstreetmap.josm.data.coor.EastNorth;
     20import org.openstreetmap.josm.data.osm.Node;
    2121import org.openstreetmap.josm.gui.MainApplication;
    2222import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
     
    2828import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    2929import org.openstreetmap.josm.io.IllegalDataException;
    30 import org.openstreetmap.josm.testutils.JOSMTestRules;
    31 
    32 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     30import org.openstreetmap.josm.testutils.annotations.Projection;
    3331
    3432/**
    3533 * Unit tests for Session reading.
    3634 */
     35@Projection
    3736class SessionReaderTest {
    38 
    39     /**
    40      * Setup tests.
    41      */
    42     @RegisterExtension
    43     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    44     public JOSMTestRules test = new JOSMTestRules().projection();
    45 
    4637    private static String getSessionDataDir() {
    4738        return TestUtils.getTestDataRoot() + "/sessions";
     
    161152    }
    162153
     154    @Test
     155    void testReadGeojson() throws IOException, IllegalDataException {
     156        final List<Layer> layers = testRead("geojson.jos");
     157        assertEquals(1, layers.size());
     158        final OsmDataLayer osmDataLayer = assertInstanceOf(OsmDataLayer.class, layers.get(0));
     159        assertEquals("Geojson layer name", osmDataLayer.getName());
     160        assertEquals(1, osmDataLayer.getDataSet().allPrimitives().size());
     161        final Node node = assertInstanceOf(Node.class, osmDataLayer.getDataSet().allPrimitives().iterator().next());
     162        assertEquals(2d, node.lat(), 1e-9);
     163        assertEquals(1d, node.lon(), 1e-9);
     164        assertEquals("Test point", node.get("name"));
     165    }
     166
    163167    /**
    164168     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17701">Bug #17701</a>.
Note: See TracChangeset for help on using the changeset viewer.