Changeset 10852 in josm
- Timestamp:
- 2016-08-19T02:40:50+02:00 (8 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
r10755 r10852 102 102 org.openstreetmap.josm.io.OsmGzipExporter.class, 103 103 org.openstreetmap.josm.io.OsmBzip2Exporter.class, 104 org.openstreetmap.josm.io.GeoJSONExporter.CurrentProjection.class, // needs to be considered earlier than GeoJSONExporter105 104 org.openstreetmap.josm.io.GeoJSONExporter.class, 106 105 org.openstreetmap.josm.io.WMSLayerExporter.class, -
trunk/src/org/openstreetmap/josm/io/GeoJSONExporter.java
r8813 r10852 10 10 import java.nio.file.Files; 11 11 12 import org.openstreetmap.josm.Main;13 12 import org.openstreetmap.josm.actions.ExtensionFileFilter; 14 import org.openstreetmap.josm.data.projection.Projection;15 13 import org.openstreetmap.josm.gui.layer.Layer; 16 14 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 17 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;18 import org.openstreetmap.josm.tools.Utils;19 15 16 /** 17 * Exporter to write map data to a GeoJSON file. 18 * @since 4886 19 */ 20 20 public class GeoJSONExporter extends FileExporter { 21 21 22 protected final Projection projection;22 /** File extension filter for .geojson files */ 23 23 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter( 24 24 "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 }36 25 37 26 /** … … 39 28 */ 40 29 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); 47 31 } 48 32 … … 50 34 public void exportData(File file, Layer layer) throws IOException { 51 35 if (layer instanceof OsmDataLayer) { 52 String json = new GeoJSONWriter((OsmDataLayer) layer, Utils.firstNonNull(projection, Main.getProjection())).write();53 36 try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { 54 out.write( json);37 out.write(new GeoJSONWriter((OsmDataLayer) layer).write()); 55 38 } 56 39 } else { -
trunk/src/org/openstreetmap/josm/io/GeoJSONWriter.java
r10817 r10852 32 32 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 33 33 import org.openstreetmap.josm.gui.mappaint.ElemStyles; 34 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 34 35 import org.openstreetmap.josm.tools.Pair; 35 36 36 37 /** 37 38 * 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> 38 41 */ 39 42 public class GeoJSONWriter { … … 46 49 * Constructs a new {@code GeoJSONWriter}. 47 50 * @param layer The OSM data layer to save 48 * @ param projection The projection to use for coordinates51 * @since 10852 49 52 */ 50 public GeoJSONWriter(OsmDataLayer layer , Projection projection) {53 public GeoJSONWriter(OsmDataLayer layer) { 51 54 this.layer = layer; 52 this.projection = projection;55 this.projection = ProjectionPreference.wgs84.getProjection(); 53 56 } 54 57 … … 74 77 JsonObjectBuilder object = Json.createObjectBuilder() 75 78 .add("type", "FeatureCollection") 76 .add("crs", Json.createObjectBuilder().add("type", "name").add(77 "properties", Json.createObjectBuilder().add("name", projection.toCode())))78 79 .add("generator", "JOSM"); 79 80 appendLayerBounds(layer.data, object); -
trunk/test/unit/org/openstreetmap/josm/io/GeoJSONWriterTest.java
r10817 r10852 4 4 import static org.junit.Assert.assertEquals; 5 5 import static org.junit.Assert.assertTrue; 6 7 import java.io.FileInputStream; 6 8 7 9 import org.junit.BeforeClass; … … 13 15 import org.openstreetmap.josm.data.osm.Node; 14 16 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 15 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;16 17 import java.io.FileInputStream;18 17 19 18 /** … … 41 40 ds.addPrimitive(node); 42 41 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); 44 43 assertEquals(("" + 45 44 "{\n" + 46 45 " 'type':'FeatureCollection',\n" + 47 " 'crs':{\n" +48 " 'type':'name',\n" +49 " 'properties':{\n" +50 " 'name':'EPSG:4326'\n" +51 " }\n" +52 " },\n" +53 46 " 'generator':'JOSM',\n" + 54 47 " 'features':[\n" + … … 73 66 /** 74 67 * Unit test for multipolygon 68 * @throws Exception if an error occurs 75 69 */ 76 70 @Test … … 79 73 DataSet ds = OsmReader.parseDataSet(in, null); 80 74 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); 82 76 assertTrue(writer.write().contains("MultiPolygon")); 83 77 }
Note:
See TracChangeset
for help on using the changeset viewer.