-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Incorporate geobuf for passing geojson
According to mapbox/mapbox-gl-js#1504 the transferring of GeoJSON files between main and worker threads can cause a bottleneck. We should be able to reduce this bottleneck by converting and passing the GeoJSON file created in the Worker as a geobuf and decoding it on the main thread. This commit is broken because of a possible bug with Webworkify not pulling in correct dependencies. See browserify/webworkify#14
- Loading branch information
Nick Peihl
committed
Feb 19, 2016
1 parent
036f79f
commit 1482953
Showing
4 changed files
with
115 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/node_modules/ | ||
/.tern-port |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,10 @@ | |
"author": "Nick Peihl <[email protected]>", | ||
"license": "ISC", | ||
"dependencies": { | ||
"geobuf": "^2.0.0", | ||
"geojsonhint": "^1.2.0", | ||
"mapbox-gl": "^0.14.1", | ||
"pbf": "^1.3.5", | ||
"turf-concave": "^1.1.3", | ||
"webworkify": "^1.1.0" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,48 @@ | ||
var concave = require('turf-concave'); | ||
var geojsonhint = require('geojsonhint'); | ||
var geobuf = require('geobuf'); | ||
var Pbf = require('pbf'); | ||
|
||
debugger; | ||
|
||
module.exports = function(self) { | ||
self.addEventListener('message', function(ev) { | ||
var dataUrl = ev.data[0]; | ||
self.makeRequest(dataUrl); | ||
}); | ||
self.addEventListener('message', function(ev) { | ||
var dataUrl = ev.data[0]; | ||
self.makeRequest(dataUrl); | ||
}); | ||
|
||
self.makeRequest = function(url) { | ||
var req = new XMLHttpRequest(); | ||
req.addEventListener('load', self.validateData); | ||
req.addEventListener('error', self.transferFailed); | ||
req.open('GET', url); | ||
req.send(); | ||
}; | ||
self.makeRequest = function(url) { | ||
var req = new XMLHttpRequest(); | ||
req.addEventListener('load', self.validateData); | ||
req.addEventListener('error', self.transferFailed); | ||
req.open('GET', url); | ||
req.send(); | ||
}; | ||
|
||
self.validateData = function() { | ||
var errors = geojsonhint.hint(this.responseText); | ||
if (errors.len > 0) { | ||
self.postMessage(['Errors', errors.join(', ') ]); | ||
} else { | ||
self.turfIt(this.responseText); | ||
} | ||
}; | ||
self.validateData = function() { | ||
var errors = geojsonhint.hint(this.responseText); | ||
if (errors.len > 0) { | ||
self.postMessage(['Errors', errors.join(', ') ]); | ||
} else { | ||
self.turfIt(this.responseText); | ||
} | ||
}; | ||
|
||
self.transferFailed = function() { | ||
self.postMessage(['Error', this.responseText]); | ||
}; | ||
self.transferFailed = function() { | ||
self.postMessage(['Error', this.responseText]); | ||
}; | ||
|
||
self.turfIt = function(data) { | ||
var results = concave(JSON.parse(data), 1, 'miles'); | ||
self.postMessage(['Done', results]); | ||
}; | ||
self.turfIt = function(data) { | ||
var results = concave(JSON.parse(data), 1, 'miles'); | ||
var pbf = new Pbf(geobuf.encode(results, new Pbf())); | ||
/* The pbf does not contain the readFields method when called via | ||
the web worker. However, I can set a breakpoint on the line below, | ||
then rewrite the above line using a different variable (e.g. pbf2). | ||
The pbf2 variable does contain the readFields method. I believe this | ||
is due to the scope of the pbf buffer existing in the browser, but not | ||
in the web worker. Possibly releated to this issue | ||
(https://github.com/substack/webworkify/issues/14). | ||
*/ | ||
self.postMessage(['Done', pbf]); | ||
}; | ||
}; |