]> mj.ucw.cz Git - leo.git/blob - shp.c
Introduced multiple data sources
[leo.git] / shp.c
1 /*
2  *      Hic Est Leo -- Reading ESRI Shape Files
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  *
6  *      FIXME: Currently, this parser handles only the subset
7  *      of shape file syntax which is used by gdal_contours.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include <ucw/lib.h>
13 #include <ucw/conf.h>
14 #include <ucw/fastbuf.h>
15 #include <ucw/gary.h>
16 #include <ucw/unaligned.h>
17
18 #include <fcntl.h>
19 #include <stdio.h>
20
21 #include "leo.h"
22 #include "osm.h"
23 #include "map.h"
24 #include "shp.h"
25
26 // FIXME: Hack
27 static osm_id_t shp_id_counter = 1000000000000;
28
29 static double shp_get_double(byte *p)
30 {
31   // FIXME: This is non-portable!
32   double x = 0;
33   memcpy(&x, p, 8);
34   return x;
35 }
36
37 static void shp_record_polyline(byte *buf, u32 len)
38 {
39   if (len < 44)
40     die("Polyline record too short: %u bytes", len);
41
42   u32 num_parts = get_u32_le(buf+36);
43   u32 num_points = get_u32_le(buf+40);
44   DBG("%u parts, %u points", num_parts, num_points);
45   if (num_parts != 1)
46     die("Polylines with multiple parts are not supported yet");
47
48   if (len < 44 + 4*num_parts + 16*num_points)
49     die("Polyline record too short for %u parts and %u points: %u bytes", num_parts, num_points, len);
50
51   struct osm_way *w = osm_way_new(shp_id_counter++);
52   osm_obj_add_tag(&w->o, "shape", "contour");
53
54   byte *p = buf + 44 + 4*num_parts;
55   for (uint i=0; i < num_points; i++)
56     {
57       double x = shp_get_double(p);
58       double y = shp_get_double(p+8);
59       DBG(">>> x=%.10g y=%.10g", x, y);
60       struct osm_node *n = osm_node_new(shp_id_counter++);
61       n->x = x;
62       n->y = y;
63       osm_way_add_node(w, n);
64       p += 16;
65     }
66 }
67
68 void shp_parse(const char *name)
69 {
70   msg(L_INFO, "Loading shape file %s", name);
71   struct fastbuf *fb = bopen_file(name, O_RDONLY, NULL);
72
73   byte *buf;
74   GARY_INIT(buf, 100);
75
76   uns len = bread(fb, buf, 100);
77   if (len != 100 || get_u32_be(buf) != 9994)
78     die("Invalid shape file header");
79
80   u32 version = get_u32_le(buf+28);
81   if (version != 1000)
82     die("Unknown shape file version %u", version);
83
84   u32 type = get_u32_le(buf+32);
85   if (type != 3)
86     die("Unsupported shape file type %u", type);
87
88   u32 last_recno = 0;
89   while (len = bread(fb, buf, 8))
90     {
91       if (len != 8)
92         die("Truncated record header");
93       u32 recno = get_u32_be(buf);
94       u32 reclen = get_u32_be(buf+4) * 2;
95       DBG("@%ju: recno %u len %u", (uintmax_t) btell(fb) - 8, recno, reclen);
96       if (recno != ++last_recno)
97         die("Unexpected record #%u, should be #%u", recno, last_recno);
98       if (reclen > GARY_SIZE(buf))
99         GARY_RESIZE(buf, reclen);
100       len = bread(fb, buf, reclen);
101       if (len != reclen)
102         die("Truncated record: %u < %u", len, reclen);
103       if (len < 4)
104         die("Record too short: %u bytes", reclen);
105
106       u32 rectype = get_u32_le(buf);
107       if (rectype == 3)
108         shp_record_polyline(buf, len);
109     }
110
111   GARY_FREE(buf);
112   bclose(fb);
113 }