geohash-poly
geohash-poly copied to clipboard
Massive polygon throws an error from turf
Error: Each LinearRing of a Polygon must have 4 or more Positions.
When attempting to run the streaming example on the massive polygon.
same problem (
@phatal This repo hasn't been updated in a while, but you might try changing the dependency for turf to some of the more recent versions to see if they've corrected what might have caused the issues.
@derrickpelletier Actually I found solution :) stringify coordinates array before sending them from parsed *.geojson. That helped me.
Oh interesting, so the coordinates array is actually a string? When i try that with the examples/streaming.js
file, and change the massive
poly to massive = JSON.stringify(massive)
i continue to get the same error.
@derrickpelletier Code that almost worked for me. At least it's accept my coordinates array. Look strange why we need stringify and then parse it again, but it work )) `var hasher = require('../index'), async = require('async'), through2 = require('through2'), ngeohash = require('ngeohash'), queue = require('queue-async'), fs = require('fs');
var currDir = process.cwd(); var inputDir = 'input'; var outputDir = 'output'; var fileName = 'world_level2_test.geojson'; var inputFilePath = currDir+'/'+inputDir+'/'+fileName;
var worldData = JSON.parse(fs.readFileSync(inputFilePath));
var n = worldData.features.length; var i = 0; var q = queue();
function convertMe(polygonArray){ var toWrite = '';
var polygon = JSON.parse(polygonArray);
var isFirst = true;
var printFeature = function (name, poly) {
var out = {
"type": "Feature",
"properties": {
'name': name,
},
"geometry": {
"type": "Polygon",
"coordinates": poly
}
};
out = JSON.stringify(out);
if(!isFirst) {
out = ',' + out
}
isFirst = false;
toWrite = toWrite + out;
}
toWrite = toWrite + '{"type": "FeatureCollection","features": [';
async.mapSeries([polygon], function (poly, cb) {
var options = {
coords: poly,
precision: 6,
rowMode: true,
hashMode: 'extent',
//threshold: 0.2
};
printFeature('shape', poly);
var rowStream = hasher.stream(options),
a = 0;
rowStream
.on('end', cb)
.pipe(through2.obj(function (arr, enc, callback) {
for(var i = 0; i < arr.length; i++) {
var bb = ngeohash.decode_bbox(arr[i]);
printFeature(arr[i], [
[
[bb[1], bb[2]],
[bb[3], bb[2]],
[bb[3], bb[0]],
[bb[1], bb[0]],
[bb[1], bb[2]]
]]);
}
callback();
toWrite = toWrite + ']}';
}));
}, function () {
return toWrite;
});
};
worldData.features.forEach(function(feature){
q.defer(function(done){
var dataToConvert = JSON.stringify(feature.geometry.coordinates);
console.log('Start');
console.log(convertMe(dataToConvert));
fs.writeFileSync(currDir+'/'+outputDir+'/'+'cell_'+i+'.s2', dataToWrite);
i++;
done();
console.log('Finish');
});
});`
But I have problem with asynchronous call in foreach =/ convertMe(dataToConvert) always return undefined, i know that asynchronous call, but dunno how to fix it to get it work.
I used multiPolygon and my polygon worked as expected.