static void line_samp_to_proj(ImageInfo *ii, double line, double samp, double *x, double *y) { meta_parameters *meta = ii->meta; if (meta_supports_meta_get_latLon(meta)) { double lat, lon, projZ; meta_get_latLon(meta, line, samp, 0, &lat, &lon); if (meta->projection && meta->projection->type != LAT_LONG_PSEUDO_PROJECTION) { latlon_to_proj(meta->projection, 'R', lat*D2R, lon*D2R, 0, x, y, &projZ); } else { int zone; if (meta_is_valid_double(meta->general->center_longitude) && meta->general->center_longitude > -180 && meta->general->center_longitude < 180) { zone = utm_zone(meta->general->center_longitude); } else { zone = utm_zone(lon); } latLon2UTM_zone(lat, lon, 0, zone, x, y); } } else { *x = samp; *y = line; } }
SIGNAL_CALLBACK void on_add_overlay_button_clicked(GtkWidget *w) { if (!meta_supports_meta_get_latLon(curr->meta)) { message_box("Cannot add overlays to an image without " "geolocation information."); return; } #ifdef win32 OPENFILENAME of; int retval; char fname[1024]; fname[0] = '\0'; memset(&of, 0, sizeof(of)); #ifdef OPENFILENAME_SIZE_VERSION_400 of.lStructSize = OPENFILENAME_SIZE_VERSION_400; #else of.lStructSize = sizeof(of); #endif of.hwndOwner = NULL; of.lpstrFilter = "Shape Files (*.shp)\0*.shp\0" "Generic CSV Files (*.csv)\0*.csv\0" "All Files\0*\0"; of.lpstrCustomFilter = NULL; of.nFilterIndex = 1; of.lpstrFile = fname; of.nMaxFile = sizeof(fname); of.lpstrFileTitle = NULL; of.lpstrInitialDir = "."; of.lpstrTitle = "Select File"; of.lpstrDefExt = NULL; of.Flags = OFN_HIDEREADONLY | OFN_EXPLORER; retval = GetOpenFileName(&of); if (!retval) { if (CommDlgExtendedError()) message_box("File dialog box error"); return; } add_overlay_file(fname); #else // #ifdef win32 if (!add_overlay_widget) create_file_chooser_dialog(); gtk_widget_show(add_overlay_widget); #endif // #ifdef win32 }
void update_pixel_info(ImageInfo *ii) { // update the left-hand "clicked pixel" information char buf[512]; GtkWidget *img = get_widget_checked("big_image"); GdkPixbuf *shown_pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(img)); double x = crosshair_samp; double y = crosshair_line; int nl = ii->meta->general->line_count; int ns = ii->meta->general->sample_count; CachedImage *data_ci = ii->data_ci; meta_parameters *meta = ii->meta; sprintf(buf, "Line: %.1f, Sample: %.1f\n", y, x); if (x < 0 || x >= ns || y < 0 || y >= nl) { // outside of the image sprintf(&buf[strlen(buf)], "Pixel Value: (outside image)\n"); } else { assert(meta); assert(shown_pixbuf); if (data_ci->data_type == GREYSCALE_FLOAT) { float fval = cached_image_get_pixel(data_ci, crosshair_line, crosshair_samp); if (have_lut()) { unsigned char r, g, b; cached_image_get_rgb(data_ci, crosshair_line, crosshair_samp, &r, &g, &b); if (is_ignored(&ii->stats, fval)) { sprintf(&buf[strlen(buf)], "Pixel Value: %f [ignored]\n", fval); } else { sprintf(&buf[strlen(buf)], "Pixel Value: %f -> R:%d G:%d B:%d\n", fval, (int)r, (int)g, (int)b); } } else { int uval = calc_scaled_pixel_value(&(ii->stats), fval); if (is_ignored(&ii->stats, fval)) { sprintf(&buf[strlen(buf)], "Pixel Value: %f [ignored]\n", fval); } else { sprintf(&buf[strlen(buf)], "Pixel Value: %f -> %d\n", fval, uval); } } } else if (data_ci->data_type == RGB_BYTE) { unsigned char r, g, b; float rf, gf, bf; cached_image_get_rgb(data_ci, crosshair_line, crosshair_samp, &r, &g, &b); cached_image_get_rgb_float(data_ci, crosshair_line, crosshair_samp, &rf, &gf, &bf); if (!is_ignored_rgb(&ii->stats_r, rf) && !is_ignored_rgb(&ii->stats_g, gf) && !is_ignored_rgb(&ii->stats_b, bf)) { sprintf(&buf[strlen(buf)], "Pixel Value: R,G,B = %d, %d, %d\n", (int)r, (int)g, (int)b); } else { sprintf(&buf[strlen(buf)], "Pixel Value: R,G,B = %d,%d,%d [ignored]\n", (int)rf, (int)gf, (int)bf); } } else if (data_ci->data_type == GREYSCALE_BYTE) { unsigned char r, g, b; cached_image_get_rgb(data_ci, crosshair_line, crosshair_samp, &r, &g, &b); if (have_lut()) { int gs = (int)cached_image_get_pixel(data_ci, crosshair_line, crosshair_samp); if (is_ignored(&ii->stats, (float)gs)) sprintf(&buf[strlen(buf)], "Pixel Value: %d [ignored]\n", gs); else sprintf(&buf[strlen(buf)], "Pixel Value: %d -> R:%d G:%d B:%d\n", gs, (int)r, (int)g, (int)b); } else { int gs = (int)cached_image_get_pixel(data_ci, crosshair_line, crosshair_samp); if (is_ignored(&ii->stats, gs)) { sprintf(&buf[strlen(buf)], "Pixel Value: %d [ignored]\n", gs); } else if (ii->stats.truncate) { sprintf(&buf[strlen(buf)], "Pixel Value: %d\n", gs); } else { sprintf(&buf[strlen(buf)], "Pixel Value: %d -> %d\n", gs, (int)r); } } } else if (data_ci->data_type == RGB_FLOAT) { unsigned char r, g, b; float rf, gf, bf; cached_image_get_rgb(data_ci, crosshair_line, crosshair_samp, &r, &g, &b); cached_image_get_rgb_float(data_ci, crosshair_line, crosshair_samp, &rf, &gf, &bf); if (is_ignored_rgb(&ii->stats_r, rf)) sprintf(&buf[strlen(buf)], "Red: %f [ignored]\n", rf); else sprintf(&buf[strlen(buf)], "Red: %f -> %d\n", rf, (int)r); if (is_ignored_rgb(&ii->stats_g, gf)) sprintf(&buf[strlen(buf)], "Green: %f [ignored]\n", gf); else sprintf(&buf[strlen(buf)], "Green: %f -> %d\n", gf, (int)g); if (is_ignored_rgb(&ii->stats_b, bf)) sprintf(&buf[strlen(buf)], "Blue: %f [ignored]\n", rf); else sprintf(&buf[strlen(buf)], "Blue: %f -> %d\n", bf, (int)b); } } double lat=0, lon=0; if (meta_supports_meta_get_latLon(meta)) { meta_get_latLon(meta, y, x, 0, &lat, &lon); sprintf(&buf[strlen(buf)], "Lat, Lon: %.5f, %.5f (deg)\n", lat, lon); //double px, py; //latLon2UTM(lat,lon,0,&px,&py); //printf("%14.7f %14.7f --> %13.2f %13.2f\n", lat, lon, px, py); } // skip projection coords if not projected, or lat/long pseudo (since // in that case the projection coords are just the lat/long values // we are already showing) if (meta->projection && meta->projection->type != LAT_LONG_PSEUDO_PROJECTION) { double projX, projY, projZ; latlon_to_proj(meta->projection, 'R', lat*D2R, lon*D2R, 0, &projX, &projY, &projZ); sprintf(&buf[strlen(buf)], "Proj X,Y: %.1f, %.1f m\n", projX, projY); } if (!meta->projection && meta->state_vectors && meta->sar) { double s,t; meta_get_timeSlantDop(meta, y, x, &t, &s, NULL); sprintf(&buf[strlen(buf)], "Incid: %.4f, Look: %.4f (deg)\n" "Slant: %.1f m Time: %.3f s\n" "Yaw: %.4f (deg)\n", R2D*meta_incid(meta,y,x), R2D*meta_look(meta,y,x), s, t, R2D*meta_yaw(meta,y,x)); } if (meta->projection && meta->projection->type != LAT_LONG_PSEUDO_PROJECTION && meta->projection->type != SCANSAR_PROJECTION) { distortion_t d; map_distortions(meta->projection, lat*D2R, lon*D2R, &d); sprintf(&buf[strlen(buf)], "Meridian scale factor: %.6f\n", d.h); sprintf(&buf[strlen(buf)], "Parallel scale factor: %.6f\n", d.k); sprintf(&buf[strlen(buf)], "Areal scale factor: %.6f\n", d.s); sprintf(&buf[strlen(buf)], "Angular distortion: %.4f (deg)\n", d.omega); } if (g_poly->n > 0) { // start distance measure at crosshair coords double cross_x, cross_y, prev_x, prev_y; line_samp_to_proj(ii, y, x, &cross_x, &cross_y); prev_x = cross_x; prev_y = cross_y; // iterate through ctrl-clicked coords int i; double d=0, A=0; // d=distance, A=area for (i=0; i<g_poly->n; ++i) { double proj_x, proj_y; line_samp_to_proj(ii, g_poly->line[i], g_poly->samp[i], &proj_x, &proj_y); d += hypot(proj_x-prev_x, proj_y-prev_y); A += prev_x * proj_y - proj_x * prev_y; prev_x = proj_x; prev_y = proj_y; // for the area calc, we close the polygon automatically if (i==g_poly->n-1) A += prev_x * cross_y - cross_x * prev_y; } A /= 2.; char *units = "m"; if (!meta_supports_meta_get_latLon(meta)) units = "pixels"; if (g_poly->n == 1) sprintf(&buf[strlen(buf)], "Distance to %.1f,%.1f: %.1f %s", g_poly->line[0], g_poly->samp[0], d, units); else sprintf(&buf[strlen(buf)], "Total distance: %.1f %s (%d points)\n" "Area (of closure): %.1f %s^2", d, units, g_poly->n+1, fabs(A), units); } else { sprintf(&buf[strlen(buf)], "Distance: (ctrl-click to measure)"); } put_text_in_textview(buf, "info_textview"); //GtkWidget *lbl = get_widget_checked("upper_label"); //gtk_label_set_text(GTK_LABEL(lbl), buf); }
static void add_generic_csv(meta_parameters *meta, const char *csv_file, int auto_close_polys) { int num_meta_cols, num_data_cols; csv_meta_column_t *meta_column_info; csv_data_column_t *data_column_info; if (!meta_supports_meta_get_latLon(meta)) { asfPrintStatus("No geolocation info - can't add CSV: %s\n", csv_file); return; } asfPrintStatus("Adding: %s\n", csv_file); if (!fileExists(csv_file)) { asfPrintWarning("File not found: %s\n", csv_file); return; } FILE *ifp = csv_open(csv_file, &num_meta_cols, &meta_column_info, &num_data_cols, &data_column_info); // csv_open() returns NULL if the file can't be processed if (!ifp) return; // this is just for debugging //csv_info(num_meta_cols, meta_column_info, num_data_cols, data_column_info); // start line counter at 1 (header line is not part of this loop) int i,line_num=1; int num = 0; char line[1024]; while (fgets(line, 1023, ifp)) { ++line_num; char **column_data; double *lats, *lons; int ok = csv_line_parse(line, line_num, num_meta_cols, meta_column_info, num_data_cols, data_column_info, &column_data, &lats, &lons); // csv_line_parse() will return FALSE when the line is invalid if (!ok) continue; ++num; csv_free(num_meta_cols, column_data, lats, lons); } FCLOSE(ifp); FREE(meta_column_info); meta_column_info = NULL; FREE(data_column_info); data_column_info = NULL; // Use line_num-1 so we do not count the header line asfPrintStatus("File had %d line%s, %d valid line%s.\n", line_num-1, line_num-1==1?"":"s", num, num==1?"":"s"); // free pre-existing loaded shapes if (g_shapes) free_shapes(); // set up global array of shapes num_shapes = num; g_shapes = MALLOC(sizeof(Shape*)*num_shapes); for (i=0; i<num_shapes; ++i) g_shapes[i] = NULL; // now open file again, this time we will actually process! ifp = csv_open(csv_file, &num_meta_cols, &meta_column_info, &num_data_cols, &data_column_info); assert(ifp); line_num = 1; num = 0; while (fgets(line, 1023, ifp)) { ++line_num; char **column_data; double *lats, *lons; int ok = csv_line_parse(line, line_num, num_meta_cols, meta_column_info, num_data_cols, data_column_info, &column_data, &lats, &lons); // csv_line_parse() will return FALSE when the line is invalid if (!ok) continue; // dealing with metadata Shape *s = MALLOC(sizeof(Shape)); s->num_meta_cols = num_meta_cols; s->meta_cols = meta_column_info; s->meta_info = MALLOC(sizeof(char*)*num_meta_cols); for (i=0; i<num_meta_cols; ++i) s->meta_info[i] = STRDUP(column_data[i]); // dealing with data s->num_points = num_data_cols; if (auto_close_polys) s->num_points++; s->lines = MALLOC(sizeof(double)*s->num_points); s->samps = MALLOC(sizeof(double)*s->num_points); for (i=0; i<num_data_cols; ++i) { double line, samp; meta_get_lineSamp(meta, lats[i], lons[i], 0, &line, &samp); s->lines[i] = line; s->samps[i] = samp; } if (auto_close_polys) { s->lines[num_data_cols] = s->lines[0]; s->samps[num_data_cols] = s->samps[0]; } // display info s->color_code = num%27 + 10; s->marker_code = 1; // square g_shapes[num] = s; ++num; csv_free(num_meta_cols, column_data, lats, lons); } FCLOSE(ifp); FREE(data_column_info); // do not free meta_column_info -- pointed to by g_shape now }
static void add_shapefile(meta_parameters *meta, char *inFile) { if (!meta_supports_meta_get_latLon(meta)) { asfPrintStatus("No geolocation info - can't add shapefile: %s\n", inFile); return; } asfPrintStatus("Adding: %s\n", inFile); if (!fileExists(inFile)) { asfPrintWarning("File not found: %s\n", inFile); return; } DBFHandle dbase; SHPHandle shape; SHPObject *shapeObject; int ii, kk, nEntities, nVertices, pointType; // Open shapefile open_shape(inFile, &dbase, &shape); // Extract the vital information out of the shapefile SHPGetInfo(shape, &nEntities, &pointType, NULL, NULL); switch (pointType) { case SHPT_POLYGON: case SHPT_POINT: break; case SHPT_ARC: //asfPrintWarning("Shape file data type 'Arc' not supported\n"); //return; break; case SHPT_MULTIPOINT: asfPrintWarning("Shape file data type 'Multipoint' not supported\n"); return; default: asfPrintWarning("Unexpected or unrecognized shape file data format\n"); return; } asfPrintStatus(" Shapefile contains %d shapes.\n", nEntities); // free pre-existing loaded shapes if (g_shapes) free_shapes(); // first figure out how many shapes we will actually be showing double lat_min, lat_max, lon_min, lon_max; meta_get_bounding_box(meta, &lat_min, &lat_max, &lon_min, &lon_max); int nl = meta->general->line_count; int ns = meta->general->sample_count; int num_shapes_visible = 0; for (ii=0; ii<nEntities; ii++) { // Read object for the number of vertices shapeObject = SHPReadObject(shape, ii); nVertices = shapeObject->nVertices-1; for (kk=0; kk<nVertices; ++kk) { double lat = shapeObject->padfY[kk]; double lon = shapeObject->padfX[kk]; if (lon<-180) lon+=360; else if (lon>180) lon-=360; if (lat<lat_min || lat>lat_max || lon<lon_min || lon>lon_max) continue; double line, samp; meta_get_lineSamp(meta, lat, lon, 0, &line, &samp); if (line<0 || line>nl || samp<0 || samp>ns) continue; ++num_shapes_visible; break; } SHPDestroyObject(shapeObject); } if (num_shapes_visible == 0) { asfPrintWarning("Shapefile contained no shapes within the scene.\n"); num_shapes = 0; g_shapes = NULL; return; } asfPrintStatus(" ... visible shapes: %d\n", num_shapes_visible); // set up global array of shapes num_shapes = num_shapes_visible; g_shapes = MALLOC(sizeof(Shape*)*num_shapes); for (ii=0; ii<num_shapes; ++ii) g_shapes[ii] = NULL; int num=0,nverts=0; for (ii=0; ii<nEntities; ii++) { // Read object for the number of vertices shapeObject = SHPReadObject(shape, ii); nVertices = shapeObject->nVertices; int num_visible_points = 0; for (kk=0; kk<nVertices; ++kk) { double lat = shapeObject->padfY[kk]; double lon = shapeObject->padfX[kk]; if (lon<-180) lon+=360; else if (lon>180) lon-=360; if (lat<lat_min || lat>lat_max || lon<lon_min || lon>lon_max) continue; double line, samp; meta_get_lineSamp(meta, lat, lon, 0, &line, &samp); if (line<0 || line>nl || samp<0 || samp>ns) continue; ++num_visible_points; } if (num_visible_points > 0) { // there is no metadata Shape *s = MALLOC(sizeof(Shape)); s->num_meta_cols = 0; s->meta_cols = NULL; s->meta_info = NULL; // dealing with data s->num_points = num_visible_points; s->lines = MALLOC(sizeof(double)*s->num_points); s->samps = MALLOC(sizeof(double)*s->num_points); int nn=0; for (kk=0; kk<nVertices; ++kk) { double lat = shapeObject->padfY[kk]; double lon = shapeObject->padfX[kk]; if (lon<-180) lon+=360; else if (lon>180) lon-=360; if (lat<lat_min || lat>lat_max || lon<lon_min || lon>lon_max) continue; double line, samp; meta_get_lineSamp(meta, lat, lon, 0, &line, &samp); if (line<0 || line>nl || samp<0 || samp>ns) continue; assert(nn<s->num_points); s->lines[nn] = line; s->samps[nn] = samp; ++nn; ++nverts; } assert(nn==num_visible_points); // display info s->color_code = 35; s->marker_code = 0; // no markers, just draw the lines assert(num<num_shapes_visible); assert(num<num_shapes); g_shapes[num] = s; ++num; } SHPDestroyObject(shapeObject); } assert(num==num_shapes); asfPrintStatus(" ... visible vertices: %d\n", nverts); // Close shapefile close_shape(dbase, shape); }