Debian Codegraph @

shapefile/shp.js

raw
1 var file = require("./file");
2
3 exports.read = require("./read")(reader);
4 exports.reader = reader;
5
6 function reader(filename) {
7 var fileReader = file.reader(filename),
8 shapeType;
9
10 function readHeader(callback) {
11 fileReader.read(100, function(error, fileHeader) {
12 if (fileHeader === end) error = new Error("unexpected EOF");
13 if (error) return void callback(error);
14 callback(null, {
15 fileCode: fileHeader.readInt32BE(0), // TODO verify 9994
16 version: fileHeader.readInt32LE(28), // TODO verify 1000
17 shapeType: shapeType = fileHeader.readInt32LE(32),
18 box: [fileHeader.readDoubleLE(36), fileHeader.readDoubleLE(44), fileHeader.readDoubleLE(52), fileHeader.readDoubleLE(60)]
19 // TODO zMin: fileHeader.readDoubleLE(68)
20 // TODO zMax: fileHeader.readDoubleLE(76)
21 // TODO mMin: fileHeader.readDoubleLE(84)
22 // TODO mMax: fileHeader.readDoubleLE(92)
23 });
24 });
25 return this;
26 }
27
28 function readRecord(callback) {
29 if (!shapeType) return callback(new Error("must read header before reading records")), this;
30 if (!(shapeType in readShape)) return callback(new Error("unsupported shape type: " + shapeType)), this;
31 var readShapeType = readShape[shapeType];
32 fileReader.read(8, function readRecordHeader(error, recordHeader) {
33 if (recordHeader === end) return callback(null, end);
34 if (error) return void callback(error);
35 // TODO verify var recordNumber = recordHeader.readInt32BE(0);
36 fileReader.read(recordHeader.readInt32BE(4) * 2, function readRecord(error, record) {
37 if (record === end) error = new Error("unexpected EOF");
38 if (error) return void callback(error);
39 var shapeType = record.readInt32LE(0);
40 callback(null, shapeType ? readShapeType(record) : null);
41 });
42 });
43 return this;
44 }
45
46 function close(callback) {
47 fileReader.close(callback);
48 return this;
49 }
50
51 return {
52 readHeader: readHeader,
53 readRecord: readRecord,
54 close: close
55 };
56 }
57
58 var end = exports.end = file.end;
59
60 var readShape = {
61 0: readNull,
62 1: readPoint,
63 3: readPoly(3), // PolyLine
64 5: readPoly(5), // Polygon
65 8: readMultiPoint,
66 11: readPoint, // PointZ
67 13: readPoly(3), // PolyLineZ
68 15: readPoly(5), // PolygonZ
69 18: readMultiPoint // MultiPointZ
70 // 21: TODO readPointM
71 // 23: TODO readPolyLineM
72 // 25: TODO readPolygonM
73 // 28: TODO readMultiPointM
74 // 31: TODO readMultiPatch
75 };
76
77 function readNull() {
78 return null;
79 }
80
81 function readPoint(record) {
82 var x = record.readDoubleLE(4),
83 y = record.readDoubleLE(12);
84 return {
85 shapeType: 1,
86 x: x,
87 y: y
88 };
89 }
90
91 function readPoly(shapeType) {
92 return function(record) {
93 var box = [record.readDoubleLE(4), record.readDoubleLE(12), record.readDoubleLE(20), record.readDoubleLE(28)],
94 numParts = record.readInt32LE(36),
95 numPoints = record.readInt32LE(40),
96 parts = new Array(numParts),
97 points = new Array(numPoints),
98 i = 44,
99 j;
100 for (j = 0; j < numParts; ++j, i += 4) parts[j] = record.readInt32LE(i);
101 for (j = 0; j < numPoints; ++j, i += 16) points[j] = [record.readDoubleLE(i), record.readDoubleLE(i + 8)];
102 return {
103 shapeType: shapeType,
104 box: box,
105 parts: parts,
106 points: points
107 };
108 };
109 }
110
111 function readMultiPoint(record) {
112 var box = [record.readDoubleLE(4), record.readDoubleLE(12), record.readDoubleLE(20), record.readDoubleLE(28)],
113 numPoints = record.readInt32LE(36),
114 points = new Array(numPoints),
115 i = 40,
116 j;
117 for (j = 0; j < numPoints; ++j, i += 16) points[j] = [record.readDoubleLE(i), record.readDoubleLE(i + 8)];
118 return {
119 shapeType: 8,
120 box: box,
121 points: points
122 };
123 }