Debian Codegraph @

shapefile/index.js

raw
1 var shp = require("./shp"),
2 dbf = require("./dbf");
3
4 exports.version = require("./package.json").version;
5 exports.read = read;
6 exports.reader = reader;
7
8 var readInternal = require("./read")(reader);
9
10 function read(filename, options, callback) {
11 if (arguments.length < 3) callback = options, options = null;
12 readInternal(filename, options, function(error, header, records) {
13 if (error) return callback(error);
14 callback(null, {
15 type: "FeatureCollection",
16 bbox: header.bbox,
17 features: records
18 });
19 });
20 }
21
22 function reader(filename, options) {
23 var convertProperties,
24 convertGeometry,
25 encoding = null,
26 ignoreProperties = false,
27 dbfReader,
28 shpReader;
29
30 if (typeof options === "string") options = {encoding: options};
31
32 if (options)
33 "encoding" in options && (encoding = options["encoding"]),
34 "ignore-properties" in options && (ignoreProperties = !!options["ignore-properties"]);
35
36 if (/\.shp$/.test(filename)) filename = filename.substring(0, filename.length - 4);
37
38 if (!ignoreProperties) dbfReader = dbf.reader(filename + ".dbf", encoding);
39 shpReader = shp.reader(filename + ".shp");
40
41 function readHeader(callback) {
42 dbfReader.readHeader(function(error, header) {
43 if (header === end) error = new Error("unexpected EOF");
44 if (error) return callback(error);
45 convertProperties = new Function("d", "return {"
46 + header.fields.map(function(field, i) { return JSON.stringify(field.name) + ":d[" + i + "]"; })
47 + "};");
48 readShpHeader(callback);
49 });
50 return this;
51 }
52
53 function readShpHeader(callback) {
54 shpReader.readHeader(function(error, header) {
55 if (header === end) error = new Error("unexpected EOF");
56 if (error) return callback(error);
57 convertGeometry = convertGeometryTypes[header.shapeType];
58 callback(null, {bbox: header.box});
59 });
60 return this;
61 }
62
63 function readRecord(callback) {
64 dbfReader.readRecord(function(error, dbfRecord) {
65 if (dbfRecord === end) return callback(null, end);
66 if (error) return callback(error);
67 shpReader.readRecord(function(error, shpRecord) {
68 if (shpRecord === end) error = new Error("unexpected EOF");
69 if (error) return callback(error);
70 callback(null, {
71 type: "Feature",
72 properties: convertProperties(dbfRecord),
73 geometry: shpRecord == null ? null : convertGeometry(shpRecord)
74 });
75 });
76 });
77 return this;
78 }
79
80 function readShpRecord(callback) {
81 shpReader.readRecord(function(error, shpRecord) {
82 if (shpRecord === end) return callback(null, end);
83 if (error) return callback(error);
84 callback(null, {
85 type: "Feature",
86 properties: {},
87 geometry: shpRecord == null ? null : convertGeometry(shpRecord)
88 });
89 });
90 return this;
91 }
92
93 function close(callback) {
94 dbfReader.close(function(error) {
95 if (error) return callback(error);
96 closeShp(callback);
97 });
98 return this;
99 }
100
101 function closeShp(callback) {
102 shpReader.close(callback);
103 return this;
104 }
105
106 return dbfReader ? {
107 readHeader: readHeader,
108 readRecord: readRecord,
109 close: close
110 } : {
111 readHeader: readShpHeader,
112 readRecord: readShpRecord,
113 close: closeShp
114 };
115 }
116
117 var end = exports.end = shp.end;
118
119 var convertGeometryTypes = {
120 1: convertPoint,
121 3: convertPolyLine,
122 5: convertPolygon,
123 8: convertMultiPoint,
124 11: convertPoint, // PointZ
125 13: convertPolyLine, // PolyLineZ
126 15: convertPolygon, // PolygonZ
127 18: convertMultiPoint // MultiPointZ
128 };
129
130 function readEmptyNextProperties(callback) {
131 callback(null, {});
132 }
133
134 function convertPoint(record) {
135 return {
136 type: "Point",
137 coordinates: [record.x, record.y]
138 };
139 }
140
141 function convertPolyLine(record) {
142 return record.parts.length === 1 ? {
143 type: "LineString",
144 coordinates: record.points
145 } : {
146 type: "MultiLineString",
147 coordinates: record.parts.map(function(i, j) {
148 return record.points.slice(i, record.parts[j + 1]);
149 })
150 };
151 }
152
153 function convertPolygon(record) {
154 var parts = record.parts.map(function(i, j) { return record.points.slice(i, record.parts[j + 1]); }),
155 polygons = [],
156 holes = [];
157
158 parts.forEach(function(part) {
159 if (ringClockwise(part)) polygons.push([part]);
160 else holes.push(part);
161 });
162
163 holes.forEach(function(hole) {
164 var point = hole[0];
165 polygons.some(function(polygon) {
166 if (ringContains(polygon[0], point)) {
167 polygon.push(hole);
168 return true;
169 }
170 }) || polygons.push([hole]);
171 });
172
173 return polygons.length > 1
174 ? {type: "MultiPolygon", coordinates: polygons}
175 : {type: "Polygon", coordinates: polygons[0]};
176 }
177
178 function convertMultiPoint(record) {
179 return {
180 type: "MultiPoint",
181 coordinates: record.points
182 };
183 }
184
185 function ringClockwise(ring) {
186 if ((n = ring.length) < 4) return false;
187 var i = 0,
188 n,
189 area = ring[n - 1][1] * ring[0][0] - ring[n - 1][0] * ring[0][1];
190 while (++i < n) area += ring[i - 1][1] * ring[i][0] - ring[i - 1][0] * ring[i][1];
191 return area >= 0;
192 }
193
194 function ringContains(ring, point) {
195 var x = point[0],
196 y = point[1],
197 contains = false;
198 for (var i = 0, n = ring.length, j = n - 1; i < n; j = i++) {
199 var pi = ring[i], xi = pi[0], yi = pi[1],
200 pj = ring[j], xj = pj[0], yj = pj[1];
201 if (((yi > y) ^ (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi)) contains = !contains;
202 }
203 return contains;
204 }