Skip to content

Commit

Permalink
Fix lib transpile bbox-clip
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Apr 16, 2018
1 parent a078be7 commit 449e2be
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 31 deletions.
20 changes: 20 additions & 0 deletions packages/turf-bbox-clip/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BBox, Feature, LineString, MultiLineString, MultiPolygon, Polygon, Properties } from "@turf/helpers";
/**
* Takes a {@link Feature} and a bbox and clips the feature to the bbox using
* [lineclip](https://github.com/mapbox/lineclip).
* May result in degenerate edges when clipping Polygons.
*
* @name bboxClip
* @param {Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature feature to clip to the bbox
* @param {BBox} bbox extent in [minX, minY, maxX, maxY] order
* @returns {Feature<LineString|MultiLineString|Polygon|MultiPolygon>} clipped Feature
* @example
* var bbox = [0, 0, 10, 10];
* var poly = turf.polygon([[[2, 2], [8, 4], [12, 8], [3, 7], [2, 2]]]);
*
* var clipped = turf.bboxClip(poly, bbox);
*
* //addToMap
* var addToMap = [bbox, poly, clipped]
*/
export default function bboxClip<G extends Polygon | MultiPolygon | LineString | MultiLineString, P = Properties>(feature: Feature<G, P> | G, bbox: BBox): Feature<LineString, {}> | Feature<MultiLineString, {}> | Feature<Polygon, {}> | Feature<MultiPolygon, {}>;
49 changes: 24 additions & 25 deletions packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as lineclip from './lib/lineclip';
import { getGeom, getCoords } from '@turf/invariant';
import {
lineString, multiLineString, polygon, multiPolygon,
Feature, Polygon, MultiPolygon, LineString, MultiLineString, Properties, BBox
} from '@turf/helpers';
BBox, Feature, LineString, lineString,
multiLineString, MultiLineString, multiPolygon, MultiPolygon, polygon, Polygon, Properties,
} from "@turf/helpers";
import { getCoords, getGeom } from "@turf/invariant";
import * as lineclip from "./lib/lineclip";

/**
* Takes a {@link Feature} and a bbox and clips the feature to the bbox using [lineclip](https://github.com/mapbox/lineclip).
* Takes a {@link Feature} and a bbox and clips the feature to the bbox using
* [lineclip](https://github.com/mapbox/lineclip).
* May result in degenerate edges when clipping Polygons.
*
* @name bboxClip
Expand All @@ -22,40 +23,40 @@ import {
* //addToMap
* var addToMap = [bbox, poly, clipped]
*/
function bboxClip<G extends Polygon | MultiPolygon | LineString | MultiLineString, P = Properties>(
export default function bboxClip<G extends Polygon | MultiPolygon | LineString | MultiLineString, P = Properties>(
feature: Feature<G, P> | G,
bbox: BBox
bbox: BBox,
) {
const geom = getGeom(feature);
const type = geom.type;
const properties = feature.type === 'Feature' ? feature.properties : {};
const properties = feature.type === "Feature" ? feature.properties : {};
let coords: any[] = geom.coordinates;

switch (type) {
case 'LineString':
case 'MultiLineString':
const lines = [];
if (type === 'LineString') coords = [coords];
coords.forEach(function (line) {
case "LineString":
case "MultiLineString":
const lines: any[] = [];
if (type === "LineString") { coords = [coords]; }
coords.forEach((line) => {
lineclip.polyline(line, bbox, lines);
});
if (lines.length === 1) return lineString(lines[0], properties);
if (lines.length === 1) { return lineString(lines[0], properties); }
return multiLineString(lines, properties);
case 'Polygon':
case "Polygon":
return polygon(clipPolygon(coords, bbox), properties);
case 'MultiPolygon':
return multiPolygon(coords.map((polygon) => {
return clipPolygon(polygon, bbox);
case "MultiPolygon":
return multiPolygon(coords.map((poly) => {
return clipPolygon(poly, bbox);
}), properties);
default:
throw new Error('geometry ' + type + ' not supported');
throw new Error("geometry " + type + " not supported");
}
}

function clipPolygon(rings: any[], bbox: BBox) {
var outRings = [];
for (var i = 0; i < rings.length; i++) {
var clipped = lineclip.polygon(rings[i], bbox);
const outRings = [];
for (const ring of rings) {
const clipped: any = lineclip.polygon(ring, bbox);
if (clipped.length > 0) {
if (clipped[0][0] !== clipped[clipped.length - 1][0] || clipped[0][1] !== clipped[clipped.length - 1][1]) {
clipped.push(clipped[0]);
Expand All @@ -67,5 +68,3 @@ function clipPolygon(rings: any[], bbox: BBox) {
}
return outRings;
}

export default bboxClip
1 change: 0 additions & 1 deletion packages/turf-bbox-clip/lib/.gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions packages/turf-bbox-clip/lib/lineclip.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { lineclip as polyline, polygonclip as polygon };
export default function lineclip(points: any, bbox: any, result: any): any;
declare function polygonclip(points: any, bbox: any): any[] | undefined;
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
export {lineclip as polyline, polygonclip as polygon}
'use strict';

module.exports = lineclip;
module.exports.default = lineclip;

lineclip.polyline = lineclip;
lineclip.polygon = polygonclip;


// Cohen-Sutherland line clippign algorithm, adapted to efficiently
// handle polylines rather than just segments
export default function lineclip(points, bbox, result) {

function lineclip(points, bbox, result) {

var len = points.length,
codeA = bitCode(points[0], bbox),
Expand Down Expand Up @@ -114,4 +122,4 @@ function bitCode(p, bbox) {
else if (p[1] > bbox[3]) code |= 8; // top

return code;
}
}
5 changes: 3 additions & 2 deletions packages/turf-bbox-clip/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@turf/bbox-clip",
"version": "6.0.1",
"version": "6.0.3",
"description": "turf bbox-clip module",
"main": "index",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"lib"
"lib/lineclip.js",
"lib/lineclip.d.ts"
],
"scripts": {
"prepare": "tsc",
Expand Down
17 changes: 17 additions & 0 deletions packages/turf-bbox-clip/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */

/* Module Resolution Options */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"files": [
"index.ts"
]
}
9 changes: 9 additions & 0 deletions packages/turf-bbox-clip/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {},
"rulesDirectory": []
}

0 comments on commit 449e2be

Please sign in to comment.