PROJ * proj_init(int argc, char **argv) { const char *s, *name; PROJ *(*proj)(PROJ *); paralist *curr = 0, *start = 0; int i; PROJ *PIN = 0; errno = proj_errno = 0; /* put arguments into internal linked list */ if (argc <= 0) { proj_errno = -1; goto bum_call; } for (i = 0; i < argc; ++i) if (i) curr = curr->next = proj_mkparam(argv[i]); else start = curr = proj_mkparam(argv[i]); if (proj_errno) goto bum_call; /* find projection selection */ if (!(name = proj_param(start, "sproj").s)) { proj_errno = -4; goto bum_call; } for (i = 0; (s = proj_list[i].id) && strcmp(name, s) ; ++i) ; if (!s) { proj_errno = -5; goto bum_call; } proj = proj_list[i].proj; /* allocate projection structure */ if (!(PIN = (*proj)(0))) goto bum_call; PIN->params = start; /* set ellipsoid/sphere parameters */ if (proj_ell_set(start, &PIN->a, &PIN->es)) goto bum_call; PIN->e = sqrt(PIN->es); PIN->ra = 1. / PIN->a; PIN->one_es = 1. - PIN->es; if (PIN->one_es == 0.) { proj_errno = -6; goto bum_call; } PIN->rone_es = 1./PIN->one_es; /* set PIN->geoc coordinate system */ PIN->geoc = (PIN->es && proj_param(start, "bgeoc").i); /* over-ranging flag */ PIN->over = proj_param(start, "bover").i; /* central meridian */ PIN->lam0=proj_param(start, "rlon_0").f; /* central latitude */ PIN->phi0 = proj_param(start, "rlat_0").f; /* false easting and northing */ PIN->x0 = proj_param(start, "dx_0").f; PIN->y0 = proj_param(start, "dy_0").f; /* general scaling factor */ if (proj_param(start, "tk_0").i) PIN->k0 = proj_param(start, "dk_0").f; else if (proj_param(start, "tk").i) PIN->k0 = proj_param(start, "dk").f; else PIN->k0 = 1.; if (PIN->k0 <= 0.) { proj_errno = -31; goto bum_call; } /* set units */ s = 0; if ((name = proj_param(start, "sunits").s)) { for (i = 0; (s = proj_units[i].id) && strcmp(name, s) ; ++i) ; if (!s) { proj_errno = -7; goto bum_call; } s = proj_units[i].to_meter; } if (s || (s = proj_param(start, "sto_meter").s)) { PIN->to_meter = strtod(s, (char **)&s); if (*s == '/') /* ratio number */ PIN->to_meter /= strtod(++s, 0); PIN->fr_meter = 1. / PIN->to_meter; } else PIN->to_meter = PIN->fr_meter = 1.; /* projection specific initialization */ if (!(PIN = (*proj)(PIN)) || errno || proj_errno) { bum_call: /* cleanup error return */ if (!proj_errno) proj_errno = errno; if (PIN) proj_free(PIN); else for ( ; start; start = curr) { curr = start->next; free(start); } PIN = 0; } return PIN; }
int runSimulator(char *ip, int port) { int bear; int knots; int mps=0; int myTime =0; int lapse=defaultLapse; long x, y, d; float lat, lon; frm_cmd_gps_t gps; char buff[TRANS_MAX_BUFF_SIZE]; size_t transLen; int seq =0; int sckt; struct sockaddr_in server; time_t timestamp; PROJ *p; PROJ_XY xy; PROJ_LP lp; // INIT SOCKET printf("Incializando...\n"); memset(&server,0,sizeof(server)); sckt= socket(AF_INET,SOCK_DGRAM,0); if (sckt<0) { die("Error en la creacion del socket"); } server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(ip); server.sin_port = htons(port); //Projection initialization //WGS84 p = proj_initstr("proj=merc ellps=WGS84"); lp.phi = ORIG_LAT * D_TO_R; lp.lam = ORIG_LON * D_TO_R; xy = proj_fwd(lp,p); printf("Empezando simulacion...(lapso=%ds)\n\n",lapse); while (true) { if (myTime<=0) { myTime = rnd1(30); bear = rnd0(359); mps = rnd1(MAX_MPS); } d = mps * lapse; // Ojo origen de rumbo PI xy.y += round( d * cos(bear * D_TO_R)); xy.x += round( d * sin(bear * D_TO_R)); lp = proj_inv(xy,p); knots = mps * MPS_TO_KNOTS; int offset=0; transLen = sizeof(buff); lat = lp.phi* R_TO_D; lon = lp.lam * R_TO_D; timestamp = time(NULL); frame_encode_gps(seq++,bear,knots,lat,lon,1,rnd1(20),timestamp,buff,&transLen); offset +=transLen; if (addCnxFrame) { transLen = sizeof(buff) - offset; frame_encode_cnx("1234567890",0,1,0,buff+offset,&transLen); offset +=transLen; } transLen = sizeof(buff); if ( frame_encode_transport(1,NULL,offset,buff,&transLen) ) { // if (frame_test_transport(buff,transLen)) { // printf("Trama de transporte verificada\n"); // } printf("Enviando: %d lat=%f lon=%f bear=%d knots=%d time=%d\n",seq, lat,lon,bear,knots,timestamp); sendto(sckt,buff,transLen,0,(struct sockaddr *)&server,sizeof(server)); } myTime -=lapse; sleep(lapse); } proj_free(p); close(sckt); return 0; }