Ignore:
Timestamp:
2013-03-22T22:18:26+01:00 (13 years ago)
Author:
zverik
Message:

ImageryID refactoring and javadocs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetTools.java

    r29382 r29384  
    66import org.openstreetmap.josm.data.coor.EastNorth;
    77import org.openstreetmap.josm.data.coor.LatLon;
    8 import org.openstreetmap.josm.data.imagery.ImageryInfo;
    98import org.openstreetmap.josm.data.projection.Projection;
    109import org.openstreetmap.josm.gui.MapView;
     
    1312
    1413/**
    15  * Some common static methods for querying imagery layers.
     14 * Some common static methods for querying and processing imagery layers.
    1615 *
    17  * @author zverik
     16 * @author Zverik
     17 * @license WTFPL
    1818 */
    1919public class ImageryOffsetTools {
     20    /**
     21     * A title for all dialogs created in this plugin.
     22     */
    2023    public static final String DIALOG_TITLE = tr("Imagery Offset");
    2124   
     25    /**
     26     * Returns the topmost visible imagery layer.
     27     * @return the layer, or null if it hasn't been found.
     28     */
    2229    public static ImageryLayer getTopImageryLayer() {
    2330        if( Main.map == null || Main.map.mapView == null )
     
    3239    }
    3340   
     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     */
    3445    public static LatLon getMapCenter() {
    3546        Projection proj = Main.getProjection();
     
    3849    }
    3950   
     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     */
    4058    public static LatLon getLayerOffset( ImageryLayer layer, LatLon center ) {
    4159        Projection proj = Main.getProjection();
     
    4664    }
    4765   
     66    /**
     67     * Applies the offset to the imagery layer.
     68     * @see #calculateOffset(iodb.ImageryOffset)
     69     * @see #getLayerOffset
     70     */
    4871    public static void applyLayerOffset( ImageryLayer layer, ImageryOffset offset ) {
    4972        double[] dxy = calculateOffset(offset);
     
    5376    /**
    5477     * Calculate dx and dy for imagery offset.
    55      * @return [dx, dy]
     78     * @return An array of [dx, dy].
     79     * @see #applyLayerOffset
    5680     */
    5781    public static double[] calculateOffset( ImageryOffset offset ) {
     
    6286    }
    6387   
     88    /**
     89     * Generate unique imagery identifier based on its type and URL.
     90     * @param layer imagery layer.
     91     * @return imagery id.
     92     */
    6493    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    }
    7597
    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 protocol
    84         int i = url.indexOf("://");
    85         url = url.substring(i + 3);
    86 
    87         // Split URL into address and query string
    88         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 map
    96         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 layers
    105             if( isWMS && removeWMSParams.contains(kv[0]) )
    106                 continue;
    107             // TMS: skip parameters with variable values
    108             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 parameters
    114         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 parts
    125         url = url.replaceAll("\\/\\{[^}]+\\}(?:\\.\\w+)?", "");
    126         // TMS: remove variable parts
    127         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    
    13798    // Following three methods were snatched from TMSLayer
    13899    private static double latToTileY(double lat, int zoom) {
     
    169130    }
    170131
    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     */
    188135    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);
    190137        if( d < 0.095 )  return formatDistance(d * 100,  tr("cm"), true );
    191138        if( d < 0.95 )   return formatDistance(d * 100,  tr("cm"), false);
     
    196143    }
    197144
     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     */
    198152    private static String formatDistance( double d, String si, boolean floating ) {
    199153        return MessageFormat.format(floating ? "{0,number,0.0} {1}" : "{0,number,0} {1}", d, si);
    200154    }
    201 
    202     public static String getServerURL() {
    203         return Main.pref.get("iodb.server.url", "http://offsets.textual.ru/");
    204     }
    205155}
Note: See TracChangeset for help on using the changeset viewer.