kml.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "kml.h"
  2. #define TAG "kml"
  3. bool kml_open_file(Storage* storage, KMLFile* kml, const char* path) {
  4. kml->file = storage_file_alloc(storage);
  5. if (!storage_file_open(kml->file,
  6. path,
  7. FSAM_WRITE,
  8. FSOM_CREATE_ALWAYS)) {
  9. FURI_LOG_E(TAG, "failed to open KML file %s", path);
  10. storage_file_free(kml->file);
  11. return false;
  12. }
  13. // with the file opened, we need to write the intro KML tags
  14. const char* kml_intro =
  15. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  16. "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
  17. " <Document>\n"
  18. " <name>Paths</name>\n"
  19. " <Style id=\"yellowLineGreenPoly\">\n"
  20. " <LineStyle>\n"
  21. " <color>7f00ffff</color>\n"
  22. " <width>4</width>\n"
  23. " </LineStyle>\n"
  24. " <PolyStyle>\n"
  25. " <color>7f00ff00</color>\n"
  26. " </PolyStyle>\n"
  27. " </Style>\n"
  28. " <Placemark>\n"
  29. " <name>Path 1</name>\n"
  30. " <description>Path 1</description>\n"
  31. " <styleUrl>#yellowLineGreenPoly</styleUrl>\n"
  32. " <LineString>\n"
  33. " <tessellate>1</tessellate>\n"
  34. " <extrude>1</extrude>\n"
  35. " <altitudeMode>absolute</altitudeMode>\n"
  36. " <coordinates>\n";
  37. if (!storage_file_write(kml->file,
  38. kml_intro,
  39. strlen(kml_intro))) {
  40. storage_file_close(kml->file);
  41. storage_file_free(kml->file);
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool kml_add_path_point(KMLFile* kml, double lat, double lon, uint32_t alt) {
  47. // KML is longitude then latitude for some reason
  48. FuriString* point = furi_string_alloc_printf(" %f,%f,%lu\n", lon, lat, alt);
  49. if (!storage_file_write(kml->file,
  50. furi_string_get_cstr(point),
  51. furi_string_size(point))) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. bool kml_close_file(KMLFile* kml) {
  57. const char* kml_outro =
  58. " </coordinates>\n"
  59. " </LineString>\n"
  60. " </Placemark>\n"
  61. " </Document>\n"
  62. "</kml>";
  63. if (!storage_file_write(kml->file,
  64. kml_outro,
  65. strlen(kml_outro))) {
  66. storage_file_close(kml->file);
  67. storage_file_free(kml->file);
  68. return false;
  69. }
  70. storage_file_close(kml->file);
  71. storage_file_free(kml->file);
  72. return true;
  73. }