geojson2dm

Convert GeoJSON to format suitable for input to datamaps
git clone git://git.sikmir.ru/geojson2dm
Log | Files | Refs | README | LICENSE

geojson2dm.c (1117B)


      1 #include <ctype.h>
      2 #include <errno.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "util.h"
      8 #include "json.h"
      9 
     10 struct pos {
     11 	char lat[16];
     12 	char lon[16];
     13 };
     14 
     15 static struct pos pos;
     16 
     17 void
     18 processnode(struct json_node *nodes, size_t depth, const char *value)
     19 {
     20 	if (depth != 7 ||
     21 	    nodes[4].type != JSON_TYPE_ARRAY ||
     22 	    nodes[5].type != JSON_TYPE_ARRAY ||
     23 	    strcmp(nodes[4].name, "coordinates") != 0)
     24 		return;
     25 
     26 	switch (nodes[5].index) {
     27 	case 0:
     28 		switch (nodes[4].index) {
     29 		case 0:
     30 			memset(&pos, 0, sizeof(pos));
     31 			break;
     32 		default:
     33 			printf("%s,%s", pos.lat, pos.lon);
     34 			break;
     35 		}
     36 		strlcpy(pos.lon, value, sizeof(pos.lon));
     37 		break;
     38 	case 1:
     39 		strlcpy(pos.lat, value, sizeof(pos.lat));
     40 		switch (nodes[4].index) {
     41 		case 0:
     42 			// do nothing
     43 			break;
     44 		default:
     45 			printf(" %s,%s\n", pos.lat, pos.lon);
     46 			break;
     47 		}
     48 		break;
     49 	}
     50 }
     51 
     52 int
     53 main(void)
     54 {
     55 	switch (parsejson(processnode)) {
     56 	case JSON_ERROR_MEM:
     57 		fputs("error: cannot allocate enough memory\n", stderr);
     58 		return 2;
     59 	case JSON_ERROR_INVALID:
     60 		fputs("error: invalid JSON\n", stderr);
     61 		return 1;
     62 	}
     63 
     64 	return 0;
     65 }