[4886] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[5749] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[4886] | 6 | import java.io.File;
|
---|
| 7 | import java.io.IOException;
|
---|
[5749] | 8 | import java.io.Writer;
|
---|
[7082] | 9 | import java.nio.charset.StandardCharsets;
|
---|
[7315] | 10 | import java.nio.file.Files;
|
---|
[5749] | 11 |
|
---|
[8813] | 12 | import org.openstreetmap.josm.Main;
|
---|
[4886] | 13 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
[8813] | 14 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
[4886] | 15 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[8813] | 17 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
| 18 | import org.openstreetmap.josm.tools.Utils;
|
---|
[4886] | 19 |
|
---|
| 20 | public class GeoJSONExporter extends FileExporter {
|
---|
| 21 |
|
---|
[8813] | 22 | protected final Projection projection;
|
---|
[5361] | 23 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
[7214] | 24 | "geojson,json", "geojson", tr("GeoJSON Files") + " (*.geojson *.json)");
|
---|
[8813] | 25 | public static final ExtensionFileFilter FILE_FILTER_PROJECTED = new ExtensionFileFilter(
|
---|
| 26 | "proj.geojson", "proj.geojson", tr("Projected GeoJSON Files") + " (*.proj.geojson)");
|
---|
[6070] | 27 |
|
---|
[7033] | 28 | /**
|
---|
[8813] | 29 | * A GeoJSON exporter which obtains the current map projection when exporting ({@link #exportData(File, Layer)}).
|
---|
[7033] | 30 | */
|
---|
[8813] | 31 | public static class CurrentProjection extends GeoJSONExporter {
|
---|
| 32 | public CurrentProjection() {
|
---|
| 33 | super(FILE_FILTER_PROJECTED, null);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Constructs a new {@code GeoJSONExporter} with WGS84 projection.
|
---|
| 39 | */
|
---|
[4886] | 40 | public GeoJSONExporter() {
|
---|
[8813] | 41 | this(FILE_FILTER, ProjectionPreference.wgs84.getProjection());
|
---|
[4886] | 42 | }
|
---|
| 43 |
|
---|
[8813] | 44 | private GeoJSONExporter(ExtensionFileFilter fileFilter, Projection projection) {
|
---|
| 45 | super(fileFilter);
|
---|
| 46 | this.projection = projection;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[4886] | 49 | @Override
|
---|
| 50 | public void exportData(File file, Layer layer) throws IOException {
|
---|
| 51 | if (layer instanceof OsmDataLayer) {
|
---|
[8813] | 52 | String json = new GeoJSONWriter((OsmDataLayer) layer, Utils.firstNonNull(projection, Main.getProjection())).write();
|
---|
[7315] | 53 | try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
|
---|
[5749] | 54 | out.write(json);
|
---|
| 55 | }
|
---|
[4886] | 56 | } else {
|
---|
[4898] | 57 | throw new IllegalArgumentException(tr("Layer ''{0}'' not supported", layer.getClass().toString()));
|
---|
[4886] | 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|