/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. */
  5. package org.ala.spatial.wms;
  6. /**
  7. *
  8. * @author ajay
  9. */
  10. public class Utils {
  11. static int map_zoom = 21;
  12. static int map_offset = 268435456; // half the Earth's circumference at zoom level 21
  13. static double map_radius = map_offset / Math.PI;
  14. static double meters_per_pixel = 78271.5170; //at zoom level 1
  15. static int current_zoom = 0;
  16. static public int convertLngToPixel(double lng) {
  17. return (int) Math.round(map_offset + map_radius * lng * Math.PI / 180);
  18. }
  19. static public double convertPixelToLng(int px) {
  20. return (px - map_offset) / map_radius * 180 / Math.PI;
  21. }
  22. static public int convertLatToPixel(double lat) {
  23. return (int) Math.round(map_offset - map_radius
  24. * Math.log((1 + Math.sin(lat * Math.PI / 180))
  25. / (1 - Math.sin(lat * Math.PI / 180))) / 2);
  26. }
  27. static public double convertPixelToLat(int px) {
  28. 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;
  29. }
  30. static public double convertMetersToPixels(double meters, double latitude, int zoom) {
  31. return meters / ((Math.cos(latitude * Math.PI / 180.0) * 2 * Math.PI * 6378137) / (256 * Math.pow(2, zoom)));
  32. }
  33. static public double convertPixelsToMeters(int pixels, double latitude, int zoom) {
  34. return ((Math.cos(latitude * Math.PI / 180.0) * 2 * Math.PI * 6378137) / (256 * Math.pow(2, zoom))) * pixels;
  35. }
  36. static public double convertMetersToLng(double meters) {
  37. return meters / 20037508.342789244 * 180;
  38. }
  39. static public double convertMetersToLat(double meters) {
  40. return 180.0 / Math.PI * (2 * Math.atan(Math.exp(meters / 20037508.342789244 * Math.PI)) - Math.PI / 2.0);
  41. }
  42. static public int planeDistance(double lat1, double lng1, double lat2, double lng2, int zoom) {
  43. // Given a pair of lat/long coordinates and a map zoom level, returns
  44. // the distance between the two points in pixels
  45. int x1 = convertLngToPixel(lng1);
  46. int y1 = convertLatToPixel(lat1);
  47. int x2 = convertLngToPixel(lng2);
  48. int y2 = convertLatToPixel(lat2);
  49. int distance = (int) Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
  50. return distance >> (map_zoom - zoom);
  51. }
  52. static String convertGeoToPoints(String geometry) {
  53. if (geometry == null) {
  54. return "";
  55. }
  56. geometry = geometry.replace(" ", ":");
  57. geometry = geometry.replace("MULTIPOLYGON(((", "");
  58. geometry = geometry.replace("POLYGON((", "");
  59. while (geometry.contains(")")) {
  60. geometry = geometry.replace(")", "");
  61. }
  62. //for case of more than one polygon
  63. while (geometry.contains(",((")) {
  64. geometry = geometry.replace(",((", "S");
  65. }
  66. while (geometry.contains(",(")) {
  67. geometry = geometry.replace(",(", "S");
  68. }
  69. return geometry;
  70. }
  71. }