Skip to content

Commit

Permalink
Work towards enabling typescript's strict mode (#2053)
Browse files Browse the repository at this point in the history
* Work towards enabling typescript's strict mode

* Remove build time check for * version in devDependencies
  • Loading branch information
mfedderly committed Jun 17, 2021
1 parent c054821 commit 3b20c56
Show file tree
Hide file tree
Showing 32 changed files with 422 additions and 207 deletions.
4 changes: 2 additions & 2 deletions packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ export default function bboxClip<
}
}

function clipPolygon(rings: any[], bbox: BBox) {
function clipPolygon(rings: number[][][], bbox: BBox) {
const outRings = [];
for (const ring of rings) {
const clipped: any = polygonclip(ring, bbox);
const clipped = polygonclip(ring, bbox);
if (clipped.length > 0) {
if (
clipped[0][0] !== clipped[clipped.length - 1][0] ||
Expand Down
37 changes: 24 additions & 13 deletions packages/turf-bbox-clip/lib/lineclip.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// Cohen-Sutherland line clippign algorithm, adapted to efficiently
// Cohen-Sutherland line clipping algorithm, adapted to efficiently
// handle polylines rather than just segments

export function lineclip(points, bbox, result) {
import { BBox } from "@turf/helpers";

export function lineclip(
points: number[][],
bbox: BBox,
result?: number[][][]
): number[][][] {
var len = points.length,
codeA = bitCode(points[0], bbox),
part = [],
part = [] as number[][],
i,
a,
b,
codeB,
lastCode;
let a: number[];
let b: number[];

if (!result) result = [];

Expand Down Expand Up @@ -41,11 +47,11 @@ export function lineclip(points, bbox, result) {
break;
} else if (codeA) {
// a outside, intersect with clip edge
a = intersect(a, b, codeA, bbox);
a = intersect(a, b, codeA, bbox)!;
codeA = bitCode(a, bbox);
} else {
// b outside
b = intersect(a, b, codeB, bbox);
b = intersect(a, b, codeB, bbox)!;
codeB = bitCode(b, bbox);
}
}
Expand All @@ -60,8 +66,8 @@ export function lineclip(points, bbox, result) {

// Sutherland-Hodgeman polygon clipping algorithm

export function polygonclip(points, bbox) {
var result, edge, prev, prevInside, i, p, inside;
export function polygonclip(points: number[][], bbox: BBox): number[][] {
var result: number[][], edge, prev, prevInside, i, p, inside;

// clip against each side of the clip rectangle
for (edge = 1; edge <= 8; edge *= 2) {
Expand All @@ -74,7 +80,7 @@ export function polygonclip(points, bbox) {
inside = !(bitCode(p, bbox) & edge);

// if segment goes through the clip window, add an intersection
if (inside !== prevInside) result.push(intersect(prev, p, edge, bbox));
if (inside !== prevInside) result.push(intersect(prev, p, edge, bbox)!);

if (inside) result.push(p); // add a point if it's inside

Expand All @@ -87,12 +93,17 @@ export function polygonclip(points, bbox) {
if (!points.length) break;
}

return result;
return result!;
}

// intersect a segment against one of the 4 lines that make up the bbox

function intersect(a, b, edge, bbox) {
function intersect(
a: number[],
b: number[],
edge: number,
bbox: BBox
): number[] | null {
return edge & 8
? [a[0] + ((b[0] - a[0]) * (bbox[3] - a[1])) / (b[1] - a[1]), bbox[3]] // top
: edge & 4
Expand All @@ -111,7 +122,7 @@ function intersect(a, b, edge, bbox) {
// mid 0001 0000 0010
// bottom 0101 0100 0110

function bitCode(p, bbox) {
function bitCode(p: number[], bbox: BBox) {
var code = 0;

if (p[0] < bbox[0]) code |= 1;
Expand Down
37 changes: 25 additions & 12 deletions packages/turf-boolean-crosses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import lineIntersect from "@turf/line-intersect";
import { polygonToLine } from "@turf/polygon-to-line";
import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
import { getGeom } from "@turf/invariant";
import { point, Feature, Geometry, Polygon } from "@turf/helpers";
import {
point,
Feature,
Geometry,
Polygon,
LineString,
MultiPoint,
} from "@turf/helpers";

/**
* Boolean-Crosses returns True if the intersection results in a geometry whose dimension is one less than
Expand Down Expand Up @@ -66,7 +73,10 @@ function booleanCrosses(
}
}

function doMultiPointAndLineStringCross(multiPoint, lineString) {
function doMultiPointAndLineStringCross(
multiPoint: MultiPoint,
lineString: LineString
) {
var foundIntPoint = false;
var foundExtPoint = false;
var pointLength = multiPoint.coordinates.length;
Expand Down Expand Up @@ -95,7 +105,7 @@ function doMultiPointAndLineStringCross(multiPoint, lineString) {
return foundIntPoint && foundExtPoint;
}

function doLineStringsCross(lineString1, lineString2) {
function doLineStringsCross(lineString1: LineString, lineString2: LineString) {
var doLinesIntersect = lineIntersect(lineString1, lineString2);
if (doLinesIntersect.features.length > 0) {
for (var i = 0; i < lineString1.coordinates.length - 1; i++) {
Expand All @@ -120,7 +130,7 @@ function doLineStringsCross(lineString1, lineString2) {
return false;
}

function doLineStringAndPolygonCross(lineString, polygon: Polygon) {
function doLineStringAndPolygonCross(lineString: LineString, polygon: Polygon) {
const line: any = polygonToLine(polygon);
const doLinesIntersect = lineIntersect(lineString, line);
if (doLinesIntersect.features.length > 0) {
Expand All @@ -129,21 +139,19 @@ function doLineStringAndPolygonCross(lineString, polygon: Polygon) {
return false;
}

function doesMultiPointCrossPoly(multiPoint, polygon) {
function doesMultiPointCrossPoly(multiPoint: MultiPoint, polygon: Polygon) {
var foundIntPoint = false;
var foundExtPoint = false;
var pointLength = multiPoint.coordinates[0].length;
var i = 0;
while (i < pointLength && foundIntPoint && foundExtPoint) {
if (booleanPointInPolygon(point(multiPoint.coordinates[0][i]), polygon)) {
var pointLength = multiPoint.coordinates.length;
for (let i = 0; i < pointLength && (!foundIntPoint || !foundExtPoint); i++) {
if (booleanPointInPolygon(point(multiPoint.coordinates[i]), polygon)) {
foundIntPoint = true;
} else {
foundExtPoint = true;
}
i++;
}

return foundExtPoint && foundExtPoint;
return foundExtPoint && foundIntPoint;
}

/**
Expand All @@ -158,7 +166,12 @@ function doesMultiPointCrossPoly(multiPoint, polygon) {
* @param {boolean} incEnd whether the point is allowed to fall on the line ends
* @returns {boolean} true/false
*/
function isPointOnLineSegment(lineSegmentStart, lineSegmentEnd, pt, incEnd) {
function isPointOnLineSegment(
lineSegmentStart: number[],
lineSegmentEnd: number[],
pt: number[],
incEnd: boolean
) {
var dxc = pt[0] - lineSegmentStart[0];
var dyc = pt[1] - lineSegmentStart[1];
var dxl = lineSegmentEnd[0] - lineSegmentStart[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPoint",
"coordinates": [
[3, 3],
[4, 4]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[0, 2],
[2, 2],
[2, 0],
[0, 0],
[0, 2]
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPoint",
"coordinates": [
[3, 3],
[1, 1]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[0, 2],
[2, 2],
[2, 0],
[0, 0],
[0, 2]
]
]
}
}
]
}
4 changes: 2 additions & 2 deletions packages/turf-boolean-overlap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function booleanOverlap(
case "MultiLineString":
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineOverlap(segment1, segment2).features.length) overlap++;
if (lineOverlap(segment1!, segment2!).features.length) overlap++;
});
});
break;
Expand All @@ -78,7 +78,7 @@ export default function booleanOverlap(
case "MultiPolygon":
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineIntersect(segment1, segment2).features.length) overlap++;
if (lineIntersect(segment1!, segment2!).features.length) overlap++;
});
});
break;
Expand Down
15 changes: 11 additions & 4 deletions packages/turf-boolean-parallel/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import cleanCoords from "@turf/clean-coords";
import lineSegment from "@turf/line-segment";
import rhumbBearing from "@turf/rhumb-bearing";
import { bearingToAzimuth, Feature, LineString } from "@turf/helpers";
import {
bearingToAzimuth,
Feature,
Geometry,
LineString,
Position,
} from "@turf/helpers";

/**
* Boolean-Parallel returns True if each segment of `line1` is parallel to the correspondent segment of `line2`
Expand Down Expand Up @@ -49,7 +55,7 @@ function booleanParallel(
* @param {Geometry|Feature<LineString>} segment2 Geometry or Feature
* @returns {boolean} if slopes are equal
*/
function isParallel(segment1, segment2) {
function isParallel(segment1: Position[], segment2: Position[]) {
var slope1 = bearingToAzimuth(rhumbBearing(segment1[0], segment1[1]));
var slope2 = bearingToAzimuth(rhumbBearing(segment2[0], segment2[1]));
return slope1 === slope2;
Expand All @@ -63,8 +69,9 @@ function isParallel(segment1, segment2) {
* @param {string} name of the variable
* @returns {string} Feature's type
*/
function getType(geojson, name) {
if (geojson.geometry && geojson.geometry.type) return geojson.geometry.type;
function getType(geojson: Geometry | Feature<any>, name: string) {
if ((geojson as Feature).geometry && (geojson as Feature).geometry.type)
return (geojson as Feature).geometry.type;
if (geojson.type) return geojson.type; // if GeoJSON geometry
throw new Error("Invalid GeoJSON object for " + name);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/turf-boolean-touches/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import booleanPointOnLine from "@turf/boolean-point-on-line";
import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
import { getGeom } from "@turf/invariant";
import { Feature, Geometry } from "@turf/helpers";
import { Feature, Geometry, LineString, Point } from "@turf/helpers";

/**
* Boolean-touches true if none of the points common to both geometries
Expand Down Expand Up @@ -766,7 +766,7 @@ function booleanTouches(
}
}

function isPointOnLineEnd(point, line) {
function isPointOnLineEnd(point: Point, line: LineString) {
if (compareCoords(line.coordinates[0], point.coordinates)) return true;
if (
compareCoords(
Expand All @@ -786,7 +786,7 @@ function isPointOnLineEnd(point, line) {
* @param {Position} pair2 point [x,y]
* @returns {boolean} true/false if coord pairs match
*/
function compareCoords(pair1, pair2) {
function compareCoords(pair1: number[], pair2: number[]) {
return pair1[0] === pair2[0] && pair1[1] === pair2[1];
}

Expand Down
18 changes: 14 additions & 4 deletions packages/turf-boolean-valid/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { getGeom } from "@turf/invariant";
import { polygon, lineString, Feature, Geometry } from "@turf/helpers";
import {
polygon,
lineString,
Feature,
Geometry,
Position,
} from "@turf/helpers";
import booleanDisjoint from "@turf/boolean-disjoint";
import booleanCrosses from "@turf/boolean-crosses";
import lineIntersect from "@turf/line-intersect";
Expand Down Expand Up @@ -87,14 +93,14 @@ export default function booleanValid(feature: Feature<any> | Geometry) {
}
}

function checkRingsClose(geom) {
function checkRingsClose(geom: Position[]) {
return (
geom[0][0] === geom[geom.length - 1][0] ||
geom[0][1] === geom[geom.length - 1][1]
);
}

function checkRingsForSpikesPunctures(geom) {
function checkRingsForSpikesPunctures(geom: Position[]) {
for (var i = 0; i < geom.length - 1; i++) {
var point = geom[i];
for (var ii = i + 1; ii < geom.length - 2; ii++) {
Expand All @@ -105,7 +111,11 @@ function checkRingsForSpikesPunctures(geom) {
return false;
}

function checkPolygonAgainstOthers(poly, geom, index) {
function checkPolygonAgainstOthers(
poly: Position[][],
geom: Position[][][],
index: number
) {
var polyToCheck = polygon(poly);
for (var i = index + 1; i < geom.length; i++) {
if (!booleanDisjoint(polyToCheck, polygon(geom[i]))) {
Expand Down

0 comments on commit 3b20c56

Please sign in to comment.