-------------------------------------------------- gps2csv.c -------------------------------------------------- /** ** gps2csv - conver lat/lon to Antz fromat ** ** CREATED: 2014.03.05 ABS copied from test_glyph_tools ** MODIFIED: 2014.03.10 ABS added Scale & Z, corners ** MODIFIED: 2014.03.12 ABS added tags ** MODIFIED: 2014.03.15 ABS tweaked ** MODIFIED: 2014.05.01 ABS mods for use by Ray's GUI ** MODIFIED: 2014.05.03 ABS corners out, header processing ** MODIFIED: 2014.05.04 ABS fencepost error; UNIX -> Windows style line termination ** MODIFIED: 2014.05.05 ABS added -k for Kludge (ignore last input line) ** MODIFIED: 2014.05.06 ABS looking for corners bug ** MODIFIED: 2014.05.13 ABS yet another corners bug ** MODIFIED: 2014.05.13 ABS restored corners input ** MODIFIED: 2014.05.23 ABS fixed off-by-one bug producing glyphs at 0,0,0 ** MODIFIED: 2014.05.25 ABS ignores blank lines, better EOF detection ** MODIFIED: 2014.05.28 ABS tracking down bug Ray found ** **/ #include "gps2csv.h" main(argc, argv) int argc; char** argv; { /* declare functions */ void read_glyph(); int write_glyph(); char *replace(); /* declare local variables */ int id, row, len, stripped_len; int i, g, old_id, v, num_header_lines, template_lines, tl; int adl; /* short version of ActualNumDataLines */ int atEOF; /* Boolean */ int gotAGoodHeader; /* Boolean */ int gotAGoodData; /* Boolean */ double x_width, new_x_width; double y_width, new_y_width; double northeast_lon, southwest_lon, northeast_lat, southwest_lat; double xc, yc; double lat_min, lat_max, lon_min, lon_max; double lat_denom, lon_denom; char junk[MAX_LINE]; char input_data_line[MAX_LINE]; char *temp_data_line; char kml_line[MAX_LINE]; char *new_kml_line0 = NULL; char *new_kml_line1 = NULL; char *new_kml_line2 = NULL; char *new_kml_line3 = NULL; char header_line[MAX_GLYPH_LINE]; char sw_lat_str[MAX_LINE]; char sw_lon_str[MAX_LINE]; char ne_lat_str[MAX_LINE]; char ne_lon_str[MAX_LINE]; FILE *t_fp; /* tags */ FILE *d_fp; /* data */ FILE *g_fp; /* glyphs */ FILE *c_fp; /* corners */ FILE *k_fp; /* kml */ FILE *x_fp; /* text */ /* initialize global variables (including parameters) */ strcpy(ProgName, argv[0]); defaultParms(); parseArgs(argc, argv); SW_lat = 0.0; SW_lon = 0.0; NE_lat = 0.0; NE_lon = 0.0; if (NumDataLines == 0) { NumDataLines = lines_in_file(DataFile); if (Kludge) { NumDataLines = NumDataLines - 1; } } if (NumGlyphLines == 0) { NumGlyphLines = lines_in_file(GlyphFile); } if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: ARGS\n", ProgName); fprintf(stderr, "%s: DEBUG: Debug = %d\n", ProgName, Debug); fprintf(stderr, "%s: DEBUG: CornersFile = %s\n", ProgName, CornersFile); fprintf(stderr, "%s: DEBUG: CornersTemplateFile = %s\n", ProgName, CornersTemplateFile); fprintf(stderr, "%s: DEBUG: NumDataLines = %d\n", ProgName, NumDataLines); fprintf(stderr, "%s: DEBUG: GlyphFile = %s\n", ProgName, GlyphFile); fprintf(stderr, "%s: DEBUG: IgnoreHeader = %d\n", ProgName, IgnoreHeader); fprintf(stderr, "%s: DEBUG: Kludge = %d\n", ProgName, Kludge); fprintf(stderr, "%s: DEBUG: PrintCorners = %d\n", ProgName, PrintCorners); fprintf(stderr, "%s: DEBUG: InputCorners = %d\n", ProgName, InputCorners); fprintf(stderr, "%s: DEBUG: Margin = %f\n", ProgName, Margin); fprintf(stderr, "%s: DEBUG: DataFile = %s\n", ProgName, DataFile); fprintf(stderr, "%s: DEBUG: NumGlyphLines = %d\n", ProgName, NumGlyphLines); fprintf(stderr, "%s: DEBUG: Scale = %f\n", ProgName, Scale); fprintf(stderr, "%s: DEBUG: Z = %f\n", ProgName, Z); fprintf(stderr, "%s: DEBUG: SW_lat_in = %f\n", ProgName, SW_lat_in); fprintf(stderr, "%s: DEBUG: SW_lon_in = %f\n", ProgName, SW_lon_in); fprintf(stderr, "%s: DEBUG: NE_lat_in = %f\n", ProgName, NE_lat_in); fprintf(stderr, "%s: DEBUG: NE_lon_in = %f\n", ProgName, NE_lon_in); } /* initialize local variables */ t_fp = fopen(TagsFile, "w"); if (t_fp == NULL) { fprintf(stderr, "%s: ERROR: fopen of %s failed, error: %s\n", ProgName, TagsFile, strerror(errno)); exit(0); } /* read glyph file */ read_glyph(GlyphFile, NumGlyphLines); if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: FirstGoodLine = %d\n", ProgName, FirstGoodLine); } /* ok? */ if ( Glyph_array[PARENT_ID_4][FirstGoodLine] != 0.0) { fprintf(stderr, "%s: ERROR: Glyph_array[PARENT_ID_4][%d] = %f, should be 0.0\n", ProgName, FirstGoodLine, Glyph_array[PARENT_ID_4][FirstGoodLine]); exit(0); } /* read data in gps file */ /* sample row: */ /* 70.77356,34.61004,0,,Bar Nurgal */ d_fp = fopen(DataFile, "r"); if (d_fp == NULL) { fprintf(stderr, "%s: ERROR: fopen of %s failed, error: %s\n", ProgName, DataFile, strerror(errno)); exit(0); } /* throw away header */ atEOF = FALSE; gotAGoodHeader = FALSE; while (!atEOF && !gotAGoodHeader) { len = getl(d_fp, junk, MAX_LINE); if (Debug) { fprintf(stderr, "%s: DEBUG: raw header junk:>%s<\n", ProgName, junk); } /* strip carriage returns (\r) (^M) */ temp_data_line = replace(junk, "\r", ""); strcpy(junk, temp_data_line); stripped_len = strlen(junk); if (Debug) { fprintf(stderr, "%s: DEBUG: stripped_len: %d, processed header junk:>%s<\n", ProgName, stripped_len, junk); } if (stripped_len <= 1) { /* blank line or eof */ if (feof(d_fp)) { fprintf(stderr, "%s: ERROR: no header in data file\n", ProgName); exit(0); } else { /* do it again */ if (Debug) { fprintf(stderr, "%s: DEBUG: got a blank line in data header\n", ProgName); } } } else { gotAGoodHeader = TRUE; } } /* read data rows */ /* TODO: make NumDataLines long int */ ActualNumDataLines = 0; atEOF = FALSE; for (row = 1; row < NumDataLines; row++) { /* we skipped the header line */ if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: ================ top of loop: row: %d\n", ProgName, row); } /* pull data into string and parse there */ gotAGoodData = FALSE; while (!atEOF && !gotAGoodData) { len = getl(d_fp, input_data_line, MAX_LINE); if (Debug) { fprintf(stderr, "%s: DEBUG: raw input_data_line:>%s<\n", ProgName, input_data_line); } /* strip carriage returns (\r) (^M) */ temp_data_line = replace(input_data_line, "\r", ""); strcpy(input_data_line, temp_data_line); if (Debug) { fprintf(stderr, "%s: DEBUG: processed input_data_line:>%s<\n", ProgName, input_data_line); } stripped_len = strlen(input_data_line); if (Debug) { fprintf(stderr, "%s: DEBUG: stripped_len = %d\n", ProgName, stripped_len); } gotAGoodData = TRUE; /* true faith */ if (stripped_len <= 1) { /* blank line or eof */ gotAGoodData = FALSE; /* either way we don't parse */ if (feof(d_fp)) { atEOF = TRUE; } else { /* do it again */ if (Debug) { fprintf(stderr, "%s: DEBUG: got a blank line in data body\n", ProgName); } } } else { ActualNumDataLines++; } } if (gotAGoodData && !atEOF) { adl = ActualNumDataLines; sscanf(input_data_line, "%lf,%lf,%lf,,%[^\n]s\n", &Lon[adl], &Lat[adl], &Ele[adl], &Tags[adl][0]); if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: ---- DATA lINE ----\n", ProgName); fprintf(stderr, "%s: DEBUG: final input_data_line: >%s<\n", ProgName, input_data_line); fprintf(stderr, "%s: DEBUG: Lat[%d] = %lf\n", ProgName, adl, Lat[adl]); fprintf(stderr, "%s: DEBUG: Lon[%d] = %lf\n", ProgName, adl, Lon[adl]); fprintf(stderr, "%s: DEBUG: Ele[%d] = %lf\n", ProgName, adl, Ele[adl]); fprintf(stderr, "%s: DEBUG: Tags[%d] = %s\n", ProgName, adl, &Tags[adl][0]); } if (adl == 1) { lat_max = Lat[adl]; lat_min = Lat[adl]; lon_max = Lon[adl]; lon_min = Lon[adl]; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: init OK\n", ProgName, lat_min); } } if (row > 1 && Lat[adl] > lat_max) { lat_max = Lat[adl]; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: lat_max now: %lf\n", ProgName, lat_max); } } if (adl > 1 && Lat[adl] < lat_min) { lat_min = Lat[adl]; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: lat_min now: %lf\n", ProgName, lat_min); } } if (adl > 1 && Lon[adl] > lon_max) { lon_max = Lon[adl]; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: lon_max now: %lf\n", ProgName, lon_max); } } if (adl > 1 && Lon[adl] < lon_min) { lon_min = Lon[adl]; if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: lon_min now: %lf\n", ProgName, lon_min); } } } } /* end for row */ if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: final ActualNumDataLines: %d\n", ProgName, ActualNumDataLines); } /* compute corners or set from flags */ if (InputCorners) { NE_lat = NE_lat_in; NE_lon = NE_lon_in; SW_lat = SW_lat_in; SW_lon = SW_lon_in; } else { /* Remember kids: x is lon and y is lat! */ /* map from min/max to NSEW */ southwest_lat = lat_min; southwest_lon = lon_min; northeast_lat = lat_max; northeast_lon = lon_max; x_width = northeast_lon - southwest_lon; y_width = northeast_lat - southwest_lat; if (x_width/y_width == 2.0) { new_x_width = x_width*(1.0 + (double)Margin); new_y_width = y_width*(1.0 + (double)Margin); } if (x_width/y_width > 2.0) { /* make y bigger */ new_x_width = x_width*(1.0 + (double)Margin); new_y_width = new_x_width/2.0; } if (x_width/y_width < 2.0) { /* make x bigger */ new_y_width = y_width*(1.0 + (double)Margin); new_x_width = 2.0*new_y_width; } /* Finally compute center of original extents */ xc = (northeast_lon + southwest_lon)/2.0; yc = (northeast_lat + southwest_lat)/2.0; /* compute final extents: */ NE_lon = xc + new_x_width/2.0; SW_lon = xc - new_x_width/2.0; NE_lat = yc + new_y_width/2.0; SW_lat = yc - new_y_width/2.0; } if (PrintCorners) { /* txt */ x_fp = fopen(TextFile, "w"); if (x_fp == NULL) { fprintf(stderr, "%s: ERROR: fopen of %s failed, error: %s\n", ProgName, TextFile, strerror(errno)); exit(0); } fprintf(x_fp, "old SW corner lat (min) = %lf\r\n", lat_min); fprintf(x_fp, "old SW corner lon (min) = %lf\r\n", lon_min); fprintf(x_fp, "old NE corner lat (max) = %lf\r\n", lat_max); fprintf(x_fp, "old NE corner lon (max) = %lf\r\n", lon_max); fprintf(x_fp, "new SW corner lat = %lf\r\n", SW_lat); fprintf(x_fp, "new SW corner lon = %lf\r\n", SW_lon); fprintf(x_fp, "new NE corner lat = %lf\r\n", NE_lat); fprintf(x_fp, "new NE corner lon = %lf\r\n", NE_lon); fclose(x_fp); /* kml */ sprintf(sw_lat_str, "%.8lf", SW_lat); sprintf(sw_lon_str, "%.8lf", SW_lon); sprintf(ne_lat_str, "%.8lf", NE_lat); sprintf(ne_lon_str, "%.8lf", NE_lon); template_lines = lines_in_file(CornersTemplateFile); c_fp = fopen(CornersTemplateFile, "r"); if (c_fp == NULL) { fprintf(stderr, "%s: ERROR: fopen of %s failed, error: %s\n", ProgName, CornersTemplateFile, strerror(errno)); exit(0); } k_fp = fopen(CornersFile, "w"); if (k_fp == NULL) { fprintf(stderr, "%s: ERROR: fopen of %s failed, error: %s\n", ProgName, CornersFile, strerror(errno)); exit(0); } for (tl = 0; tl < template_lines; tl++) { getl(c_fp, kml_line, MAX_LINE); new_kml_line0 = replace(kml_line, "_SW_lon_", sw_lon_str); new_kml_line1 = replace(new_kml_line0, "_SW_lat_", sw_lat_str); new_kml_line2 = replace(new_kml_line1, "_NE_lon_", ne_lon_str); new_kml_line3 = replace(new_kml_line2, "_NE_lat_", ne_lat_str); fprintf(k_fp, "%s", new_kml_line3); } fclose(c_fp); fclose(k_fp); } /* compute Newlat & NewLon */ for (row = 1; row <= ActualNumDataLines; row++) { /* we skipped the header line */ if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: 2nd loop, row: %d\n", ProgName, row); } if (SW_lat != NE_lat && SW_lon != NE_lon) { /* convert Lat/Lon to map coordinates */ lat_denom = NE_lat - SW_lat; lon_denom = NE_lon - SW_lon; NewLat[row] = 180.0*(Lat[row] - SW_lat)/lat_denom - 90.0; NewLon[row] = 360.0*(Lon[row] - SW_lon)/lon_denom - 180.0; } else { NewLat[row] = Lat[row]; NewLon[row] = Lon[row]; } if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: NewLat[row] = %lf\n", ProgName, NewLat[row]); fprintf(stderr, "%s: DEBUG: NewLon[row] = %lf\n", ProgName, NewLon[row]); fprintf(stderr, "%s: DEBUG: lat_denom = %lf\n", ProgName, lat_denom); fprintf(stderr, "%s: DEBUG: lon_denom = %lf\n", ProgName, lon_denom); } } /* end for row */ if (Debug) { fprintf(stderr, "%s: DEBUG: lat_min = %lf\n", ProgName, lat_min); fprintf(stderr, "%s: DEBUG: lat_max = %lf\n", ProgName, lat_max); fprintf(stderr, "%s: DEBUG: lon_min = %lf\n", ProgName, lon_min); fprintf(stderr, "%s: DEBUG: lon_max = %lf\n", ProgName, lon_max); } /* process & write */ /* write headers */ fprintf(t_fp, "id,record_id,table_id,title,description\r\n"); if (!IgnoreHeader) { num_header_lines = FirstGoodLine; g_fp = fopen(GlyphFile, "r"); for (row = 0; row < num_header_lines; row++) { getl(g_fp, header_line, MAX_GLYPH_LINE); fprintf(stdout, "%s", header_line); } fclose(g_fp); } i = 0; id = 300; for (row = 0; row < ActualNumDataLines; row++) { if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: about to write row %d\n", ProgName, row); } /* Glyph_array indexed from 0, NewLon indexed from 1 */ Glyph_array[SELECTED_3][FirstGoodLine] = 0.0; Glyph_array[SELECTED_3][FirstGoodLine + 1] = 0.0; /* KLUDGE */ Glyph_array[SCALE_X_25][FirstGoodLine] = Scale; Glyph_array[SCALE_Y_26][FirstGoodLine] = Scale; Glyph_array[SCALE_Z_27][FirstGoodLine] = Scale; Glyph_array[TRANSLATE_X_28][FirstGoodLine] = (float)NewLon[row + 1]; Glyph_array[TRANSLATE_Y_29][FirstGoodLine] = (float)NewLat[row + 1]; Glyph_array[TRANSLATE_Z_30][FirstGoodLine] = Z; old_id = id; id = write_glyph(NumGlyphLines, id); if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: at row %d got back id = %d\n", ProgName, row, id); } /* write tags */ v = 0; for (g = old_id; g < id; g++) { fprintf(t_fp, "%d,%d,0,\"%s - var %d\",\"unused\"\r\n", i++, g, Tags[row + 1], v++); /* tags start at zero, data starts at one */ } } /* end for */ if (Debug != FALSE) { fprintf(stderr, "%s: DEBUG: id = %d\n", ProgName, id); } } /* end main */ /* * defaultParms - initialize global variables */ defaultParms() { Debug = FALSE; IgnoreHeader = FALSE; InputCorners = FALSE; NumDataLines = 0; Scale = 1.0; Z = 0.0; Margin = 0.1; /**** Afghanistan **** SW_lat = 33.955454; SW_lon = 69.837091; NE_lat = 34.764398; NE_lon = 71.503645; ********************/ SW_lat_in = 0.0; SW_lon_in = 0.0; NE_lat_in = 0.0; NE_lon_in = 0.0; strcpy(DataFile, "data_in.csv"); strcpy(GlyphFile, "glyph_in.csv"); strcpy(TagsFile, "ANTzTag0001.csv"); strcpy(TextFile, "corners.txt"); strcpy(CornersTemplateFile, "corners_template.csv"); strcpy(CornersFile, "corners.csv"); } /* end defaultParms() */ /* * parseArgs - parse arguments into global variables */ parseArgs(argc, argv) int argc; char** argv; { int i; for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D' : Debug = TRUE; break; case 'c' : InputCorners = TRUE; SW_lat_in = atof(&argv[++i][0]); SW_lon_in = atof(&argv[++i][0]); NE_lat_in = atof(&argv[++i][0]); NE_lon_in = atof(&argv[++i][0]); break; case 'C' : strcpy(CornersTemplateFile, &argv[++i][0]); strcpy(CornersFile, &argv[++i][0]); break; case 'd' : strcpy(DataFile, &argv[++i][0]); break; case 'g' : NumGlyphLines = atoi(&argv[++i][0]); break; case 'G' : strcpy(GlyphFile, &argv[++i][0]); break; case 'h' : help(argv); exit(0); break; case 'i' : IgnoreHeader = TRUE; break; case 'k' : Kludge = TRUE; break; case 'm' : Margin = atof(&argv[++i][0]); break; case 'p' : PrintCorners = TRUE; break; case 'n' : NumDataLines = atoi(&argv[++i][0]); break; case 's' : Scale = atof(&argv[++i][0]); break; case 'T' : strcpy(TagsFile, &argv[++i][0]); break; case 'X' : strcpy(TextFile, &argv[++i][0]); break; case 'z' : Z = atof(&argv[++i][0]); break; case 'u' : usage(argv); exit(0); default: fprintf(stderr, "%s: ERROR: illegal flag: %s\n", ProgName, argv[i]); exit(0); break; } /* end switch */ } /* end if */ } /* end for(i) */ } /* end parseArgs() */ /* * getl - modified from Kernighan & Ritchie to take stream */ int getl(stream, s, lim) /* get line into s, return length */ FILE *stream; char s[]; int lim; { int c, i; i = 0; while (--lim > 0 && (c = getc(stream)) != EOF && c != '\n') s[i++] = c; if (c == '\n') s[i++] = c; s[i] = EOS; return(i); } /* end getl() */ /* * lines_in_file - from Stacktrace */ int lines_in_file(filename) char filename[]; { FILE *fp; /* */ int c; /* Nb. int (not char) for the EOF */ unsigned long newline_count = 0; /* count the newline characters */ fp = fopen(filename, "r"); while ( (c=fgetc(fp)) != EOF ) { if ( c == '\n' ) { newline_count++; } } fclose(fp); return newline_count; } /* end lines_in_file() */ -------------------------------------------------- gps2csv.h -------------------------------------------------- /* * gps2csv.h -- include file for gps2csv.c */ /* includes */ #include #include #include #include #include #include #include "synglyphx.h" /* constants */ #define TRUE (1) #define FALSE (0) #define MAX_LINE (255) #define MAX_GLYPH_LINE (4096) #define NUM_COLUMNS (94) #define MAX_CODE (5) #define MAX_NODE_NUM (500) #define EOS (0) #define STRING_EQUAL (0) #define MAX_ROWS (1600000) #define MAX_GLYPH_ROWS (400) #define ERROR_CODE (-1) #define NO_MATCH (-1) #define UNIVERSAL_PARENT (0) #define START_ID (33) #define CIRCLE_DEG (360.0) /* global variables */ int ActualNumDataLines; /* */ double Lat[MAX_ROWS]; /* latitude (NS) */ double Lon[MAX_ROWS]; /* longitude (EW) */ double Ele[MAX_ROWS]; /* elevation */ double NewLat[MAX_ROWS]; /* adjusted latitude (NS) */ double NewLon[MAX_ROWS]; /* adjusted longitude (EW) */ double NE_lat; /* NE map corner lat */ double NE_lon; /* NE map corner lon */ double SW_lat; /* SW map corner lat */ double SW_lon; /* SW map corner lon */ int InputCorners; /* Boolean */ char Tags[MAX_ROWS][MAX_LINE]; /* tags */ char ProgName[MAX_LINE]; /* */ /* used by glyph_tools */ int FirstGoodLine; int First_id; float Glyph_array[NUM_COLUMNS][MAX_GLYPH_ROWS]; /* flag-settable parameters (global variables) */ int Debug; /* Boolean: TRUE = debug writes */ int IgnoreHeader; /* Boolean: TRUE = don't add header */ int Kludge; /* Boolean: TRUE = ignore last input line */ int PrintCorners; /* Boolean: TRUE = don't add header */ float Scale; /* */ float Margin; /* */ float Z; /* */ int NumDataLines; /* */ int NumGlyphLines; /* */ char GlyphFile[MAX_LINE]; /* glyph file name */ char DataFile[MAX_LINE]; /* data file name */ char TagsFile[MAX_LINE]; /* tags file name */ char TextFile[MAX_LINE]; /* text file name */ char CornersFile[MAX_LINE]; /* corners file name */ char CornersTemplateFile[MAX_LINE]; /* corners template file name */ double NE_lat_in; /* NE map corner lat */ double NE_lon_in; /* NE map corner lon */ double SW_lat_in; /* SW map corner lat */ double SW_lon_in; /* SW map corner lon */ /* global variables */ /* macros */ -------------------------------------------------- glyph_tools.c -------------------------------------------------- /** ** glyph_tools: contains: ** ** read_glyph() ** write_glyph() **/ #include "gps2csv.h" /* * read_glyph - read from a CSV file into Glyph_array[][] */ void read_glyph(filename, num_lines) char * filename; int num_lines; { /* declare local variables */ int len, col, i_col, d_col, line_num; char temp_char; char input_line[MAX_GLYPH_LINE]; char data_array[MAX_GLYPH_LINE]; FILE* g_fp; /* init local variables */ if (Debug != FALSE) { fprintf(stderr, "%s: read_glyph(): DEBUG: num_lines = %d\n", ProgName, num_lines); } FirstGoodLine = 0; /* read */ g_fp = fopen(GlyphFile, "r"); if (g_fp == NULL) { fprintf(stderr, "%s: read_glyph(): ERROR: fopen failed, error: %s\n", ProgName, strerror(errno)); exit(0); } len = getl(g_fp, &input_line, MAX_GLYPH_LINE); /* text header */ if (Debug != FALSE) { fprintf(stderr, "%s: read_glyph(): DEBUG: header length = %d\n", ProgName, len); #ifdef SUPER_DEBUG fprintf(stderr, "%s: read_glyph(): DEBUG: header input_line = >%s<\n", ProgName, input_line); #endif } for (line_num = 1; line_num < num_lines; line_num++) { len = getl(g_fp, &input_line, MAX_GLYPH_LINE); #ifdef SUPER_DEBUG if (Debug != FALSE) { fprintf(stderr, "%s: read_glyph(): DEBUG: input_line number %d: >%s<\n", ProgName, line_num, input_line); } #endif if (len == 0) { fprintf(stderr, "%s: read_glyph(): ERROR: zero length line number %d\n", ProgName, line_num); break; } i_col = 0; for (col = 0; col < NUM_COLUMNS - 1; col++) { d_col = 0; temp_char = input_line[i_col]; while (temp_char != ',') { temp_char = input_line[i_col++]; data_array[d_col++] = temp_char; } data_array[d_col - 1] = EOS; sscanf(data_array, "%f", &Glyph_array[col][line_num]); #ifdef SUPER_DEBUG if (Debug != FALSE) { fprintf(stderr, "%s: read_glyph(): DEBUG: data_array = >%s<\n", ProgName, data_array); fprintf(stderr, "%s: read_glyph(): DEBUG: Glyph_array[%d][%d] = %f\n", ProgName, col, line_num, Glyph_array[col][line_num]); } #endif } d_col = 0; temp_char = input_line[i_col]; while (temp_char != ',') { temp_char = input_line[i_col++]; data_array[d_col++] = temp_char; } data_array[d_col - 1] = EOS; sscanf(data_array, "%f", &Glyph_array[NUM_COLUMNS - 1][line_num]); if ((!FirstGoodLine) && (Glyph_array[1][line_num] == 5.0)) { FirstGoodLine = line_num; #ifdef SUPER_DEBUG if (Debug != FALSE) { fprintf(stderr, "%s: read_glyph(): DEBUG: FirstGoodLine = %d\n", ProgName, FirstGoodLine); } #endif } if (Glyph_array[NUM_COLUMNS - 1][line_num] != 420.0) { fprintf(stderr, "%s: read_glyph(): ERROR: record %d was: %f, should be 420.0\n", NUM_COLUMNS - 1, ProgName, Glyph_array[NUM_COLUMNS - 1][line_num]); exit(0); } } fclose(g_fp); } /* * write_glyph - write from Glyph_array[][] into a CSV file */ int write_glyph(num_lines, start_id) int num_lines; int start_id; { int col, row, temp_int; #ifdef SUPER_DEBUG if (Debug != FALSE) { fprintf(stderr, "%s: write_glyph(): DEBUG: num_lines = %d\n", "write_glyph()", num_lines); fprintf(stderr, "%s: write_glyph(): DEBUG: start_id = %d\n", "write_glyph()", start_id); } #endif for (row = FirstGoodLine; row < num_lines; row++) { if (FirstGoodLine == row) { First_id = Glyph_array[ID_0][row]; if (Debug != FALSE) { fprintf(stderr, "%s: write_glyph(): DEBUG: First_id = %d\n", "write_glyph()", First_id); } } /* do all surgery here */ Glyph_array[ID_0][row] = Glyph_array[ID_0][row] - First_id + start_id; Glyph_array[DATA_2][row] = Glyph_array[ID_0][row]; /* anything with parent zero stays that way */ if (Glyph_array[PARENT_ID_4][row] != 0.0) { Glyph_array[PARENT_ID_4][row] = Glyph_array[PARENT_ID_4][row] - First_id + start_id; } Glyph_array[RECORD_ID_92][row] = Glyph_array[ID_0][row]; /* end of surgery */ for (col = 0; col < NUM_COLUMNS; col++) { temp_int = (int)Glyph_array[col][row]; if ((float)temp_int == Glyph_array[col][row]) { fprintf(stdout, "%d", (int)Glyph_array[col][row]); } else { fprintf(stdout, "%f", Glyph_array[col][row]); } if (col < NUM_COLUMNS - 1) { fprintf(stdout, ","); } } fprintf(stdout, "\n"); } return start_id + num_lines - FirstGoodLine; } -------------------------------------------------- help.c -------------------------------------------------- /* * help - give program help */ #include help(argv) char** argv; { fprintf(stderr, "%s: HELP:\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " gps2csv - convert lon/lat and tags to ANTz files\n"); fprintf(stderr, "\n"); fprintf(stderr, " This program is designed to take as input the output\n"); fprintf(stderr, " of a Python script to convert Google Earth kml and\n"); fprintf(stderr, " kmz files into plain text, with a one-line\n"); fprintf(stderr, " header followed by this format:\n"); fprintf(stderr, "\n"); fprintf(stderr, " longitude,latitude,0,,name\n"); fprintf(stderr, "\n"); fprintf(stderr, " example:\n"); fprintf(stderr, "\n"); fprintf(stderr, " 70.77356,34.61004,0,,Bar Nurgal\n"); fprintf(stderr, "\n"); fprintf(stderr, " plus a template glyph file in ANTZ csv fotmat,\n"); fprintf(stderr, " and then produce an ANTz csv file with one copy\n"); fprintf(stderr, " of the glyph for each data row.\n"); fprintf(stderr, "\n"); } /* end of help */ -------------------------------------------------- usage.c -------------------------------------------------- /* * usage - give program usage */ #include usage(argv) char** argv; { fprintf(stderr, "%s: USAGE: %s [flags] > outfile\n", argv[0], argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " flag meaning default\n"); fprintf(stderr, " ------------- ---------------------- -------\n"); fprintf(stderr, " -D debug FALSE \n"); fprintf(stderr, " -c SWlat SWlon NElat NElon 0 0 0 0\n"); fprintf(stderr, " corners provided as input\n"); fprintf(stderr, " -C filename filename corners_template.kml corners.csv\n"); fprintf(stderr, " two file names\n"); fprintf(stderr, " -d filename data file data_in.csv\n"); fprintf(stderr, " -g int num glyph lines 0\n"); fprintf(stderr, " (0=read size from file)\n"); fprintf(stderr, " -G filename glyph file glyph_in.csv\n"); fprintf(stderr, " -h help on this program \n"); fprintf(stderr, " -i ignore header FALSE \n"); fprintf(stderr, " -k kludge (skip last line) FALSE \n"); fprintf(stderr, " -m float margin for corners 0.1\n"); fprintf(stderr, " -n int num data lines 0\n"); fprintf(stderr, " (0=read size from file)\n"); fprintf(stderr, " -u usage (this message) \n"); fprintf(stderr, " -p print out corners FALSE \n"); fprintf(stderr, " -s float scale for parent glyphs 1.0\n"); fprintf(stderr, " -z float Z val for parent glyphs 0.0\n"); fprintf(stderr, " -T filename tags file ANTzTag0001.csv\n"); fprintf(stderr, " -x filename text file for corners corners.txt\n"); fprintf(stderr, "\n"); } /* end of usage */ -------------------------------------------------- synglyphx.h -------------------------------------------------- #define ANTZ_TYPE_PIN (5) #define ANTZ_TYPE_LINK (7) #define ANTZ_GEOM_CUBE_WIRE (0) #define ANTZ_GEOM_CUBE (1) #define ANTZ_GEOM_SPHERE_WIRE (2) #define ANTZ_GEOM_SPHERE (3) #define ANTZ_GEOM_CONE_WIRE (4) #define ANTZ_GEOM_CONE (5) #define ANTZ_GEOM_TOROID (7) #define ANTZ_GEOM_DODEC_WIRE (8) #define ANTZ_GEOM_DODEC (9) #define ANTZ_GEOM_OCTO_WIRE (10) #define ANTZ_GEOM_OCTO (11) #define ANTZ_GEOM_TETRA_WIRE (12) #define ANTZ_GEOM_TETRA (13) #define ANTZ_GEOM_ICOS_WIRE (14) #define ANTZ_GEOM_ICOS (15) #define ANTZ_GEOM_PIN (16) #define ANTZ_GEOM_PIN_WIRE (17) #define ANTZ_GEOM_CYLINDER_WIRE (18) #define ANTZ_GEOM_CYLINDER (19) #define ANTZ_TOPO_LINK (0) #define ANTZ_TOPO_CUBE (1) #define ANTZ_TOPO_SPHERE (2) #define ANTZ_TOPO_TORUS (3) #define ANTZ_TOPO_CYLINDER (4) #define ANTZ_TOPO_PIN (5) #define ANTZ_TOPO_ROD (6) #define ANTZ_TOPO_POINT (7) #define ID_0 (0) #define TYPE_1 (1) #define DATA_2 (2) #define SELECTED_3 (3) #define PARENT_ID_4 (4) #define BRANCH_LEVEL_5 (5) #define CHILD_ID_6 (6) #define CHILD_INDEX_7 (7) #define CHILD_COUNT_8 (8) #define CH_INPUT_ID_9 (9) #define CH_OUTPUT_ID_10 (10) #define CH_LAST_UPDATED_11 (11) #define AVERAGE_12 (12) #define SAMPLE_13 (13) #define AUX_A_X_14 (14) #define AUX_A_Y_15 (15) #define AUX_A_Z_16 (16) #define AUX_B_X_17 (17) #define AUX_B_Y_18 (18) #define AUX_B_Z_19 (19) #define COLOR_SHIFT_20 (20) #define ROTATE_VEC_X_21 (21) #define ROTATE_VEC_Y_22 (22) #define ROTATE_VEC_Z_23 (23) #define ROTATE_VEC_S_24 (24) #define SCALE_X_25 (25) #define SCALE_Y_26 (26) #define SCALE_Z_27 (27) #define TRANSLATE_X_28 (28) #define TRANSLATE_Y_29 (29) #define TRANSLATE_Z_30 (30) #define TAG_OFFSET_X_31 (31) #define TAG_OFFSET_Y_32 (32) #define TAG_OFFSET_Z_33 (33) #define ROTATE_RATE_X_34 (34) #define ROTATE_RATE_Y_35 (35) #define ROTATE_RATE_Z_36 (36) #define ROTATE_X_37 (37) #define ROTATE_Y_38 (38) #define ROTATE_Z_39 (39) #define SCALE_RATE_X_40 (40) #define SCALE_RATE_Y_41 (41) #define SCALE_RATE_Z_42 (42) #define TRANSLATE_RATE_X_43 (43) #define TRANSLATE_RATE_X_44 (44) #define TRANSLATE_RATE_X_45 (45) #define TRANSLATE_VEC_X_46 (46) #define TRANSLATE_VEC_X_47 (47) #define TRANSLATE_VEC_X_48 (48) #define SHADER_49 (49) #define GEOMETRY_50 (50) #define LINE_WIDTH_51 (51) #define POINT_SIZE_52 (52) #define RATIO_53 (53) #define COLOR_INDEX_54 (54) #define COLOR_R_55 (55) #define COLOR_G_56 (56) #define COLOR_B_57 (57) #define COLOR_A_58 (58) /* color_fade texture_id hide freeze topo facet auto_zoom_x auto_zoom_y auto_zoom_z trigger_hi_x trigger_hi_y trigger_hi_z trigger_lo_x trigger_lo_y trigger_lo_z set_hi_x set_hi_y set_hi_z set_lo_x set_lo_y set_lo_z proximity_x proximity_y proximity_z proximity_mode_x proximity_mode_y proximity_mode_z segments_x segments_y segments_z */ #define TAG_MODE_89 (89) #define FORMAT_ID_90 (90) #define TABLE_ID_91 (91) #define RECORD_ID_92 (92) #define SIZE_93 (93) -------------------------------------------------- Makefile -------------------------------------------------- O_FILES = gps2csv.o glyph_tools.o replace.o help.o usage.o CFLAGS = -lc -g -lm CC = gcc gps2csv: $(O_FILES) $(CC) $(CFLAGS) -o gps2csv.exe $(O_FILES) -------------------------------------------------- Test -------------------------------------------------- # dest=/cygdrive/c/Users/Alan/Antz2014/antz_msw_2013-11-21/usr/csv # ./gps2csv.exe -D -p -c -90 -180 90 180 -C corners_template.kml korners.kml -G glyph_in.csv -g `wc -l glyph_in.csv` -n `wc -l gps_data.txt` -d gps_data.txt > gps_out.csv # cp gps_out.csv ANTz0001.csv # cp ANTz0001.csv ${dest} cp ANTzTag0001.csv ${dest} cp glyph_in.csv ${dest} -------------------------------------------------- corners_template.kml -------------------------------------------------- _SW_lat_,_SW_lon_ _NE_lat_,_NE_lon_ degrees -------------------------------------------------- gps_data.txt -------------------------------------------------- Name,Coordinates -73.53992,40.791654,0,,Jericho -88.082314,41.522973,0,,Joliet -115.901064,33.873293,0,,Joshua Tree --------------------------------------------------