kml.c 2.8 KB

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