alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
560 stars 58 forks source link

Allow Polygons with the Same Coordinate at the Start and End of their Array to be Valid #78

Closed frastlin closed 3 years ago

frastlin commented 3 years ago

Hello, I'm wondering if shapes that end with the starting point can please be made valid shapes? The Geojson spec states: " o The first and last positions are equivalent, and they MUST contain identical values; their representation SHOULD also be identical."

let p = new Polygon([[1,1],[1,2],[2,2],[2,1],[1,1]])
p.isValid()

should return "true".

frastlin commented 3 years ago

Hello, Here are two functions that seem to work. If you remove the duplicate points, that would be fine IMO. The first function removes duplicates from a flattened array, and the second function goes through nested arrays until it hits the flattened array:

const filterFlattenedDuplicates = (input) =>
    Array.from(new Set(input.map(JSON.stringify)), JSON.parse) // this removes duplicate points from a list of points

let filterDuplicates = poly =>
    poly[0][0].length === undefined ? filterFlattenedDuplicates(poly) : poly.map(p=>filterDuplicates(p))
alexbol99 commented 3 years ago

Hello @frastlin ,

Creation of polygons complient with Geojson spec since v1.2.20, see example https://next.observablehq.com/@alexbol99/support-geojson-polygon-issue-79

Issue closed,

Best regards, Alex Bol