geohash-poly
Advanced tools
Comparing version 0.1.0 to 0.1.2
54
index.js
@@ -26,7 +26,20 @@ var Readable = require('stream').Readable, | ||
*/ | ||
var Hasher = function (geoJSONCoords, precision) { | ||
this.isMulti = Array.isArray(geoJSONCoords[0][0][0]); | ||
this.geojson = !this.isMulti ? [geoJSONCoords] : geoJSONCoords; | ||
this.precision = precision; | ||
Readable.call(this); | ||
var Hasher = function (options) { | ||
var defaults = { | ||
precision: 6, | ||
rowMode: false, | ||
geojson: [] | ||
} | ||
options = options || {}; | ||
for (var attrname in defaults) { | ||
this[attrname] = options.hasOwnProperty(attrname) && (options[attrname] !== null && typeof options[attrname] !== 'undefined') ? options[attrname] : defaults[attrname]; | ||
} | ||
this.isMulti = Array.isArray(this.geojson[0][0][0]); | ||
this.geojson = !this.isMulti ? [this.geojson] : this.geojson; | ||
this.buffer = []; | ||
Readable.call(this, { | ||
objectMode: this.rowMode | ||
}); | ||
} | ||
@@ -43,11 +56,21 @@ util.inherits(Hasher, Readable); | ||
Hasher.prototype._read = function (size) { | ||
if(!this.geojson.length) return this.push(null); | ||
// still some hashes in the buffer, no rush. | ||
if(this.buffer.length) { | ||
return this.push(this.buffer.shift()); | ||
} | ||
var hashes = this.getNextRow(); | ||
var hashes = []; | ||
if(!hashes.length) return this.push(''); | ||
// Iterate over the poly rows until we get some hashes, or the polys are done. | ||
while(this.geojson.length && !hashes.length) { | ||
hashes = this.getNextRow(); | ||
} | ||
if(!this.geojson.length && !hashes.length) return this.push(null); | ||
if(this.rowMode) return this.push(hashes); | ||
while(hashes.length) { | ||
this.push(hashes.shift()); | ||
} | ||
this.buffer = this.buffer.concat(hashes); | ||
}; | ||
@@ -94,4 +117,8 @@ | ||
*/ | ||
module.exports.stream = function (geoJSONCoords, precision) { | ||
return new Hasher(geoJSONCoords, precision); | ||
module.exports.stream = function (geoJSONCoords, precision, rowMode) { | ||
return new Hasher({ | ||
geojson: geoJSONCoords, | ||
precision: precision, | ||
rowMode: rowMode ? true : false | ||
}); | ||
}; | ||
@@ -104,3 +131,6 @@ | ||
module.exports.sync = function (geoJSONCoords, precision) { | ||
var hasher = new Hasher(geoJSONCoords, precision); | ||
var hasher = new Hasher({ | ||
geojson: geoJSONCoords, | ||
precision: precision | ||
}); | ||
var results = []; | ||
@@ -107,0 +137,0 @@ |
{ | ||
"name": "geohash-poly", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"description": "Transform a GeoJSON Polygon or MultiPolygon to a list of geohashes that form it.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,2 +14,5 @@ # Geohash-poly | ||
Hashes can be streamed. Each _read will generate a row of hashes into buffer, as some form of throttling. This allows massive polygons with high precision hashes to avoid memory constraint issues. If your polys have the potential to hit memory issues, use this method. | ||
If you specify rowMode as true, such as `.stream(poly, precision, rowMode)`, each chunk in the stream will be an array using streams2 objectMode. | ||
```javascript | ||
@@ -16,0 +19,0 @@ |
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
139262
630
2
56
0