Changeset 3094 in josm for trunk/src/org/openstreetmap/josm/gui/download/TileSelection.java
- Timestamp:
- 2010-03-07T18:48:56+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/download/TileSelection.java
r3083 r3094 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Dimension; 7 import java.awt.GridBagLayout; 8 import java.awt.event.FocusAdapter; 9 import java.awt.event.FocusEvent; 10 import java.awt.event.FocusListener; 11 12 import javax.swing.JLabel; 13 import javax.swing.JPanel; 14 import javax.swing.JSpinner; 15 import javax.swing.JTextField; 16 import javax.swing.SpinnerNumberModel; 6 import java.beans.PropertyChangeEvent; 7 import java.beans.PropertyChangeListener; 17 8 18 9 import org.openstreetmap.josm.data.Bounds; 19 import org.openstreetmap.josm. data.coor.LatLon;20 import org.openstreetmap.josm. tools.GBC;10 import org.openstreetmap.josm.gui.bbox.BBoxChooser; 11 import org.openstreetmap.josm.gui.bbox.TileSelectionBBoxChooser; 21 12 /** 22 13 * Tile selector. … … 27 18 * 28 19 */ 29 public class TileSelection implements DownloadSelection {20 public class TileSelection implements DownloadSelection, PropertyChangeListener { 30 21 31 private JTextField tileX0 = new JTextField(7); 32 private JTextField tileY0 = new JTextField(7); 33 private JTextField tileX1 = new JTextField(7); 34 private JTextField tileY1 = new JTextField(7); 35 private JSpinner tileZ = new JSpinner(new SpinnerNumberModel(12, 10, 18, 1)); 22 private TileSelectionBBoxChooser chooser; 23 private DownloadDialog parent; 24 25 protected void build() { 26 chooser = new TileSelectionBBoxChooser(); 27 chooser.addPropertyChangeListener(this); 28 } 29 30 public TileSelection() { 31 build(); 32 } 36 33 37 34 public void addGui(final DownloadDialog gui) { 38 39 JPanel smpanel = new JPanel(new GridBagLayout()); 40 smpanel.add(new JLabel(tr("zoom level")), GBC.std().insets(0,0,10,0)); 41 smpanel.add(new JLabel(tr("x from")), GBC.std().insets(10,0,5,0)); 42 smpanel.add(tileX0, GBC.std()); 43 smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 44 smpanel.add(tileX1, GBC.eol()); 45 smpanel.add(tileZ, GBC.std().insets(0,0,10,0)); 46 smpanel.add(new JLabel(tr("y from")), GBC.std().insets(10,0,5,0)); 47 smpanel.add(tileY0, GBC.std()); 48 smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 49 smpanel.add(tileY1, GBC.eol()); 50 51 final FocusListener dialogUpdater = new FocusAdapter() { 52 @Override public void focusLost(FocusEvent e) { 53 try { 54 int zoomlvl = (Integer) tileZ.getValue(); 55 int fromx = Integer.parseInt(tileX0.getText()); 56 int tox = fromx; 57 if (tileX1.getText().length()>0) { 58 tox = Integer.parseInt(tileX1.getText()); 59 } 60 if (tox<fromx) { int i = fromx; fromx=tox; tox=i; } 61 62 int fromy = Integer.parseInt(tileY0.getText()); 63 int toy = fromy; 64 if (tileY1.getText().length()>0) { 65 toy = Integer.parseInt(tileY1.getText()); 66 } 67 if (toy<fromy) { int i = fromy; fromy=toy; toy=i; } 68 69 Bounds b = new Bounds( 70 new LatLon(tileYToLat(zoomlvl, toy + 1), tileXToLon(zoomlvl, fromx)), 71 new LatLon(tileYToLat(zoomlvl, fromy), tileXToLon(zoomlvl, tox + 1)) 72 ); 73 gui.boundingBoxChanged(b, TileSelection.this); 74 //repaint(); 75 } catch (NumberFormatException x) { 76 // ignore 77 } 78 } 79 }; 80 81 for (JTextField f : new JTextField[] { tileX0, tileX1, tileY0, tileY1 }) { 82 f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height)); 83 f.addFocusListener(dialogUpdater); 84 } 85 86 gui.addDownloadAreaSelector(smpanel, tr("Tile Numbers")); 35 gui.addDownloadAreaSelector(chooser, tr("Tile Numbers")); 36 parent = gui; 87 37 } 88 38 89 39 public void setDownloadArea(Bounds area) { 90 if (area == null) 91 return; 92 int z = ((Integer) tileZ.getValue()).intValue(); 93 tileX0.setText(Integer.toString(lonToTileX(z, area.getMin().lon()))); 94 tileX1.setText(Integer.toString(lonToTileX(z, area.getMax().lon()-.00001))); 95 tileY0.setText(Integer.toString(latToTileY(z, area.getMax().lat()-.00001))); 96 tileY1.setText(Integer.toString(latToTileY(z, area.getMin().lat()))); 40 chooser.setBoundingBox(area); 97 41 } 98 42 99 public static int latToTileY(int zoom, double lat) { 100 if ((zoom < 3) || (zoom > 18)) return -1; 101 double l = lat / 180 * Math.PI; 102 double pf = Math.log(Math.tan(l) + (1/Math.cos(l))); 103 return (int) ((1<<(zoom-1)) * (Math.PI - pf) / Math.PI); 104 } 105 106 public static int lonToTileX(int zoom, double lon) { 107 if ((zoom < 3) || (zoom > 18)) return -1; 108 return (int) ((1<<(zoom-3)) * (lon + 180.0) / 45.0); 109 } 110 111 public static double tileYToLat(int zoom, int y) { 112 if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 113 return Math.atan(Math.sinh(Math.PI - (Math.PI*y / (1<<(zoom-1))))) * 180 / Math.PI; 114 } 115 116 public static double tileXToLon(int zoom, int x) { 117 if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 118 return x * 45.0 / (1<<(zoom-3)) - 180.0; 119 43 @Override 44 public void propertyChange(PropertyChangeEvent evt) { 45 if (evt.getPropertyName().equals(BBoxChooser.BBOX_PROP)) { 46 Bounds bbox = (Bounds)evt.getNewValue(); 47 parent.boundingBoxChanged(bbox, this); 48 } 120 49 } 121 50 }
Note:
See TracChangeset
for help on using the changeset viewer.