Skip to content

Instantly share code, notes, and snippets.

@yurukov
Last active August 29, 2015 14:17
Show Gist options
  • Save yurukov/0777aa1540c3bce918b3 to your computer and use it in GitHub Desktop.
Save yurukov/0777aa1540c3bce918b3 to your computer and use it in GitHub Desktop.
Detecting loops in polygons and multipolygons in geojson.
var fs = require("fs");
var geojson = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
for (var n=0;n<geojson.features.length;n++) {
var feature = geojson.features[n];
var geom = feature.geometry.coordinates;
if (feature.geometry.type=='Polygon')
geom=[geom];
var list=[];
for (var i=0;i<geom.length;i++)
for (var j=0;j<geom[i].length;j++)
for (var k=0;k<geom[i][j].length-1;k++)
for (var m=k+1;m<geom[i][j].length;m++)
if ((k!=0 || m!=geom[i][j].length-1) && geom[i][j][k][0]==geom[i][j][m][0] && geom[i][j][k][1]==geom[i][j][m][1])
list.push(geom[i][j][k]);
if (list.length>0)
console.log(feature.properties.id+" "+list.join(" "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment