Ignore:
Timestamp:
2013-03-18T21:58:17+01:00 (12 years ago)
Author:
zverik
Message:

some updates to iodb plugin

File:
1 edited

Legend:

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

    r28008 r29371  
    11package iodb;
    22
    3 import java.util.HashMap;
    4 import java.util.List;
     3import java.util.*;
    54import org.openstreetmap.josm.Main;
    65import org.openstreetmap.josm.data.coor.EastNorth;
    76import org.openstreetmap.josm.data.coor.LatLon;
     7import org.openstreetmap.josm.data.imagery.ImageryInfo;
    88import org.openstreetmap.josm.data.projection.Projection;
    99import org.openstreetmap.josm.gui.MapView;
    1010import org.openstreetmap.josm.gui.layer.ImageryLayer;
     11import static org.openstreetmap.josm.tools.I18n.tr;
    1112
    1213/**
     
    1617 */
    1718public class ImageryOffsetTools {
    18     private static HashMap<String, String> imageryAliases;
     19    public static final String DIALOG_TITLE = tr("Imagery Offset");
    1920   
    2021    public static ImageryLayer getTopImageryLayer() {
     22        if( Main.map == null || Main.map.mapView == null )
     23            return null;
    2124        List<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class);
    2225        for( ImageryLayer layer : layers ) {
     
    2831    }
    2932   
    30     private static LatLon getMapCenter() {
     33    public static LatLon getMapCenter() {
    3134        Projection proj = Main.getProjection();
    3235        return Main.map == null || Main.map.mapView == null
     
    3740        Projection proj = Main.getProjection();
    3841        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?
    4043        LatLon offsetLL = proj.eastNorth2latlon(centerOffset);
    4144        return offsetLL;
     
    4649        EastNorth center = proj.latlon2eastNorth(offset.getPosition());
    4750        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());
    4952    }
    5053   
     
    5760            return null;
    5861       
    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;
    77122    }
    78123   
     
    110155        return intResult;
    111156    }
     157
     158    public static String getServerURL() {
     159        return Main.pref.get("iodb.server.url", "http://offsets.textual.ru/");
     160    }
    112161}
Note: See TracChangeset for help on using the changeset viewer.