Skip to content

Commit

Permalink
Fix strict typing errors for polygonize and dbscan
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner committed Jun 20, 2021
1 parent 4d84de9 commit fd85627
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
12 changes: 6 additions & 6 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 @@ -62,7 +62,7 @@ function clustersDbscan(

// create clustered ids
var dbscan = new clustering.DBSCAN();
var clusteredIds = dbscan.run(
var clusteredIds: number[][] = dbscan.run(
coordAll(points),
convertLength(maxDistance, options.units),
options.minPoints,
Expand All @@ -74,7 +74,7 @@ function clustersDbscan(
clusteredIds.forEach(function (clusterIds) {
clusterId++;
// assign cluster ids to input points
clusterIds.forEach(function (idx) {
clusterIds.forEach(function (idx: number) {
var clusterPoint = points.features[idx];
if (!clusterPoint.properties) clusterPoint.properties = {};
clusterPoint.properties.cluster = clusterId;
Expand All @@ -84,14 +84,14 @@ function clustersDbscan(

// handle noise points, if any
// edges points are tagged by DBSCAN as both 'noise' and 'cluster' as they can "reach" less than 'minPoints' number of points
dbscan.noise.forEach(function (noiseId) {
dbscan.noise.forEach(function (noiseId: number) {
var noisePoint = points.features[noiseId];
if (!noisePoint.properties) noisePoint.properties = {};
if (noisePoint.properties.cluster) noisePoint.properties.dbscan = "edge";
else noisePoint.properties.dbscan = "noise";
});

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

export default clustersDbscan;
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[] = [];
const 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 | undefined;
public symetric: Edge | undefined;
public from: Node;
public to: Node;
public next: Edge;
public ring: EdgeRing;
public next: Edge | undefined;
public ring: EdgeRing | undefined;

/**
* 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
36 changes: 19 additions & 17 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,
AllGeoJSON,
} 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: AllGeoJSON) {
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 as Edge).label as number)) {
this.removeEdge(edge.symetric as Edge);
this.removeEdge(edge);
}
});
Expand All @@ -175,9 +176,10 @@ export default class Graph {
);
} else {
node.getOuterEdges().forEach((edge, i) => {
node.getOuterEdge(
(i === 0 ? node.getOuterEdges().length : i) - 1
).symetric.next = edge;
(
node.getOuterEdge((i === 0 ? node.getOuterEdges().length : i) - 1)
.symetric as Edge
).next = edge;
});
}
}
Expand All @@ -193,9 +195,9 @@ export default class Graph {
* @param {Node} node - Node
* @param {number} label - Ring's label
*/
_computeNextCCWEdges(node: Node, label: number) {
_computeNextCCWEdges(node: Node, label: number | undefined) {
const edges = node.getOuterEdges();
let firstOutDE, prevInDE;
let firstOutDE: undefined | Edge, prevInDE: undefined | Edge;

for (let i = edges.length - 1; i >= 0; --i) {
let de = edges[i],
Expand All @@ -205,7 +207,7 @@ export default class Graph {

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

if (sym.label === label) inDE = sym;
if (((sym as Edge).label as number) === label) inDE = sym;

if (!outDE || !inDE)
// This edge is not in edgering
Expand All @@ -223,7 +225,7 @@ export default class Graph {
}
}

if (prevInDE) prevInDE.next = firstOutDE;
if (prevInDE) prevInDE.next = firstOutDE as Edge;
}

/**
Expand All @@ -234,17 +236,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 as number) >= 0) return;

edgeRingStarts.push(edge);

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

label++;
Expand Down Expand Up @@ -273,7 +275,7 @@ export default class Graph {
});
});

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

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

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

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

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

return edgeRing;
Expand Down

0 comments on commit fd85627

Please sign in to comment.