1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
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.Writer;
|
---|
9 | import java.nio.charset.StandardCharsets;
|
---|
10 | import java.nio.file.Files;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
13 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
14 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Exporter to write map data to a GeoJSON file.
|
---|
18 | * @since 4886
|
---|
19 | */
|
---|
20 | public class GeoJSONExporter extends FileExporter {
|
---|
21 |
|
---|
22 | /** File extension filter for .geojson files */
|
---|
23 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
24 | "geojson,json", "geojson", tr("GeoJSON Files") + " (*.geojson *.json)");
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code GeoJSONExporter} with WGS84 projection.
|
---|
28 | */
|
---|
29 | public GeoJSONExporter() {
|
---|
30 | super(FILE_FILTER);
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public void exportData(File file, Layer layer) throws IOException {
|
---|
35 | if (layer instanceof OsmDataLayer) {
|
---|
36 | try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
|
---|
37 | out.write(new GeoJSONWriter((OsmDataLayer) layer).write());
|
---|
38 | }
|
---|
39 | } else {
|
---|
40 | throw new IllegalArgumentException(tr("Layer ''{0}'' not supported", layer.getClass().toString()));
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|