Ignore:
Timestamp:
2011-08-08T17:37:37+02:00 (13 years ago)
Author:
bastik
Message:

fix: Get map scale from the center of the image and not from the center of the screen. Otherwise, the displayed image will grow and shrink when panning the map up and down. (yes, this is somewhat pedantic)

Location:
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java

    r26487 r26489  
    2525import java.awt.Color;
    2626import java.awt.Component;
    27 import java.awt.Graphics;
    2827import java.awt.Graphics2D;
    2928import java.awt.Image;
    3029import java.awt.Toolkit;
    3130import java.awt.event.ActionEvent;
    32 import java.awt.image.BufferedImage;
    3331import java.io.File;
    3432import java.io.FileInputStream;
    35 import java.io.FileNotFoundException;
    36 import java.io.FileOutputStream;
    3733import java.io.IOException;
    3834import java.util.List;
     
    4945import org.openstreetmap.josm.data.Bounds;
    5046import org.openstreetmap.josm.data.coor.EastNorth;
     47import org.openstreetmap.josm.data.coor.LatLon;
    5148import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    5249import org.openstreetmap.josm.gui.MapView;
     
    202199            EastNorth center = Main.map.mapView.getCenter();
    203200            EastNorth leftop = Main.map.mapView.getEastNorth( 0, 0 );
     201            // Number of pixels for one unit in east north space.
     202            // This is the same in x- and y- direction.
    204203            double pixel_per_en = ( Main.map.mapView.getWidth() / 2.0 ) / ( center.east() - leftop.east() );
    205204
     
    208207            double pic_offset_y = (( leftop.north() - m_position.north() ) * pixel_per_en);
    209208
    210             // Let's use Graphics 2D
    211209            Graphics2D g = (Graphics2D)g2.create();
    212210            // Move
     
    215213            g.rotate( m_angle * Math.PI / 180.0 );
    216214            // Scale
    217             double scalex = m_scalex * m_initial_scale / Main.map.mapView.getDist100Pixel();
    218             double scaley = m_scaley * m_initial_scale / Main.map.mapView.getDist100Pixel();
     215            double scalex = m_scalex * m_initial_scale * pixel_per_en / getMetersPerEasting(m_position) / 100;
     216            double scaley = m_scaley * m_initial_scale * pixel_per_en / getMetersPerNorthing(m_position) / 100;
    219217            g.scale( scalex, scaley );
    220218            // Shear
     
    239237        }
    240238    }
    241 
     239   
     240    /**
     241     * Returns the distance in meter, that corresponds to one unit in east north
     242     * space. For normal projections, it is about 1 (but usually changing with
     243     * latitude).
     244     * For EPSG:4326, it is the distance from one meridian of full degree to the
     245     * next (a couple of kilometers).
     246     */
     247    private double getMetersPerEasting(EastNorth en) {
     248        /* Natural scale in east/north units per pixel.
     249         * This means, the projection should be able to handle
     250         * a shift of that size in east north space without
     251         * going out of bounds.
     252         *
     253         * Also, this should get us somewhere in the range of meters,
     254         * so we get the result at the point 'en' and not some average.
     255         */
     256        double naturalScale = Main.getProjection().getDefaultZoomInPPD();
     257        naturalScale *= 0.01; // make a little smaller
     258       
     259        LatLon ll1 = Main.getProjection().eastNorth2latlon(
     260                new EastNorth(en.east() - naturalScale, en.north()));
     261        LatLon ll2 = Main.getProjection().eastNorth2latlon(
     262                new EastNorth(en.east() + naturalScale, en.north()));
     263       
     264        double dist = ll1.greatCircleDistance(ll2) / naturalScale / 2;
     265        return dist;
     266    }
     267
     268    /* see getMetersPerEasting */
     269    private double getMetersPerNorthing(EastNorth en) {
     270        double naturalScale = Main.getProjection().getDefaultZoomInPPD();
     271        naturalScale *= 0.01;
     272       
     273        LatLon ll1 = Main.getProjection().eastNorth2latlon(
     274                new EastNorth(en.east(), en.north()- naturalScale));
     275        LatLon ll2 = Main.getProjection().eastNorth2latlon(
     276                new EastNorth(en.east(), en.north() + naturalScale));
     277       
     278        double dist = ll1.greatCircleDistance(ll2) / naturalScale / 2;
     279        return dist;
     280    }
     281   
    242282    /**
    243283     * Moves the picture. Scaled in EastNorth...
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java

    r26487 r26489  
    6262        super(info);
    6363
    64         System.err.println("hi there!");
    6564        // Create menu entry
    6665        if ( Main.main.menu != null ) {
Note: See TracChangeset for help on using the changeset viewer.