Ignore:
Timestamp:
2012-03-06T21:32:44+01:00 (12 years ago)
Author:
zverik
Message:

Something works :)

File:
1 edited

Legend:

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

    r27986 r28008  
    3737    private List<ImageryOffsetBase> offsets;
    3838   
    39     private HashMap<String, String> imageryAliases;
    40    
    4139    public GetImageryOffsetAction() {
    4240        super(tr("Get Imagery Offset..."), "getoffset", tr("Download offsets for current imagery from a server"),
     
    4846        Projection proj = Main.map.mapView.getProjection();
    4947        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();
    5355        Future<?> future = Main.worker.submit(download);
    5456        try {
     
    5759            ex.printStackTrace();
    5860            return;
    59         }
     61        }*/
    6062       
    6163        // todo: show a dialog for selecting one of the offsets (without "update" flag)
     
    6365        if( offset != null ) {
    6466            // 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            }
    6572        }
    6673    }
    6774   
    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();
    7178        List<ImageryOffsetBase> result = null;
    7279        try {
    73             query = query + "&imagery=" + URLEncoder.encode(getImageryID(), "utf-8");
     80            query = query + "&imagery=" + URLEncoder.encode(imagery, "utf-8");
    7481            URL url = new URL(base + query);
     82            System.out.println("url=" + url);
    7583            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    7684            connection.connect();
     
    7987            if( inp != null ) {
    8088                result = new IODBReader(inp).parse();
     89                System.out.println("result.size() = " + result.size());
    8190            }
    8291            connection.disconnect();
     
    95104            result = new ArrayList<ImageryOffsetBase>();
    96105        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, etc
    118     }
    119    
    120     private void loadImageryAliases() {
    121         if( imageryAliases == null )
    122             imageryAliases = new HashMap<String, String>();
    123         else
    124             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 TMSLayer
    132     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;
    163106    }
    164107   
Note: See TracChangeset for help on using the changeset viewer.