-
Notifications
You must be signed in to change notification settings - Fork 113
/
shapefile.js
375 lines (305 loc) · 11.3 KB
/
shapefile.js
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
(function(window,undefined){
if(window.document && window.Worker){
var worker = null;
var Shapefile = function(o, callback){
var
t = this,
o = typeof o == "string" ? {shp: o} : o
if (!worker) {
var path = (o.jsRoot || "") + "shapefile.js"
var w = worker = this.worker = new Worker(path)
} else {
var w = worker
}
w.onmessage = function(e){
t.data = e.date
if(callback) callback(e.data)
}
w.postMessage(["Load", o])
if(o.dbf) this.dbf = new DBF(o.dbf,function(data){
w.postMessage(["Add DBF Attributes", data])
})
}
window["Shapefile"] = Shapefile
return
}
var IN_WORKER = !window.document
if (IN_WORKER) {
importScripts('stream.js')
onmessage = function(e){
switch (e.data[0]) {
case "Load":
window.shapefile = new Shapefile(e.data[1])
break
case "Add DBF Attributes":
window.shapefile.addDBFDataToGeoJSON(e.data[1])
window.shapefile._postMessage()
break
default:
}
};
}
var SHAPE_TYPES = {
"0": "Null Shape",
"1": "Point", // standard shapes
"3": "PolyLine",
"5": "Polygon",
"8": "MultiPoint",
"11": "PointZ", // 3d shapes
"13": "PolyLineZ",
"15": "PolygonZ",
"18": "MultiPointZ",
"21": "PointM", // user-defined measurement shapes
"23": "PolyLineM",
"25": "PolygonM",
"28": "MultiPointM",
"31": "MultiPatch"
}
var Shapefile = function(o,callback){
var o = typeof o == "string" ? {shp: o} : o
this.callback = callback
if (!!(o.shp.lastModifiedDate || o.shp.lastModified))
this.handleFile(o);
else
this.handleUri(o);
}
Shapefile.prototype = {
constructor: Shapefile,
handleUri: function(o) {
var xhr = new XMLHttpRequest(),
that = this
xhr.open("GET", o.shp, false)
xhr.overrideMimeType("text/plain; charset=x-user-defined")
xhr.send()
if(200 != xhr.status)
throw "Unable to load " + o.shp + " status: " + xhr.status
this.url = o.shp
this.stream = new Gordon.Stream(xhr.responseText)
this.readFileHeader()
this.readRecords()
this.formatIntoGeoJson()
if(o.dbf) this.dbf = IN_WORKER ?
null :
new DBF(o.dbf,function(data){
that.addDBFDataToGeoJSON(data)
that._postMessage()
})
else this._postMessage()
},
handleFile: function(o) {
this.options = o
if (!!window.FileReader) {
var reader = new FileReader();
} else {
var reader = new FileReaderSync();
}
reader.onload = (function(that){
return function(e){
that.onFileLoad(e.target.result)
}
})(this);
if (!!window.FileReader) {
reader.readAsBinaryString(o.shp);
} else {
this.onFileLoad(reader.readAsBinaryString(o.shp));
}
},
onFileLoad: function(data) {
this.stream = new Gordon.Stream(data)
this.readFileHeader()
this.readRecords()
this.formatIntoGeoJson()
if(this.options.dbf) this.dbf = IN_WORKER ?
null :
new DBF(this.options.dbf,function(data){
that.addDBFDataToGeoJSON(data)
that._postMessage()
})
else this._postMessage()
},
_postMessage: function() {
var data = {
header: this.header,
records: this.records,
dbf: this.dbf,
geojson: this.geojson
}
if (IN_WORKER) postMessage(data)
else if (this.callback) this.callback(data)
},
readFileHeader: function(){
var s = this.stream,
header = this.header = {}
// The main file header is fixed at 100 bytes in length
if(s < 100) throw "Invalid Header Length"
// File code (always hex value 0x0000270a)
header.fileCode = s.readSI32(true)
if(header.fileCode != parseInt(0x0000270a))
throw "Invalid File Code"
// Unused; five uint32
s.offset += 4 * 5
// File length (in 16-bit words, including the header)
header.fileLength = s.readSI32(true) * 2
header.version = s.readSI32()
header.shapeType = SHAPE_TYPES[s.readSI32()]
// Minimum bounding rectangle (MBR) of all shapes contained within the shapefile; four doubles in the following order: min X, min Y, max X, max Y
this._readBounds(header)
// Z axis range
header.rangeZ = {
min: s.readDouble(),
max: s.readDouble()
}
// User defined measurement range
header.rangeM = {
min: s.readDouble(),
max: s.readDouble()
}
},
readRecords: function(){
var s = this.stream,
records = this.records = [],
record
do {
record = {}
// Record number (1-based)
record.id = s.readSI32(true)
if(record.id == 0) break //no more records
// Record length (in 16-bit words)
record.length = s.readSI32(true) * 2
record.shapeType = SHAPE_TYPES[s.readSI32()]
// Read specific shape
this["_read" + record.shapeType](record);
records.push(record);
} while(true);
},
_readBounds: function(object){
var s = this.stream
object.bounds = {
left: s.readDouble(),
bottom: s.readDouble(),
right: s.readDouble(),
top: s.readDouble()
}
return object
},
_readParts: function(record){
var s = this.stream,
nparts,
parts = []
nparts = record.numParts = s.readSI32()
// since number of points always proceeds number of parts, capture it now
record.numPoints = s.readSI32()
// parts array indicates at which index the next part starts at
while(nparts--) parts.push(s.readSI32())
record.parts = parts
return record
},
_readPoint: function(record){
var s = this.stream
record.x = s.readDouble()
record.y = s.readDouble()
return record
},
_readPoints: function(record){
var s = this.stream,
points = [],
npoints = record.numPoints || (record.numPoints = s.readSI32())
while(npoints--)
points.push({
x: s.readDouble(),
y: s.readDouble()
})
record.points = points
return record
},
_readMultiPoint: function(record){
var s = this.stream
this._readBounds(record)
this._readPoints(record)
return record
},
_readPolygon: function(record){
var s = this.stream
this._readBounds(record)
this._readParts(record)
this._readPoints(record)
return record
},
_readPolyLine: function(record){
return this._readPolygon(record);
},
formatIntoGeoJson: function(){
var bounds = this.header.bounds,
records = this.records,
features = [],
feature, geometry, points, fbounds, gcoords, parts, point,
geojson = {}
geojson.type = "FeatureCollection"
geojson.bbox = [
bounds.left,
bounds.bottom,
bounds.right,
bounds.top
]
geojson.features = features
for (var r = 0, record; record = records[r]; r++){
feature = {}, fbounds = record.bounds, points = record.points, parts = record.parts
feature.type = "Feature"
if (record.shapeType !== 'Point') {
feature.bbox = [
fbounds.left,
fbounds.bottom,
fbounds.right,
fbounds.top
]
}
geometry = feature.geometry = {}
switch (record.shapeType) {
case "Point":
geometry.type = "Point"
geometry.coordinates = [
record.x,
record.y ]
break
case "MultiPoint":
case "PolyLine":
geometry.type = (record.shapeType == "PolyLine" ? "LineString" : "MultiPoint")
gcoords = geometry.coordinates = []
for (var p = 0; p < points.length; p++){
var point = points[p]
gcoords.push([point.x,point.y])
}
break
case "Polygon":
geometry.type = "Polygon"
gcoords = geometry.coordinates = []
for (var pt = 0; pt < parts.length; pt++){
var partIndex = parts[pt],
part = [],
point
// partIndex 0 == main poly, partIndex > 0 == holes in poly
for (var p = partIndex; p < (parts[pt+1] || points.length); p++){
point = points[p]
part.push([point.x,point.y])
}
gcoords.push(part)
}
break
default:
}
features.push(feature)
}
this.geojson = geojson
if(this._addDataAfterLoad) this.addDBFDataToGeoJSON(this._addDataAfterLoad);
},
addDBFDataToGeoJSON: function(dbfData){
if(!this.geojson) return (this._addDataAfterLoad = dbfData)
this.dbf = dbfData
var features = this.geojson.features,
len = features.length,
records = dbfData.records
while(len--) features[len].properties = records[len]
}
}
window["Shapefile"] = Shapefile;
})(self);