gpx2yaml.c (4326B)
1 #include <stdio.h> 2 #include <string.h> 3 4 #include "util.h" 5 #include "xml.h" 6 7 struct pos { 8 char lat[16]; 9 char lon[16]; 10 }; 11 12 static XMLParser parser; /* XML parser state */ 13 static char curelement[32]; 14 static char curgpxelement[16]; 15 static struct pos pos; 16 static int linkindex; 17 static int trksegindex; 18 19 static int 20 istag(const char *s1, const char *s2) 21 { 22 return !strcasecmp(s1, s2); 23 } 24 25 static int 26 isattr(const char *s1, const char *s2) 27 { 28 return !strcasecmp(s1, s2); 29 } 30 31 void 32 xml_handler_start_element(XMLParser *p, const char *tag, size_t taglen) 33 { 34 if (istag(tag, "gpx")) { 35 printf("---\n"); 36 printf("type: FeatureCollection\n"); 37 printf("features:\n"); 38 } else if (istag(tag, "wpt")) { 39 strlcpy(curgpxelement, tag, sizeof(curgpxelement)); 40 memset(&pos, 0, sizeof(pos)); 41 linkindex = 1; 42 printf("- type: Feature\n"); 43 printf(" properties:\n"); 44 } else if (istag(tag, "trk")) { 45 strlcpy(curgpxelement, tag, sizeof(curgpxelement)); 46 linkindex = 1; 47 trksegindex = 1; 48 printf("- type: Feature\n"); 49 printf(" properties:\n"); 50 } else if (istag(tag, "name") || istag(tag, "desc") || istag(tag, "type") || 51 istag(tag, "text") || istag(tag, "gpxtrkx:Distance") || 52 istag(tag, "gpxtrkx:Ascent") || istag(tag, "gpxtrkx:Descent") || 53 istag(tag, "gpxtrkx:MovingTime") || istag(tag, "gpxtrkx:StoppedTime")) { 54 strlcpy(curelement, tag, sizeof(curelement)); 55 } else if (istag(tag, "trkseg")) { 56 if (trksegindex == 1) { 57 printf(" geometry:\n"); 58 printf(" type: LineString\n"); 59 printf(" coordinates:\n"); 60 } 61 trksegindex++; 62 } else if (istag(tag, "trkpt")) { 63 memset(&pos, 0, sizeof(pos)); 64 } 65 } 66 67 void 68 xml_handler_end_element(XMLParser *p, const char *tag, size_t taglen, int isshort) 69 { 70 if (istag(tag, "wpt")) { 71 curgpxelement[0] = '\0'; 72 printf(" geometry:\n"); 73 printf(" type: Point\n"); 74 printf(" coordinates:\n"); 75 printf(" - %s\n", pos.lon); 76 printf(" - %s\n", pos.lat); 77 } else if (istag(tag, "trk")) { 78 curgpxelement[0] = '\0'; 79 } else if (istag(tag, "name") || istag(tag, "desc") || istag(tag, "type") || 80 istag(tag, "text") || istag(tag, "gpxtrkx:Distance") || 81 istag(tag, "gpxtrkx:Ascent") || istag(tag, "gpxtrkx:Descent") || 82 istag(tag, "gpxtrkx:MovingTime") || istag(tag, "gpxtrkx:StoppedTime")) { 83 curelement[0] = '\0'; 84 } else if (istag(tag, "link")) { 85 linkindex++; 86 } else if (istag(tag, "trkpt")) { 87 printf(" - - %s\n", pos.lon); 88 printf(" - %s\n", pos.lat); 89 } 90 } 91 92 void 93 xml_handler_attr(XMLParser *p, const char *tag, size_t taglen, const char *name, size_t namelen, 94 const char *value, size_t valuelen) 95 { 96 if (strcmp(curgpxelement, "wpt") != 0 && strcmp(curgpxelement, "trk") != 0) 97 return; 98 99 if (istag(tag, "wpt") || istag(tag, "trkpt")) { 100 if (isattr(name, "lat")) { 101 strlcpy(pos.lat, value, sizeof(pos.lat)); 102 } else if (isattr(name, "lon")) { 103 strlcpy(pos.lon, value, sizeof(pos.lon)); 104 } 105 } else if (istag(tag, "link")) { 106 if (isattr(name, "href")) { 107 printf(" link%d_href: \"%s\"\n", linkindex, value); 108 } 109 } 110 } 111 112 void 113 xml_handler_data(XMLParser *p, const char *data, size_t datalen) 114 { 115 if (strcmp(curgpxelement, "wpt") != 0 && strcmp(curgpxelement, "trk") != 0) 116 return; 117 118 if (strcmp(curelement, "name") == 0) { 119 printf(" name: \"%s\"\n", data); 120 } else if (strcmp(curelement, "desc") == 0) { 121 printf(" desc: \"%s\"\n", data); 122 } else if (strcmp(curelement, "type") == 0) { 123 printf(" type: \"%s\"\n", data); 124 } else if (strcmp(curelement, "text") == 0) { 125 printf(" link%d_text: \"%s\"\n", linkindex, data); 126 } else if (strcmp(curelement, "gpxtrkx:Distance") == 0) { 127 printf(" distance: %s\n", data); 128 } else if (strcmp(curelement, "gpxtrkx:Ascent") == 0) { 129 printf(" ascent: %s\n", data); 130 } else if (strcmp(curelement, "gpxtrkx:Descent") == 0) { 131 printf(" descent: %s\n", data); 132 } else if (strcmp(curelement, "gpxtrkx:MovingTime") == 0) { 133 printf(" moving_time: %s\n", data); 134 } else if (strcmp(curelement, "gpxtrkx:StoppedTime") == 0) { 135 printf(" stopped_time: %s\n", data); 136 } 137 } 138 139 int 140 main(void) 141 { 142 parser.xmltagstart = xml_handler_start_element; 143 parser.xmltagend = xml_handler_end_element; 144 parser.xmlattr = xml_handler_attr; 145 parser.xmldata = xml_handler_data; 146 147 parser.getnext = getchar; 148 149 xml_parse(&parser); 150 151 return 0; 152 }