- Timestamp:
- 2015-10-01T21:28:35+02:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
r8510 r8813 4 4 import java.io.File; 5 5 import java.util.ArrayList; 6 import java.util.Arrays; 6 7 import java.util.Collections; 7 8 import java.util.Comparator; … … 47 48 importers = new ArrayList<>(); 48 49 49 String[] importerNames = {50 "org.openstreetmap.josm.io.OsmImporter",51 "org.openstreetmap.josm.io.OsmGzipImporter",52 "org.openstreetmap.josm.io.OsmZipImporter",53 "org.openstreetmap.josm.io.OsmChangeImporter",54 "org.openstreetmap.josm.io.GpxImporter",55 "org.openstreetmap.josm.io.NMEAImporter",56 "org.openstreetmap.josm.io.NoteImporter",57 "org.openstreetmap.josm.io.OsmBzip2Importer",58 "org.openstreetmap.josm.io.JpgImporter",59 "org.openstreetmap.josm.io.WMSLayerImporter",60 "org.openstreetmap.josm.io.AllFormatsImporter",61 "org.openstreetmap.josm.io.session.SessionImporter"62 };63 64 for ( String classname: importerNames) {50 final List<Class<? extends FileImporter>> importerNames = Arrays.asList( 51 org.openstreetmap.josm.io.OsmImporter.class, 52 org.openstreetmap.josm.io.OsmGzipImporter.class, 53 org.openstreetmap.josm.io.OsmZipImporter.class, 54 org.openstreetmap.josm.io.OsmChangeImporter.class, 55 org.openstreetmap.josm.io.GpxImporter.class, 56 org.openstreetmap.josm.io.NMEAImporter.class, 57 org.openstreetmap.josm.io.NoteImporter.class, 58 org.openstreetmap.josm.io.OsmBzip2Importer.class, 59 org.openstreetmap.josm.io.JpgImporter.class, 60 org.openstreetmap.josm.io.WMSLayerImporter.class, 61 org.openstreetmap.josm.io.AllFormatsImporter.class, 62 org.openstreetmap.josm.io.session.SessionImporter.class 63 ); 64 65 for (final Class<? extends FileImporter> importerClass : importerNames) { 65 66 try { 66 FileImporter importer = (FileImporter) Class.forName(classname).newInstance();67 FileImporter importer = importerClass.newInstance(); 67 68 importers.add(importer); 68 69 MapView.addLayerChangeListener(importer); … … 92 93 exporters = new ArrayList<>(); 93 94 94 String[] exporterNames = { 95 "org.openstreetmap.josm.io.GpxExporter", 96 "org.openstreetmap.josm.io.OsmExporter", 97 "org.openstreetmap.josm.io.OsmGzipExporter", 98 "org.openstreetmap.josm.io.OsmBzip2Exporter", 99 "org.openstreetmap.josm.io.GeoJSONExporter", 100 "org.openstreetmap.josm.io.WMSLayerExporter", 101 "org.openstreetmap.josm.io.NoteExporter" 102 }; 103 104 for (String classname : exporterNames) { 95 final List<Class<? extends FileExporter>> exporterClasses = Arrays.asList( 96 org.openstreetmap.josm.io.GpxExporter.class, 97 org.openstreetmap.josm.io.OsmExporter.class, 98 org.openstreetmap.josm.io.OsmGzipExporter.class, 99 org.openstreetmap.josm.io.OsmBzip2Exporter.class, 100 org.openstreetmap.josm.io.GeoJSONExporter.CurrentProjection.class, // needs to be considered earlier than GeoJSONExporter 101 org.openstreetmap.josm.io.GeoJSONExporter.class, 102 org.openstreetmap.josm.io.WMSLayerExporter.class, 103 org.openstreetmap.josm.io.NoteExporter.class 104 ); 105 106 for (final Class<? extends FileExporter> exporterClass : exporterClasses) { 105 107 try { 106 FileExporter exporter = (FileExporter) Class.forName(classname).newInstance();108 FileExporter exporter = exporterClass.newInstance(); 107 109 exporters.add(exporter); 108 110 MapView.addLayerChangeListener(exporter); -
trunk/src/org/openstreetmap/josm/io/GeoJSONExporter.java
r7937 r8813 10 10 import java.nio.file.Files; 11 11 12 import org.openstreetmap.josm.Main; 12 13 import org.openstreetmap.josm.actions.ExtensionFileFilter; 14 import org.openstreetmap.josm.data.projection.Projection; 13 15 import org.openstreetmap.josm.gui.layer.Layer; 14 16 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 17 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 18 import org.openstreetmap.josm.tools.Utils; 15 19 16 20 public class GeoJSONExporter extends FileExporter { 17 21 22 protected final Projection projection; 18 23 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter( 19 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)"); 20 27 21 28 /** 22 * Constructs a new {@code GeoJSONExporter}. 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 37 /** 38 * Constructs a new {@code GeoJSONExporter} with WGS84 projection. 23 39 */ 24 40 public GeoJSONExporter() { 25 super(FILE_FILTER); 41 this(FILE_FILTER, ProjectionPreference.wgs84.getProjection()); 42 } 43 44 private GeoJSONExporter(ExtensionFileFilter fileFilter, Projection projection) { 45 super(fileFilter); 46 this.projection = projection; 26 47 } 27 48 … … 29 50 public void exportData(File file, Layer layer) throws IOException { 30 51 if (layer instanceof OsmDataLayer) { 31 String json = new GeoJSONWriter((OsmDataLayer) layer ).write();52 String json = new GeoJSONWriter((OsmDataLayer) layer, Utils.firstNonNull(projection, Main.getProjection())).write(); 32 53 try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { 33 54 out.write(json); -
trunk/src/org/openstreetmap/josm/io/GeoJSONWriter.java
r8510 r8813 15 15 16 16 import org.openstreetmap.josm.data.Bounds; 17 import org.openstreetmap.josm.data.coor.EastNorth; 17 18 import org.openstreetmap.josm.data.coor.LatLon; 18 19 import org.openstreetmap.josm.data.osm.DataSet; … … 24 25 import org.openstreetmap.josm.data.osm.Way; 25 26 import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor; 27 import org.openstreetmap.josm.data.projection.Projection; 26 28 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 27 29 … … 31 33 public class GeoJSONWriter { 32 34 33 private OsmDataLayer layer; 35 private final OsmDataLayer layer; 36 private final Projection projection; 34 37 private static final boolean skipEmptyNodes = true; 35 38 … … 37 40 * Constructs a new {@code GeoJSONWriter}. 38 41 * @param layer The OSM data layer to save 42 * @param projection The projection to use for coordinates 39 43 */ 40 public GeoJSONWriter(OsmDataLayer layer ) {44 public GeoJSONWriter(OsmDataLayer layer, Projection projection) { 41 45 this.layer = layer; 46 this.projection = projection; 42 47 } 43 48 … … 63 68 JsonObjectBuilder object = Json.createObjectBuilder() 64 69 .add("type", "FeatureCollection") 70 .add("crs", Json.createObjectBuilder().add("type", "name").add("name", projection.toCode())) 65 71 .add("generator", "JOSM"); 66 72 appendLayerBounds(layer.data, object); … … 71 77 } 72 78 73 private staticclass GeometryPrimitiveVisitor implements PrimitiveVisitor {79 private class GeometryPrimitiveVisitor implements PrimitiveVisitor { 74 80 75 81 private final JsonObjectBuilder geomObj; … … 84 90 LatLon ll = n.getCoor(); 85 91 if (ll != null) { 86 geomObj.add("coordinates", getCoorArray( n.getCoor()));92 geomObj.add("coordinates", getCoorArray(Json.createArrayBuilder(), n.getCoor())); 87 93 } 88 94 } … … 96 102 LatLon ll = n.getCoor(); 97 103 if (ll != null) { 98 array.add(getCoorArray( ll));104 array.add(getCoorArray(Json.createArrayBuilder(), ll)); 99 105 } 100 106 } … … 106 112 public void visit(IRelation r) { 107 113 } 108 109 private JsonArrayBuilder getCoorArray(LatLon c) {110 return Json.createArrayBuilder().add(c.lon()).add(c.lat());111 }112 114 } 113 115 114 protected static void appendPrimitive(OsmPrimitive p, JsonArrayBuilder array) { 116 private JsonArrayBuilder getCoorArray(JsonArrayBuilder builder, LatLon c) { 117 return getCoorArray(builder, projection.latlon2eastNorth(c)); 118 } 119 120 private JsonArrayBuilder getCoorArray(JsonArrayBuilder builder, EastNorth c) { 121 return builder.add(c.getX()).add(c.getY()); 122 } 123 124 protected void appendPrimitive(OsmPrimitive p, JsonArrayBuilder array) { 115 125 if (p.isIncomplete()) { 116 126 return; … … 136 146 } 137 147 138 protected staticvoid appendLayerBounds(DataSet ds, JsonObjectBuilder object) {148 protected void appendLayerBounds(DataSet ds, JsonObjectBuilder object) { 139 149 if (ds != null) { 140 150 Iterator<Bounds> it = ds.getDataSourceBounds().iterator(); … … 149 159 } 150 160 151 protected staticvoid appendBounds(Bounds b, JsonObjectBuilder object) {161 protected void appendBounds(Bounds b, JsonObjectBuilder object) { 152 162 if (b != null) { 153 object.add("bbox", Json.createArrayBuilder() 154 .add(b.getMinLon()).add(b.getMinLat()) 155 .add(b.getMaxLon()).add(b.getMaxLat())); 163 JsonArrayBuilder builder = Json.createArrayBuilder(); 164 getCoorArray(builder, b.getMin()); 165 getCoorArray(builder, b.getMax()); 166 object.add("bbox", builder); 156 167 } 157 168 } 158 169 159 protected staticvoid appendLayerFeatures(DataSet ds, JsonObjectBuilder object) {170 protected void appendLayerFeatures(DataSet ds, JsonObjectBuilder object) { 160 171 JsonArrayBuilder array = Json.createArrayBuilder(); 161 172 if (ds != null) {
Note:
See TracChangeset
for help on using the changeset viewer.