Changeset 11376 in osm for applications/viewer


Ignore:
Timestamp:
2008-10-21T16:27:42+02:00 (16 years ago)
Author:
stotz
Message:

Bug fix and optimization (patch provided by r_x)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmMercator.java

    r9095 r11376  
    2121
    2222        public static double radius(int aZoomlevel) {
    23                 return (TILE_SIZE * Math.pow(2, aZoomlevel)) / (2 * Math.PI);
     23                return (TILE_SIZE * (1 << aZoomlevel)) / (2.0 * Math.PI);
    2424        }
    2525
     
    3232         */
    3333        public static int getMaxPixels(int aZoomlevel) {
    34                 return (int) (TILE_SIZE * Math.pow(2, aZoomlevel));
     34                return TILE_SIZE * (1 << aZoomlevel);
    3535        }
    3636
    3737        public static int falseEasting(int aZoomlevel) {
    38                 return (int) getMaxPixels(aZoomlevel) / 2;
     38                return getMaxPixels(aZoomlevel) / 2;
    3939        }
    4040
    4141        public static int falseNorthing(int aZoomlevel) {
    42                 return (int) (-1 * getMaxPixels(aZoomlevel) / 2);
     42                return (-1 * getMaxPixels(aZoomlevel) / 2);
    4343        }
    4444
     
    5252        public static int LonToX(double aLongitude, int aZoomlevel) {
    5353                double longitude = Math.toRadians(aLongitude);
    54                 return (int) ((radius(aZoomlevel) * longitude) + falseEasting(aZoomlevel));
     54                int x = (int) ((radius(aZoomlevel) * longitude) + falseEasting(aZoomlevel));
     55                x = Math.min(x, getMaxPixels(aZoomlevel) - 1);
     56                return x;
    5557        }
    5658
     
    6870                        aLat = MAX_LAT;
    6971                double latitude = Math.toRadians(aLat);
    70                 return (int) (-1
    71                                 * (radius(aZoomlevel) / 2.0 * Math.log((1.0 + Math
    72                                                 .sin(latitude))
    73                                                 / (1.0 - Math.sin(latitude)))) - falseNorthing(aZoomlevel));
     72                int y =
     73                                (int) (-1
     74                                                * (radius(aZoomlevel) / 2.0 * Math.log((1.0 + Math.sin(latitude))
     75                                                                / (1.0 - Math.sin(latitude)))) - falseNorthing(aZoomlevel));
     76                y = Math.min(y, getMaxPixels(aZoomlevel) - 1);
     77                return y;
    7478        }
    7579
     
    8589                double longRadians = aX / radius(aZoomlevel);
    8690                double longDegrees = Math.toDegrees(longRadians);
    87                 double rotations = Math.floor((longDegrees + 180) / 360);
    88                 double longitude = longDegrees - (rotations * 360);
    89                 return longitude;
     91                return longDegrees;
    9092        }
    9193
     
    99101        public static double YToLat(int aY, int aZoomlevel) {
    100102                aY += falseNorthing(aZoomlevel);
    101                 double latitude = (Math.PI / 2)
    102                                 - (2 * Math.atan(Math.exp(-1.0 * aY / radius(aZoomlevel))));
     103                double latitude = (Math.PI / 2) - (2 * Math.atan(Math.exp(-1.0 * aY / radius(aZoomlevel))));
    103104                return -1 * Math.toDegrees(latitude);
    104105        }
Note: See TracChangeset for help on using the changeset viewer.