Changeset 30738 in osm for applications/editors/josm/plugins/canvec_helper/src
- Timestamp:
- 2014-10-19T01:27:04+02:00 (10 years ago)
- Location:
- applications/editors/josm/plugins/canvec_helper/src/org/openstreetmap/josm/plugins/canvec_helper
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/canvec_helper/src/org/openstreetmap/josm/plugins/canvec_helper/CanVecTile.java
r30737 r30738 23 23 24 24 public class CanVecTile { 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 238 239 240 241 242 243 244 245 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 25 CanvecLayer layer; 26 public boolean can_download = false; 27 private ArrayList<String> sub_tile_ids = new ArrayList<>(); 28 private boolean zip_scanned = false; 29 30 private ArrayList<CanVecTile> sub_tiles = new ArrayList<>(); 31 private boolean sub_tiles_made = false; 32 33 private ArrayList<String> index; 34 private int depth; 35 36 int corda,cordc; 37 private boolean valid = false; 38 String cordb,cordd; 39 private Bounds bounds; 40 public String tileid; 41 public CanVecTile(String tileid,CanvecLayer layer) { 42 String parta,partb,partc,partd; 43 parta = tileid.substring(0,3); 44 partb = tileid.substring(3, 4); 45 partc = tileid.substring(4, 6); 46 partd = tileid.substring(6); 47 int a,c; 48 a = Integer.parseInt(parta); 49 c = Integer.parseInt(partc); 50 real_init(a,partb,c,partd,layer,new ArrayList<String>()); 51 } 52 public CanVecTile(int a,String b,int c,String d,CanvecLayer layer,ArrayList<String> index) { 53 real_init(a,b,c,d,layer,index); 54 } 55 public void real_init(int a,String b,int c,String d,CanvecLayer layer, ArrayList<String> index) { 56 this.index = index; 57 this.layer = layer; 58 corda = a; 59 cordb = b; 60 cordc = c; 61 cordd = d; 62 double zero_point_lat,zero_point_lon; 63 double lat_span,lon_span; 64 double lat2,lon2; 65 if ((a >= 0) && (a <= 119)) { // main block of tiles 66 int column = a / 10; 67 int row = a % 10; 68 if (row > 6) { 69 // cant handle x7 x8 and x9 yet 70 return; 71 } 72 zero_point_lat = 40 + 4 * row; 73 zero_point_lon = -56 - 8 * column; 74 75 // size of each grid 76 if (row <= 6) { 77 // each is 4x8 degrees, broken into a 4x4 grid 78 lat_span = 4; 79 lon_span = 8; 80 depth = 1; 81 } else { 82 return; 83 } 84 } else { // last few tiles, very far north 85 return; 86 } 87 88 // a 4x4 grid of A thru P 89 // map A-P to 1-16 90 int grid2; 91 if (b == "") grid2 = 0; 92 else grid2 = b.charAt(0) - 64; 93 int rows1[] = { 0, 0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3 }; 94 int cols1[] = { 0, 3,2,1,0, 0,1,2,3, 3,2,1,0, 0,1,2,3 }; 95 lat2 = zero_point_lat + (lat_span/4)*rows1[grid2]; 96 lon2 = zero_point_lon + (lon_span/4)*cols1[grid2]; 97 98 if (grid2 != 0) { 99 lat_span = lat_span / 4; 100 lon_span = lon_span / 4; 101 depth = 2; 102 } 103 104 int rows3[] = { 0, 0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3 }; 105 lat2 = lat2 + (lat_span/4)*rows3[c]; 106 int cols3[] = { 0, 3,2,1,0, 0,1,2,3, 3,2,1,0, 0,1,2,3 }; 107 lon2 = lon2 + (lon_span/4)*cols3[c]; 108 109 if (c != 0) { 110 lat_span = lat_span / 4; 111 lon_span = lon_span / 4; 112 depth = 3; 113 } 114 115 if (cordd != "") { 116 depth = 4; 117 System.out.println("cordd: "+cordd); 118 String foo[] = cordd.split("\\."); 119 for (int i = 0; i < foo.length; i++) { 120 int cell; 121 System.out.println(foo[i]); 122 if (foo[i] == "osm") break; 123 if (foo[i] == "") continue; 124 try { 125 cell = Integer.parseInt(foo[i]); 126 } catch (NumberFormatException e) { 127 continue; 128 } 129 switch (cell) { 130 case 0: 131 break; 132 case 1: 133 lat2 = lat2 + lat_span/2; 134 break; 135 case 2: 136 lat2 = lat2 + lat_span/2; 137 lon2 = lon2 + lon_span/2; 138 break; 139 case 3: 140 lon2 = lon2 + lon_span/2; 141 break; 142 } 143 lat_span = lat_span/2; 144 lon_span = lon_span/2; 145 } 146 } 147 148 bounds = new Bounds(lat2,lon2,lat2+lat_span,lon2+lon_span); 149 if (cordb == "") this.tileid = String.format("%03d",corda); 150 else if (cordc == 0) this.tileid = String.format("%03d%s",corda,cordb); 151 else if (cordd == "") this.tileid = String.format("%03d%s%02d",corda,cordb,cordc); 152 else this.tileid = String.format("%03d%s%02d%s",corda,cordb,cordc,cordd); 153 valid = true; 154 //debug(index.toString()); 155 //debug("creating tileid: "+this.tileid); 156 } 157 public boolean isValid() { return valid; } 158 public String getTileId() { 159 return this.tileid; 160 } 161 private void debug(String line) { 162 System.out.println(depth + "_" + tileid + ": " + line); 163 } 164 public boolean isVisible(Bounds view) { 165 return view.intersects(bounds); 166 } 167 public Point[] getCorners(MapView mv) { 168 LatLon min = bounds.getMin(); 169 LatLon max = bounds.getMax(); 170 LatLon x1 = new LatLon(min.lat(),max.lon()); 171 LatLon x2 = new LatLon(max.lat(),min.lon()); 172 return new Point[] { 173 mv.getPoint(min), // south west 174 mv.getPoint(x1), 175 mv.getPoint(max), 176 mv.getPoint(x2) // north west 177 }; 178 } 179 public String getDownloadUrl() { 180 return String.format("http://ftp2.cits.rncan.gc.ca/OSM/pub/%1$03d/%2$s/%1$03d%2$s%3$02d.zip",corda,cordb,cordc); 181 } 182 private ZipFile open_zip() throws IOException { 183 File download_path = new File(layer.plugin_self.getPluginDir() + File.separator); 184 download_path.mkdir(); 185 CachedFile tile_zip = new CachedFile(getDownloadUrl()).setDestDir(download_path.toString()); 186 return new ZipFile(tile_zip.getFile()); 187 } 188 public void downloadSelf() { 189 if (zip_scanned) return; 190 ZipFile zipFile; 191 try { 192 zipFile = open_zip(); 193 } catch (IOException e) { 194 e.printStackTrace(); 195 return; 196 } 197 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 198 while (entries.hasMoreElements()) { 199 ZipEntry entry = entries.nextElement(); 200 if (entry.getName().equals("Metadata.txt")) continue; 201 sub_tile_ids.add(entry.getName()); 202 zip_scanned = true; 203 CanVecTile final_tile = new CanVecTile(entry.getName(),layer); 204 if (final_tile.isValid()) sub_tiles.add(final_tile); 205 } 206 } 207 public void load_raw_osm() { 208 ZipFile zipFile; 209 try { 210 zipFile = open_zip(); 211 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 212 while (entries.hasMoreElements()) { 213 ZipEntry entry = entries.nextElement(); 214 if (tileid.equals(entry.getName())) { 215 debug("found myself!"); 216 InputStream rawtile = zipFile.getInputStream(entry); 217 OsmImporter importer = new OsmImporter(); 218 debug("loading raw osm"); 219 OsmImporterData temp = importer.loadLayer(rawtile, null, entry.getName(), null); 220 Main.worker.submit(temp.getPostLayerTask()); 221 Main.main.addLayer(temp.getLayer()); 222 temp.getLayer().data.setUploadDiscouraged(false); 223 } 224 } 225 } catch (IOException e) { 226 e.printStackTrace(); 227 return; 228 } catch (IllegalDataException e) { 229 e.printStackTrace(); 230 return; 231 } 232 } 233 private void make_sub_tiles(int layer) { 234 ArrayList<String> buffer = new ArrayList<>(); 235 Pattern p; 236 if (sub_tiles_made) return; 237 switch (layer) { 238 case 1: 239 p = Pattern.compile("\\d\\d\\d([A-Z]).*"); 240 String last_cell = ""; 241 for (int i = 0; i < index.size(); i++) { 242 Matcher m = p.matcher(index.get(i)); 243 m.matches(); 244 245 String cell = m.group(1); 246 if (cell.equals(last_cell)) { 247 buffer.add(m.group(0)); 248 } else if (last_cell == "") { 249 buffer.add(m.group(0)); 250 } else { 251 sub_tiles.add(new CanVecTile(corda,last_cell,0,"",this.layer,buffer)); 252 buffer = new ArrayList<>(); 253 buffer.add(m.group(0)); 254 } 255 last_cell = cell; 256 } 257 sub_tiles.add(new CanVecTile(corda,last_cell,0,"",this.layer,buffer)); 258 break; 259 case 2: 260 p = Pattern.compile("\\d\\d\\d[A-Z](\\d\\d).*"); 261 int last_cell2 = -1; 262 for (int i = 0; i < index.size(); i++) { 263 Matcher m = p.matcher(index.get(i)); 264 m.matches(); 265 266 int cell = Integer.parseInt(m.group(1)); 267 if (cell == last_cell2) { 268 buffer.add(m.group(0)); 269 } else if (last_cell2 == -1) { 270 buffer.add(m.group(0)); 271 } else { 272 sub_tiles.add(new CanVecTile(corda,cordb,last_cell2,"",this.layer,buffer)); 273 buffer = new ArrayList<>(); 274 buffer.add(m.group(0)); 275 } 276 last_cell2 = cell; 277 } 278 if (last_cell2 != -1) sub_tiles.add(new CanVecTile(corda,cordb,last_cell2,"",this.layer,buffer)); 279 break; 280 } 281 sub_tiles_made = true; 282 } 283 public void paint(Graphics2D g, MapView mv, Bounds bounds, int max_zoom) { 284 boolean show_sub_tiles = false; 285 if (!isVisible(bounds)) return; 286 if (depth == 4) { 287 layer.openable.add(this); 288 } 289 if ((depth == 3) && (bounds.getArea() < 0.5)) { // 022B01 290 if (zip_scanned) { 291 show_sub_tiles = true; 292 } else if (can_download) { 293 downloadSelf(); 294 show_sub_tiles = true; 295 } else { 296 layer.downloadable.add(this); 297 } 298 } else if ((depth == 2) && (bounds.getArea() < 20)) { // its a layer2 tile 299 make_sub_tiles(2); 300 show_sub_tiles = true; 301 } else if ((depth == 1) && (bounds.getArea() < 40)) { // its a layer1 tile and zoom too small 302 // draw layer2 tiles for self 303 make_sub_tiles(1); 304 show_sub_tiles = true; 305 } 306 if (show_sub_tiles && (depth < max_zoom)) { 307 for (int i = 0; i < sub_tiles.size(); i++) { 308 CanVecTile tile = sub_tiles.get(i); 309 tile.paint(g,mv,bounds,max_zoom); 310 } 311 } else { 312 Point corners[] = getCorners(mv); 313 int xs[] = { corners[0].x, corners[1].x, corners[2].x, corners[3].x }; 314 int ys[] = { corners[0].y, corners[1].y, corners[2].y, corners[3].y }; 315 Polygon shape = new Polygon(xs,ys,4); 316 g.draw(shape); 317 g.drawString(getTileId(),corners[0].x,corners[0].y); 318 } 319 } 320 320 } -
applications/editors/josm/plugins/canvec_helper/src/org/openstreetmap/josm/plugins/canvec_helper/CanvecHelper.java
r29854 r30738 8 8 public class CanvecHelper extends Plugin { 9 9 10 11 12 13 14 10 public CanvecHelper(PluginInformation info) { 11 super(info); 12 Main.main.menu.imagerySubMenu.add(new CanvecHelperAction(this)); 13 } 14 15 15 @Override 16 17 18 16 public void mapFrameInitialized(MapFrame old, MapFrame new1) { 17 updateLayer(); 18 } 19 19 20 21 20 private synchronized void updateLayer() { 21 } 22 22 } -
applications/editors/josm/plugins/canvec_helper/src/org/openstreetmap/josm/plugins/canvec_helper/CanvecHelperAction.java
r29854 r30738 5 5 6 6 public class CanvecHelperAction extends JosmAction { 7 8 9 10 11 7 private CanvecHelper parent_temp; 8 public CanvecHelperAction(CanvecHelper parent) { 9 super("CanVec Helper","layericon24",null,null,false); 10 parent_temp = parent; 11 } 12 12 @Override 13 14 15 16 17 13 public void actionPerformed(java.awt.event.ActionEvent action) { 14 CanvecLayer layer; 15 layer = new CanvecLayer("canvec tile helper",parent_temp); 16 Main.main.addLayer(layer); 17 } 18 18 } -
applications/editors/josm/plugins/canvec_helper/src/org/openstreetmap/josm/plugins/canvec_helper/CanvecLayer.java
r30737 r30738 1 1 package org.openstreetmap.josm.plugins.canvec_helper; 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 2 4 3 5 import java.awt.Color; 4 6 import java.awt.Component; 7 import java.awt.Graphics2D; 8 import java.awt.Toolkit; 5 9 import java.awt.event.ActionEvent; 6 10 import java.awt.event.MouseEvent; 7 11 import java.awt.event.MouseListener; 8 import java.awt.Graphics2D;9 import java.awt.Toolkit;10 12 import java.io.BufferedReader; 13 import java.io.IOException; 11 14 import java.io.InputStream; 12 15 import java.io.InputStreamReader; 13 import java.io.IOException;14 16 import java.util.ArrayList; 15 17 import java.util.List; 16 18 import java.util.regex.Matcher; 17 19 import java.util.regex.Pattern; 20 18 21 import javax.swing.AbstractAction; 19 22 import javax.swing.Action; … … 22 25 import javax.swing.JMenu; 23 26 import javax.swing.JMenuItem; 27 28 import org.openstreetmap.josm.Main; 24 29 import org.openstreetmap.josm.data.Bounds; 25 30 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 31 import org.openstreetmap.josm.gui.MapView; 26 32 import org.openstreetmap.josm.gui.dialogs.LayerListDialog; 27 33 import org.openstreetmap.josm.gui.dialogs.LayerListPopup; 28 34 import org.openstreetmap.josm.gui.layer.Layer; 29 import org.openstreetmap.josm.gui.MapView;30 35 import org.openstreetmap.josm.io.CachedFile; 31 import static org.openstreetmap.josm.tools.I18n.tr;32 36 33 37 // most of the layout was copied from the openstreetbugs plugin to get things started 34 38 public class CanvecLayer extends Layer implements MouseListener { 35 private Icon layerIcon = null; 36 private int max_zoom = 4; 37 public CanvecHelper plugin_self; 38 private ArrayList<CanVecTile> tiles = new ArrayList<>(); 39 public ArrayList<CanVecTile> downloadable = new ArrayList<>(); 40 public ArrayList<CanVecTile> openable = new ArrayList<>(); 41 42 public CanvecLayer(String name,CanvecHelper self){ 43 super(name); 44 plugin_self = self; 45 this.setBackgroundLayer(true); 46 /* for (int i = 0; i < 119; i++) { 47 CanVecTile tile = new CanVecTile(i,"",0,"",plugin_self); 48 if (tile.isValid()) tiles.add(tile); 49 }*/ 50 layerIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/layericon.png"))); 51 try { 52 long start = System.currentTimeMillis(); 53 Pattern p = Pattern.compile("(\\d\\d\\d)([A-Z]\\d\\d).*"); 54 InputStream index = new CachedFile("http://ftp2.cits.rncan.gc.ca/OSM/pub/ZippedOsm.txt").getInputStream(); 55 BufferedReader br = new BufferedReader(new InputStreamReader(index)); 56 String line; 57 int last_cell = -1; 58 ArrayList<String> list = new ArrayList<>(); 59 while ((line = br.readLine()) != null) { 60 Matcher m = p.matcher(line); 61 if (m.find()) { 62 int cell = Integer.parseInt(m.group(1)); 63 if (cell == last_cell) { 64 list.add(m.group(0)); 65 } else if (last_cell != -1) { 66 CanVecTile tile = new CanVecTile(last_cell,"",0,"",this,list); 67 if (tile.isValid()) tiles.add(tile); 68 list = new ArrayList<>(); 69 list.add(m.group(0)); 70 } 71 last_cell = cell; 72 } else if (line.contains("Metadata.txt")) { 73 } else { 74 System.out.print("bad line '" + line + "'\n"); 75 } 76 } 77 br.close(); 78 CanVecTile tile = new CanVecTile(last_cell,"",0,"",this,list); 79 if (tile.isValid()) tiles.add(tile); 80 81 long end = System.currentTimeMillis(); 82 System.out.println((end-start)+"ms spent"); 83 } catch (IOException e) { 84 System.out.println("exception getting index"); 85 e.printStackTrace(); 86 } 87 } 88 @Override 89 public Action[] getMenuEntries() { 90 return new Action[]{ 91 LayerListDialog.getInstance().createShowHideLayerAction(), 92 LayerListDialog.getInstance().createDeleteLayerAction(), 93 SeparatorLayerAction.INSTANCE, 94 new LayerListPopup.InfoAction(this), 95 new MaxZoomAction(this), 96 new DownloadCanvecAction(this), 97 new OpenOsmAction(this)}; 98 } 99 public class MaxZoomAction extends AbstractAction implements LayerAction { 100 private CanvecLayer parent; 101 public MaxZoomAction(CanvecLayer parent) { 102 this.parent = parent; 103 } 104 @Override 105 public void actionPerformed(ActionEvent e) {} 106 @Override 107 public boolean supportLayers(List<Layer> layers) { 108 return false; 109 } 110 @Override 111 public Component createMenuComponent() { 112 JMenu max_zoom = new JMenu("max zoom"); 113 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,1))); 114 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,2))); 115 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,3))); 116 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,4))); 117 return max_zoom; 118 } 119 } 120 private class AllowDownload extends AbstractAction { 121 CanVecTile tile; 122 public AllowDownload(CanVecTile tile) { 123 super(tile.tileid); 124 this.tile = tile; 125 } 126 @Override 127 public void actionPerformed(ActionEvent arg0) { 128 tile.can_download = true; 129 } 130 } 131 private class OpenOsmAction extends AbstractAction implements LayerAction { 132 private CanvecLayer layer; 133 public OpenOsmAction(CanvecLayer layer) { 134 this.layer = layer; 135 } 136 @Override 137 public void actionPerformed(ActionEvent e) {} 138 @Override 139 public Component createMenuComponent() { 140 JMenu OpenOsm = new JMenu("Open tile"); 141 for (int i = 0; i < layer.openable.size(); i++) { 142 OpenOsm.add(new JMenuItem(new DoOpenOsm(layer.openable.get(i)))); 143 } 144 return OpenOsm; 145 } 146 @Override 147 public boolean supportLayers(List<Layer> layers) { 148 return false; 149 } 150 } 151 private class DoOpenOsm extends AbstractAction { 152 CanVecTile tile; 153 public DoOpenOsm(CanVecTile tile) { 154 super(tile.tileid); 155 this.tile = tile; 156 } 157 @Override 158 public void actionPerformed(ActionEvent e) { 159 tile.load_raw_osm(); 160 } 161 } 162 private class DownloadCanvecAction extends AbstractAction implements LayerAction { 163 private CanvecLayer parent; 164 public DownloadCanvecAction(CanvecLayer parent) { 165 this.parent = parent; 166 } 167 @Override 168 public void actionPerformed(ActionEvent e) {} 169 @Override 170 public boolean supportLayers(List<Layer> layers) { 171 return false; 172 } 173 @Override 174 public Component createMenuComponent() { 175 JMenu downloadCanvec = new JMenu("Download zip's"); 176 for (int i = 0; i < parent.downloadable.size(); i++) { 177 downloadCanvec.add(new JMenuItem(new AllowDownload(parent.downloadable.get(i)))); 178 } 179 return downloadCanvec; 180 } 181 } 182 public void setMaxZoom(int max_zoom) { 183 this.max_zoom = max_zoom; 184 } 185 @Override 186 public Object getInfoComponent() { 187 return getToolTipText(); 188 } 189 @Override 190 public String getToolTipText() { 191 return tr("canvec tile helper"); 192 } 193 @Override 194 public void visitBoundingBox(BoundingXYVisitor v) {} 195 @Override 196 public boolean isMergable(Layer other) { 197 return false; 198 } 199 @Override 200 public void mergeFrom(Layer from) {} 201 @Override 202 public Icon getIcon() { return layerIcon; } 203 @Override 204 public void paint(Graphics2D g, MapView mv, Bounds bounds) { 205 //long start = System.currentTimeMillis(); 206 //System.out.println("painting the area covered by "+bounds.toString()); 207 downloadable = new ArrayList<>(); 208 openable = new ArrayList<>(); 209 // loop over each canvec tile in the db and check bounds.intersects(Bounds) 210 g.setColor(Color.red); 211 for (int i = 0; i < tiles.size(); i++) { 212 CanVecTile tile = tiles.get(i); 213 tile.paint(g,mv,bounds,max_zoom); 214 } 215 //long end = System.currentTimeMillis(); 216 //System.out.println((end-start)+"ms spent"); 217 } 218 @Override 219 public void mouseExited(MouseEvent e) {} 220 @Override 221 public void mouseEntered(MouseEvent e) {} 222 @Override 223 public void mouseReleased(MouseEvent e) {} 224 @Override 225 public void mousePressed(MouseEvent e) {} 226 @Override 227 public void mouseClicked(MouseEvent e) { 228 System.out.println("click!"); 229 } 39 private Icon layerIcon = null; 40 private int max_zoom = 4; 41 public CanvecHelper plugin_self; 42 private ArrayList<CanVecTile> tiles = new ArrayList<>(); 43 public ArrayList<CanVecTile> downloadable = new ArrayList<>(); 44 public ArrayList<CanVecTile> openable = new ArrayList<>(); 45 46 public CanvecLayer(String name,CanvecHelper self){ 47 super(name); 48 plugin_self = self; 49 this.setBackgroundLayer(true); 50 /* for (int i = 0; i < 119; i++) { 51 CanVecTile tile = new CanVecTile(i,"",0,"",plugin_self); 52 if (tile.isValid()) tiles.add(tile); 53 }*/ 54 layerIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/layericon.png"))); 55 long start = System.currentTimeMillis(); 56 try ( 57 InputStream index = new CachedFile("http://ftp2.cits.rncan.gc.ca/OSM/pub/ZippedOsm.txt").getInputStream(); 58 BufferedReader br = new BufferedReader(new InputStreamReader(index)); 59 ) { 60 Pattern p = Pattern.compile("(\\d\\d\\d)([A-Z]\\d\\d).*"); 61 String line; 62 int last_cell = -1; 63 ArrayList<String> list = new ArrayList<>(); 64 while ((line = br.readLine()) != null) { 65 Matcher m = p.matcher(line); 66 if (m.find()) { 67 int cell = Integer.parseInt(m.group(1)); 68 if (cell == last_cell) { 69 list.add(m.group(0)); 70 } else if (last_cell != -1) { 71 CanVecTile tile = new CanVecTile(last_cell,"",0,"",this,list); 72 if (tile.isValid()) tiles.add(tile); 73 list = new ArrayList<>(); 74 list.add(m.group(0)); 75 } 76 last_cell = cell; 77 } else if (line.contains("Metadata.txt")) { 78 } else { 79 System.out.print("bad line '" + line + "'\n"); 80 } 81 } 82 CanVecTile tile = new CanVecTile(last_cell,"",0,"",this,list); 83 if (tile.isValid()) tiles.add(tile); 84 85 if (Main.isDebugEnabled()) { 86 long end = System.currentTimeMillis(); 87 Main.debug((end-start)+"ms spent"); 88 } 89 } catch (IOException e) { 90 Main.error("exception getting index"); 91 Main.error(e); 92 } 93 } 94 @Override 95 public Action[] getMenuEntries() { 96 return new Action[]{ 97 LayerListDialog.getInstance().createShowHideLayerAction(), 98 LayerListDialog.getInstance().createDeleteLayerAction(), 99 SeparatorLayerAction.INSTANCE, 100 new LayerListPopup.InfoAction(this), 101 new MaxZoomAction(this), 102 new DownloadCanvecAction(this), 103 new OpenOsmAction(this)}; 104 } 105 public class MaxZoomAction extends AbstractAction implements LayerAction { 106 private CanvecLayer parent; 107 public MaxZoomAction(CanvecLayer parent) { 108 this.parent = parent; 109 } 110 @Override 111 public void actionPerformed(ActionEvent e) {} 112 @Override 113 public boolean supportLayers(List<Layer> layers) { 114 return false; 115 } 116 @Override 117 public Component createMenuComponent() { 118 JMenu max_zoom = new JMenu("max zoom"); 119 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,1))); 120 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,2))); 121 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,3))); 122 max_zoom.add(new JMenuItem(new SetMaxZoom(parent,4))); 123 return max_zoom; 124 } 125 } 126 private class AllowDownload extends AbstractAction { 127 CanVecTile tile; 128 public AllowDownload(CanVecTile tile) { 129 super(tile.tileid); 130 this.tile = tile; 131 } 132 @Override 133 public void actionPerformed(ActionEvent arg0) { 134 tile.can_download = true; 135 } 136 } 137 private class OpenOsmAction extends AbstractAction implements LayerAction { 138 private CanvecLayer layer; 139 public OpenOsmAction(CanvecLayer layer) { 140 this.layer = layer; 141 } 142 @Override 143 public void actionPerformed(ActionEvent e) {} 144 @Override 145 public Component createMenuComponent() { 146 JMenu OpenOsm = new JMenu("Open tile"); 147 for (int i = 0; i < layer.openable.size(); i++) { 148 OpenOsm.add(new JMenuItem(new DoOpenOsm(layer.openable.get(i)))); 149 } 150 return OpenOsm; 151 } 152 @Override 153 public boolean supportLayers(List<Layer> layers) { 154 return false; 155 } 156 } 157 private class DoOpenOsm extends AbstractAction { 158 CanVecTile tile; 159 public DoOpenOsm(CanVecTile tile) { 160 super(tile.tileid); 161 this.tile = tile; 162 } 163 @Override 164 public void actionPerformed(ActionEvent e) { 165 tile.load_raw_osm(); 166 } 167 } 168 private class DownloadCanvecAction extends AbstractAction implements LayerAction { 169 private CanvecLayer parent; 170 public DownloadCanvecAction(CanvecLayer parent) { 171 this.parent = parent; 172 } 173 @Override 174 public void actionPerformed(ActionEvent e) {} 175 @Override 176 public boolean supportLayers(List<Layer> layers) { 177 return false; 178 } 179 @Override 180 public Component createMenuComponent() { 181 JMenu downloadCanvec = new JMenu("Download zip's"); 182 for (int i = 0; i < parent.downloadable.size(); i++) { 183 downloadCanvec.add(new JMenuItem(new AllowDownload(parent.downloadable.get(i)))); 184 } 185 return downloadCanvec; 186 } 187 } 188 public void setMaxZoom(int max_zoom) { 189 this.max_zoom = max_zoom; 190 } 191 @Override 192 public Object getInfoComponent() { 193 return getToolTipText(); 194 } 195 @Override 196 public String getToolTipText() { 197 return tr("canvec tile helper"); 198 } 199 @Override 200 public void visitBoundingBox(BoundingXYVisitor v) {} 201 @Override 202 public boolean isMergable(Layer other) { 203 return false; 204 } 205 @Override 206 public void mergeFrom(Layer from) {} 207 @Override 208 public Icon getIcon() { return layerIcon; } 209 @Override 210 public void paint(Graphics2D g, MapView mv, Bounds bounds) { 211 //long start = System.currentTimeMillis(); 212 //System.out.println("painting the area covered by "+bounds.toString()); 213 downloadable = new ArrayList<>(); 214 openable = new ArrayList<>(); 215 // loop over each canvec tile in the db and check bounds.intersects(Bounds) 216 g.setColor(Color.red); 217 for (int i = 0; i < tiles.size(); i++) { 218 CanVecTile tile = tiles.get(i); 219 tile.paint(g,mv,bounds,max_zoom); 220 } 221 //long end = System.currentTimeMillis(); 222 //System.out.println((end-start)+"ms spent"); 223 } 224 @Override 225 public void mouseExited(MouseEvent e) {} 226 @Override 227 public void mouseEntered(MouseEvent e) {} 228 @Override 229 public void mouseReleased(MouseEvent e) {} 230 @Override 231 public void mousePressed(MouseEvent e) {} 232 @Override 233 public void mouseClicked(MouseEvent e) { 234 System.out.println("click!"); 235 } 230 236 } -
applications/editors/josm/plugins/canvec_helper/src/org/openstreetmap/josm/plugins/canvec_helper/SetMaxZoom.java
r29854 r30738 5 5 6 6 class SetMaxZoom extends AbstractAction { 7 8 9 10 11 12 13 7 private CanvecLayer parent; 8 private int level; 9 public SetMaxZoom(CanvecLayer parent,int level) { 10 super(""+level); 11 this.level = level; 12 this.parent = parent; 13 } 14 14 @Override 15 16 17 15 public void actionPerformed(ActionEvent ev) { 16 parent.setMaxZoom(level); 17 } 18 18 }
Note:
See TracChangeset
for help on using the changeset viewer.