Changeset 28008 in osm for applications


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

Something works :)

Location:
applications/editors/josm/plugins/imagery_offset_db
Files:
4 added
7 edited

Legend:

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

    r27986 r28008  
    11package iodb;
    22
    3 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    4 import org.openstreetmap.josm.data.osm.User;
     3import java.util.Map;
     4import org.openstreetmap.josm.data.osm.*;
    55
    66/**
     
    1818
    1919    public CalibrationObject(OsmPrimitive object) {
    20         this(object, -1);
     20        this(object, getLastUserId(object));
    2121    }
    2222
     
    2828        return object;
    2929    }
     30   
     31    private static long getLastUserId( OsmPrimitive object ) {
     32        return object.getUser() == null ? -1 : object.getUser().getId(); // todo?
     33    }
     34
     35    @Override
     36    public void putServerParams( Map<String, String> map ) {
     37        super.putServerParams(map);
     38        map.put("object", object instanceof Node ? "node" : "way");
     39        map.put("id", String.valueOf(object.getId()));
     40        map.put("lastuser", String.valueOf(lastUserId));
     41    }
     42   
    3043}
  • 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   
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffset.java

    r27986 r28008  
    11package iodb;
    22
     3import java.util.Map;
     4import org.openstreetmap.josm.data.coor.CoordinateFormat;
    35import org.openstreetmap.josm.data.coor.LatLon;
    46
     
    1921        this.maxZoom = 30;
    2022    }
    21 
     23   
    2224    public void setMaxZoom(int maxZoom) {
    2325        this.maxZoom = maxZoom;
     
    4345        return minZoom;
    4446    }
     47
     48    @Override
     49    public void putServerParams( Map<String, String> map ) {
     50        super.putServerParams(map);
     51        map.put("imagery", imagery);
     52        map.put("imlat", imageryPos.latToString(CoordinateFormat.DECIMAL_DEGREES));
     53        map.put("imlon", imageryPos.lonToString(CoordinateFormat.DECIMAL_DEGREES));
     54        if( minZoom > 0 )
     55            map.put("minzoom", String.valueOf(minZoom));
     56        if( maxZoom < 30 )
     57            map.put("maxzoom", String.valueOf(maxZoom));
     58    }
    4559}
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetBase.java

    r27986 r28008  
    22
    33import java.util.Date;
     4import java.util.Map;
     5import org.openstreetmap.josm.data.coor.CoordinateFormat;
    46import org.openstreetmap.josm.data.coor.LatLon;
    57
     
    5153        return position;
    5254    }
     55   
     56    public void putServerParams( Map<String, String> map ) {
     57        map.put("lat", position.latToString(CoordinateFormat.DECIMAL_DEGREES));
     58        map.put("lon", position.lonToString(CoordinateFormat.DECIMAL_DEGREES));
     59        map.put("author", author);
     60        map.put("description", description);
     61    }
    5362}
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialog.java

    r27986 r28008  
    1414 * @author zverik
    1515 */
    16 public class OffsetDialog extends JDialog {
     16public class OffsetDialog extends JDialog implements ActionListener {
    1717    private List<ImageryOffsetBase> offsets;
    18     private int selectedOffset;
     18    private ImageryOffsetBase selectedOffset;
    1919
    2020    public OffsetDialog( List<ImageryOffsetBase> offsets ) {
     
    2727        JPanel buttonPanel = new JPanel(new GridLayout(offsets.size() + 1, 1));
    2828        for( ImageryOffsetBase offset : offsets ) {
    29             buttonPanel.add(new OffsetDialogButton(offset));
     29            OffsetDialogButton button = new OffsetDialogButton(offset);
     30            button.addActionListener(this);
     31            buttonPanel.add(button);
    3032        }
    3133        JButton cancelButton = new JButton("Cancel");
    32         cancelButton.addActionListener(new ActionListener() {
    33             public void actionPerformed(ActionEvent e) {
    34                 selectedOffset = -1;
    35                 OffsetDialog.this.setVisible(false);
    36             }
    37         });
     34        cancelButton.addActionListener(this);
    3835        buttonPanel.add(cancelButton); // todo: proper button
    3936        setContentPane(buttonPanel);
     
    4340   
    4441    public ImageryOffsetBase showDialog() {
    45         selectedOffset = -1;
     42        selectedOffset = null;
    4643        prepareDialog();
    4744        setVisible(true);
    48         return selectedOffset < 0 ? null : offsets.get(selectedOffset);
     45        return selectedOffset;
     46    }
     47
     48    public void actionPerformed( ActionEvent e ) {
     49        if( e.getSource() instanceof OffsetDialogButton ) {
     50            selectedOffset = ((OffsetDialogButton)e.getSource()).getOffset();
     51        } else
     52            selectedOffset = null;
     53        setVisible(false);
    4954    }
    5055}
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialogButton.java

    r27986 r28008  
    99 */
    1010public class OffsetDialogButton extends JButton {
     11   
     12    private ImageryOffsetBase offset;
    1113
    1214    public OffsetDialogButton( ImageryOffsetBase offset ) {
    1315        super(offset.getDescription() + " (" + offset.getPosition().lat() + ", " + offset.getPosition().lon() + ")");
     16        this.offset = offset;
     17    }
     18
     19    public ImageryOffsetBase getOffset() {
     20        return offset;
    1421    }
    1522   
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/StoreImageryOffsetAction.java

    r27986 r28008  
    22
    33import java.awt.event.ActionEvent;
     4import java.util.HashMap;
     5import java.util.Map;
    46import org.openstreetmap.josm.Main;
    57import org.openstreetmap.josm.actions.JosmAction;
     
    2022
    2123    public void actionPerformed(ActionEvent e) {
     24        // todo: check that there is an imagery
     25        // and that it is moved
    2226        Projection proj = Main.map.mapView.getProjection();
    2327        LatLon center = proj.eastNorth2latlon(Main.map.mapView.getCenter());
     
    2731        // todo: upload object info to server
    2832    }
     33   
     34    private static void upload( ImageryOffsetBase offset ) {
     35        String base = Main.pref.get("iodb.server.url", "http://offsets.textual.ru/");
     36        Map<String, String> params = new HashMap<String, String>();
     37        offset.putServerParams(params);
     38        // todo
     39    }
    2940}
Note: See TracChangeset for help on using the changeset viewer.