Changeset 12778 in osm for applications/editors/josm/plugins/grid
- Timestamp:
- 2009-01-01T18:28:53+01:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/grid/src/grid
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/grid/src/grid/GridLayer.java
r1544 r12778 45 45 */ 46 46 public class GridLayer extends Layer { 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 // 65 // 66 // 67 68 // 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 // 167 168 169 170 171 172 // 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 47 48 private static Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(GridPlugin.class.getResource("/images/grid.png"))); 49 private LatLon origin, pole; 50 private float gridunits; 51 private boolean drawLabels; 52 private Helmert gridtoworld; //worldtogrid; 53 private Color majcol = Color.RED; 54 public double a, b, c; 55 public GridLayer(String url) { 56 super(url.indexOf('/') != -1 ? url.substring(url.indexOf('/')+1) : url); 57 origin = new LatLon(0.0,0.0); 58 pole = new LatLon(0.0,90.0); 59 drawLabels = true; 60 gridtoworld = new Helmert(0.0, 0.0, 0); 61 //worldtogrid = new Helmert(0.0, 0.0, 0); 62 } 63 64 // private void setGrid(LatLon origin, LatLon pole){ 65 // this.origin = origin; 66 // this.pole = pole; 67 //need to check pole is perpendicular from origin; 68 // } 69 private void setGrid(double a, double b, double c){ 70 System.out.println("Setting grid to :" + a + ", " + b + ", " + c); 71 this.origin = origin; 72 this.pole = pole; 73 //need to chech pole is perpendicular from origin; 74 this.a=a; 75 this.b=b; 76 this.c=c; 77 gridtoworld = new Helmert(a, b, c); 78 System.out.println(new LatLon(10,10) + "->" + 79 gridtoworld.transform(new LatLon(10,10)) + "->" + 80 gridtoworld.inverseTransform(gridtoworld.transform(new LatLon(10,10)))); 81 //worldtogrid = new Helmert(-a, -b, -c); 82 } 83 84 private void setUnits(float units){ 85 gridunits = units; 86 } 87 88 private void setUnitsToLatLon(){ 89 gridunits = 0; 90 } 91 92 93 private void toggleLabels(){ 94 drawLabels=!drawLabels; 95 } 96 97 private class toggleLabelsAction extends AbstractAction { 98 GridLayer layer; 99 public toggleLabelsAction(GridLayer layer) { 100 super("show labels"); 101 this.layer = layer; 102 } 103 public void actionPerformed(ActionEvent e) { 104 layer.toggleLabels(); 105 Main.map.repaint(); 106 } 107 } 108 109 private class setWorldAction extends AbstractAction { 110 GridLayer layer; 111 public setWorldAction(GridLayer layer) { 112 super("set to world"); 113 this.layer = layer; 114 } 115 public void actionPerformed(ActionEvent e) { 116 layer.setGrid(0,0,0); 117 Main.map.repaint(); 118 } 119 } 120 private class incAAction extends AbstractAction { 121 GridLayer layer; 122 public incAAction(GridLayer layer) { 123 super("increase a"); 124 this.layer = layer; 125 } 126 public void actionPerformed(ActionEvent e) { 127 layer.setGrid(layer.a+10,layer.b,layer.c); 128 Main.map.repaint(); 129 } 130 } 131 private class incBAction extends AbstractAction { 132 GridLayer layer; 133 public incBAction(GridLayer layer) { 134 super("increase b"); 135 this.layer = layer; 136 } 137 public void actionPerformed(ActionEvent e) { 138 layer.setGrid(layer.a,layer.b+10,layer.c); 139 Main.map.repaint(); 140 } 141 } 142 private class incCAction extends AbstractAction { 143 GridLayer layer; 144 public incCAction(GridLayer layer) { 145 super("increase c"); 146 this.layer = layer; 147 } 148 public void actionPerformed(ActionEvent e) { 149 layer.setGrid(layer.a,layer.b,layer.c+10); 150 Main.map.repaint(); 151 } 152 } 153 private class setColorAction extends AbstractAction { 154 GridLayer layer; 155 public setColorAction(GridLayer layer) { 156 super("Customize Color", ImageProvider.get("colorchooser")); 157 this.layer = layer; 158 } 159 public void actionPerformed(ActionEvent e) { 160 String col=ColorHelper.color2html(layer.majcol); 161 JColorChooser c = new JColorChooser(ColorHelper.html2color(col)); 162 Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")}; 163 int answer = JOptionPane.showOptionDialog(Main.parent, c, tr("Choose a color"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); 164 switch (answer) { 165 case 0: 166 // Main.pref.put("color.layer "+name, ColorHelper.color2html(c.getColor())); 167 majcol = c.getColor(); 168 break; 169 case 1: 170 return; 171 case 2: 172 // Main.pref.put("color.layer "+name, null); 173 majcol = Color.RED; 174 break; 175 } 176 Main.map.repaint(); 177 } 178 } 179 180 private class setGridLayoutAction extends AbstractAction { 181 GridLayer layer; 182 public setGridLayoutAction(GridLayer layer) { 183 super("Set grid origin"); 184 this.layer = layer; 185 } 186 public void actionPerformed(ActionEvent e) { 187 NumberFormat nf = NumberFormat.getInstance(); 188 JPanel p = new JPanel(new GridBagLayout()); 189 JTextField latText = new JTextField(nf.format(layer.a)); 190 JTextField lonText = new JTextField(nf.format(layer.b)); 191 JTextField devText = new JTextField(nf.format(layer.c)); 192 p.add(new JLabel(tr("Grid origin location")), GBC.eol()); 193 p.add(new JLabel(tr("Latitude"))); 194 p.add(latText, GBC.eol()); 195 p.add(new JLabel(tr("Longitude"))); 196 p.add(lonText, GBC.eol()); 197 p.add(new JLabel(tr("Grid rotation"))); 198 p.add(devText, GBC.eol()); 199 Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("World")}; 200 int answer = JOptionPane.showOptionDialog(Main.parent, p,tr("Grid layout"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); 201 switch (answer) { 202 case 0: 203 layer.setGrid( 204 -Double.parseDouble(latText.getText()), 205 -Double.parseDouble(lonText.getText()), 206 Double.parseDouble(devText.getText())); 207 208 case 1: 209 return; 210 case 2: 211 layer.setGrid(0,0,0); 212 } 213 Main.map.repaint(); 214 } 215 } 216 217 @Override public Icon getIcon() { 218 return icon; 219 } 220 221 @Override public String getToolTipText() { 222 return tr("Grid layer:" + a + "," + b + "," + c); 223 } 224 225 @Override public boolean isMergable(Layer other) { 226 return false; 227 } 228 229 @Override public void mergeFrom(Layer from) { 230 } 231 232 @Override public void paint(Graphics g, final MapView mv) { 233 //establish viewport size 234 int w = mv.getWidth(); 235 int h = mv.getHeight(); 236 237 //establish viewport world coordinates 238 238 LatLon tl = mv.getLatLon(0,0); 239 239 LatLon br = mv.getLatLon(w,h); 240 240 241 //establish max visible world coordinates 241 //establish max visible world coordinates 242 242 double wminlat = Math.max(Math.min(tl.lat(),br.lat()),-Main.proj.MAX_LAT); 243 243 double wmaxlat = Math.min(Math.max(tl.lat(),br.lat()), Main.proj.MAX_LAT); 244 244 double wminlon = Math.max(Math.min(tl.lon(),br.lon()),-Main.proj.MAX_LON); 245 245 double wmaxlon = Math.min(Math.max(tl.lon(),br.lon()), Main.proj.MAX_LON); 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 // 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 } 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 246 247 //establish viewport grid coordinates 248 //because grid is arbitrarily orientated and may be curved check several points on border 249 double minlat = 180, maxlat=-180, minlon=90, maxlon=-90; 250 251 for(double x=0;x<=1;x+=0.2){ 252 LatLon[] p = new LatLon[] { 253 gridtoworld.inverseTransform(new LatLon(wminlat, x*wminlon+(1-x)*wmaxlon)), 254 gridtoworld.inverseTransform(new LatLon(wmaxlat, x*wminlon+(1-x)*wmaxlon)), 255 gridtoworld.inverseTransform(new LatLon(x*wminlat+(1-x)*wmaxlat, wminlon)), 256 gridtoworld.inverseTransform(new LatLon(x*wminlat+(1-x)*wmaxlat, wmaxlon))}; 257 for(int i=0;i<4;i++){ 258 maxlat=Math.max(p[i].lat(),maxlat); 259 minlat=Math.min(p[i].lat(),minlat); 260 maxlon=Math.max(p[i].lon(),maxlon); 261 minlon=Math.min(p[i].lon(),minlon); 262 } 263 } 264 265 //also check if the singularities are visible 266 LatLon northpole = gridtoworld.transform(new LatLon(90,0)); 267 LatLon southpole = gridtoworld.transform(new LatLon(-90,0)); 268 if((northpole.lat()>=wminlat) && (northpole.lat()<=wmaxlat) && (northpole.lon()>=wminlon) && (northpole.lon()<=wmaxlon)){ 269 maxlat=90; 270 minlon=-180; 271 maxlon=180; 272 } 273 if((southpole.lat()>=wminlat) && (southpole.lat()<=wmaxlat) && (southpole.lon()>=wminlon) && (southpole.lon()<=wmaxlon)){ 274 minlat=-90; 275 minlon=-180; 276 maxlon=180; 277 } 278 279 //span is maximum lat/lon span across visible grid normalised to 1600pixels 280 double latspan = (maxlat-minlat) * 1600.0/Math.max(h,w); 281 double lonspan = (maxlon-minlon) * 1600.0/Math.max(h,w); 282 283 //grid spacing is power of ten to use for grid interval. 284 double latspacing = Math.pow(10,Math.floor(Math.log(latspan)/Math.log(10.0))-1.0); 285 double lonspacing = Math.pow(10,Math.floor(Math.log(lonspan)/Math.log(10.0))-1.0); 286 if (Math.max(latspan,lonspan)/Math.min(latspan,lonspan)<4){ 287 lonspacing = latspacing = Math.max(latspacing,lonspacing); 288 } 289 double latmaj = (latspacing>=10)?3:10; 290 double lonmaj = (lonspacing>=10)?3:10; 291 292 //set up stuff need to draw grid 293 NumberFormat nf = NumberFormat.getInstance(); 294 Color mincol = (majcol.darker()).darker(); 295 296 g.setFont (new Font("Helvetica", Font.PLAIN, 8)); 297 FontMetrics fm = g.getFontMetrics(); 298 // g.setWidth(0); 299 for(double lat=latspacing*Math.floor(minlat/latspacing);lat<maxlat;lat+=latspacing){ 300 for(double lon=lonspacing*Math.floor(minlon/lonspacing);lon<maxlon;lon+=lonspacing){ 301 LatLon ll0, lli, llj; 302 ll0 = gridtoworld.transform(new LatLon(lat,lon)); 303 lli = gridtoworld.transform(new LatLon(lat+latspacing,lon)); 304 llj = gridtoworld.transform(new LatLon(lat,lon+lonspacing)); 305 Point p0=mv.getPoint(Main.proj.latlon2eastNorth(ll0)); 306 Point pi=mv.getPoint(Main.proj.latlon2eastNorth(lli)); 307 Point pj=mv.getPoint(Main.proj.latlon2eastNorth(llj)); 308 309 if(Math.round(lon/lonspacing)%lonmaj==0) 310 g.setColor(majcol); 311 else 312 g.setColor(mincol); 313 314 drawGridLine(g, mv, ll0, lli); 315 316 317 if(Math.round(lat/latspacing)%latmaj==0) 318 g.setColor(majcol); 319 else 320 g.setColor(mincol); 321 322 drawGridLine(g, mv, ll0, llj); 323 324 if((Math.round(lon/lonspacing))%lonmaj==0 && (Math.round(lat/latspacing))%latmaj==0 && drawLabels){ 325 String label = nf.format(lat); 326 int tw = fm.stringWidth(label); 327 g.drawString(label,p0.x-tw,p0.y-8); 328 label = nf.format(lon); 329 g.drawString(label,p0.x+2,p0.y+8); 330 } 331 } 332 } 333 334 } 335 336 private void drawGridLine(Graphics g, final MapView mv, LatLon ll0, LatLon ll1){ 337 Point p0=mv.getPoint(Main.proj.latlon2eastNorth(ll0)); 338 Point p1=mv.getPoint(Main.proj.latlon2eastNorth(ll1)); 339 340 if(Math.abs(ll0.lon()-ll1.lon())<180){ 341 g.drawLine(p0.x,p0.y,p1.x,p1.y); 342 } else { 343 double lat0, lat1, lon0, lon1, latm; 344 lon0 = ll0.lon(); 345 lon1 = ll1.lon(); 346 if(lon0<0) lon0+=360; 347 if(lon1<0) lon1+=360; 348 latm = ll0.lat() + (180-lon0)*(ll1.lat()-ll0.lat())/(lon1-lon0); 349 Point pm1 = mv.getPoint(Main.proj.latlon2eastNorth(new LatLon(latm,180))); 350 Point pm2 = mv.getPoint(Main.proj.latlon2eastNorth(new LatLon(latm,-180))); 351 if(lon0<=180){ 352 g.drawLine(p0.x,p0.y,pm1.x,pm1.y); 353 g.drawLine(p1.x,p1.y,pm2.x,pm2.y); 354 } else { 355 g.drawLine(p0.x,p0.y,pm2.x,pm2.y); 356 g.drawLine(p1.x,p1.y,pm1.x,pm1.y); 357 } 358 } 359 } 360 361 @Override public void visitBoundingBox(BoundingXYVisitor v) { 362 // doesn't have a bounding box 363 } 364 365 @Override public Object getInfoComponent() { 366 return getToolTipText(); 367 } 368 369 @Override public Component[] getMenuEntries() { 370 return new Component[]{ 371 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 372 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 373 new JMenuItem(new toggleLabelsAction(this)), 374 new JSeparator(), 375 new JMenuItem(new setGridLayoutAction(this)), 376 new JMenuItem(new setWorldAction(this)), 377 new JMenuItem(new incAAction(this)), 378 new JMenuItem(new incBAction(this)), 379 new JMenuItem(new incCAction(this)), 380 new JMenuItem(new setColorAction(this)), 381 new JSeparator(), 382 new JMenuItem(new LayerListPopup.InfoAction(this))}; 383 } 384 384 } -
applications/editors/josm/plugins/grid/src/grid/GridPlugin.java
r1484 r12778 25 25 26 26 private class Action extends AbstractAction { 27 28 29 30 31 32 33 34 27 public Action() { 28 super("add grid"); 29 } 30 public void actionPerformed(ActionEvent e) { 31 GridLayer gridLayer = new GridLayer(""); 32 if (gridLayer != null) 33 Main.main.addLayer(gridLayer); 34 } 35 35 } 36 36 private JMenu edit; 37 37 private JMenuItem addGridMenu = new JMenuItem(new Action()); 38 38 39 39 public GridPlugin() { 40 JMenuBar menu = Main.main.menu; 41 edit = null; 42 for (int i = 0; i < menu.getMenuCount(); ++i) { 43 if (menu.getMenu(i) != null && tr("Edit").equals(menu.getMenu(i).getName())) { 44 edit = menu.getMenu(i); 45 break; 46 } 47 } 48 if (edit == null) { 49 edit = new JMenu(tr("Edit")); 50 menu.add(edit, 2); 51 edit.setVisible(false); 52 } 53 edit.add(addGridMenu); 54 addGridMenu.setVisible(false); 40 JMenuBar menu = Main.main.menu; 41 edit = null; 42 for (int i = 0; i < menu.getMenuCount(); ++i) { 43 if (menu.getMenu(i) != null && tr("Edit").equals(menu.getMenu(i).getName())) { 44 edit = menu.getMenu(i); 45 break; 46 } 55 47 } 56 @Override 57 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 58 if (oldFrame != null && newFrame == null) { 59 // disable 60 addGridMenu.setVisible(false); 61 if (edit.getMenuComponentCount() == 1) 62 edit.setVisible(false); 63 } else if (oldFrame == null && newFrame != null) { 64 // enable 65 addGridMenu.setVisible(true); 66 if (edit.getMenuComponentCount() == 1) 67 edit.setVisible(true); 68 } 69 } 48 if (edit == null) { 49 edit = new JMenu(tr("Edit")); 50 menu.add(edit, 2); 51 edit.setVisible(false); 52 } 53 edit.add(addGridMenu); 54 addGridMenu.setVisible(false); 55 } 56 @Override 57 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 58 if (oldFrame != null && newFrame == null) { 59 // disable 60 addGridMenu.setVisible(false); 61 if (edit.getMenuComponentCount() == 1) 62 edit.setVisible(false); 63 } else if (oldFrame == null && newFrame != null) { 64 // enable 65 addGridMenu.setVisible(true); 66 if (edit.getMenuComponentCount() == 1) 67 edit.setVisible(true); 68 } 69 } 70 70 71 71 }
Note:
See TracChangeset
for help on using the changeset viewer.