|
| 1 | +/* |
| 2 | +
|
| 3 | +/* |
| 4 | + lib_mysqludf_fPROJ4 - An extended set of scientific functions which |
| 5 | + converts geographic longitude and latitude coordinates into cartesian |
| 6 | + coordinates and vice versa. |
| 7 | + |
| 8 | + Copyright (C) 2010 Ferhat Bingöl |
| 9 | + web: http://www.mysqludf.org/ |
| 10 | + |
| 11 | + |
| 12 | + This library is free software; you can redistribute it and/or |
| 13 | + modify it under the terms of the GNU Lesser General Public |
| 14 | + License as published by the Free Software Foundation; either |
| 15 | + version 2.1 of the License, or (at your option) any later version. |
| 16 | + |
| 17 | + This library is distributed in the hope that it will be useful, |
| 18 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | + Lesser General Public License for more details. |
| 21 | + |
| 22 | + You should have received a copy of the GNU Lesser General Public |
| 23 | + License along with this library; if not, write to the Free Software |
| 24 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | +*/ |
| 26 | + |
| 27 | + |
| 28 | +#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32) |
| 29 | +#define DLLEXP __declspec(dllexport) |
| 30 | +#else |
| 31 | +#define DLLEXP |
| 32 | +#endif |
| 33 | + |
| 34 | +#ifdef STANDARD |
| 35 | +#include <string.h> |
| 36 | +#include <stdlib.h> |
| 37 | +#include <time.h> |
| 38 | +#ifdef __WIN__ |
| 39 | +typedef unsigned __int64 ulonglong; |
| 40 | +typedef __int64 longlong; |
| 41 | +#else |
| 42 | +typedef unsigned long long ulonglong; |
| 43 | +typedef long long longlong; |
| 44 | +#endif /*__WIN__*/ |
| 45 | +#else |
| 46 | + |
| 47 | +#include <my_global.h> |
| 48 | +#include <my_sys.h> |
| 49 | +#endif |
| 50 | +#include <proj_api.h> |
| 51 | +#include <mysql.h> |
| 52 | + |
| 53 | +#include <m_ctype.h> |
| 54 | +#include <m_string.h> |
| 55 | +#include <stdlib.h> |
| 56 | +#include <string.h> |
| 57 | + |
| 58 | + |
| 59 | +#include <ctype.h> |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +#ifdef HAVE_DLOPEN |
| 64 | + |
| 65 | +#define LIBVERSION "lib_mysqludf_fPROJ4 version 0.1 (2010.05.09)" |
| 66 | + |
| 67 | + |
| 68 | +#ifdef __cplusplus |
| 69 | +extern "C" { |
| 70 | +#endif |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +// fPROJ4_info |
| 75 | +// LIBRARY INFORMATION |
| 76 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 77 | +DLLEXP my_bool lib_mysqludf_fPROJ4_info_init(UDF_INIT *initid,UDF_ARGS *args, char *message); |
| 78 | +DLLEXP void lib_mysqludf_fPROJ4_info_deinit(UDF_INIT *initid); |
| 79 | +DLLEXP char* lib_mysqludf_fPROJ4_info(UDF_INIT *initid,UDF_ARGS *args,char* result, unsigned long* length, char *is_null, char *error); |
| 80 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 81 | + |
| 82 | +// FUNCTION: |
| 83 | +// fPROJ4_transform |
| 84 | +// PORTED FROM: |
| 85 | +// int pj_transform( projPJ src, projPJ dst, long point_count, int point_offset, double *x, double *y, double *z ); |
| 86 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 87 | +DLLEXP my_bool fPROJ4_transform_init(UDF_INIT *initid,UDF_ARGS *args,char *message); |
| 88 | +DLLEXP void fPROJ4_transform_deinit(UDF_INIT *initid); |
| 89 | +DLLEXP double fPROJ4_transform(UDF_INIT *initid,UDF_ARGS *args,char *is_null,char *error); |
| 90 | + |
| 91 | +// FUNCTION: |
| 92 | +// fPROJ4_Geo2UTM |
| 93 | +// MySci specific function, based on pj_transform(...) |
| 94 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 95 | +DLLEXP my_bool fPROJ4_Geo2UTM_init(UDF_INIT *initid,UDF_ARGS *args,char *message); |
| 96 | +DLLEXP void fPROJ4_Geo2UTM_deinit(UDF_INIT *initid); |
| 97 | +DLLEXP double fPROJ4_Geo2UTM(UDF_INIT *initid,UDF_ARGS *args,char *is_null,char *error); |
| 98 | + |
| 99 | +// FUNCTION: |
| 100 | +// fPROJ4_UTM2Geo |
| 101 | +// MySci specific function, based on pj_transform(...) |
| 102 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 103 | +DLLEXP my_bool fPROJ4_UTM2Geo_init(UDF_INIT *initid,UDF_ARGS *args,char *message); |
| 104 | +DLLEXP void fPROJ4_UTM2Geo_deinit(UDF_INIT *initid); |
| 105 | +DLLEXP double fPROJ4_UTM2Geo(UDF_INIT *initid,UDF_ARGS *args,char *is_null,char *error); |
| 106 | + |
| 107 | + |
| 108 | +#ifdef __cplusplus |
| 109 | +} |
| 110 | +#endif |
| 111 | + |
| 112 | + |
| 113 | +my_bool lib_mysqludf_fPROJ4_info_init(UDF_INIT *initid,UDF_ARGS *args,char *message) |
| 114 | +{ |
| 115 | + my_bool status; |
| 116 | + if(args->arg_count!=0) |
| 117 | + { |
| 118 | + strcpy(message,"No arguments allowed (usage: lib_mysqludf_fPROJ4_info() )"); |
| 119 | + status = 1; |
| 120 | + } |
| 121 | + else status = 0; |
| 122 | + |
| 123 | + return status; |
| 124 | +} |
| 125 | +void lib_mysqludf_fPROJ4_info_deinit(UDF_INIT *initid) |
| 126 | +{ |
| 127 | + // Nothing to do |
| 128 | +} |
| 129 | +char* lib_mysqludf_fPROJ4_info(UDF_INIT *initid,UDF_ARGS *args,char* result,unsigned long* length,char *is_null,char *error) |
| 130 | +{ |
| 131 | + char temp[100]; |
| 132 | + const char *release; |
| 133 | + release=pj_get_release(); |
| 134 | + sprintf(temp,"%s (PROJ4 %s)",LIBVERSION,release); |
| 135 | + strcpy(result,temp); |
| 136 | + *length = strlen(result); |
| 137 | + return result; |
| 138 | +} |
| 139 | + |
| 140 | +// FUNCTION: fPROJ4_transform |
| 141 | +////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 142 | +my_bool fPROJ4_transform_init(UDF_INIT *initid,UDF_ARGS *args,char *message) |
| 143 | +{ |
| 144 | + my_bool status; |
| 145 | + if(args->arg_count!=6) |
| 146 | + { |
| 147 | + strcpy( message ,"Input 6 argument(s) (usage: fPROJ4_transform(double x, double y, double z, char *projPJ_src, char *projPJ_dst, int xyz) )"); |
| 148 | + status = 1; |
| 149 | + } |
| 150 | + else if(!(initid->ptr = malloc(sizeof(double)))) |
| 151 | + { |
| 152 | + initid->ptr = NULL; |
| 153 | + strcpy(message, "Could not allocate memory (fPROJ4_transform)"); |
| 154 | + status = 1; |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + initid->maybe_null= 1; |
| 159 | + initid->decimals= 6; |
| 160 | + |
| 161 | + args->arg_type[0] = REAL_RESULT; |
| 162 | + args->arg_type[1] = REAL_RESULT; |
| 163 | + args->arg_type[2] = REAL_RESULT; |
| 164 | + args->arg_type[3] = STRING_RESULT; |
| 165 | + args->arg_type[4] = STRING_RESULT; |
| 166 | + args->arg_type[5] = INT_RESULT; |
| 167 | + |
| 168 | + *((double *)initid->ptr) = 0; |
| 169 | + status = 0; |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + return status; |
| 174 | +} |
| 175 | +void fPROJ4_transform_deinit(UDF_INIT *initid) |
| 176 | +{ |
| 177 | + if(initid->ptr!=NULL) free(initid->ptr); |
| 178 | +} |
| 179 | +double fPROJ4_transform(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) |
| 180 | +{ |
| 181 | + projPJ pj_in, pj_out; |
| 182 | + double x, y,z; |
| 183 | + int p,t; |
| 184 | + |
| 185 | + if(args->args[5]!=NULL && atoi(args->args[5])<3) |
| 186 | + { |
| 187 | + x=*((const double*)args->args[0]); |
| 188 | + y=*((const double*)args->args[1]); |
| 189 | + z=*((const double*)args->args[2]); |
| 190 | + //x *= DEG_TO_RAD; |
| 191 | + //y *= DEG_TO_RAD; |
| 192 | + if (!(pj_in = pj_init_plus(args->args[3]) ) ) exit(0); |
| 193 | + if (!(pj_out = pj_init_plus(args->args[4]) ) ) exit(0); |
| 194 | + t=*((const int*)args->args[5]); |
| 195 | + |
| 196 | + p = pj_transform(pj_in, pj_out, 1, 1, &x, &y, &z ); |
| 197 | + |
| 198 | + if(t==0) *((double *)initid->ptr) = x; |
| 199 | + else if(t==1) *((double *)initid->ptr) = y; |
| 200 | + else if(t==2) *((double *)initid->ptr) = z; |
| 201 | + else *((double *)initid->ptr) = 0.0; |
| 202 | + } |
| 203 | + else *((double *)initid->ptr) = 0.0; |
| 204 | + |
| 205 | + pj_free(pj_in); |
| 206 | + pj_free(pj_out); |
| 207 | + |
| 208 | + return *((double *)initid->ptr); |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | +// FUNCTION: fPROJ4_Geo2UTM |
| 213 | +////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 214 | +my_bool fPROJ4_Geo2UTM_init(UDF_INIT *initid,UDF_ARGS *args,char *message) |
| 215 | +{ |
| 216 | + my_bool status; |
| 217 | + if(args->arg_count!=4) |
| 218 | + { |
| 219 | + strcpy( message ,"Input 4 argument(s) (usage: fPROJ4_Geo2UTM(double x, double y, int zone, int xyz) )"); |
| 220 | + status = 1; |
| 221 | + } |
| 222 | + else if(!(initid->ptr = malloc(sizeof(double)))) |
| 223 | + { |
| 224 | + initid->ptr = NULL; |
| 225 | + strcpy(message, "Could not allocate memory (fPROJ4_Geo2UTM)"); |
| 226 | + status = 1; |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + initid->maybe_null= 1; |
| 231 | + initid->decimals= 10; |
| 232 | + |
| 233 | + args->arg_type[0] = REAL_RESULT; |
| 234 | + args->arg_type[1] = REAL_RESULT; |
| 235 | + args->arg_type[2] = INT_RESULT; |
| 236 | + args->arg_type[3] = INT_RESULT; |
| 237 | + |
| 238 | + *((double *)initid->ptr) = 0; |
| 239 | + status = 0; |
| 240 | + |
| 241 | + } |
| 242 | + |
| 243 | + return status; |
| 244 | +} |
| 245 | +void fPROJ4_Geo2UTM_deinit(UDF_INIT *initid) |
| 246 | +{ |
| 247 | + if(initid->ptr!=NULL) free(initid->ptr); |
| 248 | +} |
| 249 | +double fPROJ4_Geo2UTM(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) |
| 250 | +{ |
| 251 | + projPJ pj_in, pj_out; |
| 252 | + double x, y; |
| 253 | + int p,t,zone; |
| 254 | + char *tmp; |
| 255 | + |
| 256 | + tmp=(char*)malloc(sizeof(char)*200); |
| 257 | + |
| 258 | + if(args->args[3]!=NULL && atoi(args->args[3])<2) |
| 259 | + { |
| 260 | + x=*((const double*)args->args[0]); |
| 261 | + y=*((const double*)args->args[1]); |
| 262 | + x *= DEG_TO_RAD; |
| 263 | + y *= DEG_TO_RAD; |
| 264 | + zone=*((const int*)args->args[2]); |
| 265 | + if (!(pj_in = pj_init_plus("+proj=latlong +ellps=clrk66") ) ) exit(0); |
| 266 | + sprintf(tmp,"+proj=utm +datum=WGS84 +zone=%i",zone); |
| 267 | + if (!(pj_out = pj_init_plus(tmp) ) ) exit(0); |
| 268 | + free(tmp); |
| 269 | + t=*((const int*)args->args[3]); |
| 270 | + |
| 271 | + p = pj_transform(pj_in, pj_out, 1, 1, &x, &y, 0 ); |
| 272 | + |
| 273 | + if(t==0) *((double *)initid->ptr) = x; |
| 274 | + else if(t==1) *((double *)initid->ptr) = y; |
| 275 | + else *((double *)initid->ptr) = 0.0; |
| 276 | + } |
| 277 | + else *((double *)initid->ptr) = 0.0; |
| 278 | + |
| 279 | + pj_free(pj_in); |
| 280 | + pj_free(pj_out); |
| 281 | + |
| 282 | + return *((double *)initid->ptr); |
| 283 | +} |
| 284 | + |
| 285 | + |
| 286 | + |
| 287 | +// FUNCTION: fPROJ4_UTM2Geo |
| 288 | +////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 289 | +my_bool fPROJ4_UTM2Geo_init(UDF_INIT *initid,UDF_ARGS *args,char *message) |
| 290 | +{ |
| 291 | + my_bool status; |
| 292 | + if(args->arg_count!=4) |
| 293 | + { |
| 294 | + strcpy( message ,"Input 4 argument(s) (usage: fPROJ4_UTM2Geo(double x, double y, int zone, int xyz) )"); |
| 295 | + status = 1; |
| 296 | + } |
| 297 | + else if(!(initid->ptr = malloc(sizeof(double)))) |
| 298 | + { |
| 299 | + initid->ptr = NULL; |
| 300 | + strcpy(message, "Could not allocate memory (fPROJ4_UTM2Geo)"); |
| 301 | + status = 1; |
| 302 | + } |
| 303 | + else |
| 304 | + { |
| 305 | + initid->maybe_null= 1; |
| 306 | + initid->decimals= 10; |
| 307 | + |
| 308 | + args->arg_type[0] = REAL_RESULT; |
| 309 | + args->arg_type[1] = REAL_RESULT; |
| 310 | + args->arg_type[2] = INT_RESULT; |
| 311 | + args->arg_type[3] = INT_RESULT; |
| 312 | + |
| 313 | + *((double *)initid->ptr) = 0; |
| 314 | + status = 0; |
| 315 | + |
| 316 | + } |
| 317 | + |
| 318 | + return status; |
| 319 | +} |
| 320 | +void fPROJ4_UTM2Geo_deinit(UDF_INIT *initid) |
| 321 | +{ |
| 322 | + if(initid->ptr!=NULL) free(initid->ptr); |
| 323 | +} |
| 324 | +double fPROJ4_UTM2Geo(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) |
| 325 | +{ |
| 326 | + projPJ pj_in, pj_out; |
| 327 | + double x, y; |
| 328 | + int p,t,zone; |
| 329 | + char *tmp; |
| 330 | + |
| 331 | + tmp=(char*)malloc(sizeof(char)*200); |
| 332 | + |
| 333 | + if(args->args[3]!=NULL && atoi(args->args[3])<2) |
| 334 | + { |
| 335 | + x=*((const double*)args->args[0]); |
| 336 | + y=*((const double*)args->args[1]); |
| 337 | + //x *= DEG_TO_RAD; |
| 338 | + //y *= DEG_TO_RAD; |
| 339 | + zone=*((const int*)args->args[2]); |
| 340 | + sprintf(tmp,"+proj=utm +datum=WGS84 +zone=%i",zone); |
| 341 | + if (!(pj_in = pj_init_plus(tmp) ) ) exit(0); |
| 342 | + |
| 343 | + if (!(pj_out = pj_init_plus("+proj=latlong +ellps=clrk66") ) ) exit(0); |
| 344 | + free(tmp); |
| 345 | + t=*((const int*)args->args[3]); |
| 346 | + |
| 347 | + p = pj_transform(pj_in, pj_out, 1, 1, &x, &y, 0 ); |
| 348 | + |
| 349 | + if(t==0) *((double *)initid->ptr) = x/DEG_TO_RAD; |
| 350 | + else if(t==1) *((double *)initid->ptr) = y/DEG_TO_RAD; |
| 351 | + else *((double *)initid->ptr) = 0.0; |
| 352 | + } |
| 353 | + else *((double *)initid->ptr) = 0.0; |
| 354 | + |
| 355 | + pj_free(pj_in); |
| 356 | + pj_free(pj_out); |
| 357 | + |
| 358 | + return *((double *)initid->ptr); |
| 359 | +} |
| 360 | +#endif /* HAVE_DLOPEN */ |
| 361 | + |
0 commit comments