Changeset 12486 in josm
- Timestamp:
- 2017-07-22T14:25:19+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/SessionLoadAction.java
r11848 r12486 20 20 21 21 import org.openstreetmap.josm.Main; 22 import org.openstreetmap.josm.data.ViewportData;23 22 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 24 23 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 25 24 import org.openstreetmap.josm.gui.layer.Layer; 25 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 26 26 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 27 27 import org.openstreetmap.josm.gui.util.FileFilterAllFiles; … … 30 30 import org.openstreetmap.josm.io.session.SessionImporter; 31 31 import org.openstreetmap.josm.io.session.SessionReader; 32 import org.openstreetmap.josm.io.session.SessionReader.SessionProjectionChoiceData; 33 import org.openstreetmap.josm.io.session.SessionReader.SessionViewportData; 32 34 import org.openstreetmap.josm.tools.CheckParameterUtil; 33 35 import org.openstreetmap.josm.tools.JosmRuntimeException; … … 73 75 private Layer active; 74 76 private List<Runnable> postLoadTasks; 75 private ViewportData viewport; 77 private SessionViewportData viewport; 78 private SessionProjectionChoiceData projectionChoice; 76 79 77 80 /** … … 116 119 if (canceled) 117 120 return; 121 if (projectionChoice != null) { 122 ProjectionPreference.setProjection( 123 projectionChoice.getProjectionChoiceId(), 124 projectionChoice.getSubPreferences(), 125 false); 126 } 118 127 addLayers(); 119 128 runPostLoadTasks(); … … 132 141 Main.getLayerManager().setActiveLayer(active); 133 142 } 134 if (noMap ) {135 Main.map.mapView.scheduleZoomTo(viewport );143 if (noMap && viewport != null) { 144 Main.map.mapView.scheduleZoomTo(viewport.getEastNorthViewport(Main.getProjection())); 136 145 } 137 146 } … … 169 178 postLoadTasks = reader.getPostLoadTasks(); 170 179 viewport = reader.getViewport(); 180 projectionChoice = reader.getProjectionChoice(); 171 181 } finally { 172 182 if (tempFile) { -
trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java
r12485 r12486 501 501 */ 502 502 private ProjectionChoice setupProjectionCombo() { 503 String pcId = projectionChoice != null ? projectionChoice : PROP_PROJECTION_DEFAULT.get();503 String pcId = getCurrentProjectionChoiceId(); 504 504 ProjectionChoice pc = null; 505 505 for (int i = 0; i < projectionCombo.getItemCount(); ++i) { 506 506 ProjectionChoice pc1 = projectionCombo.getItemAt(i); 507 pc1.setPreferences(getSubprojectionPreference(pc1 ));507 pc1.setPreferences(getSubprojectionPreference(pc1.getId())); 508 508 if (pc1.getId().equals(pcId)) { 509 509 projectionCombo.setSelectedIndex(i); … … 524 524 } 525 525 526 private static Collection<String> getSubprojectionPreference(ProjectionChoice pc) { 527 return Main.pref.getCollection("projection.sub."+pc.getId(), null); 526 /** 527 * Get the id of the projection choice that is currently set. 528 * @return id of the projection choice that is currently set 529 */ 530 public static String getCurrentProjectionChoiceId() { 531 return projectionChoice != null ? projectionChoice : PROP_PROJECTION_DEFAULT.get(); 532 } 533 534 /** 535 * Get the preferences that have been selected the last time for the given 536 * projection choice. 537 * @param pcId id of the projection choice 538 * @return projection choice parameters that have been selected by the user 539 * the last time; null if user has never selected the given projection choice 540 */ 541 public static Collection<String> getSubprojectionPreference(String pcId) { 542 return Main.pref.getCollection("projection.sub."+pcId, null); 528 543 } 529 544 -
trunk/src/org/openstreetmap/josm/io/session/SessionReader.java
r12279 r12486 16 16 import java.nio.charset.StandardCharsets; 17 17 import java.util.ArrayList; 18 import java.util.Collection; 18 19 import java.util.Collections; 19 20 import java.util.Enumeration; … … 43 44 import org.openstreetmap.josm.io.Compression; 44 45 import org.openstreetmap.josm.io.IllegalDataException; 46 import org.openstreetmap.josm.tools.CheckParameterUtil; 45 47 import org.openstreetmap.josm.tools.JosmRuntimeException; 46 48 import org.openstreetmap.josm.tools.MultiMap; … … 58 60 public class SessionReader { 59 61 62 /** 63 * Data class for projection saved in the session file. 64 */ 65 public static class SessionProjectionChoiceData { 66 private final String projectionChoiceId; 67 private final Collection<String> subPreferences; 68 69 /** 70 * Construct a new SessionProjectionChoiceData. 71 * @param projectionChoiceId projection choice id 72 * @param subPreferences parameters for the projection choice 73 */ 74 public SessionProjectionChoiceData(String projectionChoiceId, Collection<String> subPreferences) { 75 this.projectionChoiceId = projectionChoiceId; 76 this.subPreferences = subPreferences; 77 } 78 79 /** 80 * Get the projection choice id. 81 * @return the projection choice id 82 */ 83 public String getProjectionChoiceId() { 84 return projectionChoiceId; 85 } 86 87 /** 88 * Get the parameters for the projection choice 89 * @return parameters for the projection choice 90 */ 91 public Collection<String> getSubPreferences() { 92 return subPreferences; 93 } 94 } 95 96 /** 97 * Data class for viewport saved in the session file. 98 */ 99 public static class SessionViewportData { 100 private final LatLon center; 101 private final double meterPerPixel; 102 103 /** 104 * Construct a new SessionViewportData. 105 * @param center the lat/lon coordinates of the screen center 106 * @param meterPerPixel scale in meters per pixel 107 */ 108 public SessionViewportData(LatLon center, double meterPerPixel) { 109 CheckParameterUtil.ensureParameterNotNull(center); 110 this.center = center; 111 this.meterPerPixel = meterPerPixel; 112 } 113 114 /** 115 * Get the lat/lon coordinates of the screen center. 116 * @return lat/lon coordinates of the screen center 117 */ 118 public LatLon getCenter() { 119 return center; 120 } 121 122 /** 123 * Get the scale in meters per pixel. 124 * @return scale in meters per pixel 125 */ 126 public double getScale() { 127 return meterPerPixel; 128 } 129 130 /** 131 * Convert this viewport data to a {@link ViewportData} object (with projected coordinates). 132 * @param proj the projection to convert from lat/lon to east/north 133 * @return the corresponding ViewportData object 134 */ 135 public ViewportData getEastNorthViewport(Projection proj) { 136 EastNorth centerEN = proj.latlon2eastNorth(center); 137 // Get a "typical" distance in east/north units that 138 // corresponds to a couple of pixels. Shouldn't be too 139 // large, to keep it within projection bounds and 140 // not too small to avoid rounding errors. 141 double dist = 0.01 * proj.getDefaultZoomInPPD(); 142 LatLon ll1 = proj.eastNorth2latlon(new EastNorth(centerEN.east() - dist, centerEN.north())); 143 LatLon ll2 = proj.eastNorth2latlon(new EastNorth(centerEN.east() + dist, centerEN.north())); 144 double meterPerEasting = ll1.greatCircleDistance(ll2) / dist / 2; 145 double scale = meterPerPixel / meterPerEasting; // unit: easting per pixel 146 return new ViewportData(centerEN, scale); 147 } 148 } 149 60 150 private static final Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>(); 61 151 … … 66 156 private int active = -1; 67 157 private final List<Runnable> postLoadTasks = new ArrayList<>(); 68 private ViewportData viewport; 158 private SessionViewportData viewport; 159 private SessionProjectionChoiceData projectionChoice; 69 160 70 161 static { … … 130 221 /** 131 222 * Return the viewport (map position and scale). 132 * @return The viewport. Can be null when no viewport info is found in the file.133 */ 134 public ViewportData getViewport() {223 * @return the viewport; can be null when no viewport info is found in the file 224 */ 225 public SessionViewportData getViewport() { 135 226 return viewport; 227 } 228 229 /** 230 * Return the projection choice data. 231 * @return the projection; can be null when no projection info is found in the file 232 */ 233 public SessionProjectionChoiceData getProjectionChoice() { 234 return projectionChoice; 136 235 } 137 236 … … 339 438 } 340 439 341 Element viewportEl = getElementByTagName(root, "viewport"); 342 if (viewportEl != null) { 343 EastNorth center = null; 344 Element centerEl = getElementByTagName(viewportEl, "center"); 345 if (centerEl != null && centerEl.hasAttribute("lat") && centerEl.hasAttribute("lon")) { 346 try { 347 LatLon centerLL = new LatLon(Double.parseDouble(centerEl.getAttribute("lat")), 348 Double.parseDouble(centerEl.getAttribute("lon"))); 349 center = Projections.project(centerLL); 350 } catch (NumberFormatException ex) { 351 Main.warn(ex); 352 } 353 } 354 if (center != null) { 355 Element scaleEl = getElementByTagName(viewportEl, "scale"); 356 if (scaleEl != null && scaleEl.hasAttribute("meter-per-pixel")) { 357 try { 358 double meterPerPixel = Double.parseDouble(scaleEl.getAttribute("meter-per-pixel")); 359 Projection proj = Main.getProjection(); 360 // Get a "typical" distance in east/north units that 361 // corresponds to a couple of pixels. Shouldn't be too 362 // large, to keep it within projection bounds and 363 // not too small to avoid rounding errors. 364 double dist = 0.01 * proj.getDefaultZoomInPPD(); 365 LatLon ll1 = proj.eastNorth2latlon(new EastNorth(center.east() - dist, center.north())); 366 LatLon ll2 = proj.eastNorth2latlon(new EastNorth(center.east() + dist, center.north())); 367 double meterPerEasting = ll1.greatCircleDistance(ll2) / dist / 2; 368 double scale = meterPerPixel / meterPerEasting; // unit: easting per pixel 369 viewport = new ViewportData(center, scale); 370 } catch (NumberFormatException ex) { 371 Main.warn(ex); 372 } 373 } 374 } 375 } 440 viewport = readViewportData(root); 441 projectionChoice = readProjectionChoiceData(root); 376 442 377 443 Element layersEl = getElementByTagName(root, "layers"); … … 550 616 } 551 617 618 private SessionViewportData readViewportData(Element root) { 619 Element viewportEl = getElementByTagName(root, "viewport"); 620 if (viewportEl == null) return null; 621 LatLon center = null; 622 Element centerEl = getElementByTagName(viewportEl, "center"); 623 if (centerEl == null || !centerEl.hasAttribute("lat") || !centerEl.hasAttribute("lon")) return null; 624 try { 625 center = new LatLon(Double.parseDouble(centerEl.getAttribute("lat")), 626 Double.parseDouble(centerEl.getAttribute("lon"))); 627 } catch (NumberFormatException ex) { 628 Main.warn(ex); 629 } 630 if (center == null) return null; 631 Element scaleEl = getElementByTagName(viewportEl, "scale"); 632 if (scaleEl == null || !scaleEl.hasAttribute("meter-per-pixel")) return null; 633 try { 634 double scale = Double.parseDouble(scaleEl.getAttribute("meter-per-pixel")); 635 return new SessionViewportData(center, scale); 636 } catch (NumberFormatException ex) { 637 Main.warn(ex); 638 return null; 639 } 640 } 641 642 private SessionProjectionChoiceData readProjectionChoiceData(Element root) { 643 Element projectionEl = getElementByTagName(root, "projection"); 644 if (projectionEl == null) return null; 645 Element projectionChoiceEl = getElementByTagName(projectionEl, "projection-choice"); 646 if (projectionChoiceEl == null) return null; 647 Element idEl = getElementByTagName(projectionChoiceEl, "id"); 648 if (idEl == null) return null; 649 String id = idEl.getTextContent(); 650 Element parametersEl = getElementByTagName(projectionChoiceEl, "parameters"); 651 if (parametersEl == null) return null; 652 Collection<String> parameters = new ArrayList<>(); 653 NodeList paramNl = parametersEl.getElementsByTagName("param"); 654 for (int i=0; i<paramNl.getLength(); i++) { 655 Element paramEl = (Element) paramNl.item(i); 656 parameters.add(paramEl.getTextContent()); 657 } 658 return new SessionProjectionChoiceData(id, parameters); 659 } 660 552 661 /** 553 662 * Show Dialog when there is an error for one layer. -
trunk/src/org/openstreetmap/josm/io/session/SessionWriter.java
r11795 r12486 11 11 import java.nio.charset.StandardCharsets; 12 12 import java.util.ArrayList; 13 import java.util.Collection; 13 14 import java.util.HashMap; 14 15 import java.util.List; … … 40 41 import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer; 41 42 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 43 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 42 44 import org.openstreetmap.josm.tools.JosmRuntimeException; 43 45 import org.openstreetmap.josm.tools.MultiMap; … … 213 215 doc.appendChild(root); 214 216 215 Element viewportEl = doc.createElement("viewport"); 216 root.appendChild(viewportEl); 217 Element centerEl = doc.createElement("center"); 218 viewportEl.appendChild(centerEl); 219 EastNorth center = Main.map.mapView.getCenter(); 220 LatLon centerLL = Projections.inverseProject(center); 221 centerEl.setAttribute("lat", Double.toString(centerLL.lat())); 222 centerEl.setAttribute("lon", Double.toString(centerLL.lon())); 223 Element scale = doc.createElement("scale"); 224 viewportEl.appendChild(scale); 225 double dist100px = Main.map.mapView.getDist100Pixel(); 226 scale.setAttribute("meter-per-pixel", Double.toString(dist100px / 100)); 217 writeViewPort(root); 218 writeProjection(root); 227 219 228 220 Element layersEl = doc.createElement("layers"); … … 263 255 } 264 256 257 private void writeViewPort(Element root) { 258 Document doc = root.getOwnerDocument(); 259 Element viewportEl = doc.createElement("viewport"); 260 root.appendChild(viewportEl); 261 Element centerEl = doc.createElement("center"); 262 viewportEl.appendChild(centerEl); 263 EastNorth center = Main.map.mapView.getCenter(); 264 LatLon centerLL = Projections.inverseProject(center); 265 centerEl.setAttribute("lat", Double.toString(centerLL.lat())); 266 centerEl.setAttribute("lon", Double.toString(centerLL.lon())); 267 Element scale = doc.createElement("scale"); 268 viewportEl.appendChild(scale); 269 double dist100px = Main.map.mapView.getDist100Pixel(); 270 scale.setAttribute("meter-per-pixel", Double.toString(dist100px / 100)); 271 } 272 273 private void writeProjection(Element root) { 274 Document doc = root.getOwnerDocument(); 275 Element projectionEl = doc.createElement("projection"); 276 root.appendChild(projectionEl); 277 String pcId = ProjectionPreference.getCurrentProjectionChoiceId(); 278 Element projectionChoiceEl = doc.createElement("projection-choice"); 279 projectionEl.appendChild(projectionChoiceEl); 280 Element idEl = doc.createElement("id"); 281 projectionChoiceEl.appendChild(idEl); 282 idEl.setTextContent(pcId); 283 Collection<String> parameters = ProjectionPreference.getSubprojectionPreference(pcId); 284 Element parametersEl = doc.createElement("parameters"); 285 projectionChoiceEl.appendChild(parametersEl); 286 for (String param : parameters) { 287 Element paramEl = doc.createElement("param"); 288 parametersEl.appendChild(paramEl); 289 paramEl.setTextContent(param); 290 } 291 String code = Main.getProjection().toCode(); 292 Element codeEl = doc.createElement("code"); 293 projectionEl.appendChild(codeEl); 294 codeEl.setTextContent(code); 295 } 296 265 297 /** 266 298 * Writes given .jos document to an output stream.
Note:
See TracChangeset
for help on using the changeset viewer.