Changeset 10852 in josm


Ignore:
Timestamp:
2016-08-19T02:40:50+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13358 - GeoJSON no longer permits projections other than WGS84, see https://tools.ietf.org/html/rfc7946

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java

    r10755 r10852  
    102102                org.openstreetmap.josm.io.OsmGzipExporter.class,
    103103                org.openstreetmap.josm.io.OsmBzip2Exporter.class,
    104                 org.openstreetmap.josm.io.GeoJSONExporter.CurrentProjection.class, // needs to be considered earlier than GeoJSONExporter
    105104                org.openstreetmap.josm.io.GeoJSONExporter.class,
    106105                org.openstreetmap.josm.io.WMSLayerExporter.class,
  • trunk/src/org/openstreetmap/josm/io/GeoJSONExporter.java

    r8813 r10852  
    1010import java.nio.file.Files;
    1111
    12 import org.openstreetmap.josm.Main;
    1312import org.openstreetmap.josm.actions.ExtensionFileFilter;
    14 import org.openstreetmap.josm.data.projection.Projection;
    1513import org.openstreetmap.josm.gui.layer.Layer;
    1614import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    17 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    18 import org.openstreetmap.josm.tools.Utils;
    1915
     16/**
     17 * Exporter to write map data to a GeoJSON file.
     18 * @since 4886
     19 */
    2020public class GeoJSONExporter extends FileExporter {
    2121
    22     protected final Projection projection;
     22    /** File extension filter for .geojson files */
    2323    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
    2424            "geojson,json", "geojson", tr("GeoJSON Files") + " (*.geojson *.json)");
    25     public static final ExtensionFileFilter FILE_FILTER_PROJECTED = new ExtensionFileFilter(
    26             "proj.geojson", "proj.geojson", tr("Projected GeoJSON Files") + " (*.proj.geojson)");
    27 
    28     /**
    29      * A GeoJSON exporter which obtains the current map projection when exporting ({@link #exportData(File, Layer)}).
    30      */
    31     public static class CurrentProjection extends GeoJSONExporter {
    32         public CurrentProjection() {
    33             super(FILE_FILTER_PROJECTED, null);
    34         }
    35     }
    3625
    3726    /**
     
    3928     */
    4029    public GeoJSONExporter() {
    41         this(FILE_FILTER, ProjectionPreference.wgs84.getProjection());
    42     }
    43 
    44     private GeoJSONExporter(ExtensionFileFilter fileFilter, Projection projection) {
    45         super(fileFilter);
    46         this.projection = projection;
     30        super(FILE_FILTER);
    4731    }
    4832
     
    5034    public void exportData(File file, Layer layer) throws IOException {
    5135        if (layer instanceof OsmDataLayer) {
    52             String json = new GeoJSONWriter((OsmDataLayer) layer, Utils.firstNonNull(projection, Main.getProjection())).write();
    5336            try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
    54                 out.write(json);
     37                out.write(new GeoJSONWriter((OsmDataLayer) layer).write());
    5538            }
    5639        } else {
  • trunk/src/org/openstreetmap/josm/io/GeoJSONWriter.java

    r10817 r10852  
    3232import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3333import org.openstreetmap.josm.gui.mappaint.ElemStyles;
     34import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    3435import org.openstreetmap.josm.tools.Pair;
    3536
    3637/**
    3738 * Writes OSM data as a GeoJSON string, using JSR 353: Java API for JSON Processing (JSON-P).
     39 * <p>
     40 * See <a href="https://tools.ietf.org/html/rfc7946">RFC7946: The GeoJSON Format</a>
    3841 */
    3942public class GeoJSONWriter {
     
    4649     * Constructs a new {@code GeoJSONWriter}.
    4750     * @param layer The OSM data layer to save
    48      * @param projection The projection to use for coordinates
     51     * @since 10852
    4952     */
    50     public GeoJSONWriter(OsmDataLayer layer, Projection projection) {
     53    public GeoJSONWriter(OsmDataLayer layer) {
    5154        this.layer = layer;
    52         this.projection = projection;
     55        this.projection = ProjectionPreference.wgs84.getProjection();
    5356    }
    5457
     
    7477            JsonObjectBuilder object = Json.createObjectBuilder()
    7578                    .add("type", "FeatureCollection")
    76                     .add("crs", Json.createObjectBuilder().add("type", "name").add(
    77                             "properties", Json.createObjectBuilder().add("name", projection.toCode())))
    7879                    .add("generator", "JOSM");
    7980            appendLayerBounds(layer.data, object);
  • trunk/test/unit/org/openstreetmap/josm/io/GeoJSONWriterTest.java

    r10817 r10852  
    44import static org.junit.Assert.assertEquals;
    55import static org.junit.Assert.assertTrue;
     6
     7import java.io.FileInputStream;
    68
    79import org.junit.BeforeClass;
     
    1315import org.openstreetmap.josm.data.osm.Node;
    1416import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    15 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    16 
    17 import java.io.FileInputStream;
    1817
    1918/**
     
    4140        ds.addPrimitive(node);
    4241        final OsmDataLayer layer = new OsmDataLayer(ds, "foo", null);
    43         final GeoJSONWriter writer = new GeoJSONWriter(layer, ProjectionPreference.wgs84.getProjection());
     42        final GeoJSONWriter writer = new GeoJSONWriter(layer);
    4443        assertEquals(("" +
    4544                "{\n" +
    4645                "    'type':'FeatureCollection',\n" +
    47                 "    'crs':{\n" +
    48                 "        'type':'name',\n" +
    49                 "        'properties':{\n" +
    50                 "            'name':'EPSG:4326'\n" +
    51                 "        }\n" +
    52                 "    },\n" +
    5346                "    'generator':'JOSM',\n" +
    5447                "    'features':[\n" +
     
    7366    /**
    7467     * Unit test for multipolygon
     68     * @throws Exception if an error occurs
    7569     */
    7670    @Test
     
    7973            DataSet ds = OsmReader.parseDataSet(in, null);
    8074            final OsmDataLayer layer = new OsmDataLayer(ds, "foo", null);
    81             final GeoJSONWriter writer = new GeoJSONWriter(layer, ProjectionPreference.wgs84.getProjection());
     75            final GeoJSONWriter writer = new GeoJSONWriter(layer);
    8276            assertTrue(writer.write().contains("MultiPolygon"));
    8377        }
Note: See TracChangeset for help on using the changeset viewer.