18#define _POSIX_C_SOURCE 200809L
45#define PI 3.14159265358979323846
46#define EARTH_RADIUS_M 6378137.0
47#define DEG_TO_RAD 0.017453292519943295
49#define MAX_PATH_LENGTH 4096U
50#define MAX_TILE_COUNT 4096U
51#define MAX_OUTPUT_PIXELS 16000000U
52#define MAX_INPUT_PIXELS 1048576U
53#define MAX_CSV_POINTS 200000U
54#define MAX_CSV_LINE 8192U
56#define DEFAULT_MAX_ZOOM 20U
57#define DEFAULT_MARGIN_M 15.0
58#define DEFAULT_ALPHA 0.55
59#define DEFAULT_TRACK_GAP_S 1.5
60#define JPEG_QUALITY 92
172 image->pixels = NULL;
175 image->capacity_bytes = 0;
188 FILE *input = fopen(path,
"rb");
189 if (input == NULL) {
return -1; }
190 struct jpeg_decompress_struct
decoder;
194 if (setjmp(failure.
recovery) != 0) {
195 jpeg_destroy_decompress(&
decoder);
200 jpeg_create_decompress(&
decoder);
201 jpeg_stdio_src(&
decoder, input);
202 jpeg_read_header(&
decoder, TRUE);
203 decoder.out_color_space = JCS_RGB;
204 jpeg_start_decompress(&
decoder);
207 jpeg_destroy_decompress(&
decoder);
211 unsigned int width =
decoder.output_width;
212 unsigned int height =
decoder.output_height;
213 size_t bytes = (size_t)width * height * 3U;
215 jpeg_destroy_decompress(&
decoder);
219 image->width = width;
220 image->height = height;
225 jpeg_finish_decompress(&
decoder);
226 jpeg_destroy_decompress(&
decoder);
227 return fclose(input) == 0 ? 0 : -1;
240 FILE *
output = fopen(path,
"wb");
241 if (
output == NULL) {
return -1; }
242 struct jpeg_compress_struct
encoder;
246 if (setjmp(failure.
recovery) != 0) {
247 jpeg_destroy_compress(&
encoder);
251 jpeg_create_compress(&
encoder);
256 encoder.in_color_space = JCS_RGB;
259 jpeg_start_compress(&
encoder, TRUE);
264 jpeg_finish_compress(&
encoder);
265 jpeg_destroy_compress(&
encoder);
266 return fclose(
output) == 0 ? 0 : -1;
281 double parsed = strtod(text, &end);
282 if (end == text || errno == ERANGE || !isfinite(parsed) || *end !=
'\0') {
return false; }
297 unsigned long parsed = strtoul(text, &end, 10);
298 if (end == text || errno == ERANGE || *end !=
'\0' || parsed > UINT32_MAX) {
return false; }
299 *value = (
unsigned int)parsed;
309 printf(
"%s\nUsage: %s HEATMAP.jpg [options]\n"
310 "Overlay a USHER EARcam heatmap on cached/downloaded satellite tiles.\n\n"
311 " --geo FILE Sidecar path (default: HEATMAP.geo)\n"
312 " -o, --output FILE Output JPEG (default: HEATMAP_sat.jpg)\n"
313 " --source google|esri Tile source (default: google)\n"
314 " --cache DIR Shared tile cache (default: XDG_DATA_HOME/paparazzi/pprzgcs/map)\n"
315 " --zoom N Tile zoom (otherwise choose from heatmap resolution)\n"
316 " --max-zoom N Maximum automatic zoom (default: 20)\n"
317 " --alpha N Heatmap opacity from 0 to 1 (default: 0.55)\n"
318 " --margin-m N Imagery margin (default: 15)\n"
319 " --log FILE Session CSV; redraw sample positions\n"
320 " --track FILE Track CSV: t_s,lat_deg,lon_deg,...\n"
321 " --track-gap-s N Break track after this gap (default: 1.5)\n"
322 " --search LAT,LON[,M] Search point and radius (default radius: 25 m)\n"
323 " --truth LAT,LON Known source position for simulation comparison\n"
324 " --obstacles FILE LiDAR CSV with x_east_m,lat,lon,mnh_m columns\n"
325 " --offline Use cached tiles only; fail if any tile is missing\n"
326 " --quiet Suppress tile progress messages\n"
327 " -h, --help Show this help\n"
348 for (
int index = 1; index < argc; index++) {
349 const char *arg = argv[index];
355 if (strcmp(arg,
"--help") == 0 || strcmp(arg,
"-h") == 0) {
359 if (strcmp(arg,
"--version") == 0 || strcmp(arg,
"-V") == 0) {
363 if (strcmp(arg,
"--quiet") == 0) {
options->
quiet =
true;
continue; }
364 if (strcmp(arg,
"--offline") == 0) {
options->
offline =
true;
continue; }
365 if (index + 1 >= argc) {
return false; }
366 const char *value = argv[++index];
373 else if (strcmp(arg,
"--output") == 0 || strcmp(arg,
"-o") == 0) {
options->
output_path = value; }
374 else if (strcmp(arg,
"--source") == 0) {
options->
source = value; }
376 else if (strcmp(arg,
"--zoom") == 0) {
378 }
else if (strcmp(arg,
"--max-zoom") == 0) {
380 }
else if (strcmp(arg,
"--alpha") == 0) {
382 }
else if (strcmp(arg,
"--margin-m") == 0) {
384 }
else if (strcmp(arg,
"--track-gap-s") == 0) {
386 }
else {
return false; }
401static void mercator_xy(
double lat,
double lon,
unsigned int zoom,
double *x,
double *y)
403 const double n = ldexp(1.0, (
int)zoom);
404 double bounded_lat = fmax(-85.05112878, fmin(85.05112878, lat));
406 *x = (lon + 180.0) / 360.0 * n;
407 *y = (1.0 - asinh(tan(lat_rad)) /
PI) * 0.5 * n;
433 for (
unsigned int zoom = 12; zoom <= max_zoom; zoom++) {
448static bool parse_location(
const char *text,
bool with_radius,
double *lat,
double *lon,
double *radius)
450 if (text == NULL) {
return false; }
452 size_t length = strlen(text);
453 if (length == 0 || length >=
sizeof(buffer)) {
return false; }
454 for (
size_t index = 0; index <= length; index++) { buffer[index] = text[index]; }
455 char *first = strchr(buffer,
',');
456 if (first == NULL) {
return false; }
458 char *second = strchr(first,
',');
459 if (second != NULL) { *second++ =
'\0'; }
461 if (*lat < -90.0 || *lat > 90.0 || *lon < -180.0 || *lon > 180.0) {
return false; }
464 if (second != NULL && (!
parse_double(second, radius) || *radius <= 0.0 || *radius > 100000.0)) {
return false; }
465 return second == NULL || strchr(second,
',') == NULL;
467 return second == NULL;
482 size_t length = strlen(path);
483 size_t stem = length;
484 if (length >= 4 && (strcmp(path + length - 4,
".jpg") == 0 || strcmp(path + length - 4,
".JPG") == 0)) {
486 }
else if (length >= 5 && (strcmp(path + length - 5,
".jpeg") == 0 || strcmp(path + length - 5,
".JPEG") == 0)) {
489 if (stem + strlen(suffix) + 1 >
capacity) {
return -1; }
490 for (
size_t index = 0; index < stem; index++) {
output[index] = path[index]; }
492 return snprintf(
output + stem,
capacity - stem,
"%s", suffix) < 0 ? -1 : 0;
505 const char *xdg_data_home = getenv(
"XDG_DATA_HOME");
506 const char *home = getenv(
"HOME");
507 if (xdg_data_home != NULL && *xdg_data_home !=
'\0') {
508 if (snprintf(path,
capacity,
"%s/paparazzi/pprzgcs/map", xdg_data_home) >= (
int)
capacity) {
return -1; }
510 if (home == NULL || *home ==
'\0') {
return -1; }
511 if (snprintf(path,
capacity,
"%s/.local/share/paparazzi/pprzgcs/map", home) >= (
int)
capacity) {
return -1; }
526 if (!path) {
return -1; }
527 size_t length = strnlen(path,
sizeof(buffer));
528 if (length == 0 || length >=
sizeof(buffer)) {
return -1; }
529 for (
size_t index = 0; index <= length; index++) { buffer[index] = path[index]; }
530 char *last_separator = strrchr(buffer,
'/');
531 if (last_separator == NULL) {
return 0; }
532 *last_separator =
'\0';
533 length = strlen(buffer);
534 for (
size_t index = 1; index < length; index++) {
535 if (buffer[index] !=
'/') {
continue; }
536 buffer[index] =
'\0';
537 if (mkdir(buffer, 0775) != 0 && errno != EEXIST) {
return -1; }
540 if (mkdir(buffer, 0775) != 0 && errno != EEXIST) {
return -1; }
554static void gcs_key(
unsigned int x,
unsigned int y,
unsigned int zoom,
char *key,
size_t capacity)
556 static const char quadrants[2][2] = {{
'q',
't'}, {
'r',
's'}};
558 key[position++] =
't';
559 for (
int bit = (
int)zoom - 1; bit >= 0 && position + 1 <
capacity; bit--) {
560 key[position++] = quadrants[(x >> bit) & 1U][(y >> bit) & 1U];
562 key[position] =
'\0';
578 unsigned int zoom,
char *path,
size_t capacity)
582 if (access(path, R_OK) == 0) {
return 0; }
584 if (access(path, R_OK) == 0) {
return 0; }
586 gcs_key(x, y, zoom, key,
sizeof(key));
588 if (access(path, R_OK) == 0) {
return 0; }
592 if (access(path, R_OK) == 0) {
return 0; }
594 if (access(path, R_OK) == 0) {
return 0; }
610 unsigned int zoom,
const char *path)
614 if (snprintf(temporary,
sizeof(temporary),
"%s.part.%ld", path, (
long)getpid()) >= (
int)
sizeof(temporary)) {
return -1; }
617 snprintf(url,
sizeof(url),
"https://mt1.google.com/vt/lyrs=s&x=%u&y=%u&z=%u", x, y, zoom);
619 snprintf(url,
sizeof(url),
"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/%u/%u/%u",
622 char *
const arguments[] = {
"curl",
"--fail",
"--location",
"--silent",
"--show-error",
623 "--connect-timeout",
"15",
"--max-time",
"30",
"--retry",
"2",
624 "--retry-delay",
"1",
"--user-agent",
"sensoraia-ear-heatmap-overlay/1",
625 "--output", temporary, url, NULL};
626 pid_t
child = fork();
627 if (
child < 0) {
return -1; }
629 execvp(arguments[0], arguments);
632 int child_status = 0;
633 while (waitpid(
child, &child_status, 0) < 0) {
634 if (errno != EINTR) { unlink(temporary);
return -1; }
636 if (!WIFEXITED(child_status) || WEXITSTATUS(child_status) != 0
637 || rename(temporary, path) != 0) {
656 unsigned int zoom,
struct rgb_image *tile)
661 fprintf(stderr,
"offline mode: cached tile is missing: %s\n", path);
665 if (!
options->
quiet) { fprintf(stderr,
"warning: tile z%u %u/%u unavailable; using dark placeholder\n", zoom, x, y); }
671 tile->
pixels[index * 3] = 40;
672 tile->
pixels[index * 3 + 1] = 40;
673 tile->
pixels[index * 3 + 2] = 40;
697 int tile_x,
int tile_y,
int crop_x,
int crop_y)
699 int left = tile_x * (int)
TILE_SIZE - crop_x;
700 int top = tile_y * (int)
TILE_SIZE - crop_y;
701 int x0 = left < 0 ? 0 : left;
702 int y0 = top < 0 ? 0 : top;
705 if (x0 >= x1 || y0 >= y1) {
return 0; }
706 for (
int y = y0; y < y1; y++) {
707 size_t source_offset = ((size_t)(y - top) *
TILE_SIZE + (
unsigned int)(x0 - left)) * 3U;
708 size_t target_offset = ((size_t)y *
output->width + (
unsigned int)x0) * 3U;
709 size_t bytes = (size_t)(x1 - x0) * 3U;
710 for (
size_t index = 0; index <
bytes; index++) {
output->pixels[target_offset + index] = tile->
pixels[source_offset + index]; }
723 double n = ldexp(1.0, (
int)zoom);
724 return atan(sinh(
PI * (1.0 - 2.0 * tile_y / n))) /
DEG_TO_RAD;
742 int crop_x,
int crop_y,
double alpha)
746 double m_per_deg_lon = m_per_deg_lat * cos_lat;
749 double tile_count = ldexp(1.0, (
int)zoom);
750 for (
unsigned int y = 0; y < base->
height; y++) {
751 double tile_y = ((double)(crop_y + (int)y) + 0.5) /
TILE_SIZE;
754 if (hy < 0.0 || hy >= field->
height) {
continue; }
755 unsigned int fy = (
unsigned int)hy;
756 for (
unsigned int x = 0; x < base->
width; x++) {
757 double tile_x = ((double)(crop_x + (int)x) + 0.5) /
TILE_SIZE;
758 double lon = tile_x / tile_count * 360.0 - 180.0;
760 if (hx < 0.0 || hx >= field->
width) {
continue; }
761 unsigned int fx = (
unsigned int)hx;
762 size_t source = ((size_t)fy * field->
width + fx) * 3U;
766 if (dr * dr + dg * dg + db * db < 24 * 24) {
continue; }
767 size_t target = ((size_t)y * base->
width + x) * 3U;
768 for (
unsigned int channel = 0; channel < 3U; channel++) {
769 double value = base->
pixels[target + channel] * (1.0 - alpha) + field->
pixels[source + channel] * alpha;
770 base->
pixels[target + channel] = (uint8_t)(value + 0.5);
789 uint8_t red, uint8_t green, uint8_t blue,
double alpha)
791 if (x < 0 || y < 0 || (
unsigned int)x >=
image->width || (
unsigned int)y >=
image->height) {
return; }
792 size_t offset = ((size_t)y *
image->width + (
unsigned int)x) * 3U;
793 const uint8_t
color[3] = {red, green, blue};
794 for (
unsigned int channel = 0; channel < 3U; channel++) {
795 double value =
image->pixels[offset + channel] * (1.0 - alpha) +
color[channel] * alpha;
796 image->pixels[offset + channel] = (uint8_t)(value + 0.5);
812 uint8_t red, uint8_t green, uint8_t blue,
double alpha)
814 int radius = width / 2;
815 for (
int dy = -radius; dy <= radius; dy++) {
816 for (
int dx = -radius; dx <= radius; dx++) {
838 int width, uint8_t red, uint8_t green, uint8_t blue,
double alpha)
840 int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
841 int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
845 if (x0 == x1 && y0 == y1) {
break; }
846 int twice = 2 *
error;
847 if (twice >= dy) {
error += dy; x0 += sx; }
848 if (twice <= dx) {
error += dx; y0 += sy; }
867 int width, uint8_t red, uint8_t green, uint8_t blue,
double alpha)
869 if (radius <= 0 || radius > (
int)(
image->width +
image->height)) {
return; }
870 int inner = radius - width;
871 int x0 = cx - radius - 1, x1 = cx + radius + 1;
872 int y0 = cy - radius - 1, y1 = cy + radius + 1;
873 if (x0 < 0) { x0 = 0; }
874 if (y0 < 0) { y0 = 0; }
875 if (x1 >= (
int)
image->width) { x1 = (int)
image->width - 1; }
876 if (y1 >= (
int)
image->height) { y1 = (int)
image->height - 1; }
877 for (
int y = y0; y <= y1; y++) {
878 for (
int x = x0; x <= x1; x++) {
879 int dx = x - cx, dy = y - cy;
880 int distance2 = dx * dx + dy * dy;
881 if (distance2 <= radius * radius && (inner <= 0 || distance2 >= inner * inner)) {
900 uint8_t red, uint8_t green, uint8_t blue)
902 const int radius = 9, arm = 24, gap = 4;
903 for (
int pass = 0; pass < 2; pass++) {
904 int width = pass == 0 ? 5 : 2;
905 uint8_t r = pass == 0 ? 0 : red;
906 uint8_t g = pass == 0 ? 0 : green;
907 uint8_t b = pass == 0 ? 0 : blue;
908 for (
int axis = 0; axis < 2; axis++) {
909 int dx = axis == 0 ? 1 : 0;
910 int dy = axis == 0 ? 0 : 1;
911 draw_line(
image, x + dx * gap, y + dy * gap, x + dx * arm, y + dy * arm, width, r, g, b, 1.0);
912 draw_line(
image, x - dx * gap, y - dy * gap, x - dx * arm, y - dy * arm, width, r, g, b, 1.0);
931 uint8_t red, uint8_t green, uint8_t blue)
933 const int offsets[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
934 for (
int pass = 0; pass < 2; pass++) {
935 int width = pass == 0 ? 5 : 2;
936 uint8_t r = pass == 0 ? 0 : red;
937 uint8_t g = pass == 0 ? 0 : green;
938 uint8_t b = pass == 0 ? 0 : blue;
939 for (
int side = 0; side < 4; side++) {
940 int next = (side + 1) % 4;
941 draw_line(
image, x + offsets[side][0] * radius, y + offsets[side][1] * radius,
942 x + offsets[next][0] * radius, y + offsets[next][1] * radius,
943 width, r, g, b, 1.0);
957 static const uint8_t font[59][7] = {
958 [
'(' - 32] = {2, 4, 8, 8, 8, 4, 2}, [
')' - 32] = {8, 4, 2, 2, 2, 4, 8},
959 [
',' - 32] = {0, 0, 0, 0, 0, 4, 8}, [
'-' - 32] = {0, 0, 0, 31, 0, 0, 0},
960 [
'.' - 32] = {0, 0, 0, 0, 0, 12, 12}, [
'0' - 32] = {14, 17, 19, 21, 25, 17, 14},
961 [
'1' - 32] = {4, 12, 4, 4, 4, 4, 14}, [
'2' - 32] = {14, 17, 1, 2, 4, 8, 31},
962 [
'3' - 32] = {30, 1, 1, 14, 1, 1, 30}, [
'4' - 32] = {2, 6, 10, 18, 31, 2, 2},
963 [
'5' - 32] = {31, 16, 16, 30, 1, 1, 30}, [
'6' - 32] = {14, 16, 16, 30, 17, 17, 14},
964 [
'7' - 32] = {31, 1, 2, 4, 8, 8, 8}, [
'8' - 32] = {14, 17, 17, 14, 17, 17, 14},
965 [
'9' - 32] = {14, 17, 17, 15, 1, 1, 14}, [
':' - 32] = {0, 12, 12, 0, 12, 12, 0},
966 [
'=' - 32] = {0, 31, 0, 31, 0, 0, 0}, [
'>' - 32] = {16, 8, 4, 2, 4, 8, 16},
967 [
'A' - 32] = {14, 17, 17, 31, 17, 17, 17}, [
'B' - 32] = {30, 17, 17, 30, 17, 17, 30},
968 [
'C' - 32] = {14, 17, 16, 16, 16, 17, 14}, [
'D' - 32] = {30, 17, 17, 17, 17, 17, 30},
969 [
'E' - 32] = {31, 16, 16, 30, 16, 16, 31}, [
'F' - 32] = {31, 16, 16, 30, 16, 16, 16},
970 [
'G' - 32] = {14, 17, 16, 23, 17, 17, 15}, [
'H' - 32] = {17, 17, 17, 31, 17, 17, 17},
971 [
'I' - 32] = {14, 4, 4, 4, 4, 4, 14}, [
'J' - 32] = {7, 2, 2, 2, 18, 18, 12},
972 [
'K' - 32] = {17, 18, 20, 24, 20, 18, 17}, [
'L' - 32] = {16, 16, 16, 16, 16, 16, 31},
973 [
'M' - 32] = {17, 27, 21, 21, 17, 17, 17}, [
'N' - 32] = {17, 25, 21, 19, 17, 17, 17},
974 [
'O' - 32] = {14, 17, 17, 17, 17, 17, 14}, [
'P' - 32] = {30, 17, 17, 30, 16, 16, 16},
975 [
'Q' - 32] = {14, 17, 17, 17, 21, 18, 13}, [
'R' - 32] = {30, 17, 17, 30, 20, 18, 17},
976 [
'S' - 32] = {15, 16, 16, 14, 1, 1, 30}, [
'T' - 32] = {31, 4, 4, 4, 4, 4, 4},
977 [
'U' - 32] = {17, 17, 17, 17, 17, 17, 14}, [
'V' - 32] = {17, 17, 17, 17, 17, 10, 4},
978 [
'W' - 32] = {17, 17, 17, 21, 21, 21, 10}, [
'X' - 32] = {17, 17, 10, 4, 10, 17, 17},
979 [
'Y' - 32] = {17, 17, 10, 4, 4, 4, 4}, [
'Z' - 32] = {31, 1, 2, 4, 8, 16, 31}
981 unsigned char upper = (
unsigned char)toupper((
unsigned char)character);
982 return upper >= 32U && upper <= 90U ? font[upper - 32U] : font[
' ' - 32];
997 unsigned int scale, uint8_t red, uint8_t green, uint8_t blue)
999 for (
const unsigned char *cursor = (
const unsigned char *)text; *cursor !=
'\0'; cursor++) {
1001 for (
unsigned int row = 0;
row < 7U;
row++) {
1002 for (
unsigned int column = 0; column < 5U; column++) {
1003 if ((
glyph[
row] & (1U << (4U - column))) == 0) {
continue; }
1004 for (
unsigned int dy = 0; dy < scale; dy++) {
1005 for (
unsigned int dx = 0; dx < scale; dx++) {
1007 red, green, blue, 1.0);
1012 x += (int)(6U * scale);
1029 uint8_t red, uint8_t green, uint8_t blue)
1031 const unsigned int scale = 2U;
1032 for (
int dy = -1; dy <= 1; dy++) {
1033 for (
int dx = -1; dx <= 1; dx++) {
1051 unsigned int zoom,
int *x,
int *y)
1053 double tile_x, tile_y;
1055 *x = (int)floor((tile_x - origin_x) *
TILE_SIZE);
1056 *y = (int)floor((tile_y - origin_y) *
TILE_SIZE);
1081static bool csv_number(
const char *line,
unsigned int column,
double *value)
1083 const char *field = line;
1084 for (
unsigned int index = 0; index < column; index++) {
1085 field = strchr(field,
',');
1086 if (field == NULL) {
return false; }
1090 size_t length = strcspn(field,
",\r\n");
1091 if (length == 0 || length >=
sizeof(buffer)) {
return false; }
1092 for (
size_t index = 0; index < length; index++) { buffer[index] = field[index]; }
1093 buffer[length] =
'\0';
1108 FILE *input = fopen(path,
"r");
1109 if (input == NULL) { perror(path);
return -1; }
1112 while (fgets(line,
sizeof(line), input) != NULL) {
1113 if (line[0] ==
'\0' || line[0] ==
'#' || line[0] ==
't') {
continue; }
1124 if (ferror(input) != 0) { status = -1; }
1125 if (fclose(input) != 0) { status = -1; }
1126 if (status != 0) { fprintf(stderr,
"%s: invalid or excessive CSV input\n", path); }
1141 size_t length = strnlen(
header,
sizeof(copy));
1142 if (length >=
sizeof(copy)) {
return -1; }
1143 for (
size_t index = 0; index <= length; index++) { copy[index] =
header[index]; }
1146 for (
char *field = strtok_r(copy,
",\r\n", &save); field != NULL; field = strtok_r(NULL,
",\r\n", &save), column++) {
1147 if (strcmp(field, name) == 0) {
return column; }
1153static bool csv_column(
const char *line,
unsigned int column,
double *value)
1174 double a = *(
const double *)left;
1175 double b = *(
const double *)right;
1176 return (a > b) - (a < b);
1190 FILE *input = fopen(path,
"r");
1191 if (input == NULL) { perror(path);
return -1; }
1193 int east_column = -1, lat_column = -1, lon_column = -1, height_column = -1;
1194 if (fgets(line,
sizeof(line), input) != NULL) {
1200 if (east_column < 0 || lat_column < 0 || lon_column < 0 || height_column < 0) {
1202 fprintf(stderr,
"%s: expected x_east_m,lat,lon,mnh_m columns\n", path);
1206 size_t easting_count = 0;
1208 while (fgets(line,
sizeof(line), input) != NULL) {
1210 int columns[4] = {east_column, lat_column, lon_column, height_column};
1212 for (
int index = 0; index < 4; index++) {
1213 if (!
csv_column(line, (
unsigned int)columns[index], &values[index])) {
valid =
false;
break; }
1215 if (!
valid || fabs(values[1]) > 90.0 || fabs(values[2]) > 180.0) {
continue; }
1219 eastings[easting_count++] = values[0];
1221 if (ferror(input) != 0) { status = -1; }
1223 if (status == 0 && easting_count > 1) {
1224 qsort(eastings, easting_count,
sizeof(*eastings),
compare_double);
1225 *cell_size = INFINITY;
1226 for (
size_t index = 1; index < easting_count; index++) {
1227 double difference = eastings[index] - eastings[index - 1];
1228 if (difference > 0.0 && difference < *cell_size) { *cell_size = difference; }
1230 if (!isfinite(*cell_size)) { status = -1; }
1231 }
else { status = -1; }
1232 if (status != 0) { fprintf(stderr,
"%s: no usable obstacle-grid spacing\n", path); }
1246static double haversine_m(
double lat_a,
double lon_a,
double lat_b,
double lon_b)
1250 double a = sin(d_lat / 2.0) * sin(d_lat / 2.0)
1252 * sin(d_lon / 2.0) * sin(d_lon / 2.0);
1253 return 2.0 *
EARTH_RADIUS_M * asin(sqrt(fmax(0.0, fmin(1.0, a))));
1267 double origin_x,
double origin_y,
unsigned int zoom)
1269 for (
size_t index = 0; index < points->count; index++) {
1271 pixel_location(points->items[index].lat, points->items[index].lon, origin_x, origin_y, zoom, &x, &y);
1289 double origin_x,
double origin_y,
unsigned int zoom,
double gap_s)
1291 for (
size_t index = 1; index < points->count; index++) {
1292 if (points->items[index].time_s - points->items[index - 1].time_s > gap_s) {
continue; }
1294 pixel_location(points->items[index - 1].lat, points->items[index - 1].lon, origin_x, origin_y, zoom, &x0, &y0);
1295 pixel_location(points->items[index].lat, points->items[index].lon, origin_x, origin_y, zoom, &x1, &y1);
1315 double cell_size,
double m_per_deg_lat,
double m_per_deg_lon,
1316 double origin_x,
double origin_y,
unsigned int zoom)
1318 for (
size_t index = 0; index < obstacles->
count; index++) {
1320 if (cell->
height_m < 3.0) {
continue; }
1323 cell->
lon - cell_size / (2.0 * m_per_deg_lon), origin_x, origin_y, zoom, &x0, &y0);
1325 cell->
lon + cell_size / (2.0 * m_per_deg_lon), origin_x, origin_y, zoom, &x1, &y1);
1326 if (x0 > x1) {
int swap = x0; x0 = x1; x1 = swap; }
1327 if (y0 > y1) {
int swap = y0; y0 = y1; y1 = swap; }
1328 if (x1 < 0 || y1 < 0 || x0 >= (
int)
image->width || y0 >= (
int)
image->height) {
continue; }
1329 if (x0 < 0) { x0 = 0; }
1330 if (y0 < 0) { y0 = 0; }
1331 if (x1 >= (
int)
image->width) { x1 = (int)
image->width - 1; }
1332 if (y1 >= (
int)
image->height) { y1 = (int)
image->height - 1; }
1333 double alpha = cell->
height_m > 6.0 ? 0.48 : 0.35;
1334 for (
int y = y0; y <= y1; y++) {
1355 double search_lat = 0.0, search_lon = 0.0, search_radius = 25.0;
1358 fprintf(stderr,
"--search expects LAT,LON[,RADIUS_M]\n");
return -1;
1360 double truth_lat = 0.0, truth_lon = 0.0, unused_radius = 0.0;
1363 fprintf(stderr,
"--truth expects LAT,LON\n");
return -1;
1367 if (m_per_deg_lon <= 1e-6) { fprintf(stderr,
"Heatmap is too close to a pole.\n");
return -1; }
1376 double east = (search_lon - geo->
lon_center) * m_per_deg_lon;
1377 double north = (search_lat - geo->
lat_center) * m_per_deg_lat;
1378 double needed[4] = {east + search_radius - (east0 + width_m) + 5.0,
1379 east0 - (east - search_radius) + 5.0,
1380 north + search_radius - north0 + 5.0,
1381 (north0 -
height_m) - (north - search_radius) + 5.0};
1382 for (
unsigned int index = 0; index < 4; index++) {
if (needed[index] > margin) { margin = needed[index]; } }
1384 double lat_north = geo->
lat_center + (north0 + margin) / m_per_deg_lat;
1386 double lon_west = geo->
lon_center + (east0 - margin) / m_per_deg_lon;
1387 double lon_east = geo->
lon_center + (east0 + width_m + margin) / m_per_deg_lon;
1388 if (lat_north > 85.0 || lat_south < -85.0 || lon_west < -180.0 || lon_east > 180.0) {
1389 fprintf(stderr,
"Requested overlay crosses supported map bounds.\n");
return -1;
1392 double x_w, y_n, x_e, y_s;
1393 mercator_xy(lat_north, lon_west, zoom, &x_w, &y_n);
1394 mercator_xy(lat_south, lon_east, zoom, &x_e, &y_s);
1395 if (!(x_e > x_w && y_s > y_n)) {
return -1; }
1396 int tx0 = (int)floor(x_w), ty0 = (int)floor(y_n);
1397 int tx1 = (int)floor(x_e), ty1 = (int)floor(y_s);
1398 if (tx0 < 0 || ty0 < 0 || tx1 < tx0 || ty1 < ty0
1399 || tx1 >= (
int)ldexp(1.0, (
int)zoom) || ty1 >= (
int)ldexp(1.0, (
int)zoom)) {
1400 fprintf(stderr,
"Tile bounds are outside Web Mercator.\n");
return -1;
1403 uint64_t tile_count = (uint64_t)(tx1 - tx0 + 1) * (uint64_t)(ty1 - ty0 + 1);
1404 if (tile_count >
MAX_TILE_COUNT) { fprintf(stderr,
"Overlay requires too many map tiles (%llu).\n", (
unsigned long long)tile_count);
return -1; }
1405 int crop_x = (int)floor((x_w - tx0) *
TILE_SIZE);
1406 int crop_y = (int)floor((y_n - ty0) *
TILE_SIZE);
1407 int end_x = (int)ceil((x_e - tx0) *
TILE_SIZE);
1408 int end_y = (int)ceil((y_s - ty0) *
TILE_SIZE);
1409 if (end_x <= crop_x || end_y <= crop_y) {
return -1; }
1410 output->width = (
unsigned int)(end_x - crop_x);
1411 output->height = (
unsigned int)(end_y - crop_y);
1412 uint64_t pixels = (uint64_t)
output->width *
output->height;
1414 fprintf(stderr,
"Overlay output exceeds the %u-pixel memory limit. Increase margin less or choose a lower zoom.\n",
MAX_OUTPUT_PIXELS);
1420 for (
size_t index = 0; index < (size_t)pixels * 3U; index++) {
output->pixels[index] = 0; }
1421 for (
int ty = ty0; ty <= ty1; ty++) {
1422 for (
int tx = tx0; tx <= tx1; tx++) {
1424 if (
fetch_tile(
options, (
unsigned int)tx, (
unsigned int)ty, zoom, &tile) != 0) {
return -1; }
1429 int crop_end_x = crop_x + (int)
output->width;
1430 int crop_end_y = crop_y + (int)
output->height;
1432 double origin_x = tx0 + (double)crop_x /
TILE_SIZE;
1433 double origin_y = ty0 + (double)crop_y /
TILE_SIZE;
1446 double cell_size = 0.0;
1450 draw_obstacles(
output, &obstacles, cell_size, m_per_deg_lat, m_per_deg_lon, origin_x, origin_y, zoom);
1454 int bar_length = (int)fmin(50.0 / output_m_per_px, 0.6 *
output->width);
1455 int bar_x = (int)(0.04 *
output->width);
1456 int bar_y = (int)(0.94 *
output->height);
1457 draw_line(
output, bar_x, bar_y, bar_x + bar_length, bar_y, 5, 0, 0, 0, 1.0);
1458 draw_line(
output, bar_x, bar_y, bar_x + bar_length, bar_y, 2, 255, 255, 255, 1.0);
1459 draw_line(
output, bar_x, bar_y - 6, bar_x, bar_y + 6, 2, 255, 255, 255, 1.0);
1460 draw_line(
output, bar_x + bar_length, bar_y - 6, bar_x + bar_length, bar_y + 6, 2, 255, 255, 255, 1.0);
1471 pixel_location(search_lat, search_lon, origin_x, origin_y, zoom, &x, &y);
1472 int radius = (int)(search_radius / output_m_per_px + 0.5);
1476 char radius_label[48];
1477 snprintf(radius_label,
sizeof(radius_label),
"R=%.0f M", search_radius);
1478 draw_text(
output, x + 8, y - radius - 20,
"GIVEN POINT", 255, 255, 255);
1479 draw_text(
output, x - 12, y - radius - 4, radius_label, 255, 255, 255);
1483 pixel_location(truth_lat, truth_lon, origin_x, origin_y, zoom, &x, &y);
1488 char offset_label[48];
1489 snprintf(offset_label,
sizeof(offset_label),
"%.1f M OFF", offset_m);
1491 fprintf(stderr,
"truth-to-estimated-spot=%.1f m\n", offset_m);
1495 printf(
"overlay %ux%u, %s tiles z%u, %.3f m/px\n",
output->width,
output->height,
1512 if (argc == 2 && (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)) {
1516 if (argc == 2 && (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-h") == 0)) {
1529 fprintf(stderr,
"Invalid source, zoom, alpha, margin or track gap.\n");
1545 fprintf(stderr,
"Set HOME or pass --cache.\n");
return 2;
1551 fprintf(stderr,
"%s: cannot read valid EAR heatmap georeference\n",
options.
geo_path);
return 1;
1556 const char *field_input = access(field_path, R_OK) == 0 ? field_path :
options.
input;
1558 fprintf(stderr,
"Cannot read heatmap JPEG: %s\n", field_input);
return 1;
1562 fprintf(stderr,
"Heatmap dimensions do not match its .geo sidecar.\n");
1574 return status == 0 ? 0 : 1;
static unsigned capacity[COVERAGE_CAMERAS]
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
static FILE * session_log
int ear_heatmap_read_georef(const char *filename, struct ear_heatmap_georef *georef)
Read the georeference sidecar emitted by the heatmap renderer.
Render geotagged acoustic measurements into north-up JPEG heatmaps.
#define EAR_HEATMAP_NO_DATA_GREEN
#define EAR_HEATMAP_NO_DATA_RED
#define EAR_HEATMAP_NO_DATA_BLUE
static void draw_log_points(struct rgb_image *image, const struct point_list *points, double origin_x, double origin_y, unsigned int zoom)
Redraw sample locations as crisp dots over the blended sound field.
static uint8_t output_pixels[(size_t) MAX_OUTPUT_PIXELS *3U]
static void draw_obstacles(struct rgb_image *image, const struct obstacle_list *obstacles, double cell_size, double m_per_deg_lat, double m_per_deg_lon, double origin_x, double origin_y, unsigned int zoom)
Paint optional height-above-terrain cells over the output map.
static int header_column(const char *header, const char *name)
Locate an exact field name in a CSV header without mutating the source.
static int default_cache_path(char *path, size_t capacity)
Resolve the Paparazzi GCS map-cache root from XDG environment settings.
static bool csv_number(const char *line, unsigned int column, double *value)
Parse one complete numeric field from a bounded CSV row.
static double obstacle_eastings[MAX_CSV_POINTS]
static double mercator_m_per_pixel(double lat, unsigned int zoom)
Estimate ground metres per tile pixel at a latitude.
static bool parse_location(const char *text, bool with_radius, double *lat, double *lon, double *radius)
Parse LAT,LON or LAT,LON,RADIUS_M into bounded geographic values.
static uint8_t tile_pixels[(size_t) TILE_SIZE *TILE_SIZE *3U]
static int build_output(const struct options *options, const struct ear_heatmap_georef *geo, const struct rgb_image *heat, struct rgb_image *output)
Assemble basemap tiles, heatfield and optional annotations into one output raster.
int main(int argc, char **argv)
Validate replay artifacts, compose the map context and write a JPEG.
static void gcs_key(unsigned int x, unsigned int y, unsigned int zoom, char *key, size_t capacity)
Encode an XYZ address using Paparazzi's legacy Google quadkey alphabet.
static void print_help(const char *program)
Print command synopsis and defaults from the same option vocabulary parsed below.
static int image_read_jpeg(const char *path, struct rgb_image *image)
Decode one JPEG into the caller's bounded RGB workspace.
static struct obstacle obstacle_cells[MAX_CSV_POINTS]
static int append_obstacle(struct obstacle_list *list, const struct obstacle *obstacle)
Append one obstacle cell when fixed storage has room.
static void draw_line(struct rgb_image *image, int x0, int y0, int x1, int y1, int width, uint8_t red, uint8_t green, uint8_t blue, double alpha)
Draw a raster line with an integer Bresenham walk.
static bool parse_options(int argc, char **argv, struct options *options)
Parse one positional heatmap and the supported optional controls.
static struct point track_points[MAX_CSV_POINTS]
#define DEFAULT_TRACK_GAP_S
static void draw_crosshair(struct rgb_image *image, int x, int y, uint8_t red, uint8_t green, uint8_t blue)
Draw a distinct, high-contrast marker for the estimated loudest spot.
static struct point session_points[MAX_CSV_POINTS]
static int make_stem_path(const char *path, const char *suffix, char *output, size_t capacity)
Derive a sibling .geo, _field.jpg or _sat.jpg path from an image.
static int fetch_tile(const struct options *options, unsigned int x, unsigned int y, unsigned int zoom, struct rgb_image *tile)
Read a local hit, fetch a miss if permitted, and decode one 256x256 tile.
static void pixel_location(double lat, double lon, double origin_x, double origin_y, unsigned int zoom, int *x, int *y)
Convert a geographic annotation to pixels in the cropped tile mosaic.
static int tile_path(const struct options *options, unsigned int x, unsigned int y, unsigned int zoom, char *path, size_t capacity)
Select the first known local filename for an XYZ source tile.
static void draw_track(struct rgb_image *image, const struct point_list *points, double origin_x, double origin_y, unsigned int zoom, double gap_s)
Draw aircraft-track segments, breaking where fixes are too far apart.
static unsigned int choose_zoom(const struct ear_heatmap_georef *geo, unsigned int max_zoom)
Select the first basemap zoom at least as detailed as the heatmap.
static uint8_t input_pixels[(size_t) MAX_INPUT_PIXELS *3U]
Reused input, tile and final RGB workspaces; avoid per-image allocator churn.
static void draw_text(struct rgb_image *image, int x, int y, const char *text, uint8_t red, uint8_t green, uint8_t blue)
Draw a small outlined label over arbitrary imagery.
static void put_blended_pixel(struct rgb_image *image, int x, int y, uint8_t red, uint8_t green, uint8_t blue, double alpha)
Blend one annotation pixel while clipping out-of-image coordinates.
static bool parse_unsigned(const char *text, unsigned int *value)
Parse an unsigned integer option without accepting suffix characters.
static double mercator_latitude(double tile_y, unsigned int zoom)
Invert fractional Web Mercator tile Y into a geographic latitude.
#define MAX_OUTPUT_PIXELS
static void draw_circle(struct rgb_image *image, int cx, int cy, int radius, int width, uint8_t red, uint8_t green, uint8_t blue, double alpha)
Draw a filled disc or clipped ring with a fixed-width stroke.
static bool csv_column(const char *line, unsigned int column, double *value)
Read one required numeric column from a LiDAR CSV row.
static int make_directories(const char *path)
Create parent directories for a tile file without treating the file as a directory.
static void draw_diamond(struct rgb_image *image, int x, int y, int radius, uint8_t red, uint8_t green, uint8_t blue)
Draw the known simulation source as a shape distinct from the estimate.
static void jpeg_error_exit(j_common_ptr info)
Convert libjpeg's fatal callback into a jump to the active operation.
static void draw_brush(struct rgb_image *image, int x, int y, int width, uint8_t red, uint8_t green, uint8_t blue, double alpha)
Stamp a square brush centered at an integer pixel.
static const uint8_t * glyph_rows(char character)
Return one embedded 5x7 uppercase bitmap glyph.
static void composite_heatmap(struct rgb_image *base, const struct rgb_image *field, const struct ear_heatmap_georef *geo, unsigned int zoom, int crop_x, int crop_y, double alpha)
Resample the local equirectangular heat field onto the Mercator tile mosaic.
static bool parse_double(const char *text, double *value)
Parse a finite floating-point token in full.
static void mercator_xy(double lat, double lon, unsigned int zoom, double *x, double *y)
Project latitude/longitude into fractional Web Mercator tile coordinates.
static int read_points(const char *path, bool session_log, struct point_list *points)
Read geographic points from an EAR session or aircraft-track CSV.
static int append_point(struct point_list *list, const struct point *point)
Append a point into caller-owned bounded storage.
static int image_write_jpeg(const char *path, const struct rgb_image *image)
Encode a bounded RGB view to a JPEG file.
static void image_free(struct rgb_image *image)
Invalidate an RGB view before its shared workspace is reused.
static int compare_double(const void *left, const void *right)
Order projected eastings so adjacent positive deltas reveal cell pitch.
static double haversine_m(double lat_a, double lon_a, double lat_b, double lon_b)
Compute great-circle distance between two latitude/longitude points.
static int copy_tile_to_output(const struct rgb_image *tile, struct rgb_image *output, int tile_x, int tile_y, int crop_x, int crop_y)
Copy only the part of a source tile intersecting the cropped output.
static int download_tile(const struct options *options, unsigned int x, unsigned int y, unsigned int zoom, const char *path)
Download one cache miss with the existing curl executable.
static int read_obstacles(const char *path, struct obstacle_list *obstacles, double *cell_size)
Read the optional regular LiDAR height grid and infer its cell width.
static void draw_text_pass(struct rgb_image *image, int x, int y, const char *text, unsigned int scale, uint8_t red, uint8_t green, uint8_t blue)
Paint one foreground or outline pass of bitmap text.
static struct ear_sample samples[EAR_MAX_SAMPLES]
struct jpeg_compress_struct encoder
uint8_t row[LWIR_MAX_WIDTH *3U]
struct jpeg_error_mgr error
struct jpeg_decompress_struct decoder
static uint8_t glyph(char character, unsigned row)
One row of a built-in 5x7 bitmap font.
static void color(unsigned intensity, enum lwir_palette palette, unsigned *red, unsigned *green, unsigned *blue)
Map a window intensity to RGB for the selected palette.
static const char header[]
Compile-time build identity strings for USHER executables.
#define EAR_HEATMAP_OVERLAY_BUILD_VERSION
Validated geographic placement metadata shared by heatmap consumers.
struct jpeg_error_mgr manager
Bounded view over LiDAR cells and their inferred regular spacing.
One optional terrain-height cell supplied for visual context.
Validated capture and reporting policy, fixed before the sample loop.
const char * obstacles_path
Fixed-capacity, non-owning view over parsed session/track points.
One timestamped geographic position for a replay annotation.
Non-owning view of a bounded interleaved RGB workspace.
struct captured_image image