forked from can-luckyshot/IMSpoorViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimxSaxParser.js
235 lines (212 loc) · 5.28 KB
/
imxSaxParser.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
function handleFiles(files) {
loadXml(files[0]);
}
function loadXml(file) {
initGlobals();
var fileSize = file.size;
var chunkSize = 1024 * 1024 * 10; // bytes
var offset = 0;
var self = this; // we need a reference to the current object
var chunkReaderBlock = null;
var block = 0;
var parser = sax.parser(false, {
trim: true
});
configParser(parser);
//console.log(parser);
var readEventHandler = function (evt) {
if (evt.target.error == null) {
offset += evt.target.result.length;
block++;
console.log(block + ' ' + renderableObjects + ' ' + tagBuffer.length);
callback(evt.target.result, parser); // callback for handling read chunk
} else {
console.log("Read error: " + evt.target.error);
return;
}
if (offset >= fileSize) {
report();
return;
}
// of to the next chunk
chunkReaderBlock(offset, chunkSize, file);
}
chunkReaderBlock = function (_offset, length, _file) {
var r = new FileReader();
var blob = _file.slice(_offset, length + _offset);
r.onload = readEventHandler;
r.readAsText(blob);
}
// now let's start the read with the first block
chunkReaderBlock(offset, chunkSize, file);
}
function report() {
console.log("Done reading file");
var duration = new Date().getTime() - startTime;
console.log("Duration: " + duration);
console.log('renderableObjects: ' + renderableObjects);
console.log('renderableObjects: 324135');
console.log('typeLayers: ' + typeLayers.types.length);
for (var i = 0; i < typeLayers.types.length; ++i) {
var type = typeLayers.types[i];
console.log(type + ': ' + typeLayers[type].length + ' items');
}
//console.log(JSON.stringify(geojson));
}
function initGlobals() {
geojson = {
type: "FeatureCollection",
features: []
};
renderableObjects = 0;
tagBuffer = [];
closingTagName = null;
typeLayers = {};
typeLayers.types = [];
startTime = new Date().getTime();
}
var renderableObjects;
var startTime;
var tagBuffer;
var closingTagName;
var typeLayers;
function callback(chunkText, parser) {
parser.write(chunkText);
}
function configParser(parser) {
parser['onopentag'] = function (tag) {
//console.log(tag.name);
tagBuffer.push(tag);
if (tag.name === 'GEOGRAPHICLOCATION') {
var index = tagBuffer.length - 3;
tagBuffer = tagBuffer.slice(index);
closingTagName = tagBuffer[0].name;
renderableObjects++;
}
if (tagBuffer.length > 1000) {
tagBuffer = [];
}
};
parser['ontext'] = function (text) {
tagBuffer.push(text);
};
parser['onclosetag'] = function (tag) {
tagBuffer.push(tag);
if (tag === closingTagName) {
//buildGMLObject(tagBuffer);
buildGeoJsonFeature(tagBuffer);
tagBuffer = [];
closingTagName = null;
}
};
}
function buildGMLObject(tagBuffer) {
var type = tagBuffer[0].name;
console.log('build: ' + type);
var geomType;
var coords = '';
for (var i = 0; i < tagBuffer.length; i++) {
var tagName = tagBuffer[i].name;
if (tagName && tagName.startsWith('GML')) {
if (tagName === 'GML:COORDINATES') {
coords = tagBuffer[i + 1];
} else if (geomType == undefined) {
geomType = tagName;
}
} else if (tagBuffer[i] === 'GEOGRAPHICLOCATION') {
break;
}
}
if (geomType && coords.length > 0) {
console.log('adding: ' + geomType + ' -> coords:' + coords.length);
addToLayer(type, {
geomType,
coords
});
}
//var coordValues = poslist.replace(',', ' ').split(' ');
//console.log('build: ['+renderableObjects +'] ' + tagBuffer[0].name);
}
function buildGeoJsonFeature(tagBuffer) {
var type = tagBuffer[0].name;
var puic = tagBuffer[0].puic;
var f = {
type: 'Feature',
id: puic,
properties: {
type: type
},
geometry: makeGJGeom(tagBuffer)
};
geojson.features.push(f);
}
function makeGJGeom(tagBuffer) {
var geomType;
var coords=[];
for (var i = 0; i < tagBuffer.length; i++) {
var tagName = tagBuffer[i].name;
if (tagName && tagName.startsWith('GML')) {
if (tagName === 'GML:COORDINATES') {
coords.push(tagBuffer[i + 1]);
} else if (geomType == undefined) {
geomType = tagName;
}
} else if (tagBuffer[i] === 'GEOGRAPHICLOCATION') {
break;
}
}
if (geomType && coords.length > 0) {
console.log('adding: ' + geomType + ' -> coords:' + coords.length);
return {
type: toGJType(geomType),
coordinates: toGJCoords(coords)
};
}
return undefined;
}
function toGJCoords(coordStrings){
if(coordStrings.length == 1){
return toCoordArray(coordStrings[0]);
}
else{
var coordArray = [];
$.each(coordStrings, function (index, coordString) {
coordArray.push(toCoordArray(coordString));
});
return coordArray;
}
}
function toCoordArray(coordString){
var coordArray = [];
var vertexStrings = coordString.split(' ');
$.each(vertexStrings, function (index, vertexString) {
var vertexValues = vertexString.split(',');
var point = [];
$.each(vertexValues, function (index, vertexValue) {
point.push(parseFloat(vertexValue));
});
coordArray.push(point);
});
return coordArray;
}
function toGJType(type){
if('GML:POINT' == type){
return 'Point';
}
else if('GML:LINESTRING' == type){
return 'LineString';
}
else if('GML:POLYGON' == type){
return 'Polygon';
}
return type;
}
function addToLayer(type, item) {
var layer = typeLayers[type];
if (layer === undefined) {
layer = [];
typeLayers[type] = layer;
typeLayers.types.push(type);
}
layer.push(item);
}