aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/map/libmap/albers.c
blob: 56e3e9f591b5b0c84633cd4db0da07f6b66835aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <u.h>
#include <libc.h>
#include "map.h"

/* For Albers formulas see Deetz and Adams "Elements of Map Projection", */
/* USGS Special Publication No. 68, GPO 1921 */

static double r0sq, r1sq, d2, n, den, sinb1, sinb2;
static struct coord plat1, plat2;
static int southpole;

static double num(double s)
{
	if(d2==0)
		return(1);
	s = d2*s*s;
	return(1+s*(2./3+s*(3./5+s*(4./7+s*5./9))));
}

/* Albers projection for a spheroid, good only when N pole is fixed */

static int
Xspalbers(struct place *place, double *x, double *y)
{
	double r = sqrt(r0sq-2*(1-d2)*place->nlat.s*num(place->nlat.s)/n);
	double t = n*place->wlon.l;
	*y = r*cos(t);
	*x = -r*sin(t);
	if(!southpole)
		*y = -*y;
	else
		*x = -*x;
	return(1);
}

/* lat1, lat2: std parallels; e2: squared eccentricity */

static proj albinit(double lat1, double lat2, double e2)
{
	double r1;
	double t;
	for(;;) {
		if(lat1 < -90)
			lat1 = -180 - lat1;
		if(lat2 > 90)
			lat2 = 180 - lat2;
		if(lat1 <= lat2)
			break;
		t = lat1; lat1 = lat2; lat2 = t;
	}
	if(lat2-lat1 < 1) {
		if(lat1 > 89)
			return(azequalarea());
		return(0);
	}
	if(fabs(lat2+lat1) < 1)
		return(cylequalarea(lat1));
	d2 = e2;
	den = num(1.);
	deg2rad(lat1,&plat1);
	deg2rad(lat2,&plat2);
	sinb1 = plat1.s*num(plat1.s)/den;
	sinb2 = plat2.s*num(plat2.s)/den;
	n = (plat1.c*plat1.c/(1-e2*plat1.s*plat1.s) -
	    plat2.c*plat2.c/(1-e2*plat2.s*plat2.s)) /
	    (2*(1-e2)*den*(sinb2-sinb1));
	r1 = plat1.c/(n*sqrt(1-e2*plat1.s*plat1.s));
	r1sq = r1*r1;
	r0sq = r1sq + 2*(1-e2)*den*sinb1/n;
	southpole = lat1<0 && plat2.c>plat1.c;
	return(Xspalbers);
}

proj
sp_albers(double lat1, double lat2)
{
	return(albinit(lat1,lat2,EC2));
}

proj
albers(double lat1, double lat2)
{
	return(albinit(lat1,lat2,0.));
}

static double scale = 1;
static double twist = 0;

void
albscale(double x, double y, double lat, double lon)
{
	struct place place;
	double alat, alon, x1,y1;
	scale = 1;
	twist = 0;
	invalb(x,y,&alat,&alon);
	twist = lon - alon;
	deg2rad(lat,&place.nlat);
	deg2rad(lon,&place.wlon);
	Xspalbers(&place,&x1,&y1);
	scale = sqrt((x1*x1+y1*y1)/(x*x+y*y));
}

void
invalb(double x, double y, double *lat, double *lon)
{
	int i;
	double sinb_den, sinp;
	x *= scale;
	y *= scale;
	*lon = atan2(-x,fabs(y))/(RAD*n) + twist;
	sinb_den = (r0sq - x*x - y*y)*n/(2*(1-d2));
	sinp = sinb_den;
	for(i=0; i<5; i++)
		sinp = sinb_den/num(sinp);
	*lat = asin(sinp)/RAD;
}