-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshp-writer.js
More file actions
477 lines (406 loc) · 16.3 KB
/
shp-writer.js
File metadata and controls
477 lines (406 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
(function() {
var root = this;
var $ = root.jQuery;
/**
* Handles writing data to the shapefile
*/
ShpJS.ShpWriter = function(options) {
this.initialize.call(this,arguments);
};
$.extend(ShpJS.ShpWriter.prototype, {
_features: null,
_shapeTypeCode: 0,
_shpBuffer: null,
_shpData: null,
_shxWriter: null,
_dbfWriter: null,
initialize: function() {
},
/**
* Writes the features to a set of files (shp, shx, dbf, prj)
* required to create a shapefile. Compresses all the written
* files to a zip file. Once write is called, save can be called
* to save the file to user's selected location.
*/
write: function(featureSet) {
this._features = featureSet;
var len = this._features.features.length;
// SHP buffer length depends on type of feature, but each
// shapefile has the 100 byte header
var shpBufLen = 100;
if (this._features.geometryType == 'esriGeometryPoint') {
this.shapeTypeCode = ShpJS.Constants.POINT_SHAPE_TYPE;
// 28 bytes per point
shpBufLen += len * 28;
}
else if (this._features.geometryType == 'esriGeometryPolyline' || this._features.geometryType == 'esriGeometryPolygon') {
// Polygons and Polylines are essentially written the same way
this.shapeTypeCode = this._features.geometryType == 'esriGeometryPolyline' ? ShpJS.Constants.POLYLINE_SHAPE_TYPE : ShpJS.Constants.POLYGON_SHAPE_TYPE;
var partName = this._features.geometryType == 'esriGeometryPolyline' ? 'paths' : 'rings';
// 8 bytes for record header + 44 bytes for record contents (per record)
shpBufLen += len * (8 + 44);
// Loop through all features
var g;
for (var i=0; i<len; i++) {
g = this._features.features[i].geometry;
// 4 bytes per path (for path indices)
shpBufLen += 4*g[partName].length;
// Loop through all the paths
for (var j=0; j<g[partName].length; j++) {
// 16 bytes per point in current path
shpBufLen += 16*g[partName][j].length;
}
}
}
else if (this._features.geometryType == 'esriGeometryMultipoint') {
this.shapeTypeCode = ShpJS.Constants.MULTIPOINT_SHAPE_TYPE;
// 8 bytes for record header + 40 bytes for record contents (per record)
shpBufLen += len * (8 + 40);
var g;
for (var i=0; i<len; i++) {
g = this._features.features[i].geometry;
// 16 bytes per point
shpBufLen += g.points.length * 16;
}
}
// Initialize the shapefile buffer
this._shpBuffer = new ArrayBuffer(shpBufLen);
this._shpData = new DataView(this._shpBuffer);
// Create the SHX writer
this._shxWriter = new ShpJS.ShxWriter({ shapeTypeCode: this.shapeTypeCode, numFeatures: len });
this._shxWriter.initHeader();
this._dbfWriter = new ShpJS.DbfWriter({ numFeatures: len, fields: this._features.fields });
this._dbfWriter.initHeader();
this._initHeader();
if (this._features.geometryType == 'esriGeometryPoint')
this._writePoints();
else if (this._features.geometryType == 'esriGeometryPolyline')
this._writePolys('paths');
else if (this._features.geometryType == 'esriGeometryPolygon')
this._writePolys('rings');
else if (this._features.geometryType == 'esriGeometryMultipoint')
this._writeMultipoints();
this._dbfWriter.finishDbf();
},
/**
* Launches the saveAs dialog box to save the compressed
* shapefile data that was written.
*
* TODO: Is there any way to increase efficiency?
* Right now the work flow is:
* ArrayBuffer -> compress -> binary string -> array buffer -> blob
*/
save: function(filename) {
// Create the zip and add the necessary files to it
var zip = new JSZip("DEFLATE");
zip.add(filename + '.shp', ShpJS.Base64Util.encodeBuffer(this._shpBuffer), {base64: true});
zip.add(filename + '.shx', ShpJS.Base64Util.encodeBuffer(this._shxWriter._shxBuffer), {base64: true});
zip.add(filename + '.dbf', ShpJS.Base64Util.encodeBuffer(this._dbfWriter._dbfBuffer), {base64: true});
zip.add(filename + '.prj', ShpJS.WktStrings[this._features.spatialReference.wkid]);
// Generate the zip content as a binary string
var content = zip.generate(true);
// Create a Uint8Array from the binary string so we can get a blob out of it
var ab = new ArrayBuffer(content.length);
var ia = new Uint8Array(ab);
for (var i=0; i<content.length; i++)
ia[i] = content.charCodeAt(i);
// Create a blob from the Uint8Array
var BB = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
var bb = new BB();
bb.append(ab);
saveAs(bb.getBlob('application/zip'), filename + '.zip');
},
/**
* Initializes the shapefile header
*/
_initHeader: function() {
// Header start code
this._shpData.setInt32(0, ShpJS.Constants.HEADER_START_CODE);
// 20 unused/empty bytes
for (var i=0; i<5; i++)
this._shpData.setInt32(4+i*4, 0x00);
// File length, in 16-bit words
this._shpData.setInt32(24, this._shpBuffer.byteLength/2);
// The rest of the header info gets written in little endian mode
this._shpData.setInt32(28, ShpJS.Constants.VERSION_CODE, true);
this._shpData.setInt32(32, this.shapeTypeCode, true);
// Enclosing bounds, set to 0.0 temporarily
this._shpData.setFloat64(36, 0.0, true);
this._shpData.setFloat64(44, 0.0, true);
this._shpData.setFloat64(52, 0.0, true);
this._shpData.setFloat64(60, 0.0, true);
// 32 unused/empty bytes
for (var i=0; i<8; i++)
this._shpData.setInt32(68+i*4, 0x00);
},
/**
* Writes an array of points to the shapefile
*
* Each point record is composed of a header plus contents.
*
* Header:
*
* Bytes Type Endian Name
* 0-3 int32 big Record Number (record numbers start at 1)
* 4-8 int32 big Content Length
*
* Contents:
*
* Bytes Type Endian Name
* 0-3 int32 little Shape Type (1 for points)
* 4-11 double little X Coordinate
* 12-19 double little Y Coordinate
*
*/
_writePoints: function() {
var fs = this._features.features,
f = fs[0],
g = f.geometry,
bb = new esri.geometry.Extent(g.x, g.y, g.x, g.y, this._features.spatialReference),
offset = 100;
// Loop through all points and write them to the dataview
for (var i=0; i<fs.length; i++) {
f = fs[i];
g = f.geometry;
// Header - record number
this._shpData.setInt32(offset, i+1);
// Header - content length in 16-bit words
// (20 bytes: 4 for shape type + 2x8 for x/y = TEN 16 bit words)
this._shpData.setInt32(offset+4, 10);
// ShapeType, X/Y, all in little endian
this._shpData.setInt32(offset+8, this.shapeTypeCode, true);
this._shpData.setFloat64(offset+12, g.x, true);
this._shpData.setFloat64(offset+20, g.y, true);
// Update the extent to include the current point
bb.xmin = Math.min(bb.xmin, g.x);
bb.ymin = Math.min(bb.ymin, g.y);
bb.xmax = Math.max(bb.xmax, g.x);
bb.ymax = Math.max(bb.ymax, g.y);
// Add the record to SHX/DBF
this._shxWriter.addRecord(offset, 20);
this._dbfWriter.addRecord(f.attributes);
// Increment the offset by length of current record
offset += 28;
}
this.setExtent(bb);
this._shxWriter.setExtent(bb);
},
/**
* Writes an array of multipoints to the shapefile
*
* Each multipoint record is composed of a header plus contents.
*
* Header:
*
* Bytes Type Endian Name
* 0-3 int32 big Record Number (record numbers start at 1)
* 4-8 int32 big Content Length
*
* Contents:
*
* Bytes Type Endian Name
* 0-3 int32 little Shape Type (8 for multipoints)
* 4-35 double little Bounding box for the multipoint
* 36-39 int32 little Number of points contained by the geometry
* 40-n double little Array of length NumPoints. Stores, all the points
* contained by the multipoint (each point is composed
* of an X and a Y value).
*
*
*/
_writeMultipoints: function() {
var fs = this._features.features,
f = fs[0],
g = f.geometry,
e = g.getExtent(),
pt = g.points,
npt = pt.length,
rl = 0,
bb = g.getExtent(),
offset = 100;
// Loop through all the multipoints and write them to the dataview
for (var i=0; i<fs.length; i++) {
f = fs[i];
g = f.geometry;
e = g.getExtent();
pt = g.points;
npt = pt.length;
// Calculate the record length: 4 bytes for shape type,
// 32 bytes for bounding box, 4 bytes for # pts, 16 bytes per point
rl = 4 + 32 + 4 + 16*npt;
// Add the record to SHX/DBF
this._shxWriter.addRecord(offset, rl);
this._dbfWriter.addRecord(f.attributes);
// Header - record number
this._shpData.setInt32(offset, i+1);
// Divide by 2 to get len in 16-bit words
this._shpData.setInt32(offset+4, rl/2);
offset += 8; // 4 for record number, 4 for record length
// Content = Shape Type
this._shpData.setInt32(offset, this.shapeTypeCode, true);
offset += 4;
// Bounding box
this._shpData.setFloat64(offset, e.xmin, true);
this._shpData.setFloat64(offset+8, e.ymin, true);
this._shpData.setFloat64(offset+16, e.xmax, true);
this._shpData.setFloat64(offset+24, e.ymax, true);
offset += 32;
// Number of number of points
this._shpData.setInt32(offset, npt, true);
offset += 4;
for (var j=0; j<npt; j++) {
this._shpData.setFloat64(offset, pt[j][0], true);
this._shpData.setFloat64(offset+8, pt[j][1], true);
offset += 16;
}
bb = bb.union(e);
}
this.setExtent(bb);
this._shxWriter.setExtent(bb);
},
/**
* Writes an array of polylines or polygons to the shapefile
*
* Each polyline record is composed of a header plus contents.
*
* Header:
*
* Bytes Type Endian Name
* 0-3 int32 big Record Number (record numbers start at 1)
* 4-8 int32 big Content Length
*
* Contents:
*
* Bytes Type Endian Name
* 0-3 int32 little Shape Type (3 for polylines)
* 4-35 double little Bounding box for the polyline
* 36-39 int32 little Total number of parts in the polyline
* 40-43 int32 little Total number of points for all parts
* 44-n int32 little Array of length NumParts. Stores, for each polyline,
* the index of its first point in the points array (0-based)
* n-m double little Array of length NumPoints, stores all the points for all
* the parts. Points for part 1 are the first X points,
* followed by points for part 2, part 3, etc.
*/
_writePolys: function(partsName) {
var fs = this._features.features,
f = fs[0], // Feature
g = f.geometry, // Geometry
e = g.getExtent(), // Current geometry extent
p = g[partsName], // Paths
np = p.length, // Number of Parts
npt = 0, // Number of points
rl = 0, // Record length
bb = g.getExtent(), // Total shapefile extent
offset = 100; // Current offset
// Loop through all polylines and write them to the dataview
for (var i=0; i<fs.length; i++) {
f = fs[i];
g = f.geometry;
e = g.getExtent();
p = g[partsName];
np = p.length;
// Header - content length in 16-bit words
// 4 bytes for shape type, 32 bytes for bbox, 4 bytes for NumParts,
// 4 bytes for NumPoints, 4*NumParts bytes for Parts,
// 16*NumPoints bytes for Points
npt = 0;
for (var j=0; j<np; j++)
npt += p[j].length;
// Calculate the record length
rl = 4 + 32 + 4 + 4 + 4*np + 16*npt;
// Add the record to SHX/DBF
this._shxWriter.addRecord(offset, rl);
this._dbfWriter.addRecord(f.attributes);
// Header - record number
this._shpData.setInt32(offset, i+1);
// Divide by 2 to get len in 16-bit words
this._shpData.setInt32(offset+4, rl/2);
offset += 8; // 4 for record number, 4 for record length
// Content = Shape Type
this._shpData.setInt32(offset, this.shapeTypeCode, true);
offset += 4;
// Bounding box
this._shpData.setFloat64(offset, e.xmin, true);
this._shpData.setFloat64(offset+8, e.ymin, true);
this._shpData.setFloat64(offset+16, e.xmax, true);
this._shpData.setFloat64(offset+24, e.ymax, true);
offset += 32;
// Number of parts/number of points
this._shpData.setInt32(offset, np, true);
this._shpData.setInt32(offset+4, npt, true);
offset += 8;
// Write indices for all the parts
var partIdx = 0;
this._shpData.setInt32(offset, 0, true); // first index is 0
for (var j=1; j<np; j++) {
partIdx += j*p[j-1].length;
this._shpData.setInt32(offset + j*4, partIdx, true);
}
offset += np*4;
// Write out the points
for (var j=0; j<np; j++) {
for (var k=0; k<p[j].length; k++) {
this._shpData.setFloat64(offset, p[j][k][0], true);
this._shpData.setFloat64(offset+8, p[j][k][1], true);
offset += 16;
}
}
bb = bb.union(e);
}
this.setExtent(bb);
this._shxWriter.setExtent(bb);
},
/**
* Updates extent stored in the header. Extent is stored in little endian mode.
*/
setExtent: function(extent) {
this._shpData.setFloat64(ShpJS.Constants.HEADER_BBOX_INDEX, extent.xmin, true);
this._shpData.setFloat64(ShpJS.Constants.HEADER_BBOX_INDEX+8, extent.ymin, true);
this._shpData.setFloat64(ShpJS.Constants.HEADER_BBOX_INDEX+16, extent.xmax, true);
this._shpData.setFloat64(ShpJS.Constants.HEADER_BBOX_INDEX+24, extent.ymax, true);
},
/**
* Pretty-prints the contents of the shapefile buffer. Useful
* for debugging
*/
printShpContents: function() {
this.printHeader();
if (this._features.geometryType == 'esriGeometryPoint')
this.printShpPoints();
},
/**
* Pretty-prints the header of the shapefile buffer.
*/
printHeader: function() {
console.debug('SHAPEFILE HEADER:');
console.debug('');
console.debug('Header Start Code: ', this._shpData.getInt32(0), ' | expecting ', ShpJS.Constants.HEADER_START_CODE);
console.debug('20 empty bytes: ',
(this._shpData.getInt32(4) === 0 && this._shpData.getInt32(8) === 0 && this._shpData.getInt32(12) === 0 &&
this._shpData.getInt32(16) === 0&& this._shpData.getInt32(20) === 0) ? 'TRUE' : 'FALSE');
console.debug('File length (16-bit words): ', _shpData.getInt32(24));
console.debug('Version code: ', this._shpData.getInt32(28, true), ' | expecting ', ShpJS.Constants.VERSION_CODE);
console.debug('Shape type: ',this. _shpData.getInt32(32, true), ' | expecting ', this.shapeTypeCode);
console.debug('Enclosing bounds: [', this._shpData.getFloat64(36, true), ', ', this._shpData.getFloat64(44, true), ', ',
this._shpData.getFloat64(52, true), ', ', this._shpData.getFloat64(60, true), ']');
console.debug('32 empty bytes: ',
(this._shpData.getInt32(68) === 0 && this._shpData.getInt32(72) === 0 && this._shpData.getInt32(76) === 0 && this._shpData.getInt32(80) === 0 &&
this._shpData.getInt32(84) === 0 && this._shpData.getInt32(88) === 0 && this._shpData.getInt32(92) === 0 && this._shpData.getInt32(96) === 0) ? 'TRUE' : 'FALSE');
console.debug('');
},
/**
* Pretty-prints the points from the shapefile
*/
printShpPoints: function() {
console.debug('SHAPEFILE POINTS');
// Loop through the buffer in 28-byte blocks starting at byte 100
for (var i=0; i<this._features.features.length; i++) {
var offset = 100 + i*28;
console.debug('Record #: ', this._shpData.getInt32(offset), ' Length: ', this._shpData.getInt32(offset+4),
' Shape Type: ', this._shpData.getInt32(offset+8, true), ' Point: [',
this._shpData.getFloat64(offset+12, true), ', ', this._shpData.getFloat64(offset+20, true), ']');
}
}
});
}).call(this);