#include #include #include #include #include #include #include #include #include using namespace dgc; using namespace vlr; #ifdef __cplusplus extern "C" { #endif int create_google_webdir( char *logname, char *dirname, int force ); #ifdef __cplusplus } #endif #define MIN_GMAPS_ZOOM 13 #define MAX_GMAPS_ZOOM 20 #define TILE_WIDTH 256 #define TILE_HEIGHT 256 #define MAX_NAME_LENGTH 200 using std::vector; typedef struct { double lat, lon, yaw, x, y; } path_pose_t; vector pose; char overlaydir[MAX_NAME_LENGTH]; void write_semaphore(char *filename) { FILE *fp; fp = fopen(filename, "w"); if(fp == NULL) dgc_die("Erorr: could not open file %s for writing.\n", filename); fclose(fp); } void write_js(char *filename) { FILE *fp; fp = fopen(filename, "w"); if(fp == NULL) dgc_die("Erorr: could not open file %s for writing.\n", filename); fprintf(fp, "\n"); fprintf(fp, "\n"); fprintf(fp, " \n"); fprintf(fp, " \n"); fprintf(fp, " Stanford DGC Logfile Google Maps Overlay\n"); fprintf(fp, " \n"); fprintf(fp, " \n"); fprintf(fp, " \n"); fprintf(fp, " \n"); fprintf(fp, "
\n"); fprintf(fp, " \n"); fprintf(fp, "\n"); fclose(fp); } void generate_png(int gmaps_x, int gmaps_y, int gmaps_zoom, char *filename) { static int initialized = 0; static unsigned char buffer[TILE_WIDTH * TILE_HEIGHT * 4]; int i, last_i, first; double lat1, lon1, lat2, lon2, x, y, last_x = 0, last_y = 0; Image *image; ImageInfo *info; PixelWand *color; ExceptionInfo exception; DrawContext wand; for (i=0; i 1.0) { DrawLine ( wand, x, TILE_HEIGHT-y, last_x, TILE_HEIGHT-last_y ); last_x = x; last_y = y; last_i = i; } } DrawRender( wand ); strcpy(image->filename, filename); WriteImage(info, image); DestroyDrawingWand( wand ); DestroyPixelWand( color ); DestroyImage(image); DestroyImageInfo(info); DestroyExceptionInfo(&exception); } inline void render_image(double lat, double lon, int gmaps_zoom) { static int last_gmaps_x = -1, last_gmaps_y = -1, last_gmaps_zoom = -1; int gmaps_x, gmaps_y; char filename[MAX_NAME_LENGTH]; dgc_ll_to_gmaps_tile(lat, lon, gmaps_zoom, &gmaps_x, &gmaps_y); if(gmaps_x == last_gmaps_x && gmaps_y == last_gmaps_y && gmaps_zoom == last_gmaps_zoom) return; snprintf(filename, MAX_NAME_LENGTH, "%s/overlay_%d_%d_%d.png", overlaydir, gmaps_zoom, gmaps_x, gmaps_y); if(!dgc_file_exists(filename)) { generate_png(gmaps_x, gmaps_y, gmaps_zoom,filename); } last_gmaps_x = gmaps_x; last_gmaps_y = gmaps_y; last_gmaps_zoom = gmaps_zoom; } void read_path(char *filename) { ApplanixPose applanix_pose; LineBuffer *line_buffer = NULL; char *line = NULL, *s, utmzone[10]; path_pose_t p; dgc_FILE *fp; fp = dgc_fopen(filename, "r"); if(fp == NULL) dgc_die("Error: could not open file %s for reading.\n", filename); line_buffer = new LineBuffer; do { /* read a complete line */ line = line_buffer->ReadLine(fp); if(line != NULL) { if(strncmp(line, "APPLANIX_POSE_V2", 16) == 0) { s = StringV2ToApplanixPose(dgc_next_word(line), &applanix_pose); p.lat = applanix_pose.latitude; p.lon = applanix_pose.longitude; p.yaw = applanix_pose.yaw; latLongToUtm(p.lat, p.lon, &p.x, &p.y, utmzone); pose.push_back(p); } } } while(line != NULL); dgc_fclose(fp); } int create_google_webdir( char *logname, char *dirname, int force ) { int i, gmaps_zoom; char filename[MAX_NAME_LENGTH]; char nopose_filename[MAX_NAME_LENGTH]; strncpy(overlaydir, dirname, MAX_NAME_LENGTH ); snprintf(filename, MAX_NAME_LENGTH, "%s/overlay.html", overlaydir); snprintf(nopose_filename, MAX_NAME_LENGTH, "%s/nopose.txt", overlaydir); if(!force && dgc_file_exists(overlaydir) && ( dgc_file_exists(filename) || dgc_file_exists(nopose_filename) ) ) { return 1; } mkdir(overlaydir, 0755); read_path(logname); if (pose.size()==0) { write_semaphore( nopose_filename ); return(-1); } write_js(filename); for(gmaps_zoom = MIN_GMAPS_ZOOM; gmaps_zoom <= MAX_GMAPS_ZOOM; gmaps_zoom++) for(i = 0; i < (int)pose.size(); i++) render_image(pose[i].lat, pose[i].lon, gmaps_zoom); pose.clear(); return 0; }