void mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { Dbptr db; Point *polygon; char *pname; char *ptype; char *auth; char *dir; char *dfile; char *s_pcode; int pcode; double *lat; double *lon; long nsamp; mwSize nlat,nlon; const mwSize *dimensions; int i; int retcode; char *dbfile_name; char *dbschema_name; char errmsg[STRSZ]; if ( nrhs != 9 ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( ! get_dbptr( prhs[0], &db ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsDouble( prhs[1] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsDouble( prhs[2] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[3] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[4] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[5] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[6] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[7] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if ( !mxIsChar( prhs[8] ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } dimensions= (mwSize*) mxGetDimensions( prhs[1] ); if (dimensions[1] != 1 && dimensions[0] != 1) { mexErrMsgTxt("writepolygondata: LAT must have dimensions Nx1 or 1xN\n"); } if (dimensions[1] == 1) { nlat= dimensions[0]; } else { nlat= dimensions[1]; } dimensions= (mwSize *)mxGetDimensions( prhs[2] ); if (dimensions[1] != 1 && dimensions[0] != 1) { mexErrMsgTxt("writepolygondata: LON must have dimensions Nx1 or 1xN\n"); } if (dimensions[1] == 1) { nlon= dimensions[0]; } else { nlon= dimensions[1]; } if (nlat != nlon) { mexErrMsgTxt("writepolygondata: LAT and LON must be of same size\n"); } if( ( retcode = dbquery(db, dbDATABASE_FILENAME, &dbfile_name) ) < 0 ) { mexErrMsgTxt("writepolygondata: problem getting descriptor filename\n"); } if ( is_file(dbfile_name)==1 ) { if( ( retcode = dbquery(db, dbSCHEMA_NAME, &dbschema_name) ) < 0 ) { mexErrMsgTxt("writepolygondata: problem getting schema name\n"); } else { if (strmatches(dbschema_name,"polygon1.2.*", 0) != 1) { sprintf(errmsg, "writepolygondata: wrong schema '%s' for output database '%s'\n",dbschema_name,dbfile_name); mexErrMsgTxt(errmsg); } } } else { sprintf(errmsg, "writepolygondata: please create database schema '%s' with schema polygon1.2\n",dbfile_name); mexErrMsgTxt(errmsg); //there might be a fix, but for the moment this simply does NOT work if (dbcreate(dbfile_name,"polygon1.2",0, 0, 0) != 0 ) { mexErrMsgTxt("writepolygondata: problem with dbcreate\n"); } } nsamp= (long) nlat; lat= mxGetPr(prhs[1]); lon= mxGetPr(prhs[2]); polygon= mxCalloc(nsamp+1,sizeof(Point)); for (i= 0; i < nsamp; i++) { polygon[i].lat=lat[i]; polygon[i].lon=lon[i]; } get_malloced_string(prhs[3],&pname); get_malloced_string(prhs[4],&ptype); get_malloced_string(prhs[5],&auth); get_malloced_string(prhs[6],&dir); get_malloced_string(prhs[7],&dfile); get_malloced_string(prhs[8],&s_pcode); pcode= polycode(s_pcode); /*maybe nsamp -1, check later...*/ writePolygonData(db,polygon,nsamp,pname,1,1,ptype,auth,dir,dfile,pcode); antelope_mex_clear_register(1); mxFree(polygon); mxFree(s_pcode); mxFree(dfile); mxFree(dir); mxFree(auth); mxFree(ptype); mxFree(pname); }
int main (int argc, char **argv) { int c, verbose = 0, errflg = 0; char *dbinname=malloc(1024); char *dboutname=malloc(1024); Point *poly; double lat,lon; char *auth=strdup("regions2polygon"); char *ptype= strdup("rp"); char *dir=strdup("."); char *dfile=strdup("polygons"); int ftype=polyFLOAT; char *name; int nregions, nvertices; Tbl *sortkeys, *groupkeys; Dbptr dbin,dbout,dbi,dbo,dbg,dbb; int i,from,to,nv; int vertex; elog_init ( argc, argv ) ; while ((c = getopt (argc, argv, "vV")) != -1) { switch (c) { case 'v': verbose++ ; break; case 'V': usage (); break; case '?': errflg++; break ; } } if ((errflg) || argc < 3) usage (); dbinname = argv[optind++]; dboutname= argv[optind++]; if (dbopen(dbinname,"r",&dbin)) { elog_die(1,"cannot open database %s",dbinname); } dbi=dblookup(dbin,0,"regions",0,0); sortkeys=newtbl(2); pushtbl(sortkeys,"regname"); pushtbl(sortkeys,"vertex"); groupkeys=newtbl(1); pushtbl(groupkeys,"regname"); dbi=dbsort(dbi,sortkeys,0,"regions.sorted"); dbg=dbgroup(dbi,groupkeys,0,0); dbquery(dbg,dbRECORD_COUNT,&nregions); if (nregions <1) { elog_die(0,"table regions seems to be empty (or not present)"); } if (verbose) elog_notify(0,"creating database descriptor %s",dboutname); if (dbcreate(dboutname,"polygon1.2",0,0,0)) { elog_die(1,"cannot create database %s",dboutname); } dbopen(dboutname,"r+",&dbout); dbo=dblookup(dbout,0,"polygon",0,0); for (i=0; i< nregions; i++) { dbg.record=i; dbgetv(dbg,0,"regname",name,"bundle",&dbb,0); dbget_range(dbb,&from,&to); nvertices= to - from; if (verbose) elog_notify(0,"%s (%i nvertices)",name,nvertices); poly=malloc(2 * nvertices * sizeof(double)); nv=0; for (dbi.record=from; dbi.record<to; dbi.record++) { dbgetv(dbi,0, "regname",name, "vertex",&vertex, "lat",&lat,"lon",&lon, 0); poly[nv].lat=lat; poly[nv].lon=lon; nv++; } writePolygonData(dbo,poly,nv,name,1,0,ptype,auth,dir,dfile,ftype); free(poly); } /* */ return 0; }
int main(int argc, char **argv) { int verbose = 0; char *dbname = malloc(STRSZ); char *datafilename = malloc(STRSZ); char *name = malloc(STRSZ); char *line = malloc(STRSZ); char str1[20], str2[20], *str3 = malloc(1024); int level = 0; int closed = 0, close = 0; int maxpoints = 5000; Point *poly = malloc(maxpoints * sizeof(double)); double lat, lon; long npoints; char *auth = malloc(STRSZ); char ptype[STRSZ]; char *dir = malloc(STRSZ), *dfile = malloc(STRSZ), *tname = malloc(STRSZ), numstr[10]; int ftype = polyFLOAT; Dbptr db; FILE *fh; int putit; int polycounter = 0; elog_init(argc, argv); my_username(auth); strcat(auth, ":gmt"); strcpy(name, "-"); strcpy(dir, "-"); strcpy(dfile, "gmt.bin"); strcpy(ptype, "-"); for (argc--, argv++; argc > 0; argc--, argv++) { if (!strcmp(*argv, "-auth")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -auth argument.\n"); usage(); exit(1); } auth = *argv; } else if (!strcmp(*argv, "-dir")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -dir argument.\n"); usage(); exit(1); } dir = *argv; } else if (!strcmp(*argv, "-dfile")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -dfile argument.\n"); usage(); exit(1); } dfile = *argv; } else if (!strcmp(*argv, "-pname")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -pname argument.\n"); usage(); exit(1); } name = *argv; } else if (!strcmp(*argv, "-ptype")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -ptype argument.\n"); usage(); exit(1); } strncpy(ptype, *argv, 2); } else if (!strcmp(*argv, "-level")) { argc--; argv++; if (argc < 3) { elog_complain(0, "Need -level argument.\n"); usage(); exit(1); } level = atoi(*argv); } else if (!strcmp(*argv, "-close_polygons")) { close = 1; } else if (!strcmp(*argv, "-v")) { verbose++; } else if (**argv != '-') { break; } else { elog_complain(0, "unrecognized argument '%s'\n", *argv); usage(); exit(1); } } if (argc < 2) { elog_complain(0, "Need datfile and db arguments.\n"); usage(); exit(1); } datafilename = *argv; argc--; argv++; dbname = *argv; if (access(datafilename, 4)) { elog_die(0, "datafile %s should be READABLE!\n", datafilename); } if (dbcreate(dbname, "polygon1.2", 0, 0, 0)) { elog_die(1, "can't create database %s\n", dbname); } if (!strcmp(name, "-")) { strcpy(name, datafilename); } dbopen(dbname, "r+", &db); db = dblookup(db, 0, "polygon", 0, 0); fh = fopen(datafilename, "r"); putit = 0; npoints = 0; polycounter = 0; while (fgets(line, 80, fh) != 0) { if (line[0] == '>') { if (putit) { strcpy(tname, name); if (polycounter > 0) { sprintf(numstr, ".%d", polycounter); tname = strcat(tname, numstr); } if (verbose) { elog_notify(0, "%s: (%ld points n file %s/%s)\n", tname, npoints, dir, dfile); } if (npoints > 2 && (close || ( (poly[0].lat == poly[npoints - 1].lat) && (poly[0].lon == poly[npoints - 1].lon)))) { closed = 1; } writePolygonData(db, poly, npoints, tname, closed, level, ptype, auth, dir, dfile, ftype); closed = 0; polycounter++; } strtrim(line); npoints = 0; putit = 1; } else { sscanf(line, "%s %s %s", str1, str2, str3); lon = atof(str1); lat = atof(str2); if (npoints > maxpoints - 2) { maxpoints += 5000; poly = realloc(poly, maxpoints); } poly[npoints].lat = lat; poly[npoints].lon = lon; npoints++; } } if (npoints > 0) { strcpy(tname, name); if (polycounter > 0) { sprintf(numstr, ".%d", polycounter); strcat(tname, numstr); } if (verbose) { elog_notify(0, "%s: (%ld points n file %s/%s)\n", tname, npoints, dir, dfile); } if (npoints > 2 && (close || ( (poly[0].lat == poly[npoints - 1].lat) && (poly[0].lon == poly[npoints - 1].lon)))) { closed = 1; } writePolygonData(db, poly, npoints, tname, closed, level, ptype, auth, dir, dfile, ftype); closed = 0; } fclose(fh); elog_print(0, 0); return 0; }