Changeset 29384 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetTools.java
- Timestamp:
- 2013-03-22T22:18:26+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetTools.java
r29382 r29384 6 6 import org.openstreetmap.josm.data.coor.EastNorth; 7 7 import org.openstreetmap.josm.data.coor.LatLon; 8 import org.openstreetmap.josm.data.imagery.ImageryInfo;9 8 import org.openstreetmap.josm.data.projection.Projection; 10 9 import org.openstreetmap.josm.gui.MapView; … … 13 12 14 13 /** 15 * Some common static methods for querying imagery layers. 14 * Some common static methods for querying and processing imagery layers. 16 15 * 17 * @author zverik 16 * @author Zverik 17 * @license WTFPL 18 18 */ 19 19 public class ImageryOffsetTools { 20 /** 21 * A title for all dialogs created in this plugin. 22 */ 20 23 public static final String DIALOG_TITLE = tr("Imagery Offset"); 21 24 25 /** 26 * Returns the topmost visible imagery layer. 27 * @return the layer, or null if it hasn't been found. 28 */ 22 29 public static ImageryLayer getTopImageryLayer() { 23 30 if( Main.map == null || Main.map.mapView == null ) … … 32 39 } 33 40 41 /** 42 * Calculates the center of a visible map area. 43 * @return the center point, or (0; 0) if there's no map on the screen. 44 */ 34 45 public static LatLon getMapCenter() { 35 46 Projection proj = Main.getProjection(); … … 38 49 } 39 50 51 /** 52 * Calculates an imagery layer offset. 53 * @param center The center of a visible map area. 54 * @return Coordinates of a point on the imagery which correspond to the 55 * center point on the map. 56 * @see #applyLayerOffset 57 */ 40 58 public static LatLon getLayerOffset( ImageryLayer layer, LatLon center ) { 41 59 Projection proj = Main.getProjection(); … … 46 64 } 47 65 66 /** 67 * Applies the offset to the imagery layer. 68 * @see #calculateOffset(iodb.ImageryOffset) 69 * @see #getLayerOffset 70 */ 48 71 public static void applyLayerOffset( ImageryLayer layer, ImageryOffset offset ) { 49 72 double[] dxy = calculateOffset(offset); … … 53 76 /** 54 77 * Calculate dx and dy for imagery offset. 55 * @return [dx, dy] 78 * @return An array of [dx, dy]. 79 * @see #applyLayerOffset 56 80 */ 57 81 public static double[] calculateOffset( ImageryOffset offset ) { … … 62 86 } 63 87 88 /** 89 * Generate unique imagery identifier based on its type and URL. 90 * @param layer imagery layer. 91 * @return imagery id. 92 */ 64 93 public static String getImageryID( ImageryLayer layer ) { 65 if( layer == null ) 66 return null; 67 68 String url = layer.getInfo().getUrl(); 69 if( url == null ) 70 return null; 71 72 // predefined layers 73 if( layer.getInfo().getImageryType().equals(ImageryInfo.ImageryType.BING) || url.contains("tiles.virtualearth.net") ) 74 return "bing"; 94 return layer == null ? null : 95 ImageryIdGenerator.getImageryID(layer.getInfo().getUrl(), layer.getInfo().getImageryType()); 96 } 75 97 76 if( layer.getInfo().getImageryType().equals(ImageryInfo.ImageryType.SCANEX) && url.toLowerCase().equals("irs") )77 return "scanex_irs";78 79 boolean isWMS = layer.getInfo().getImageryType().equals(ImageryInfo.ImageryType.WMS);80 81 // System.out.println(url);82 83 // Remove protocol84 int i = url.indexOf("://");85 url = url.substring(i + 3);86 87 // Split URL into address and query string88 i = url.indexOf('?');89 String query = "";90 if( i > 0 ) {91 query = url.substring(i);92 url = url.substring(0, i);93 }94 95 // Parse query parameters into a sorted map96 final Set<String> removeWMSParams = new TreeSet<String>(Arrays.asList(new String[] {97 "srs", "width", "height", "bbox", "service", "request", "version", "format", "styles", "transparent"98 }));99 Map<String, String> qparams = new TreeMap<String, String>();100 String[] qparamsStr = query.length() > 1 ? query.substring(1).split("&") : new String[0];101 for( String param : qparamsStr ) {102 String[] kv = param.split("=");103 kv[0] = kv[0].toLowerCase();104 // WMS: if this is WMS, remove all parameters except map and layers105 if( isWMS && removeWMSParams.contains(kv[0]) )106 continue;107 // TMS: skip parameters with variable values108 if( kv.length > 1 && kv[1].indexOf('{') >= 0 && kv[1].indexOf('}') > 0 )109 continue;110 qparams.put(kv[0].toLowerCase(), kv.length > 1 ? kv[1] : null);111 }112 113 // Reconstruct query parameters114 StringBuilder sb = new StringBuilder();115 for( String qk : qparams.keySet() ) {116 if( sb.length() > 0 )117 sb.append('&');118 else if( query.length() > 0 )119 sb.append('?');120 sb.append(qk).append('=').append(qparams.get(qk));121 }122 query = sb.toString();123 124 // TMS: remove /{zoom} and /{y}.png parts125 url = url.replaceAll("\\/\\{[^}]+\\}(?:\\.\\w+)?", "");126 // TMS: remove variable parts127 url = url.replaceAll("\\{[^}]+\\}", "");128 while( url.contains("..") )129 url = url.replace("..", ".");130 if( url.startsWith(".") )131 url = url.substring(1);132 133 // System.out.println("-> " + url + query);134 return url + query;135 }136 137 98 // Following three methods were snatched from TMSLayer 138 99 private static double latToTileY(double lat, int zoom) { … … 169 130 } 170 131 171 public static double[] getLengthAndDirection( ImageryOffset offset ) { 172 return getLengthAndDirection(offset, 0.0, 0.0); 173 } 174 175 public static double[] getLengthAndDirection( ImageryOffset offset, double dx, double dy ) { 176 Projection proj = Main.getProjection(); 177 EastNorth pos = proj.latlon2eastNorth(offset.getPosition()); 178 LatLon correctedCenterLL = proj.eastNorth2latlon(pos.add(-dx, -dy)); 179 double length = correctedCenterLL.greatCircleDistance(offset.getImageryPos()); 180 double direction = length < 1e-2 ? 0.0 : correctedCenterLL.heading(offset.getImageryPos()); 181 // todo: north vs south. Meanwhile, let's fix this dirty: 182 // direction = Math.PI - direction; 183 if( direction < 0 ) 184 direction += Math.PI * 2; 185 return new double[] {length, direction}; 186 } 187 132 /** 133 * Converts distance in meters to a human-readable string. 134 */ 188 135 public static String formatDistance( double d ) { 189 if( d < 0.0095 ) return formatDistance(d * 1000, tr("mm"), false);136 if( d < 0.0095 ) return formatDistance(d * 1000, tr("mm"), true); 190 137 if( d < 0.095 ) return formatDistance(d * 100, tr("cm"), true ); 191 138 if( d < 0.95 ) return formatDistance(d * 100, tr("cm"), false); … … 196 143 } 197 144 145 /** 146 * Constructs a distance string. 147 * @param d Distance. 148 * @param si Units of measure for distance. 149 * @param floating Whether a floating point is needed. 150 * @return A formatted string. 151 */ 198 152 private static String formatDistance( double d, String si, boolean floating ) { 199 153 return MessageFormat.format(floating ? "{0,number,0.0} {1}" : "{0,number,0} {1}", d, si); 200 154 } 201 202 public static String getServerURL() {203 return Main.pref.get("iodb.server.url", "http://offsets.textual.ru/");204 }205 155 }
Note:
See TracChangeset
for help on using the changeset viewer.
