001package sec.web.render;
002
003public class GeoPixelConversion {
004
005    private static final double inchPerMeter = 39.3700787;
006    private static final double pixelsPerInch = 96;
007    private static final double METERS_PER_DEG = 111319.49079327357264771338267056;
008
009    public static double metersPerPixel(double scale) {
010        double step1 = scale / pixelsPerInch;
011        return step1 / inchPerMeter;
012    }
013
014    public static double lat2y(double latitude, double scale, double latOrigin, double metPerPix) {
015
016        double latRem = -(latitude - latOrigin);
017        double pixDis = (latRem * METERS_PER_DEG) / metPerPix;
018        return pixDis;
019    }
020
021    public static double y2lat(double yPosition, double scale, double latOrigin, double metPerPix) {
022
023        double latitude = latOrigin - ((yPosition * metPerPix) / METERS_PER_DEG);        
024        return latitude;
025    }
026
027    public static double long2x(double longitude, double scale, double longOrigin, double latitude, double metPerPix, boolean normalize) {
028        
029        double longRem = longitude-longOrigin;
030        if(normalize)
031        {
032            if (longRem > 180) {
033                longRem -= 360;
034            }
035            if (longRem < -180) {
036                longRem += 360;
037            }        
038        }
039        double metersPerDeg = GetMetersPerDegAtLat(latitude);
040        double pixDis = (longRem * metersPerDeg) / metPerPix;
041        return pixDis;
042    }
043
044    public static double x2long(double xPosition, double scale, double longOrigin, double latitude, double metPerPix) {
045        
046        double metersPerDeg = GetMetersPerDegAtLat(latitude);
047        double longitude = longOrigin + ((xPosition * metPerPix) / metersPerDeg);
048
049        if (longitude < -180) {
050            longitude += 360;
051        } else if (longitude > 180) {
052            longitude -= 360;
053        }
054
055        return longitude;
056    }
057
058    public static double Deg2Rad(double deg) {
059        double conv_factor = (2.0 * Math.PI) / 360.0;
060        return (deg * conv_factor);
061    }
062
063    public static double GetMetersPerDegAtLat(double lat) {
064        // Convert latitude to radians
065        lat = Deg2Rad(lat);
066        // Set up "Constants"
067        double p1 = 111412.84; // longitude calculation term 1
068
069        double p2 = -93.5; // longitude calculation term 2
070
071        double p3 = 0.118; // longitude calculation term 3
072
073        // Calculate the length of a degree of longitude in meters at given
074        // latitude
075        double longlen = (p1 * Math.cos(lat)) + (p2 * Math.cos(3 * lat)) + (p3 * Math.cos(5 * lat));
076
077        return longlen;
078    }
079}