| 1 |
/******************************************************************************* |
| 2 |
NAME STATE PLANE |
| 3 |
|
| 4 |
PURPOSE: Transforms input longitude and latitude to Easting and |
| 5 |
Northing for the State Plane projection. The |
| 6 |
longitude and latitude must be in radians. The Easting |
| 7 |
and Northing values will be returned in meters. |
| 8 |
|
| 9 |
PROGRAMMER DATE |
| 10 |
---------- ---- |
| 11 |
T. Mittan Apr, 1993 |
| 12 |
|
| 13 |
ALGORITHM REFERENCES |
| 14 |
|
| 15 |
1. Snyder, John P., "Map Projections--A Working Manual", U.S. Geological |
| 16 |
Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United |
| 17 |
State Government Printing Office, Washington D.C., 1987. |
| 18 |
|
| 19 |
2. Snyder, John P. and Voxland, Philip M., "An Album of Map Projections", |
| 20 |
U.S. Geological Survey Professional Paper 1453 , United State Government |
| 21 |
Printing Office, Washington D.C., 1989. |
| 22 |
HISTORY |
| 23 |
15-Nov-2005 TR NCR 44092 - Added defined(CYGWIN) |
| 24 |
*******************************************************************************/ |
| 25 |
#include <stdio.h> |
| 26 |
#include "cproj.h" |
| 27 |
|
| 28 |
static int id; |
| 29 |
static long inzone = 0; |
| 30 |
static long NAD27[134] = {101,102,5010,5300,201,202,203,301,302,401,402,403,404, |
| 31 |
405,406,407,501,502,503,600,700,901,902,903,1001,1002,5101, |
| 32 |
5102,5103,5104,5105,1101,1102,1103,1201,1202,1301,1302,1401, |
| 33 |
1402,1501,1502,1601,1602,1701,1702,1703,1801,1802,1900,2001, |
| 34 |
2002,2101,2102,2103,2111,2112,2113,2201,2202,2203,2301,2302, |
| 35 |
2401,2402,2403,2501,2502,2503,2601,2602,2701,2702,2703,2800, |
| 36 |
2900,3001,3002,3003,3101,3102,3103,3104,3200,3301,3302,3401, |
| 37 |
3402,3501,3502,3601,3602,3701,3702,3800,3901,3902,4001,4002, |
| 38 |
4100,4201,4202,4203,4204,4205,4301,4302,4303,4400,4501,4502, |
| 39 |
4601,4602,4701,4702,4801,4802,4803,4901,4902,4903,4904,5001, |
| 40 |
5002,5003,5004,5005,5006,5007,5008,5009,5201,5202,5400}; |
| 41 |
|
| 42 |
static long NAD83[134] = {101,102,5010,5300,201,202,203,301,302,401,402,403, |
| 43 |
404,405,406,0000,501,502,503,600,700,901,902,903,1001,1002, |
| 44 |
5101,5102,5103,5104,5105,1101,1102,1103,1201,1202,1301,1302, |
| 45 |
1401,1402,1501,1502,1601,1602,1701,1702,1703,1801,1802,1900, |
| 46 |
2001,2002,2101,2102,2103,2111,2112,2113,2201,2202,2203,2301, |
| 47 |
2302,2401,2402,2403,2500,0000,0000,2600,0000,2701,2702,2703, |
| 48 |
2800,2900,3001,3002,3003,3101,3102,3103,3104,3200,3301,3302, |
| 49 |
3401,3402,3501,3502,3601,3602,3701,3702,3800,3900,0000,4001, |
| 50 |
4002,4100,4201,4202,4203,4204,4205,4301,4302,4303,4400,4501, |
| 51 |
4502,4601,4602,4701,4702,4801,4802,4803,4901,4902,4903,4904, |
| 52 |
5001,5002,5003,5004,5005,5006,5007,5008,5009,5200,0000,5400}; |
| 53 |
/* Initialize the State Plane projection |
| 54 |
------------------------------------*/ |
| 55 |
int stplnforint( |
| 56 |
long zone, |
| 57 |
long sphere, |
| 58 |
char *fn27, |
| 59 |
char *fn83) |
| 60 |
{ |
| 61 |
long ind; |
| 62 |
long i; |
| 63 |
long nadval; |
| 64 |
double table[9]; |
| 65 |
char pname[32]; |
| 66 |
char buf[100]; |
| 67 |
double r_maj,r_min,scale_fact,center_lon; |
| 68 |
double center_lat,false_east,false_north; |
| 69 |
double azimuth,lat_orig,lon_orig,lon1,lat1,lon2,lat2; |
| 70 |
long mode,iflg; |
| 71 |
FILE *ptr; |
| 72 |
#ifndef BIG_ENDIAN |
| 73 |
char swap[16]; |
| 74 |
char* swapPtr; |
| 75 |
long j; |
| 76 |
#endif |
| 77 |
ind = -1; |
| 78 |
if (inzone != zone) |
| 79 |
inzone = zone; |
| 80 |
else |
| 81 |
return(OK); |
| 82 |
if (zone > 0) |
| 83 |
{ |
| 84 |
if (sphere == 0) |
| 85 |
{ |
| 86 |
for (i = 0; i < 134; i++) |
| 87 |
{ |
| 88 |
if (zone == NAD27[i]) |
| 89 |
{ |
| 90 |
ind = i; |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
else |
| 96 |
if (sphere == 8) |
| 97 |
{ |
| 98 |
for (i = 0; i < 134; i++) |
| 99 |
{ |
| 100 |
if (zone == NAD83[i]) |
| 101 |
{ |
| 102 |
ind = i; |
| 103 |
break; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
if (ind == -1) |
| 109 |
{ |
| 110 |
sprintf(buf,"Illegal zone #%4ld for spheroid #%4ld",zone,sphere); |
| 111 |
p_error(buf,"state-init"); |
| 112 |
return(21); |
| 113 |
} |
| 114 |
|
| 115 |
if (sphere == 0) |
| 116 |
ptr = fopen(fn27,"r"); |
| 117 |
else |
| 118 |
ptr = fopen(fn83,"r"); |
| 119 |
if (ptr == NULL) |
| 120 |
{ |
| 121 |
p_error("Error opening State Plane parameter file","state-for"); |
| 122 |
return(22); |
| 123 |
} |
| 124 |
fseek(ptr,(ind) * 432, 0); |
| 125 |
fread(pname,1,32,ptr); |
| 126 |
fread(&id,sizeof(int),1,ptr); |
| 127 |
fread(table,sizeof(double),9,ptr); |
| 128 |
fclose(ptr); |
| 129 |
|
| 130 |
#ifndef BIG_ENDIAN |
| 131 |
/* swap bytes as necessary */ |
| 132 |
swapPtr = (char*) (&id); |
| 133 |
for (i=0;i<sizeof(int);i++) |
| 134 |
{ |
| 135 |
swap[i] = swapPtr[sizeof(int) - 1 - i]; |
| 136 |
} |
| 137 |
id = *((int*)swap); |
| 138 |
|
| 139 |
for (i=0;i<9;i++) |
| 140 |
{ |
| 141 |
swapPtr = (char*) (table+i); |
| 142 |
for (j=0;j<sizeof(double);j++) |
| 143 |
{ |
| 144 |
swap[j] = swapPtr[sizeof(double) - 1 - j]; |
| 145 |
} |
| 146 |
table[i] = *((double*)swap); |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
if (id <= 0) |
| 151 |
{ |
| 152 |
sprintf(buf,"Illegal zone #%4ld for spheroid #%4ld",zone,sphere); |
| 153 |
p_error(buf,"state-init"); |
| 154 |
return(21); |
| 155 |
} |
| 156 |
|
| 157 |
/* Report parameters to the user |
| 158 |
-----------------------------*/ |
| 159 |
ptitle("STATE PLANE"); |
| 160 |
genrpt_long(zone,"Zone: "); |
| 161 |
if (sphere == 0) |
| 162 |
nadval = 27; |
| 163 |
else |
| 164 |
nadval = 83; |
| 165 |
genrpt_long(nadval,"Datum: NAD"); |
| 166 |
|
| 167 |
r_maj = table[0]; |
| 168 |
r_min = (sqrt(1.0 - table[1])) * table[0]; |
| 169 |
|
| 170 |
/* initialize proper projection |
| 171 |
-----------------------------*/ |
| 172 |
if (id == 1) |
| 173 |
{ |
| 174 |
scale_fact = table[3]; |
| 175 |
center_lon = paksz(pakcz(table[2]),&iflg) * D2R; |
| 176 |
if (iflg != 0) |
| 177 |
return(iflg); |
| 178 |
center_lat = paksz(pakcz(table[6]),&iflg) * D2R; |
| 179 |
if (iflg != 0) |
| 180 |
return(iflg); |
| 181 |
false_east = table[7]; |
| 182 |
false_north = table[8]; |
| 183 |
tmforint(r_maj,r_min,scale_fact,center_lon,center_lat,false_east, |
| 184 |
false_north); |
| 185 |
} |
| 186 |
else |
| 187 |
if (id == 2) |
| 188 |
{ |
| 189 |
lat1 = paksz(pakcz(table[5]),&iflg) * D2R; |
| 190 |
if (iflg != 0) |
| 191 |
return(iflg); |
| 192 |
lat2 = paksz(pakcz(table[4]),&iflg) * D2R; |
| 193 |
if (iflg != 0) |
| 194 |
return(iflg); |
| 195 |
center_lon = paksz(pakcz(table[2]),&iflg) * D2R; |
| 196 |
if (iflg != 0) |
| 197 |
return(iflg); |
| 198 |
center_lat = paksz(pakcz(table[6]),&iflg) * D2R; |
| 199 |
if (iflg != 0) |
| 200 |
return(iflg); |
| 201 |
false_east = table[7]; |
| 202 |
false_north = table[8]; |
| 203 |
lamccforint(r_maj,r_min,lat1,lat2,center_lon,center_lat,false_east, |
| 204 |
false_north); |
| 205 |
} |
| 206 |
else |
| 207 |
if (id == 3) |
| 208 |
{ |
| 209 |
center_lon = paksz(pakcz(table[2]),&iflg) * D2R; |
| 210 |
if (iflg != 0) |
| 211 |
return(iflg); |
| 212 |
center_lat = paksz(pakcz(table[3]),&iflg) * D2R; |
| 213 |
if (iflg != 0) |
| 214 |
return(iflg); |
| 215 |
false_east = table[4]; |
| 216 |
false_north = table[5]; |
| 217 |
polyforint(r_maj,r_min,center_lon,center_lat,false_east,false_north); |
| 218 |
} |
| 219 |
else |
| 220 |
if (id == 4) |
| 221 |
{ |
| 222 |
scale_fact = table[3]; |
| 223 |
azimuth = paksz(pakcz(table[5]),&iflg) * D2R; |
| 224 |
if (iflg != 0) |
| 225 |
return(iflg); |
| 226 |
lon_orig = paksz(pakcz(table[2]),&iflg) * D2R; |
| 227 |
if (iflg != 0) |
| 228 |
return(iflg); |
| 229 |
lat_orig = paksz(pakcz(table[6]),&iflg) * D2R; |
| 230 |
if (iflg != 0) |
| 231 |
return(iflg); |
| 232 |
false_east = table[7]; |
| 233 |
false_north = table[8]; |
| 234 |
mode = 1; |
| 235 |
lon1 = 0.0; |
| 236 |
lon2 = 0.0; |
| 237 |
lat1 = 0.0; /* for mode > 0 setting lat1 and lat2 to zero do |
| 238 |
make any difference in omerforint */ |
| 239 |
lat2 = 0.0; |
| 240 |
omerforint(r_maj,r_min,scale_fact,azimuth,lon_orig,lat_orig,false_east, |
| 241 |
false_north,lon1,lat1,lon2,lat2,mode); |
| 242 |
} |
| 243 |
return(OK); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/* State plane forward equations--mapping lat,long to x,y |
| 248 |
-----------------------------------------------------*/ |
| 249 |
int stplnfor( |
| 250 |
double lon, /* (I) Longitude */ |
| 251 |
double lat, /* (I) Latitude */ |
| 252 |
double *x, /* (O) X projection coordinate */ |
| 253 |
double *y) /* (O) Y projection coordinate */ |
| 254 |
{ |
| 255 |
long iflg; |
| 256 |
|
| 257 |
/* Forward equations |
| 258 |
-----------------*/ |
| 259 |
if (id == 1) |
| 260 |
{ |
| 261 |
if ((iflg = tmfor(lon, lat, x, y)) != 0) |
| 262 |
return(iflg); |
| 263 |
} |
| 264 |
else |
| 265 |
if (id == 2) |
| 266 |
{ |
| 267 |
if ((iflg = lamccfor(lon, lat, x, y)) != 0) |
| 268 |
return(iflg); |
| 269 |
} |
| 270 |
else |
| 271 |
if (id == 3) |
| 272 |
{ |
| 273 |
if ((iflg = polyfor(lon, lat, x, y)) != 0) |
| 274 |
return(iflg); |
| 275 |
} |
| 276 |
else |
| 277 |
if (id == 4) |
| 278 |
{ |
| 279 |
if ((iflg = omerfor(lon, lat, x, y)) != 0) |
| 280 |
return(iflg); |
| 281 |
} |
| 282 |
|
| 283 |
return(OK); |
| 284 |
} |