Changeset 28008 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java
- Timestamp:
- 2012-03-06T21:32:44+01:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java
r27986 r28008 37 37 private List<ImageryOffsetBase> offsets; 38 38 39 private HashMap<String, String> imageryAliases;40 41 39 public GetImageryOffsetAction() { 42 40 super(tr("Get Imagery Offset..."), "getoffset", tr("Download offsets for current imagery from a server"), … … 48 46 Projection proj = Main.map.mapView.getProjection(); 49 47 LatLon center = proj.eastNorth2latlon(Main.map.mapView.getCenter()); 50 // todo: download a list of offsets for current bbox * N 51 List<ImageryOffsetBase> offsets = download(center); // todo: async 52 DownloadOffsets download = new DownloadOffsets(); 48 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer(); 49 String imagery = ImageryOffsetTools.getImageryID(layer); 50 if( imagery == null ) 51 return; 52 53 List<ImageryOffsetBase> offsets = download(center, imagery); // todo: async 54 /*DownloadOffsets download = new DownloadOffsets(); 53 55 Future<?> future = Main.worker.submit(download); 54 56 try { … … 57 59 ex.printStackTrace(); 58 60 return; 59 } 61 }*/ 60 62 61 63 // todo: show a dialog for selecting one of the offsets (without "update" flag) … … 63 65 if( offset != null ) { 64 66 // todo: use the chosen offset 67 if( offset instanceof ImageryOffset ) { 68 ImageryOffsetTools.applyLayerOffset(layer, (ImageryOffset)offset); 69 } else if( offset instanceof CalibrationObject ) { 70 // todo: select object 71 } 65 72 } 66 73 } 67 74 68 private List<ImageryOffsetBase> download( LatLon center ) {69 String base = Main.pref.get("iodb.server.url", "http:// textual.ru/iodb.php");70 String query = " ?action=get&lat=" + center.getX() + "&lon=" + center.getY();75 private List<ImageryOffsetBase> download( LatLon center, String imagery ) { 76 String base = Main.pref.get("iodb.server.url", "http://offsets.textual.ru/"); 77 String query = "get?lat=" + center.getX() + "&lon=" + center.getY(); 71 78 List<ImageryOffsetBase> result = null; 72 79 try { 73 query = query + "&imagery=" + URLEncoder.encode( getImageryID(), "utf-8");80 query = query + "&imagery=" + URLEncoder.encode(imagery, "utf-8"); 74 81 URL url = new URL(base + query); 82 System.out.println("url=" + url); 75 83 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 76 84 connection.connect(); … … 79 87 if( inp != null ) { 80 88 result = new IODBReader(inp).parse(); 89 System.out.println("result.size() = " + result.size()); 81 90 } 82 91 connection.disconnect(); … … 95 104 result = new ArrayList<ImageryOffsetBase>(); 96 105 return result; 97 }98 99 private String getImageryID() {100 List<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class);101 String url = null;102 for( ImageryLayer layer : layers ) {103 if( layer.isVisible() ) {104 url = layer.getInfo().getUrl();105 break;106 }107 }108 if( url == null )109 return null;110 111 if( imageryAliases == null )112 loadImageryAliases();113 for( String substr : imageryAliases.keySet() )114 if( url.contains(substr) )115 return imageryAliases.get(substr);116 117 return url; // todo: strip parametric parts, etc118 }119 120 private void loadImageryAliases() {121 if( imageryAliases == null )122 imageryAliases = new HashMap<String, String>();123 else124 imageryAliases.clear();125 126 // { substring, alias }127 imageryAliases.put("bing", "bing");128 // todo: load from a resource?129 }130 131 // Following three methods were snatched from TMSLayer132 private double latToTileY(double lat, int zoom) {133 double l = lat / 180 * Math.PI;134 double pf = Math.log(Math.tan(l) + (1 / Math.cos(l)));135 return Math.pow(2.0, zoom - 1) * (Math.PI - pf) / Math.PI;136 }137 138 private double lonToTileX(double lon, int zoom) {139 return Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0;140 }141 142 private int getCurrentZoom() {143 if (Main.map == null || Main.map.mapView == null) {144 return 1;145 }146 MapView mv = Main.map.mapView;147 LatLon topLeft = mv.getLatLon(0, 0);148 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());149 double x1 = lonToTileX(topLeft.lon(), 1);150 double y1 = latToTileY(topLeft.lat(), 1);151 double x2 = lonToTileX(botRight.lon(), 1);152 double y2 = latToTileY(botRight.lat(), 1);153 154 int screenPixels = mv.getWidth() * mv.getHeight();155 double tilePixels = Math.abs((y2 - y1) * (x2 - x1) * 256 * 256);156 if (screenPixels == 0 || tilePixels == 0) {157 return 1;158 }159 double factor = screenPixels / tilePixels;160 double result = Math.log(factor) / Math.log(2) / 2 + 1;161 int intResult = (int) Math.floor(result);162 return intResult;163 106 } 164 107
Note:
See TracChangeset
for help on using the changeset viewer.