-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted turf-tesselate to Typescript (#2650)
* Renamed turf-tesselate/index.js to .ts in preparation for converting to Typescript. * Converted turf-tesselate to Typescript.
- Loading branch information
1 parent
3a6a035
commit 19d2aa9
Showing
8 changed files
with
137 additions
and
109 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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
declare module "earcut" { | ||
declare function earcut( | ||
vertices: number[], | ||
holes: number[], | ||
dimensions: number | ||
); | ||
|
||
export default earcut; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
Feature, | ||
FeatureCollection, | ||
MultiPolygon, | ||
Polygon, | ||
Position, | ||
} from "geojson"; | ||
import earcut from "earcut"; | ||
import { polygon } from "@turf/helpers"; | ||
|
||
/** | ||
* Tesselates a polygon or multipolygon into a collection of triangle polygons | ||
* using [earcut](https://github.com/mapbox/earcut). | ||
* | ||
* @name tesselate | ||
* @param {Feature<Polygon|MultiPolygon>} poly the polygon to tesselate | ||
* @returns {FeatureCollection<Polygon>} collection of polygon tesselations | ||
* @example | ||
* const poly = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]); | ||
* const triangles = turf.tesselate(poly); | ||
* | ||
* //addToMap | ||
* const addToMap = [poly, triangles] | ||
*/ | ||
function tesselate( | ||
poly: Feature<Polygon | MultiPolygon> | ||
): FeatureCollection<Polygon> { | ||
if ( | ||
!poly.geometry || | ||
(poly.geometry.type !== "Polygon" && poly.geometry.type !== "MultiPolygon") | ||
) { | ||
throw new Error("input must be a Polygon or MultiPolygon"); | ||
} | ||
|
||
const fc: FeatureCollection<Polygon> = { | ||
type: "FeatureCollection", | ||
features: [], | ||
}; | ||
|
||
if (poly.geometry.type === "Polygon") { | ||
fc.features = processPolygon(poly.geometry.coordinates); | ||
} else { | ||
poly.geometry.coordinates.forEach(function (coordinates) { | ||
fc.features = fc.features.concat(processPolygon(coordinates)); | ||
}); | ||
} | ||
|
||
return fc; | ||
} | ||
|
||
function processPolygon(coordinates: Position[][]) { | ||
const data = flattenCoords(coordinates); | ||
const dim = 2; | ||
const result = earcut(data.vertices, data.holes, dim); | ||
|
||
const features: Feature<Polygon>[] = []; | ||
const vertices: Position[] = []; | ||
|
||
result.forEach(function (vert: any, i: number) { | ||
const index = result[i]; | ||
vertices.push([data.vertices[index * dim], data.vertices[index * dim + 1]]); | ||
}); | ||
|
||
for (var i = 0; i < vertices.length; i += 3) { | ||
const coords = vertices.slice(i, i + 3); | ||
coords.push(vertices[i]); | ||
features.push(polygon([coords])); | ||
} | ||
|
||
return features; | ||
} | ||
|
||
function flattenCoords(data: Position[][]) { | ||
const dim: number = data[0][0].length, | ||
result: { vertices: number[]; holes: number[]; dimensions: number } = { | ||
vertices: [], | ||
holes: [], | ||
dimensions: dim, | ||
}; | ||
let holeIndex = 0; | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
for (let j = 0; j < data[i].length; j++) { | ||
for (let d = 0; d < dim; d++) result.vertices.push(data[i][j][d]); | ||
} | ||
if (i > 0) { | ||
holeIndex += data[i - 1].length; | ||
result.holes.push(holeIndex); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export { tesselate }; | ||
export default tesselate; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.