Changeset 30876 in osm for applications/editors/josm/plugins
- Timestamp:
- 2014-12-26T23:00:35+01:00 (10 years ago)
- Location:
- applications/editors/josm/plugins/ElevationProfile
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/ElevationProfile/build.xml
r30856 r30876 4 4 <property name="commit.message" value="[josm_elevationprofile]"/> 5 5 <!-- enter the *lowest* JOSM version this plugin is currently compatible with --> 6 <property name="plugin.main.version" value="78 24"/>6 <property name="plugin.main.version" value="7886"/> 7 7 8 8 <!-- Configure these properties (replace "..." accordingly). -
applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/ElevationGridLayer.java
r30344 r30876 15 15 import org.openstreetmap.gui.jmapviewer.Tile; 16 16 import org.openstreetmap.gui.jmapviewer.TileController; 17 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;18 17 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener; 19 18 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; … … 32 31 */ 33 32 public class ElevationGridLayer extends Layer implements TileLoaderListener { 34 private static final int ELE_ZOOM_LEVEL = 13; 35 private final IVertexRenderer vertexRenderer; 36 private final MemoryTileCache tileCache; 37 protected TileSource tileSource; 38 protected ElevationGridTileLoader tileLoader; 39 protected TileController tileController; 40 41 private Bounds lastBounds; 42 private TileSet tileSet; 43 44 /** 45 * @param info 46 */ 47 public ElevationGridLayer(String name) { 48 super(name); 49 50 setOpacity(0.8); 51 setBackgroundLayer(true); 52 vertexRenderer = new SimpleVertexRenderer(); 53 54 tileCache = new MemoryTileCache(); 55 tileCache.setCacheSize(500); 56 tileSource = new ElevationGridTileSource(name); 57 tileLoader = new ElevationGridTileLoader(this); 58 tileController = new ElevationGridTileController(tileSource, tileCache, this, tileLoader); 59 } 60 61 @Override 62 public void paint(Graphics2D g, MapView mv, Bounds box) { 63 boolean needsNewTileSet = tileSet == null || (lastBounds == null || !lastBounds.equals(box)); 64 65 if (needsNewTileSet) { 66 tileSet = new TileSet(box.getMin(), box.getMax(), ELE_ZOOM_LEVEL); // we use a vector format with constant zoom level 67 lastBounds = box; 68 System.out.println("paint " + tileSet); 69 } 70 71 if (tileSet.insane()) { 72 myDrawString(g, tr("zoom in to load any tiles"), 120, 120); 73 return; 74 } else if (tileSet.tooLarge()) { 75 myDrawString(g, tr("zoom in to load more tiles"), 120, 120); 76 return; 77 } else if (tileSet.tooSmall()) { 78 myDrawString(g, tr("increase zoom level to see more detail"), 120, 120); 79 return; 80 } 81 82 for(int x = tileSet.x0; x <= tileSet.x1; x++) { 83 for(int y = tileSet.y0; y <= tileSet.y1; y++) { 84 Tile t = tileController.getTile(x, y, ELE_ZOOM_LEVEL); 85 86 if (t != null && t.isLoaded() && t instanceof ElevationGridTile) { 87 ((ElevationGridTile)t).paintTile(g, mv, vertexRenderer); 88 } else { 89 // give some consolation... 90 Point topLeft = mv.getPoint( 91 new LatLon(tileSource.tileYToLat(y, ELE_ZOOM_LEVEL), 92 tileSource.tileXToLon(x, ELE_ZOOM_LEVEL))); 93 t.paint(g, topLeft.x, topLeft.y); 94 } 95 } 96 } 97 } 98 99 @Override 100 public String getToolTipText() { 101 // TODO Auto-generated method stub 102 return null; 103 } 104 105 @Override 106 public void visitBoundingBox(BoundingXYVisitor v) { 107 // TODO Auto-generated method stub 108 109 } 110 111 @Override 112 public Action[] getMenuEntries() { 113 // TODO Auto-generated method stub 114 return null; 115 } 116 117 @Override 118 public void tileLoadingFinished(Tile tile, boolean success) { 119 try { 120 if (Main.map != null) { 121 Main.map.repaint(100); 122 } 123 } catch(Exception ex) { 124 System.err.println(ex); 125 ex.printStackTrace(System.err); 126 } 127 } 128 129 @Override 130 public TileCache getTileCache() { 131 // TODO Auto-generated method stub 132 return tileCache; 133 } 134 135 @Override 136 public Icon getIcon() { 137 return ImageProvider.get("layer", "elevation"); 138 } 139 140 @Override 141 public void mergeFrom(Layer from) { 142 // TODO Auto-generated method stub 143 144 } 145 146 @Override 147 public boolean isMergable(Layer other) { 148 // TODO Auto-generated method stub 149 return false; 150 } 151 152 @Override 153 public Object getInfoComponent() { 154 // TODO Auto-generated method stub 155 return null; 156 } 157 158 159 // Stolen from TMSLayer... 160 void myDrawString(Graphics g, String text, int x, int y) { 161 Color oldColor = g.getColor(); 162 g.setColor(Color.black); 163 g.drawString(text,x+1,y+1); 164 g.setColor(oldColor); 165 g.drawString(text,x,y); 166 } 167 168 private class TileSet { 169 int x0, x1, y0, y1; 170 int tileMax = -1; 171 172 /** 173 * Create a TileSet by known LatLon bbox without layer shift correction 174 */ 175 public TileSet(LatLon topLeft, LatLon botRight, int zoom) { 176 if (zoom == 0) 177 return; 178 179 x0 = (int)tileSource.lonToTileX(topLeft.lon(), zoom); 180 y0 = (int)tileSource.latToTileY(topLeft.lat(), zoom); 181 x1 = (int)tileSource.lonToTileX(botRight.lon(), zoom); 182 y1 = (int)tileSource.latToTileY(botRight.lat(), zoom); 183 if (x0 > x1) { 184 int tmp = x0; 185 x0 = x1; 186 x1 = tmp; 187 } 188 if (y0 > y1) { 189 int tmp = y0; 190 y0 = y1; 191 y1 = tmp; 192 } 193 tileMax = (int)Math.pow(2.0, zoom); 194 if (x0 < 0) { 195 x0 = 0; 196 } 197 if (y0 < 0) { 198 y0 = 0; 199 } 200 if (x1 > tileMax) { 201 x1 = tileMax; 202 } 203 if (y1 > tileMax) { 204 y1 = tileMax; 205 } 206 } 207 208 int size() { 209 int x_span = x1 - x0 + 1; 210 int y_span = y1 - y0 + 1; 211 return x_span * y_span; 212 } 213 214 @Override 215 public String toString() { 216 return "TileSet [x0=" + x0 + ", x1=" + x1 + ", y0=" + y0 + ", y1=" 217 + y1 + ", size()=" + size() + ", tilesSpanned()=" 218 + tilesSpanned() + "]"; 219 } 220 221 double tilesSpanned() { 222 return Math.sqrt(1.0 * this.size()); 223 } 224 225 boolean tooSmall() { 226 return this.tilesSpanned() < 1; 227 } 228 229 boolean tooLarge() { 230 return this.tilesSpanned() > 50; 231 } 232 233 boolean insane() { 234 return this.tilesSpanned() > 200; 235 } 236 } 33 private static final int ELE_ZOOM_LEVEL = 13; 34 private final IVertexRenderer vertexRenderer; 35 private final MemoryTileCache tileCache; 36 protected TileSource tileSource; 37 protected ElevationGridTileLoader tileLoader; 38 protected TileController tileController; 39 40 private Bounds lastBounds; 41 private TileSet tileSet; 42 43 /** 44 * @param info 45 */ 46 public ElevationGridLayer(String name) { 47 super(name); 48 49 setOpacity(0.8); 50 setBackgroundLayer(true); 51 vertexRenderer = new SimpleVertexRenderer(); 52 53 tileCache = new MemoryTileCache(); 54 tileCache.setCacheSize(500); 55 tileSource = new ElevationGridTileSource(name); 56 tileLoader = new ElevationGridTileLoader(this); 57 tileController = new ElevationGridTileController(tileSource, tileCache, this, tileLoader); 58 } 59 60 @Override 61 public void paint(Graphics2D g, MapView mv, Bounds box) { 62 boolean needsNewTileSet = tileSet == null || (lastBounds == null || !lastBounds.equals(box)); 63 64 if (needsNewTileSet) { 65 tileSet = new TileSet(box.getMin(), box.getMax(), ELE_ZOOM_LEVEL); // we use a vector format with constant zoom level 66 lastBounds = box; 67 System.out.println("paint " + tileSet); 68 } 69 70 if (tileSet.insane()) { 71 myDrawString(g, tr("zoom in to load any tiles"), 120, 120); 72 return; 73 } else if (tileSet.tooLarge()) { 74 myDrawString(g, tr("zoom in to load more tiles"), 120, 120); 75 return; 76 } else if (tileSet.tooSmall()) { 77 myDrawString(g, tr("increase zoom level to see more detail"), 120, 120); 78 return; 79 } 80 81 for(int x = tileSet.x0; x <= tileSet.x1; x++) { 82 for(int y = tileSet.y0; y <= tileSet.y1; y++) { 83 Tile t = tileController.getTile(x, y, ELE_ZOOM_LEVEL); 84 85 if (t != null && t.isLoaded() && t instanceof ElevationGridTile) { 86 ((ElevationGridTile)t).paintTile(g, mv, vertexRenderer); 87 } else { 88 // give some consolation... 89 Point topLeft = mv.getPoint( 90 new LatLon(tileSource.tileYToLat(y, ELE_ZOOM_LEVEL), 91 tileSource.tileXToLon(x, ELE_ZOOM_LEVEL))); 92 t.paint(g, topLeft.x, topLeft.y); 93 } 94 } 95 } 96 } 97 98 @Override 99 public String getToolTipText() { 100 // TODO Auto-generated method stub 101 return null; 102 } 103 104 @Override 105 public void visitBoundingBox(BoundingXYVisitor v) { 106 // TODO Auto-generated method stub 107 108 } 109 110 @Override 111 public Action[] getMenuEntries() { 112 // TODO Auto-generated method stub 113 return null; 114 } 115 116 @Override 117 public void tileLoadingFinished(Tile tile, boolean success) { 118 try { 119 if (Main.map != null) { 120 Main.map.repaint(100); 121 } 122 } catch(Exception ex) { 123 System.err.println(ex); 124 ex.printStackTrace(System.err); 125 } 126 } 127 128 @Override 129 public Icon getIcon() { 130 return ImageProvider.get("layer", "elevation"); 131 } 132 133 @Override 134 public void mergeFrom(Layer from) { 135 // TODO Auto-generated method stub 136 137 } 138 139 @Override 140 public boolean isMergable(Layer other) { 141 // TODO Auto-generated method stub 142 return false; 143 } 144 145 @Override 146 public Object getInfoComponent() { 147 // TODO Auto-generated method stub 148 return null; 149 } 150 151 152 // Stolen from TMSLayer... 153 void myDrawString(Graphics g, String text, int x, int y) { 154 Color oldColor = g.getColor(); 155 g.setColor(Color.black); 156 g.drawString(text,x+1,y+1); 157 g.setColor(oldColor); 158 g.drawString(text,x,y); 159 } 160 161 private class TileSet { 162 int x0, x1, y0, y1; 163 int tileMax = -1; 164 165 /** 166 * Create a TileSet by known LatLon bbox without layer shift correction 167 */ 168 public TileSet(LatLon topLeft, LatLon botRight, int zoom) { 169 if (zoom == 0) 170 return; 171 172 x0 = (int)tileSource.lonToTileX(topLeft.lon(), zoom); 173 y0 = (int)tileSource.latToTileY(topLeft.lat(), zoom); 174 x1 = (int)tileSource.lonToTileX(botRight.lon(), zoom); 175 y1 = (int)tileSource.latToTileY(botRight.lat(), zoom); 176 if (x0 > x1) { 177 int tmp = x0; 178 x0 = x1; 179 x1 = tmp; 180 } 181 if (y0 > y1) { 182 int tmp = y0; 183 y0 = y1; 184 y1 = tmp; 185 } 186 tileMax = (int)Math.pow(2.0, zoom); 187 if (x0 < 0) { 188 x0 = 0; 189 } 190 if (y0 < 0) { 191 y0 = 0; 192 } 193 if (x1 > tileMax) { 194 x1 = tileMax; 195 } 196 if (y1 > tileMax) { 197 y1 = tileMax; 198 } 199 } 200 201 int size() { 202 int x_span = x1 - x0 + 1; 203 int y_span = y1 - y0 + 1; 204 return x_span * y_span; 205 } 206 207 @Override 208 public String toString() { 209 return "TileSet [x0=" + x0 + ", x1=" + x1 + ", y0=" + y0 + ", y1=" 210 + y1 + ", size()=" + size() + ", tilesSpanned()=" 211 + tilesSpanned() + "]"; 212 } 213 214 double tilesSpanned() { 215 return Math.sqrt(1.0 * this.size()); 216 } 217 218 boolean tooSmall() { 219 return this.tilesSpanned() < 1; 220 } 221 222 boolean tooLarge() { 223 return this.tilesSpanned() > 50; 224 } 225 226 boolean insane() { 227 return this.tilesSpanned() > 200; 228 } 229 } 237 230 }
Note:
See TracChangeset
for help on using the changeset viewer.