001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.File; 007import java.io.IOException; 008import java.io.Writer; 009import java.nio.charset.StandardCharsets; 010import java.nio.file.Files; 011 012import org.openstreetmap.josm.actions.ExtensionFileFilter; 013import org.openstreetmap.josm.gui.layer.Layer; 014import org.openstreetmap.josm.gui.layer.OsmDataLayer; 015 016/** 017 * Exporter to write map data to a GeoJSON file. 018 * @since 4886 019 */ 020public class GeoJSONExporter extends FileExporter { 021 022 /** File extension filter for .geojson files */ 023 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter( 024 "geojson,json", "geojson", tr("GeoJSON Files") + " (*.geojson *.json)"); 025 026 /** 027 * Constructs a new {@code GeoJSONExporter} with WGS84 projection. 028 */ 029 public GeoJSONExporter() { 030 super(FILE_FILTER); 031 } 032 033 @Override 034 public void exportData(File file, Layer layer) throws IOException { 035 if (layer instanceof OsmDataLayer) { 036 try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { 037 out.write(new GeoJSONWriter((OsmDataLayer) layer).write()); 038 } 039 } else { 040 throw new IllegalArgumentException(tr("Layer ''{0}'' not supported", layer.getClass().toString())); 041 } 042 } 043}