Statistics
| Revision:

svn-gvsig-desktop / branches / simbologia / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / geo / Geo.java @ 10450

History | View | Annotate | Download (2.79 KB)

1
package com.iver.cit.gvsig.fmap.tools.geo;
2

    
3
/**
4
 * GeoC is a convenience class used for a couple of things. It can
5
 * be used to specifiy constants, and can be used for calculate the geographical area.
6
 *
7
 * @author Vicente Caballero Navarro
8
 */
9
public class Geo {
10
    public static double HalfPi = 1.5707963267948966192313;
11
    public static double Degree = 57.295779513082320876798; /* degrees per radian */
12
    public static double SqMi = 273218.4; /* Square mi per spherical degree. */
13
    public static double SqKm = 707632.4; /* Square km per spherical degree. */
14
    public static double SqM = 707632400000.0; /* Square M per spherical degree. */
15

    
16
    public static double getDecimalDegrees(double m) {
17
            ///(m*180)/ (6378137.0 * Math.PI)
18
            return (m*8.983152841195214E-6);
19
    }
20

    
21
    /*  Haversine function: hav(x)= (1-cos(x))/2.  */
22
    private static double hav(double X){
23
        return (1.0 - Math.cos(X)) / 2.0;
24
    }
25

    
26
    /*  Returns the area of a spherical polygon in spherical degrees,
27
    given the latitudes and longitudes in Lat and Lon, respectively.
28
    The N data points have indices which range from 0 to N-1. */
29
    public static double sphericalPolyArea(double[] lat, double[] lon, int n) {
30
        int j;
31
        int k;
32
        double lam1;
33
        double lam2 = 0;
34
        double beta1;
35
        double beta2 = 0;
36
        double cosB1;
37
        double cosB2 = 0;
38
        double havA;
39
        double t;
40
        double a;
41
        double b;
42
        double c;
43
        double s;
44
        double sum;
45
        double excess;
46

    
47
        sum = 0;
48

    
49
        for (j = 0; j <= n; j++) {
50
            k = j + 1;
51

    
52
            if (j == 0) {
53
                lam1 = lon[j];
54
                beta1 = lat[j];
55
                lam2 = lon[j + 1];
56
                beta2 = lat[j + 1];
57
                cosB1 = Math.cos(beta1);
58
                cosB2 = Math.cos(beta2);
59
            } else {
60
                k = (j + 1) % (n + 1);
61
                lam1 = lam2;
62
                beta1 = beta2;
63
                lam2 = lon[k];
64
                beta2 = lat[k];
65
                cosB1 = cosB2;
66
                cosB2 = Math.cos(beta2);
67
            }
68

    
69
            if (lam1 != lam2) {
70
                havA = hav(beta2 - beta1) + (cosB1 * cosB2 * hav(lam2 - lam1));
71
                a = 2 * Math.asin(Math.sqrt(havA));
72
                b = HalfPi - beta2;
73
                c = HalfPi - beta1;
74
                s = 0.5 * (a + b + c);
75
                t = Math.tan(s / 2) * Math.tan((s - a) / 2) * Math.tan((s - b) / 2) * Math.tan((s -
76
                        c) / 2);
77

    
78
                excess = Math.abs(4 * Math.atan(Math.sqrt(Math.abs(t)))) * Degree;
79

    
80
                if (lam2 < lam1) {
81
                    excess = -excess;
82
                }
83

    
84
                sum = sum + excess;
85
            }
86
        }
87
        return Math.abs(sum);
88
    } /*        SphericalPolyArea. */
89
}