gpx2yaml

GPX to YAML converter
git clone git://git.sikmir.ru/gpx2yaml
Log | Files | Refs | README | LICENSE

gpx2yaml.c (3608B)


      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 		strlcpy(curelement, tag, sizeof(curelement));
     53 	} else if (istag(tag, "trkseg")) {
     54 		if (trksegindex == 1) {
     55 			printf("  geometry:\n");
     56 			printf("    type: LineString\n");
     57 			printf("    coordinates:\n");
     58 		}
     59 		trksegindex++;
     60 	} else if (istag(tag, "trkpt")) {
     61 		memset(&pos, 0, sizeof(pos));
     62 	}
     63 }
     64 
     65 void
     66 xml_handler_end_element(XMLParser *p, const char *tag, size_t taglen, int isshort)
     67 {
     68 	if (istag(tag, "wpt")) {
     69 		curgpxelement[0] = '\0';
     70 		printf("  geometry:\n");
     71 		printf("    type: Point\n");
     72 		printf("    coordinates:\n");
     73 		printf("    - %s\n", pos.lon);
     74 		printf("    - %s\n", pos.lat);
     75 	} else if (istag(tag, "trk")) {
     76 		curgpxelement[0] = '\0';
     77 	} else if (istag(tag, "name") || istag(tag, "desc") || istag(tag, "type") ||
     78 	           istag(tag, "text") || istag(tag, "gpxtrkx:Distance")) {
     79 		curelement[0] = '\0';
     80 	} else if (istag(tag, "link")) {
     81 		linkindex++;
     82 	} else if (istag(tag, "trkpt")) {
     83 		printf("    - - %s\n", pos.lon);
     84 		printf("      - %s\n", pos.lat);
     85 	}
     86 }
     87 
     88 void
     89 xml_handler_attr(XMLParser *p, const char *tag, size_t taglen, const char *name, size_t namelen,
     90         const char *value, size_t valuelen)
     91 {
     92 	if (strcmp(curgpxelement, "wpt") != 0 && strcmp(curgpxelement, "trk") != 0)
     93 		return;
     94 
     95 	if (istag(tag, "wpt") || istag(tag, "trkpt")) {
     96 		if (isattr(name, "lat")) {
     97 			strlcpy(pos.lat, value, sizeof(pos.lat));
     98 		} else if (isattr(name, "lon")) {
     99 			strlcpy(pos.lon, value, sizeof(pos.lon));
    100 		}
    101 	} else if (istag(tag, "link")) {
    102 		if (isattr(name, "href")) {
    103 			printf("    link%d_href: \"%s\"\n", linkindex, value);
    104 		}
    105 	}
    106 }
    107 
    108 void
    109 xml_handler_data(XMLParser *p, const char *data, size_t datalen)
    110 {
    111 	if (strcmp(curgpxelement, "wpt") != 0 && strcmp(curgpxelement, "trk") != 0)
    112 		return;
    113 
    114 	if (strcmp(curelement, "name") == 0) {
    115 		printf("    name: \"%s\"\n", data);
    116 	} else if (strcmp(curelement, "desc") == 0) {
    117 		printf("    desc: \"%s\"\n", data);
    118 	} else if (strcmp(curelement, "type") == 0) {
    119 		printf("    type: \"%s\"\n", data);
    120 	} else if (strcmp(curelement, "text") == 0) {
    121 		printf("    link%d_text: \"%s\"\n", linkindex, data);
    122 	} else if (strcmp(curelement, "gpxtrkx:Distance") == 0) {
    123 		printf("    distance: %s\n", data);
    124 	}
    125 }
    126 
    127 int
    128 main(void)
    129 {
    130 	parser.xmltagstart = xml_handler_start_element;
    131 	parser.xmltagend = xml_handler_end_element;
    132 	parser.xmlattr = xml_handler_attr;
    133 	parser.xmldata = xml_handler_data;
    134 
    135 	parser.getnext = getchar;
    136 
    137 	xml_parse(&parser);
    138 
    139 	return 0;
    140 }