Changeset 16294 in osm for applications/editors/josm/plugins/slippymap
- Timestamp:
- 2009-07-03T23:35:00+02:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/slippymap
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/slippymap/build.xml
r15707 r16294 26 26 <attribute name="Plugin-Description" value="Displays a slippy map grid in JOSM. Can load tiles from slippy map as background and request updates."/> 27 27 <attribute name="Plugin-Link" value="http://wiki.openstreetmap.org/index.php/JOSM/Plugins/SlippyMap"/> 28 <attribute name="Plugin-Mainversion" value="1 646"/>28 <attribute name="Plugin-Mainversion" value="1725"/> 29 29 <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/> 30 30 </manifest> -
applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapLayer.java
r16164 r16294 46 46 * @author LuVar <lubomir.varga@freemap.sk> 47 47 * @author Dave Hansen <dave@sr71.net> 48 * 48 * 49 49 */ 50 50 public class SlippyMapLayer extends Layer implements ImageObserver, … … 131 131 } 132 132 })); 133 133 134 134 tileOptionMenu.add(new JMenuItem( 135 136 137 138 139 140 141 142 143 135 new AbstractAction(tr("Flush Tile Cache")) { 136 public void actionPerformed(ActionEvent ae) { 137 System.out.print("flushing all tiles..."); 138 for (SlippyMapTile t : tileStorage.values()) { 139 t.dropImage(); 140 } 141 System.out.println("done"); 142 } 143 })); 144 144 // end of adding menu commands 145 145 … … 176 176 /** 177 177 * Zoom in, go closer to map. 178 * 179 * @return 178 * 179 * @return true, if zoom increasing was successfull, false othervise 180 180 */ 181 181 public boolean increaseZoomLevel() { … … 194 194 /** 195 195 * Zoom out from map. 196 * 197 * @return 196 * 197 * @return true, if zoom increasing was successfull, false othervise 198 198 */ 199 199 public boolean decreaseZoomLevel() { … … 271 271 272 272 synchronized SlippyMapTile getTile(int x, int y, int zoom) { 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 273 SlippyMapKey key = new SlippyMapKey(x, y, zoom); 274 if (!key.valid) { 275 key = null; 276 } 277 return tileStorage.get(key); 278 } 279 280 synchronized SlippyMapTile putTile(SlippyMapTile tile, int x, int y, int zoom) { 281 SlippyMapKey key = new SlippyMapKey(x, y, zoom); 282 if (!key.valid) { 283 key = null; 284 } 285 return tileStorage.put(key, tile); 286 } 287 288 synchronized SlippyMapTile getOrCreateTile(int x, int y, int zoom) { 289 SlippyMapTile tile = getTile(x, y, zoom); 290 if (tile != null) { 291 return tile; 292 } 293 tile = new SlippyMapTile(x, y, zoom); 294 putTile(tile, x, y, zoom); 295 return tile; 296 } 297 298 298 void loadAllTiles() { 299 299 MapView mv = Main.map.mapView; … … 302 302 303 303 TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel); 304 304 305 305 // if there is more than 18 tiles on screen in any direction, do not 306 306 // load all tiles! 307 307 if (ts.tilesSpanned() > (18*18)) { 308 309 308 System.out.println("Not downloading all tiles because there is more than 18 tiles on an axis!"); 309 return; 310 310 } 311 311 312 312 for (Tile t : ts.allTiles()) { 313 314 315 316 313 SlippyMapTile tile = getOrCreateTile(t.x, t.y, currentZoomLevel); 314 if (tile.getImage() == null) { 315 this.loadSingleTile(tile); 316 } 317 317 }//end of for Tile t 318 318 } 319 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 320 /* 321 * Attempt to approximate how much the image is being scaled. For instance, 322 * a 100x100 image being scaled to 50x50 would return 0.25. 323 */ 324 Image lastScaledImage = null; 325 326 double getImageScaling(Image img, Point p0, Point p1) { 327 int realWidth = img.getWidth(this); 328 int realHeight = img.getHeight(this); 329 if (realWidth == -1 || realHeight == -1) { 330 /* 331 * We need a good image against which to work. If 332 * the current one isn't loaded, then try the last one. 333 * Should be good enough. If we've never seen one, then 334 * guess. 335 */ 336 if (lastScaledImage != null) { 337 return getImageScaling(lastScaledImage, p0, p1); 338 } 339 realWidth = 256; 340 realHeight = 256; 341 } else { 342 lastScaledImage = img; 343 } 344 /* 345 * If the zoom scale gets really, really off, these can get into 346 * the millions, so make this a double to prevent integer 347 * overflows. 348 */ 349 double drawWidth = p1.x - p0.x; 350 double drawHeight = p1.x - p0.x; 351 // stem.out.println("drawWidth: " + drawWidth + " drawHeight: " + 352 // drawHeight); 353 354 double drawArea = drawWidth * drawHeight; 355 355 double realArea = realWidth * realHeight; 356 356 … … 358 358 } 359 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 return mv.getPoint(Main.proj.latlon2eastNorth(tmpLL));477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 360 boolean imageLoaded(Image i) { 361 if (i == null) 362 return false; 363 364 int status = Toolkit.getDefaultToolkit().checkImage(i, -1, -1, this); 365 if ((status & ALLBITS) != 0) 366 return true; 367 return false; 368 } 369 370 Double paintTileImages(Graphics g, TileSet ts, int zoom) { 371 Double imageScale = null; 372 Image img = null; 373 for (Tile t : ts.allTiles()) { 374 SlippyMapTile tile = getTile(t.x, t.y, zoom); 375 if (tile == null) { 376 // Don't trigger tile loading if this isn't 377 // the exact zoom level we're looking for 378 if (zoom != currentZoomLevel) 379 continue; 380 tile = getOrCreateTile(t.x, t.y, zoom); 381 if (SlippyMapPreferences.getAutoloadTiles()) 382 loadSingleTile(tile); 383 } 384 img = tile.getImage(); 385 if (img == null) 386 continue; 387 if ((zoom != currentZoomLevel) && !tile.isDownloaded()) 388 continue; 389 Point p = t.pixelPos(zoom); 390 /* 391 * We need to get a box in which to draw, so advance by one tile in 392 * each direction to find the other corner of the box 393 */ 394 Tile t2 = new Tile(t.x + 1, t.y + 1); 395 Point p2 = t2.pixelPos(zoom); 396 397 g.drawImage(img, p.x, p.y, p2.x - p.x, p2.y - p.y, this); 398 if (img == null) 399 continue; 400 if (imageScale == null && zoom == currentZoomLevel) 401 imageScale = new Double(getImageScaling(img, p, p2)); 402 float fadeBackground = SlippyMapPreferences.getFadeBackground(); 403 if (fadeBackground != 0f) { 404 // dimm by painting opaque rect... 405 g.setColor(new Color(1f, 1f, 1f, fadeBackground)); 406 g.fillRect(p.x, p.y, p2.x - p.x, p2.y - p.y); 407 } 408 }// end of for 409 return imageScale; 410 } 411 412 void paintTileText(TileSet ts, Graphics g, MapView mv, int zoom, Tile t) { 413 int fontHeight = g.getFontMetrics().getHeight(); 414 415 SlippyMapTile tile = getTile(t.x, t.y, zoom); 416 if (tile == null) { 417 return; 418 } 419 if (tile.getImage() == null) { 420 loadSingleTile(tile); 421 } 422 Point p = t.pixelPos(zoom); 423 int texty = p.y + 2 + fontHeight; 424 425 if (SlippyMapPreferences.getDrawDebug()) { 426 g.drawString("x=" + t.x + " y=" + t.y + " z=" + zoom + "", p.x + 2, texty); 427 texty += 1 + fontHeight; 428 if ((t.x % 32 == 0) && (t.y % 32 == 0)) { 429 g.drawString("x=" + t.x / 32 + " y=" + t.y / 32 + " z=7", p.x + 2, texty); 430 texty += 1 + fontHeight; 431 } 432 }// end of if draw debug 433 434 String md = tile.getMetadata(); 435 if (md != null) { 436 g.drawString(md, p.x + 2, texty); 437 texty += 1 + fontHeight; 438 } 439 440 Image tileImage = tile.getImage(); 441 if (tileImage == null) { 442 g.drawString(tr("image not loaded"), p.x + 2, texty); 443 texty += 1 + fontHeight; 444 } 445 if (!imageLoaded(tileImage)) { 446 g.drawString(tr("image loading..."), p.x + 2, texty); 447 needRedraw = true; 448 Main.map.repaint(100); 449 texty += 1 + fontHeight; 450 } 451 452 if (SlippyMapPreferences.getDrawDebug()) { 453 if (ts.leftTile(t)) { 454 if (t.y % 32 == 31) { 455 g.fillRect(0, p.y - 1, mv.getWidth(), 3); 456 } else { 457 g.drawLine(0, p.y, mv.getWidth(), p.y); 458 } 459 } 460 }// /end of if draw debug 461 } 462 463 private class Tile { 464 public int x; 465 public int y; 466 467 Tile(int x, int y) { 468 this.x = x; 469 this.y = y; 470 } 471 472 public Point pixelPos(int zoom) { 473 double lon = tileXToLon(this.x, zoom); 474 LatLon tmpLL = new LatLon(tileYToLat(this.y, zoom), lon); 475 MapView mv = Main.map.mapView; 476 return mv.getPoint(tmpLL); 477 } 478 } 479 480 private class TileSet { 481 int z12x0, z12x1, z12y0, z12y1; 482 int zoom; 483 484 TileSet(LatLon topLeft, LatLon botRight, int zoom) { 485 this.zoom = zoom; 486 z12x0 = lonToTileX(topLeft.lon(), zoom); 487 z12x1 = lonToTileX(botRight.lon(), zoom); 488 z12y0 = latToTileY(topLeft.lat(), zoom); 489 z12y1 = latToTileY(botRight.lat(), zoom); 490 if (z12x0 > z12x1) { 491 int tmp = z12x0; 492 z12x0 = z12x1; 493 z12x1 = tmp; 494 } 495 if (z12y0 > z12y1) { 496 int tmp = z12y0; 497 z12y0 = z12y1; 498 z12y1 = tmp; 499 } 500 } 501 502 int tilesSpanned() { 503 int x_span = z12x1 - z12x0; 504 int y_span = z12y1 - z12y0; 505 return x_span * y_span; 506 } 507 508 /* 509 * This is pretty silly. Should probably just be implemented as an 510 * iterator to keep from having to instantiate all these tiles. 511 */ 512 List<Tile> allTiles() 513 513 { 514 514 List<Tile> ret = new ArrayList<Tile>(); … … 526 526 } 527 527 528 529 530 531 532 533 534 535 536 537 538 539 540 528 boolean topTile(Tile t) { 529 if (t.y == z12y0 - 1) 530 return true; 531 return false; 532 } 533 534 boolean leftTile(Tile t) { 535 if (t.x == z12x0 - 1) 536 return true; 537 return false; 538 } 539 } 540 541 541 /** 542 542 */ 543 543 @Override 544 544 public void paint(Graphics g, MapView mv) { 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 545 long start = System.currentTimeMillis(); 546 LatLon topLeft = mv.getLatLon(0, 0); 547 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight()); 548 Graphics oldg = g; 549 550 if (botRight.lon() == 0.0 || botRight.lat() == 0) { 551 // probably still initializing 552 return; 553 } 554 if (lastTopLeft != null && lastBotRight != null && topLeft.equalsEpsilon(lastTopLeft) 555 && botRight.equalsEpsilon(lastBotRight) && bufferImage != null 556 && mv.getWidth() == bufferImage.getWidth(null) && mv.getHeight() == bufferImage.getHeight(null) 557 && !needRedraw) { 558 559 g.drawImage(bufferImage, 0, 0, null); 560 return; 561 } 562 563 needRedraw = false; 564 lastTopLeft = topLeft; 565 lastBotRight = botRight; 566 bufferImage = mv.createImage(mv.getWidth(), mv.getHeight()); 567 g = bufferImage.getGraphics(); 568 569 TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel); 570 int zoom = currentZoomLevel; 571 572 if (ts.tilesSpanned() > (18*18)) { 573 System.out.println("too many tiles, decreasing zoom from " + currentZoomLevel); 574 if (decreaseZoomLevel()) { 575 this.paint(oldg, mv); 576 } 577 return; 578 }//end of if more than 18*18 579 580 if (ts.tilesSpanned() <= 0) { 581 System.out.println("doesn't even cover one tile, increasing zoom from " + currentZoomLevel); 582 if (increaseZoomLevel()) { 583 this.paint(oldg, mv); 584 } 585 return; 586 } 587 588 int fontHeight = g.getFontMetrics().getHeight(); 589 590 g.setColor(Color.DARK_GRAY); 591 592 float fadeBackground = SlippyMapPreferences.getFadeBackground(); 593 594 /* 595 * Go looking for tiles in zoom levels *other* than the current 596 * one. Even if they might look bad, they look better than a 597 * blank tile. 598 */ 599 int otherZooms[] = {2, -2, 1, -1}; 600 for (int zoomOff : otherZooms) { 601 int zoom2 = currentZoomLevel + zoomOff; 602 if ((zoom <= 0) || (zoom > SlippyMapPreferences.getMaxZoomLvl())) { 603 continue; 604 } 605 TileSet ts2 = new TileSet(topLeft, botRight, zoom2); 606 this.paintTileImages(g, ts2, zoom2); 607 } 608 /* 609 * Save this for last since it will get painted over all the others 610 */ 611 Double imageScale = this.paintTileImages(g, ts, currentZoomLevel); 612 g.setColor(Color.red); 613 614 for (Tile t : ts.allTiles()) { 615 // This draws the vertical lines for the entire 616 // column. Only draw them for the top tile in 617 // the column. 618 if (ts.topTile(t)) { 619 Point p = t.pixelPos(currentZoomLevel); 620 if (SlippyMapPreferences.getDrawDebug()) { 621 if (t.x % 32 == 0) { 622 // level 7 tile boundary 623 g.fillRect(p.x - 1, 0, 3, mv.getHeight()); 624 } else { 625 g.drawLine(p.x, 0, p.x, mv.getHeight()); 626 } 627 } 628 } 629 this.paintTileText(ts, g, mv, currentZoomLevel, t); 630 } 631 632 oldg.drawImage(bufferImage, 0, 0, null); 633 634 if (imageScale != null) { 635 // If each source image pixel is being stretched into > 3 636 // drawn pixels, zoom in... getting too pixelated 637 if (imageScale > 3) { 638 if (SlippyMapPreferences.getAutozoom()) { 639 Main.debug("autozoom increase: scale: " + imageScale); 640 increaseZoomLevel(); 641 } 642 this.paint(oldg, mv); 643 } 644 645 // If each source image pixel is being squished into > 0.32 646 // of a drawn pixels, zoom out. 647 if (imageScale < 0.32) { 648 if (SlippyMapPreferences.getAutozoom()) { 649 Main.debug("autozoom decrease: scale: " + imageScale); 650 decreaseZoomLevel(); 651 } 652 this.paint(oldg, mv); 653 } 654 } 655 g.setColor(Color.black); 656 g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120); 657 }// end of paint method 658 658 659 659 /** … … 662 662 */ 663 663 SlippyMapTile getTileForPixelpos(int px, int py) { 664 665 666 667 668 669 670 671 664 MapView mv = Main.map.mapView; 665 Point clicked = new Point(px, py); 666 LatLon topLeft = mv.getLatLon(0, 0); 667 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight()); 668 TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel); 669 int z = currentZoomLevel; 670 671 Tile clickedTile = null; 672 672 for (Tile t1 : ts.allTiles()) { 673 673 Tile t2 = new Tile(t1.x+1, t1.y+1); … … 735 735 736 736 private int lonToTileX(double lon, int zoom) { 737 737 return (int) (Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0); 738 738 } 739 739 740 740 private double tileYToLat(int y, int zoom) { 741 741 return Math.atan(Math.sinh(Math.PI 742 742 - (Math.PI * y / Math.pow(2.0, zoom - 1)))) 743 743 * 180 / Math.PI; 744 744 } 745 745 746 746 private double tileXToLon(int x, int zoom) { 747 747 return x * 45.0 / Math.pow(2.0, zoom - 3) - 180.0; 748 748 } 749 749 750 750 private SlippyMapTile imgToTile(Image img) { 751 752 753 754 755 756 757 758 759 760 761 762 763 751 // we use the enumeration to avoid ConcurrentUpdateExceptions 752 // with other users of the tileStorage 753 Enumeration<SlippyMapTile> e = tileStorage.elements(); 754 while (e.hasMoreElements()) { 755 SlippyMapTile t = e.nextElement(); 756 if (t.getImageNoTimestamp() != img) { 757 continue; 758 } 759 return t; 760 } 761 return null; 762 } 763 764 764 private static int nr_loaded = 0; 765 765 private static int at_zoom = -1; 766 766 767 767 public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 768 boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0); 769 SlippyMapTile imageTile = imgToTile(img); 770 if (imageTile == null) { 771 return false; 772 } 773 774 if ((infoflags & ERROR) != 0) { 775 String url; // = "unknown"; 776 url = imageTile.getImageURL().toString(); 777 System.err.println("imageUpdate(" + img + ") error " + url + ")"); 778 } 779 if (((infoflags & ALLBITS) != 0)) { 780 int z = imageTile.getZoom(); 781 if (z == at_zoom) { 782 nr_loaded++; 783 } else { 784 System.out.println("downloaded " + nr_loaded + " at: " + at_zoom + " now going to " + z); 785 nr_loaded = 0; 786 at_zoom = z; 787 } 788 imageTile.markAsDownloaded(); 789 } 790 if ((infoflags & SOMEBITS) != 0) { 791 // if (y%100 == 0) 792 //System.out.println("imageUpdate("+img+") SOMEBITS ("+x+","+y+")"); 793 } 794 // Repaint immediately if we are done, otherwise batch up 795 // repaint requests every 100 milliseconds 796 needRedraw = true; 797 Main.map.repaint(done ? 0 : 100); 798 return !done; 799 } 800 800 801 801 /*
Note:
See TracChangeset
for help on using the changeset viewer.