source: josm/trunk/src/org/openstreetmap/josm/io/GeoJSONExporter.java@ 10619

Last change on this file since 10619 was 8813, checked in by simon04, 9 years ago

fix #10770 - GeoJSON export: allow to save coordinates in current map projection

Adds a *.proj.geojson exporter which writes the GeoJSON file with the currently set map projection.

  • Property svn:eol-style set to native
File size: 2.3 KB
RevLine 
[4886]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
[5749]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[4886]6import java.io.File;
7import java.io.IOException;
[5749]8import java.io.Writer;
[7082]9import java.nio.charset.StandardCharsets;
[7315]10import java.nio.file.Files;
[5749]11
[8813]12import org.openstreetmap.josm.Main;
[4886]13import org.openstreetmap.josm.actions.ExtensionFileFilter;
[8813]14import org.openstreetmap.josm.data.projection.Projection;
[4886]15import org.openstreetmap.josm.gui.layer.Layer;
16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[8813]17import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
18import org.openstreetmap.josm.tools.Utils;
[4886]19
20public 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}
Note: See TracBrowser for help on using the repository browser.