Changeset 29371 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetTools.java
- Timestamp:
- 2013-03-18T21:58:17+01:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetTools.java
r28008 r29371 1 1 package iodb; 2 2 3 import java.util.HashMap; 4 import java.util.List; 3 import java.util.*; 5 4 import org.openstreetmap.josm.Main; 6 5 import org.openstreetmap.josm.data.coor.EastNorth; 7 6 import org.openstreetmap.josm.data.coor.LatLon; 7 import org.openstreetmap.josm.data.imagery.ImageryInfo; 8 8 import org.openstreetmap.josm.data.projection.Projection; 9 9 import org.openstreetmap.josm.gui.MapView; 10 10 import org.openstreetmap.josm.gui.layer.ImageryLayer; 11 import static org.openstreetmap.josm.tools.I18n.tr; 11 12 12 13 /** … … 16 17 */ 17 18 public class ImageryOffsetTools { 18 p rivatestaticHashMap<String,String> imageryAliases;19 public static final String DIALOG_TITLE = tr("Imagery Offset"); 19 20 20 21 public static ImageryLayer getTopImageryLayer() { 22 if( Main.map == null || Main.map.mapView == null ) 23 return null; 21 24 List<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class); 22 25 for( ImageryLayer layer : layers ) { … … 28 31 } 29 32 30 p rivatestatic LatLon getMapCenter() {33 public static LatLon getMapCenter() { 31 34 Projection proj = Main.getProjection(); 32 35 return Main.map == null || Main.map.mapView == null … … 37 40 Projection proj = Main.getProjection(); 38 41 EastNorth offsetCenter = proj.latlon2eastNorth(center); 39 EastNorth centerOffset = offsetCenter.add(layer.getDx(), layer.getDy()); // todo: add or substract? 42 EastNorth centerOffset = offsetCenter.add(-layer.getDx(), -layer.getDy()); // todo: add or substract? 40 43 LatLon offsetLL = proj.eastNorth2latlon(centerOffset); 41 44 return offsetLL; … … 46 49 EastNorth center = proj.latlon2eastNorth(offset.getPosition()); 47 50 EastNorth offsetPos = proj.latlon2eastNorth(offset.getImageryPos()); 48 layer.setOffset(offsetPos.getX() -center.getX(),offsetPos.getY()- center.getY()); // todo: + or -?51 layer.setOffset(center.getX() - offsetPos.getX(), center.getY() - offsetPos.getY()); 49 52 } 50 53 … … 57 60 return null; 58 61 59 if( imageryAliases == null ) 60 loadImageryAliases(); 61 for( String substr : imageryAliases.keySet() ) 62 if( url.contains(substr) ) 63 return imageryAliases.get(substr); 64 65 return url; // todo: strip parametric parts, etc 66 } 67 68 private static void loadImageryAliases() { 69 if( imageryAliases == null ) 70 imageryAliases = new HashMap<String, String>(); 71 else 72 imageryAliases.clear(); 73 74 // { substring, alias } 75 imageryAliases.put("bing", "bing"); 76 // todo: load from a resource? 62 // predefined layers 63 if( layer.getInfo().getImageryType().equals(ImageryInfo.ImageryType.BING) || url.contains("tiles.virtualearth.net") ) 64 return "bing"; 65 66 if( layer.getInfo().getImageryType().equals(ImageryInfo.ImageryType.SCANEX) && url.toLowerCase().equals("irs") ) 67 return "scanex_irs"; 68 69 boolean isWMS = layer.getInfo().getImageryType().equals(ImageryInfo.ImageryType.WMS); 70 71 System.out.println(url); 72 73 // Remove protocol 74 int i = url.indexOf("://"); 75 url = url.substring(i + 3); 76 77 // Split URL into address and query string 78 i = url.indexOf('?'); 79 String query = ""; 80 if( i > 0 ) { 81 query = url.substring(i); 82 url = url.substring(0, i); 83 } 84 85 // Parse query parameters into a sorted map 86 Map<String, String> qparams = new TreeMap<String, String>(); 87 String[] qparamsStr = query.length() > 1 ? query.substring(1).split("&") : new String[0]; 88 for( String param : qparamsStr ) { 89 String[] kv = param.split("="); 90 kv[0] = kv[0].toLowerCase(); 91 // WMS: if this is WMS, remove all parameters except map and layers 92 if( isWMS && !(kv[0].equals("map") || kv[0].equals("layers")) ) 93 continue; 94 // TMS: skip parameters with variable values 95 if( kv.length > 1 && kv[1].indexOf('{') >= 0 && kv[1].indexOf('}') > 0 ) 96 continue; 97 qparams.put(kv[0].toLowerCase(), kv.length > 1 ? kv[1] : null); 98 } 99 100 // Reconstruct query parameters 101 StringBuilder sb = new StringBuilder(); 102 for( String qk : qparams.keySet() ) { 103 if( sb.length() > 0 ) 104 sb.append('&'); 105 else if( query.length() > 0 ) 106 sb.append('?'); 107 sb.append(qk).append('=').append(qparams.get(qk)); 108 } 109 query = sb.toString(); 110 111 // TMS: remove /{zoom} and /{y}.png parts 112 url = url.replaceAll("\\/\\{[^}]+\\}(?:\\.\\w+)?", ""); 113 // TMS: remove variable parts 114 url = url.replaceAll("\\{[^}]+\\}", ""); 115 while( url.contains("..") ) 116 url = url.replace("..", "."); 117 if( url.startsWith(".") ) 118 url = url.substring(1); 119 120 System.out.println("-> " + url + query); 121 return url + query; 77 122 } 78 123 … … 110 155 return intResult; 111 156 } 157 158 public static String getServerURL() { 159 return Main.pref.get("iodb.server.url", "http://offsets.textual.ru/"); 160 } 112 161 }
Note:
See TracChangeset
for help on using the changeset viewer.