Skip to content

Commit

Permalink
Enable typescript strict mode (#2126)
Browse files Browse the repository at this point in the history
* bezier-spline

* clusters-dbscan and clusters-kmeans, but they both need new DefinitelyTyped updates

* kind of speculative fixes for polygonize

* Enforce strict mode

* turf-clusters-kmeans by way of skmeans types

* types

Co-authored-by: Matt Fedderly <mfedderly@palantir.com>
  • Loading branch information
mfedderly and mfedderly committed Jun 23, 2021
1 parent dde1570 commit 3249a8b
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/turf-bezier-spline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function bezier<P = Properties>(
const resolution = options.resolution || 10000;
const sharpness = options.sharpness || 0.85;

const coords = [];
const coords: [number, number][] = [];
const points = getGeom(line).coordinates.map((pt) => {
return { x: pt[0], y: pt[1] };
});
Expand Down
1 change: 1 addition & 0 deletions packages/turf-boolean-equal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"test:tape": "ts-node -r esm test.js"
},
"devDependencies": {
"@types/geojson-equality": "^0.2.0",
"@types/tape": "*",
"benchmark": "*",
"boolean-shapely": "*",
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-boolean-overlap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function booleanOverlap(

// features must be not equal
const equality = new GeojsonEquality({ precision: 6 });
if (equality.compare(feature1, feature2)) return false;
if (equality.compare(feature1 as any, feature2 as any)) return false;

let overlap = 0;

Expand Down
1 change: 1 addition & 0 deletions packages/turf-boolean-overlap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"test:tape": "ts-node -r esm test.js"
},
"devDependencies": {
"@types/geojson-equality": "^0.2.0",
"@types/tape": "*",
"benchmark": "*",
"boolean-shapely": "*",
Expand Down
6 changes: 3 additions & 3 deletions packages/turf-clusters-dbscan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
import clustering from "density-clustering";

export type Dbscan = "core" | "edge" | "noise";
export interface DbscanProps extends Properties {
export type DbscanProps = Properties & {
dbscan?: Dbscan;
cluster?: number;
}
};

/**
* Takes a set of {@link Point|points} and partition them into clusters according to {@link DBSCAN's|https://en.wikipedia.org/wiki/DBSCAN} data clustering algorithm.
Expand Down Expand Up @@ -91,7 +91,7 @@ function clustersDbscan(
else noisePoint.properties.dbscan = "noise";
});

return points;
return points as FeatureCollection<Point, DbscanProps>;
}

export default clustersDbscan;
1 change: 1 addition & 0 deletions packages/turf-clusters-dbscan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"devDependencies": {
"@turf/centroid": "^6.4.0",
"@turf/clusters": "^6.4.0",
"@types/density-clustering": "^1.3.0",
"@types/tape": "*",
"benchmark": "*",
"chromatism": "*",
Expand Down
6 changes: 1 addition & 5 deletions packages/turf-clusters-kmeans/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ function clustersKmeans(
var initialCentroids = data.slice(0, options.numberOfClusters);

// create skmeans clusters
var skmeansResult = skmeans(
data,
options.numberOfClusters,
initialCentroids as any // typings are slightly wrong here
);
var skmeansResult = skmeans(data, options.numberOfClusters, initialCentroids);

// store centroids {clusterId: [number, number]}
var centroids: Record<string, number[]> = {};
Expand Down
1 change: 1 addition & 0 deletions packages/turf-clusters-kmeans/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@turf/centroid": "^6.4.0",
"@turf/clusters": "^6.4.0",
"@turf/random": "^6.4.0",
"@types/skmeans": "^0.11.2",
"@types/tape": "*",
"benchmark": "*",
"chromatism": "*",
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-polygonize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default function polygonize<T extends LineString | MultiLineString>(
graph.deleteCutEdges();

// 3. Get all holes and shells
const holes = [],
shells = [];
const holes: EdgeRing[] = [],
shells: EdgeRing[] = [];

graph
.getEdgeRings()
Expand Down
14 changes: 7 additions & 7 deletions packages/turf-polygonize/lib/Edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import EdgeRing from "./EdgeRing";
* This class is inspired by GEOS's geos::operation::polygonize::PolygonizeDirectedEdge
*/
export default class Edge {
public label: number;
public symetric: Edge;
public label?: number;
public symetric?: Edge;
public from: Node;
public to: Node;
public next: Edge;
public ring: EdgeRing;
public next?: Edge;
public ring?: EdgeRing;

/**
* Creates or get the symetric Edge.
Expand All @@ -32,7 +32,7 @@ export default class Edge {
* @param {Node} from - start node of the Edge
* @param {Node} to - end node of the edge
*/
constructor(from, to) {
constructor(from: Node, to: Node) {
this.from = from; //< start
this.to = to; //< End

Expand Down Expand Up @@ -61,7 +61,7 @@ export default class Edge {
* @param {Edge} edge - Another Edge
* @returns {boolean} - True if Edges are equal, False otherwise
*/
isEqual(edge) {
isEqual(edge: Edge) {
return this.from.id === edge.from.id && this.to.id === edge.to.id;
}

Expand All @@ -88,7 +88,7 @@ export default class Edge {
* 0 if the Edges are colinear,
* 1 otherwise
*/
compareTo(edge) {
compareTo(edge: Edge) {
return orientationIndex(
edge.from.coordinates,
edge.to.coordinates,
Expand Down
58 changes: 38 additions & 20 deletions packages/turf-polygonize/lib/EdgeRing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import {
envelopeContains,
coordinatesEqual,
} from "./util";
import { multiPoint, polygon, point, Polygon, Feature } from "@turf/helpers";
import {
multiPoint,
polygon,
point,
Polygon,
Feature,
Point,
Position,
} from "@turf/helpers";
import envelope from "@turf/envelope";
import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
import Edge from "./Edge";
Expand All @@ -18,13 +26,13 @@ import Edge from "./Edge";
*/
export default class EdgeRing {
private edges: Edge[];
private polygon: Feature<
private polygon?: Feature<
Polygon,
{
[name: string]: any;
}
>;
private envelope: Feature<
private envelope?: Feature<
Polygon,
{
[name: string]: any;
Expand All @@ -43,9 +51,7 @@ export default class EdgeRing {
* @memberof EdgeRing
* @param {Edge} edge - Edge to be inserted
*/
push(edge) {
// Emulate Array getter ([]) behaviour
this[this.edges.length] = edge;
push(edge: Edge) {
this.edges.push(edge);
this.polygon = this.envelope = undefined;
}
Expand All @@ -57,7 +63,7 @@ export default class EdgeRing {
* @param {number} i - Index
* @returns {Edge} - Edge in the i position
*/
get(i) {
get(i: number) {
return this.edges[i];
}

Expand All @@ -77,7 +83,7 @@ export default class EdgeRing {
* @memberof EdgeRing
* @param {Function} f - The same function to be passed to Array.prototype.forEach
*/
forEach(f) {
forEach(f: (edge: Edge, index: number, array: Edge[]) => void) {
this.edges.forEach(f);
}

Expand All @@ -88,7 +94,7 @@ export default class EdgeRing {
* @param {Function} f - The same function to be passed to Array.prototype.map
* @returns {Array} - The mapped values in the function
*/
map(f) {
map<T>(f: (edge: Edge, index: number, array: Edge[]) => T): T[] {
return this.edges.map(f);
}

Expand All @@ -99,7 +105,7 @@ export default class EdgeRing {
* @param {Function} f - The same function to be passed to Array.prototype.some
* @returns {boolean} - True if an Edge check the condition
*/
some(f) {
some(f: (edge: Edge, index: number, array: Edge[]) => boolean) {
return this.edges.some(f);
}

Expand Down Expand Up @@ -182,7 +188,10 @@ export default class EdgeRing {
*/
getEnvelope() {
if (this.envelope) return this.envelope;
return (this.envelope = envelope(this.toPolygon()));
return (this.envelope = envelope(this.toPolygon()) as Feature<
Polygon,
{ [name: string]: any }
>);
}

/**
Expand All @@ -193,10 +202,13 @@ export default class EdgeRing {
*
* @returns {EdgeRing} - EdgeRing which contains the testEdgeRing
*/
static findEdgeRingContaining(testEdgeRing, shellList) {
static findEdgeRingContaining(
testEdgeRing: EdgeRing,
shellList: EdgeRing[]
): EdgeRing | undefined {
const testEnvelope = testEdgeRing.getEnvelope();

let minEnvelope, minShell;
let minEnvelope: Feature<Polygon>, minShell: EdgeRing | undefined;
shellList.forEach((shell) => {
const tryEnvelope = shell.getEnvelope();

Expand All @@ -206,12 +218,18 @@ export default class EdgeRing {
if (envelopeIsEqual(tryEnvelope, testEnvelope)) return;

if (envelopeContains(tryEnvelope, testEnvelope)) {
const testPoint = testEdgeRing
.map((edge) => edge.from.coordinates)
.find(
(pt) =>
!shell.some((edge) => coordinatesEqual(pt, edge.from.coordinates))
);
const testEdgeRingCoordinates = testEdgeRing.map(
(edge) => edge.from.coordinates
);

let testPoint: Position | undefined;
for (const pt of testEdgeRingCoordinates) {
if (
!shell.some((edge) => coordinatesEqual(pt, edge.from.coordinates))
) {
testPoint = pt;
}
}

if (testPoint && shell.inside(point(testPoint))) {
if (!minShell || envelopeContains(minEnvelope, tryEnvelope))
Expand All @@ -229,7 +247,7 @@ export default class EdgeRing {
* @param {Feature<Point>} pt - Point to check if it is inside the edgeRing
* @returns {boolean} - True if it is inside, False otherwise
*/
inside(pt) {
inside(pt: Feature<Point>) {
return booleanPointInPolygon(pt, this.toPolygon());
}
}
25 changes: 13 additions & 12 deletions packages/turf-polygonize/lib/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
LineString,
MultiLineString,
Feature,
GeoJSONObject,
} from "@turf/helpers";

/**
Expand All @@ -16,7 +17,7 @@ import {
* @param {GeoJSON} geoJson - input geoJson.
* @throws {Error} if geoJson is invalid.
*/
function validateGeoJson(geoJson) {
function validateGeoJson(geoJson: GeoJSONObject) {
if (!geoJson) throw new Error("No geojson passed");

if (
Expand Down Expand Up @@ -153,8 +154,8 @@ export default class Graph {

// Cut-edges (bridges) are edges where both edges have the same label
this.edges.forEach((edge) => {
if (edge.label === edge.symetric.label) {
this.removeEdge(edge.symetric);
if (edge.label === edge.symetric!.label) {
this.removeEdge(edge.symetric!);
this.removeEdge(edge);
}
});
Expand All @@ -177,7 +178,7 @@ export default class Graph {
node.getOuterEdges().forEach((edge, i) => {
node.getOuterEdge(
(i === 0 ? node.getOuterEdges().length : i) - 1
).symetric.next = edge;
).symetric!.next = edge;
});
}
}
Expand Down Expand Up @@ -205,7 +206,7 @@ export default class Graph {

if (de.label === label) outDE = de;

if (sym.label === label) inDE = sym;
if (sym!.label === label) inDE = sym;

if (!outDE || !inDE)
// This edge is not in edgering
Expand Down Expand Up @@ -234,17 +235,17 @@ export default class Graph {
* @returns {Edge[]} edges that start rings
*/
_findLabeledEdgeRings() {
const edgeRingStarts = [];
const edgeRingStarts: Edge[] = [];
let label = 0;
this.edges.forEach((edge) => {
if (edge.label >= 0) return;
if (edge.label! >= 0) return;

edgeRingStarts.push(edge);

let e = edge;
do {
e.label = label;
e = e.next;
e = e.next!;
} while (!edge.isEqual(e));

label++;
Expand All @@ -269,11 +270,11 @@ export default class Graph {
this._findLabeledEdgeRings().forEach((edge) => {
// convertMaximalToMinimalEdgeRings
this._findIntersectionNodes(edge).forEach((node) => {
this._computeNextCCWEdges(node, edge.label);
this._computeNextCCWEdges(node, edge.label!);
});
});

const edgeRingList = [];
const edgeRingList: EdgeRing[] = [];

// find all edgerings
this.edges.forEach((edge) => {
Expand Down Expand Up @@ -302,7 +303,7 @@ export default class Graph {

if (degree > 1) intersectionNodes.push(edge.from);

edge = edge.next;
edge = edge.next!;
} while (!startEdge.isEqual(edge));

return intersectionNodes;
Expand All @@ -321,7 +322,7 @@ export default class Graph {
do {
edgeRing.push(edge);
edge.ring = edgeRing;
edge = edge.next;
edge = edge.next!;
} while (!startEdge.isEqual(edge));

return edgeRing;
Expand Down

0 comments on commit 3249a8b

Please sign in to comment.