PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/webportal/src/main/java/org/ala/spatial/wms/Utils.java

http://alageospatialportal.googlecode.com/
Java | 88 lines | 60 code | 17 blank | 11 comment | 5 complexity | c8aac9a1ee23dae9bc59bdd95e158fd3 MD5 | raw file
 1/*
 2 * To change this template, choose Tools | Templates
 3 * and open the template in the editor.
 4 */
 5package org.ala.spatial.wms;
 6
 7/**
 8 * 
 9 * @author ajay
10 */
11public class Utils {
12
13    static int map_zoom = 21;
14    static int map_offset = 268435456; // half the Earth's circumference at zoom level 21
15    static double map_radius = map_offset / Math.PI;
16    static double meters_per_pixel = 78271.5170; //at zoom level 1
17    static int current_zoom = 0;
18
19    static public int convertLngToPixel(double lng) {
20        return (int) Math.round(map_offset + map_radius * lng * Math.PI / 180);
21    }
22
23    static public double convertPixelToLng(int px) {
24        return (px - map_offset) / map_radius * 180 / Math.PI;
25    }
26
27    static public int convertLatToPixel(double lat) {
28        return (int) Math.round(map_offset - map_radius
29                * Math.log((1 + Math.sin(lat * Math.PI / 180))
30                / (1 - Math.sin(lat * Math.PI / 180))) / 2);
31    }
32
33    static public double convertPixelToLat(int px) {
34        return Math.asin((Math.pow(Math.E, ((map_offset - px) / map_radius * 2)) - 1) / (1 + Math.pow(Math.E, ((map_offset - px) / map_radius * 2)))) * 180 / Math.PI;
35    }
36
37    static public double convertMetersToPixels(double meters, double latitude, int zoom) {
38        return meters / ((Math.cos(latitude * Math.PI / 180.0) * 2 * Math.PI * 6378137) / (256 * Math.pow(2, zoom)));
39    }
40
41    static public double convertPixelsToMeters(int pixels, double latitude, int zoom) {
42        return ((Math.cos(latitude * Math.PI / 180.0) * 2 * Math.PI * 6378137) / (256 * Math.pow(2, zoom))) * pixels;
43    }
44
45    static public double convertMetersToLng(double meters) {
46        return meters / 20037508.342789244 * 180;
47    }
48
49    static public double convertMetersToLat(double meters) {
50        return 180.0 / Math.PI * (2 * Math.atan(Math.exp(meters / 20037508.342789244 * Math.PI)) - Math.PI / 2.0);
51    }
52
53    static public int planeDistance(double lat1, double lng1, double lat2, double lng2, int zoom) {
54        // Given a pair of lat/long coordinates and a map zoom level, returns
55        // the distance between the two points in pixels
56
57        int x1 = convertLngToPixel(lng1);
58        int y1 = convertLatToPixel(lat1);
59
60        int x2 = convertLngToPixel(lng2);
61        int y2 = convertLatToPixel(lat2);
62
63        int distance = (int) Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
64
65        return distance >> (map_zoom - zoom);
66    }
67
68    static String convertGeoToPoints(String geometry) {
69        if (geometry == null) {
70            return "";
71        }
72        geometry = geometry.replace(" ", ":");
73        geometry = geometry.replace("MULTIPOLYGON(((", "");
74        geometry = geometry.replace("POLYGON((", "");
75        while (geometry.contains(")")) {
76            geometry = geometry.replace(")", "");
77        }
78
79        //for case of more than one polygon
80        while (geometry.contains(",((")) {
81            geometry = geometry.replace(",((", "S");
82        }
83        while (geometry.contains(",(")) {
84            geometry = geometry.replace(",(", "S");
85        }
86        return geometry;
87    }
88}