Serialize metadata and geometry to a file

This commit is contained in:
Eric Fischer
2014-09-16 16:39:56 -07:00
parent a5b8a02ec0
commit 88dc948b6b
+54 -53
View File
@@ -21,6 +21,7 @@
#define GEOM_MULTIPOLYGON 5 /* array of arrays of arrays of arrays of positions */ #define GEOM_MULTIPOLYGON 5 /* array of arrays of arrays of arrays of positions */
#define GEOM_TYPES 6 #define GEOM_TYPES 6
#define VT_END 0
#define VT_POINT 1 #define VT_POINT 1
#define VT_LINE 2 #define VT_LINE 2
#define VT_POLYGON 3 #define VT_POLYGON 3
@@ -69,7 +70,25 @@ void latlon2tile(double lat, double lon, int zoom, unsigned int *x, unsigned int
*y = n * (1 - (log(tan(lat_rad) + 1/cos(lat_rad)) / M_PI)) / 2; *y = n * (1 - (log(tan(lat_rad) + 1/cos(lat_rad)) / M_PI)) / 2;
} }
void parse_geometry(int t, json_object *j, unsigned *bbox, int *n, unsigned **out, int op) { void serialize_int(FILE *out, int n, long long *fpos) {
fwrite(&n, sizeof(int), 1, out);
*fpos += sizeof(int);
}
void serialize_uint(FILE *out, unsigned n, long long *fpos) {
fwrite(&n, sizeof(unsigned), 1, out);
*fpos += sizeof(unsigned);
}
void serialize_string(FILE *out, char *s, long long *fpos) {
int len = strlen(s);
serialize_int(out, len, fpos);
fwrite(s, sizeof(char), len, out);
*fpos += len;
}
void parse_geometry(int t, json_object *j, unsigned *bbox, long long *fpos, FILE *out, int op) {
if (j == NULL || j->type != JSON_ARRAY) { if (j == NULL || j->type != JSON_ARRAY) {
fprintf(stderr, "expected array for type %d\n", t); fprintf(stderr, "expected array for type %d\n", t);
return; return;
@@ -77,7 +96,6 @@ void parse_geometry(int t, json_object *j, unsigned *bbox, int *n, unsigned **ou
int within = geometry_within[t]; int within = geometry_within[t];
if (within >= 0) { if (within >= 0) {
printf("[");
int i; int i;
for (i = 0; i < j->length; i++) { for (i = 0; i < j->length; i++) {
if (within == GEOM_POINT) { if (within == GEOM_POINT) {
@@ -88,63 +106,49 @@ void parse_geometry(int t, json_object *j, unsigned *bbox, int *n, unsigned **ou
} }
} }
parse_geometry(within, j->array[i], bbox, n, out, op); parse_geometry(within, j->array[i], bbox, fpos, out, op);
} }
printf("]");
} else { } else {
if (j->length == 2 && j->array[0]->type == JSON_NUMBER && j->array[1]->type == JSON_NUMBER) { if (j->length == 2 && j->array[0]->type == JSON_NUMBER && j->array[1]->type == JSON_NUMBER) {
if (out != NULL || bbox != NULL) { unsigned x, y;
unsigned x, y; double lon = j->array[0]->number;
double lon = j->array[0]->number; double lat = j->array[1]->number;
double lat = j->array[1]->number; latlon2tile(lat, lon, 32, &x, &y);
latlon2tile(lat, lon, 32, &x, &y);
if (bbox != NULL) { if (bbox != NULL) {
if (x < bbox[0]) { if (x < bbox[0]) {
bbox[0] = x; bbox[0] = x;
}
if (y < bbox[1]) {
bbox[1] = y;
}
if (x > bbox[2]) {
bbox[2] = x;
}
if (y > bbox[3]) {
bbox[3] = y;
}
} }
if (out != NULL) { if (y < bbox[1]) {
(*out)[0] = op; bbox[1] = y;
(*out)[1] = x;
(*out)[2] = y;
(*out) += 3;
} }
if (n != NULL) { if (x > bbox[2]) {
*n += 3; bbox[2] = x;
}
if (y > bbox[3]) {
bbox[3] = y;
} }
} }
printf(" %d %f,%f ", op, j->array[0]->number, j->array[1]->number); serialize_int(out, op, fpos);
serialize_uint(out, x, fpos);
serialize_uint(out, y, fpos);
} else { } else {
fprintf(stderr, "malformed point"); fprintf(stderr, "malformed point");
} }
} }
if (t == GEOM_POLYGON) { if (t == GEOM_POLYGON) {
if (out != NULL) { serialize_int(out, VT_CLOSEPATH, fpos);
(*out)[0] = VT_CLOSEPATH;
(*out) += 1;
}
if (n != NULL) {
*n += 1;
}
printf(" closepath ");
} }
} }
void read_json(FILE *f) { void read_json(FILE *f) {
json_pull *jp = json_begin_file(f); json_pull *jp = json_begin_file(f);
FILE *out = fopen("meta.out", "wb");
long long fpos = 0;
while (1) { while (1) {
json_object *j = json_read(jp); json_object *j = json_read(jp);
if (j == NULL) { if (j == NULL) {
@@ -227,23 +231,20 @@ void read_json(FILE *f) {
} }
} }
unsigned bbox[] = { UINT_MAX, UINT_MAX, 0, 0 }; serialize_int(out, m, &fpos);
int n = 0; for (i = 0; i < m; i++) {
serialize_int(out, metatype[i], &fpos);
printf("%d: ", mb_geometry[t]); serialize_string(out, metakey[i], &fpos);
parse_geometry(t, coordinates, bbox, &n, NULL, VT_MOVETO); serialize_string(out, metaval[i], &fpos);
printf("\n");
unsigned out[n];
unsigned *end = out;
parse_geometry(t, coordinates, NULL, NULL, &end, VT_MOVETO);
printf("\n-> ");
for (i = 0; i < n; i++) {
printf("%x ", out[i]);
} }
printf("\n");
unsigned bbox[] = { UINT_MAX, UINT_MAX, 0, 0 };
serialize_int(out, t, &fpos);
parse_geometry(t, coordinates, bbox, &fpos, out, VT_MOVETO);
serialize_int(out, VT_END, &fpos);
printf("%d elements, bbox %x %x %x %x\n", n, bbox[0], bbox[1], bbox[2], bbox[3]); printf("bbox %x %x %x %x\n", bbox[0], bbox[1], bbox[2], bbox[3]);
} }
next_feature: next_feature: