From 97d7377cf8f38d1c4e8e424f4a55de99428fe281 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Mon, 23 Nov 2015 22:57:19 -0500 Subject: [PATCH 01/15] Initial commit. Created turf.d.ts and turf-test.ts files. --- turf/turf-test.ts | 0 turf/turf.d.ts | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 turf/turf-test.ts create mode 100644 turf/turf.d.ts diff --git a/turf/turf-test.ts b/turf/turf-test.ts new file mode 100644 index 000000000..e69de29bb diff --git a/turf/turf.d.ts b/turf/turf.d.ts new file mode 100644 index 000000000..e69de29bb From 8ccfe0dd015abcdae1625bfc795ca7ae6615fd78 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 24 Nov 2015 14:54:21 -0500 Subject: [PATCH 02/15] Added definition for distance and pointOnLine. Added related tests. --- turf/turf-test.ts | 94 +++++++++++++++++++++++++++++++++++++++++++++++ turf/turf.d.ts | 65 ++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index e69de29bb..1cf6be9b0 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -0,0 +1,94 @@ +/// + +////////////////////////////////////////////////////////////////////////// +// Tests Aggregation +////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////// +// Tests Measurement +////////////////////////////////////////////////////////////////////////// + +var point1 = { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-75.343, 39.984] + } +}; +var point2 = { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-75.534, 39.123] + } +}; +var units = "miles"; + +var points = { + "type": "FeatureCollection", + "features": [point1, point2] +}; + +var distance = turf.distance(point1, point2, units); + +////////////////////////////////////////////////////////////////////////// +// Tests Transformation +////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////// +// Tests Misc +////////////////////////////////////////////////////////////////////////// + +var line = { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [-77.031669, 38.878605], + [-77.029609, 38.881946], + [-77.020339, 38.884084], + [-77.025661, 38.885821], + [-77.021884, 38.889563], + [-77.019824, 38.892368] + ] + } +}; +var pt = { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-77.037076, 38.884017] + } +}; + +var snapped = turf.pointOnLine(line, pt); +snapped.properties['marker-color'] = '#00f' + +var result = { + "type": "FeatureCollection", + "features": [line, pt, snapped] +}; + +////////////////////////////////////////////////////////////////////////// +// Tests Helper +////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////// +// Tests Data +////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////// +// Tests Interpolation +//////////////////////////////////////////////////////////////////////////; + +////////////////////////////////////////////////////////////////////////// +// Tests Joins +////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////// +// Tests Classification +////////////////////////////////////////////////////////////////////////// diff --git a/turf/turf.d.ts b/turf/turf.d.ts index e69de29bb..cde4b59d3 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -0,0 +1,65 @@ +// Type definitions for Turf 2.0 +// Project: http://turfjs.org/ +// Definitions by: Guillaume Croteau +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module turf { + ////////////////////////////////////////////////////////////////////////// + // Aggregation + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // Measurement + ////////////////////////////////////////////////////////////////////////// + + /** + * Calculates the distance between two points in degress, radians, miles, or kilometers. This uses the Haversine formula to account for global curvature. + * @param from Origin point + * @param to Destination point + * @param units Can be degrees, radians, miles, or kilometers. Default is kilometers. + * @returns Distance between the two points + */ + function distance(from: GeoJSON.Feature, to: GeoJSON.Feature, units?: string): number; + + ////////////////////////////////////////////////////////////////////////// + // Transformation + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // Misc + ////////////////////////////////////////////////////////////////////////// + + /** + * Takes a Point and a LineString and calculates the closest Point on the LineString. + * @param line Line to snap to + * @param point Point to snap from + * @returns Closest point on the line to point + */ + function pointOnLine(line: GeoJSON.Feature, point: GeoJSON.Feature): GeoJSON.Feature; + + ////////////////////////////////////////////////////////////////////////// + // Helper + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // Data + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // Interpolation + //////////////////////////////////////////////////////////////////////////; + + ////////////////////////////////////////////////////////////////////////// + // Joins + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // Classification + ////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////////// + // Types + ////////////////////////////////////////////////////////////////////////// +} From 9488d2229fd7f91287777bcd3d61f658b3a1c020 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 24 Nov 2015 16:53:35 -0500 Subject: [PATCH 03/15] Added along and area definition. Added related tests. --- turf/turf-test.ts | 100 ++++++++++++++++++++++++++++++++-------------- turf/turf.d.ts | 18 ++++++++- 2 files changed, 86 insertions(+), 32 deletions(-) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index 1cf6be9b0..5d1a73a93 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -1,11 +1,7 @@ /// ////////////////////////////////////////////////////////////////////////// -// Tests Aggregation -////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////// -// Tests Measurement +// Tests data initialisation ////////////////////////////////////////////////////////////////////////// var point1 = { @@ -16,6 +12,7 @@ var point1 = { "coordinates": [-75.343, 39.984] } }; + var point2 = { "type": "Feature", "properties": {}, @@ -24,22 +21,6 @@ var point2 = { "coordinates": [-75.534, 39.123] } }; -var units = "miles"; - -var points = { - "type": "FeatureCollection", - "features": [point1, point2] -}; - -var distance = turf.distance(point1, point2, units); - -////////////////////////////////////////////////////////////////////////// -// Tests Transformation -////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////// -// Tests Misc -////////////////////////////////////////////////////////////////////////// var line = { "type": "Feature", @@ -56,21 +37,78 @@ var line = { ] } }; -var pt = { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "Point", - "coordinates": [-77.037076, 38.884017] - } + +var polygons = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[ + [-67.031021, 10.458102], + [-67.031021, 10.53372], + [-66.929397, 10.53372], + [-66.929397, 10.458102], + [-67.031021, 10.458102] + ]] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[ + [-66.919784, 10.397325], + [-66.919784, 10.513467], + [-66.805114, 10.513467], + [-66.805114, 10.397325], + [-66.919784, 10.397325] + ]] + } + } + ] }; -var snapped = turf.pointOnLine(line, pt); -snapped.properties['marker-color'] = '#00f' +////////////////////////////////////////////////////////////////////////// +// Tests Aggregation +////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////// +// Tests Measurement +////////////////////////////////////////////////////////////////////////// + +// -- Test along -- +var along = turf.along(line, 1, 'miles'); var result = { "type": "FeatureCollection", - "features": [line, pt, snapped] + "features": [line, along] +}; + +// -- Test area -- +var area = turf.area(polygons); + +// -- Test distance -- +var units = "miles"; +var distance = turf.distance(point1, point2, units); + +////////////////////////////////////////////////////////////////////////// +// Tests Transformation +////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////// +// Tests Misc +////////////////////////////////////////////////////////////////////////// + +// -- Test pointOnLine -- +var snapped = turf.pointOnLine(line, point1); +snapped.properties['marker-color'] = '#00f' + +result = { + "type": "FeatureCollection", + "features": [line, point1, snapped] }; ////////////////////////////////////////////////////////////////////////// diff --git a/turf/turf.d.ts b/turf/turf.d.ts index cde4b59d3..15e9478db 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -14,11 +14,27 @@ declare module turf { // Measurement ////////////////////////////////////////////////////////////////////////// + /** + * Takes a line and returns a point at a specified distance along the line. + * @param line Input line + * @param distance Distance along the line + * @param [units=miles] Can be degrees, radians, miles, or kilometers. Default is miles + * @returns Point along the line + */ + function along(line: GeoJSON.Feature, distance: number, units?: string): GeoJSON.Feature; + + /** + * Takes one or more features and returns their area in square meters. + * @param input Input features + * @returns Area in square meters + */ + function area(input: GeoJSON.Feature | GeoJSON.FeatureCollection): number; + /** * Calculates the distance between two points in degress, radians, miles, or kilometers. This uses the Haversine formula to account for global curvature. * @param from Origin point * @param to Destination point - * @param units Can be degrees, radians, miles, or kilometers. Default is kilometers. + * @param [units=kilometers] Can be degrees, radians, miles, or kilometers. Default is kilometers. * @returns Distance between the two points */ function distance(from: GeoJSON.Feature, to: GeoJSON.Feature, units?: string): number; From 848b9d84676b802a37e6d0d9dca226780c9f311c Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 24 Nov 2015 19:52:59 -0500 Subject: [PATCH 04/15] Completed the Measurement definitions. --- turf/turf-test.ts | 157 ++++++++++++++++++++++++++++++++++++++++++---- turf/turf.d.ts | 135 ++++++++++++++++++++++++++++++++------- 2 files changed, 259 insertions(+), 33 deletions(-) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index 5d1a73a93..c248ebda1 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -71,6 +71,112 @@ var polygons = { ] }; +var polygon = { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[ + [105.818939,21.004714], + [105.818939,21.061754], + [105.890007,21.061754], + [105.890007,21.004714], + [105.818939,21.004714] + ]] + } +}; + +var features = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.522259, 35.4691] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.502754, 35.463455] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.508269, 35.463245] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.516809, 35.465779] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.515372, 35.467072] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.509363, 35.463053] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.511123, 35.466601] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.518547, 35.469327] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.519706, 35.469659] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.517839, 35.466998] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.508678, 35.464942] + } + }, { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [-97.514914, 35.463453] + } + } + ] +}; + ////////////////////////////////////////////////////////////////////////// // Tests Aggregation ////////////////////////////////////////////////////////////////////////// @@ -82,18 +188,53 @@ var polygons = { // -- Test along -- var along = turf.along(line, 1, 'miles'); -var result = { - "type": "FeatureCollection", - "features": [line, along] -}; - // -- Test area -- var area = turf.area(polygons); +// -- Test bboxPolygon -- +var bbox = [0, 0, 10, 10]; +var poly = turf.bboxPolygon(bbox); + +// -- Test bearing -- +var bearing = turf.bearing(point1, point2); + +// -- Test center +var centerPt = turf.center(features); + +// -- Test centroid -- +var centroidPt = turf.centroid(poly); + +// -- Test destination -- +var distance = 50; +var bearing = 90; +var units = 'miles'; +var destination = turf.destination(point1, distance, bearing, units); + // -- Test distance -- var units = "miles"; var distance = turf.distance(point1, point2, units); +// -- Test envelope -- +var enveloped = turf.envelope(polygons); + +// -- Test extent -- +var bbox = turf.extent(polygons); + +// -- Test lineDistance +var length = turf.lineDistance(line, 'miles'); + +// -- Test midpoint -- +var midpointed = turf.midpoint(point1, point2); + +// -- Test pointOnSurface -- +var pointOnPolygon = turf.pointOnSurface(polygon); + +// -- Test size -- +var resized = turf.size(bbox, 2); + +// -- Test square -- +var squared = turf.square(bbox); + ////////////////////////////////////////////////////////////////////////// // Tests Transformation ////////////////////////////////////////////////////////////////////////// @@ -104,12 +245,6 @@ var distance = turf.distance(point1, point2, units); // -- Test pointOnLine -- var snapped = turf.pointOnLine(line, point1); -snapped.properties['marker-color'] = '#00f' - -result = { - "type": "FeatureCollection", - "features": [line, point1, snapped] -}; ////////////////////////////////////////////////////////////////////////// // Tests Helper diff --git a/turf/turf.d.ts b/turf/turf.d.ts index 15e9478db..2b0c69c13 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -6,13 +6,13 @@ /// declare module turf { - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Aggregation - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Measurement - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// /** * Takes a line and returns a point at a specified distance along the line. @@ -29,7 +29,46 @@ declare module turf { * @returns Area in square meters */ function area(input: GeoJSON.Feature | GeoJSON.FeatureCollection): number; - + + /** + * Takes a bbox and returns an equivalent polygon. + * @param bbox An Array of bounding box coordinates in the form: [xLow, yLow, xHigh, yHigh] + * @returns A Polygon representation of the bounding box + */ + function bboxPolygon(bbox: Array): GeoJSON.Feature; + + /** + * Takes two points and finds the geographic bearing between them. + * @param start Starting Point + * @param end Ending point + * @returns Bearing in decimal degrees + */ + function bearing(start: GeoJSON.Feature, end: GeoJSON.Feature): number; + + /** + * Takes a FeatureCollection and returns the absolute center point of all features. + * @param features Input features + * @returns A Point feature at the absolute center point of all input features + */ + function center(features: GeoJSON.FeatureCollection): GeoJSON.Feature; + + /** + * Takes one or more features and calculates the centroid using the arithmetic mean of all vertices. This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons. + * @param features Input features + * @returns The centroid of the input features + */ + function centroid(features: GeoJSON.Feature | GeoJSON.FeatureCollection): GeoJSON.Feature; + + /** + * Takes a Point and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers; and bearing in degrees. This uses the Haversine formula to account for global curvature. + * @param start Starting point + * @param distance Distance from the starting point + * @param bearing Ranging from -180 and 180 + * @param units Miles, kilometers, degrees or radians + * @returns Destination point + */ + function destination(start: GeoJSON.Feature, distance: number, bearing: number, units: string): GeoJSON.Feature; + /** * Calculates the distance between two points in degress, radians, miles, or kilometers. This uses the Haversine formula to account for global curvature. * @param from Origin point @@ -39,13 +78,65 @@ declare module turf { */ function distance(from: GeoJSON.Feature, to: GeoJSON.Feature, units?: string): number; - ////////////////////////////////////////////////////////////////////////// - // Transformation - ////////////////////////////////////////////////////////////////////////// + /** + * Takes any number of features and returns a rectangular Polygon that encompasses all vertices. + * @param fc Input features + * @returns A rectangular Polygon feature that encompasses all vertices + */ + function envelope(fc: GeoJSON.FeatureCollection): GeoJSON.Feature; - ////////////////////////////////////////////////////////////////////////// + /** + * Takes a set of features, calculates the extent of all input features, and returns a bounding box. + * @param input Input features + * @returns The bounding box of input given as an array in WSEN order (west, south, east, north) + */ + function extent(input: GeoJSON.Feature | GeoJSON.FeatureCollection): Array; + + /** + * Takes a line and measures its length in the specified units. + * @param line Line to measure + * @param units Can be degrees, radians, miles, or kilometers + * @returns Length of the input line + */ + function lineDistance(line: GeoJSON.Feature, units: string): number; + + /** + * Takes two points and returns a point midway between them. + * @param pt1 First point + * @param pt2 Second point + * @returns A point midway between pt1 and pt2 + */ + function midpoint(pt1: GeoJSON.Feature, pt2: GeoJSON.Feature): GeoJSON.Feature; + + /** + * Takes a feature and returns a Point guaranteed to be on the surface of the feature. Given a Polygon, the point will be in the area of the polygon. Given a LineString, the point will be along the string. Given a Point, the point will the same as the input. + * @param input Any feature or set of features + * @returns A point on the surface of input + */ + function pointOnSurface(input: GeoJSON.Feature | GeoJSON.FeatureCollection): GeoJSON.Feature; + + /** + * Takes a bounding box and returns a new bounding box with a size expanded or contracted by a factor of X. + * @param bbox A bounding box + * @param factor The ratio of the new bbox to the input bbox + * @returns The resized bbox + */ + function size(bbox: Array, factor: number): Array; + + /** + * Takes a bounding box and calculates the minimum square bounding box that would contain the input. + * @param bbox A bounding box + * @returns A square surrounding bbox + */ + function square(bbox: Array): Array; + + ////////////////////////////////////////////////////// + // Transformation + ////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////// // Misc - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// /** * Takes a Point and a LineString and calculates the closest Point on the LineString. @@ -55,27 +146,27 @@ declare module turf { */ function pointOnLine(line: GeoJSON.Feature, point: GeoJSON.Feature): GeoJSON.Feature; - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Helper - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Data - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Interpolation - //////////////////////////////////////////////////////////////////////////; + ////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Joins - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Classification - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// // Types - ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////// } From bbc2af3a3fc34fb1c64d0596cb8c5f2be7f493b7 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 24 Nov 2015 20:22:20 -0500 Subject: [PATCH 05/15] Completed the Transformation definitions. --- turf/turf-test.ts | 52 ++++++++++++++++++++++++++++-- turf/turf.d.ts | 82 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 128 insertions(+), 6 deletions(-) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index c248ebda1..85b2c7b9a 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -71,7 +71,7 @@ var polygons = { ] }; -var polygon = { +var polygon1 = { "type": "Feature", "properties": {}, "geometry": { @@ -86,6 +86,26 @@ var polygon = { } }; +var polygon2 = { + "type": "Feature", + "properties": { + "fill": "#00f" + }, + "geometry": { + "type": "Polygon", + "coordinates": [[ + [-122.520217, 45.535693], + [-122.64038, 45.553967], + [-122.720031, 45.526554], + [-122.669906, 45.507309], + [-122.723464, 45.446643], + [-122.532577, 45.408574], + [-122.487258, 45.477466], + [-122.520217, 45.535693] + ]] + } +} + var features = { "type": "FeatureCollection", "features": [ @@ -227,7 +247,7 @@ var length = turf.lineDistance(line, 'miles'); var midpointed = turf.midpoint(point1, point2); // -- Test pointOnSurface -- -var pointOnPolygon = turf.pointOnSurface(polygon); +var pointOnPolygon = turf.pointOnSurface(polygon1); // -- Test size -- var resized = turf.size(bbox, 2); @@ -239,6 +259,34 @@ var squared = turf.square(bbox); // Tests Transformation ////////////////////////////////////////////////////////////////////////// +// -- Test bezier -- +var curved = turf.bezier(line); + +// -- Test buffer -- +var buffered = turf.buffer(point1, 500, units); + +// -- Test concave -- +var hull = turf.concave(features, 1, 'miles'); + +// -- Test convex -- +var hull = turf.convex(features); + +// -- Test difference -- +var differenced = turf.difference(polygon1, polygon2); + +// -- Test intersect -- +var intersection = turf.intersect(polygon1, polygon2); + +// -- Test merge -- +var merged = turf.merge(polygons); + +// -- Test simplify -- +var tolerance = 0.01; +var simplified = turf.simplify(polygon1, tolerance, false); + +// -- Test union -- +var union = turf.union(polygon1, polygon2); + ////////////////////////////////////////////////////////////////////////// // Tests Misc ////////////////////////////////////////////////////////////////////////// diff --git a/turf/turf.d.ts b/turf/turf.d.ts index 2b0c69c13..77bba0c74 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -18,7 +18,7 @@ declare module turf { * Takes a line and returns a point at a specified distance along the line. * @param line Input line * @param distance Distance along the line - * @param [units=miles] Can be degrees, radians, miles, or kilometers. Default is miles + * @param [units=miles] 'miles', 'feet', 'kilometers', 'meters', or 'degrees' * @returns Point along the line */ function along(line: GeoJSON.Feature, distance: number, units?: string): GeoJSON.Feature; @@ -64,7 +64,7 @@ declare module turf { * @param start Starting point * @param distance Distance from the starting point * @param bearing Ranging from -180 and 180 - * @param units Miles, kilometers, degrees or radians + * @param units 'miles', 'feet', 'kilometers', 'meters', or 'degrees' * @returns Destination point */ function destination(start: GeoJSON.Feature, distance: number, bearing: number, units: string): GeoJSON.Feature; @@ -73,7 +73,7 @@ declare module turf { * Calculates the distance between two points in degress, radians, miles, or kilometers. This uses the Haversine formula to account for global curvature. * @param from Origin point * @param to Destination point - * @param [units=kilometers] Can be degrees, radians, miles, or kilometers. Default is kilometers. + * @param [units=kilometers] 'miles', 'feet', 'kilometers', 'meters', or 'degrees' * @returns Distance between the two points */ function distance(from: GeoJSON.Feature, to: GeoJSON.Feature, units?: string): number; @@ -95,7 +95,7 @@ declare module turf { /** * Takes a line and measures its length in the specified units. * @param line Line to measure - * @param units Can be degrees, radians, miles, or kilometers + * @param units 'miles', 'feet', 'kilometers', 'meters', or 'degrees' * @returns Length of the input line */ function lineDistance(line: GeoJSON.Feature, units: string): number; @@ -134,6 +134,80 @@ declare module turf { // Transformation ////////////////////////////////////////////////////// + /** + * Takes a line and returns a curved version by applying a Bezier spline algorithm. The bezier spline implementation is by Leszek Rybicki. + * @param line Input LineString + * @param [resolution=10000] Time in milliseconds between points + * @param [sharpness=0.85] A measure of how curvy the path should be between splines + * @returns Curved line + */ + function bezier(line: GeoJSON.Feature, resolution?: number, sharpness?: number): GeoJSON.Feature; + + /** + * Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees. + * @param feature Input to be buffered + * @param distance Distance to draw the buffer + * @param units 'miles', 'feet', 'kilometers', 'meters', or 'degrees' + * @returns Buffered features + */ + function buffer(feature: GeoJSON.Feature | GeoJSON.FeatureCollection, distance: number, units: string): GeoJSON.Feature | GeoJSON.FeatureCollection; + + /** + * Takes a set of points and returns a concave hull polygon. Internally, this implements a Monotone chain algorithm. + * @param points Input points + * @param maxEdge The size of an edge necessary for part of the hull to become concave (in miles) + * @param units Used for maxEdge distance (miles or kilometers) + * @returns A concave hull + */ + function concave(points: GeoJSON.FeatureCollection, maxEdge: number, units: string): GeoJSON.Feature; + + /** + * Takes a set of points and returns a convex hull polygon. Internally this uses the convex-hull module that implements a monotone chain hull. + * @param input Input points + * @returns A convex hull + */ + function convex(points: GeoJSON.FeatureCollection): GeoJSON.Feature; + + /** + * Finds the difference between two polygons by clipping the second polygon from the first. + * @param poly1 Input Polygon feaure + * @param poly2 Polygon feature to difference from poly1 + * @returns A Polygon feature showing the area of poly1 excluding the area of poly2 + */ + function difference(poly1: GeoJSON.Feature, poly2: GeoJSON.Feature): GeoJSON.Feature; + + /** + * Takes two polygons and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. + * @param poly1 The first polygon + * @param poly2 The second polygon + * @returns If poly1 and poly2 overlap, returns a Polygon feature representing the area they overlap; if poly1 and poly2 do not overlap, returns undefined; if poly1 and poly2 share a border, a MultiLineString of the locations where their borders are shared + */ + function intersect(poly1: GeoJSON.Feature, poly2: GeoJSON.Feature): GeoJSON.Feature; + + /** + * Takes a set of polygons and returns a single merged polygon feature. If the input polygon features are not contiguous, this function returns a MultiPolygon feature. + * @param fc Input polygons + * @returns Merged polygon or multipolygon + */ + function merge(fc: GeoJSON.FeatureCollection): GeoJSON.Feature; + + /** + * Takes a LineString or Polygon and returns a simplified version. Internally uses simplify-js to perform simplification. + * @param feature Feature to be simplified + * @param tolerance Simplification tolerance + * @param highQuality Whether or not to spend more time to create a higher-quality simplification with a different algorithm + * @returns A simplified feature + */ + function simplify(feature: GeoJSON.Feature | GeoJSON.FeatureCollection | GeoJSON.GeometryCollection, tolerance: number, highQuality: boolean): GeoJSON.Feature | GeoJSON.FeatureCollection | GeoJSON.GeometryCollection; + + /** + * Takes two polygons and returns a combined polygon. If the input polygons are not contiguous, this function returns a MultiPolygon feature. + * @param poly1 Input polygon + * @param poly2 Another input polygon + * @returns A combined Polygon or MultiPolygon feature + */ + function union(poly1: GeoJSON.Feature, poly2: GeoJSON.Feature): GeoJSON.Feature; + ////////////////////////////////////////////////////// // Misc ////////////////////////////////////////////////////// From bd95f700d11867f1f18f1faaf6675906f5fa6c27 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 24 Nov 2015 21:57:25 -0500 Subject: [PATCH 06/15] Completed the Misc definitions. --- turf/turf-test.ts | 17 ++++++++++++++++- turf/turf.d.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index 85b2c7b9a..b053e761e 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -222,7 +222,7 @@ var bearing = turf.bearing(point1, point2); var centerPt = turf.center(features); // -- Test centroid -- -var centroidPt = turf.centroid(poly); +var centroidPt = turf.centroid(polygon1); // -- Test destination -- var distance = 50; @@ -291,6 +291,21 @@ var union = turf.union(polygon1, polygon2); // Tests Misc ////////////////////////////////////////////////////////////////////////// +// -- Test combine -- +var combined = turf.combine(features); + +// -- Test explode -- +var points = turf.explode(polygon1); + +// -- Test flip -- +var flipedPoint = turf.flip(point1); + +// -- Test kinks -- +var kinks = turf.kinks(polygon1); + +// -- Test lineSlice -- +var sliced = turf.lineSlice(point1, point2, line); + // -- Test pointOnLine -- var snapped = turf.pointOnLine(line, point1); diff --git a/turf/turf.d.ts b/turf/turf.d.ts index 77bba0c74..fb479ae34 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -212,6 +212,43 @@ declare module turf { // Misc ////////////////////////////////////////////////////// + /** + * Combines a FeatureCollection of Point, LineString, or Polygon features into MultiPoint, MultiLineString, or MultiPolygon features. + * @param fc A FeatureCollection of any type + * @returns A FeatureCollection of corresponding type to input + */ + function combine(fc: GeoJSON.FeatureCollection): GeoJSON.FeatureCollection; + + /** + * Takes a feature or set of features and returns all positions as points. + * @param input Input features + * @returns Points representing the exploded input features + */ + function explode(input: GeoJSON.Feature | GeoJSON.FeatureCollection): GeoJSON.FeatureCollection; + + /** + * Takes input features and flips all of their coordinates from [x, y] to [y, x]. + * @param input Input features + * @returns A feature or set of features of the same type as input with flipped coordinates + */ + function flip(input: GeoJSON.Feature | GeoJSON.FeatureCollection): GeoJSON.Feature | GeoJSON.FeatureCollection; + + /** + * Takes a polygon and returns points at all self-intersections. + * @param polygon Input polygon + * @returns Self-intersections + */ + function kinks(polygon: GeoJSON.Feature): GeoJSON.FeatureCollection; + + /** + * Takes a line, a start Point, and a stop point and returns the line in between those points. + * @param point1 Starting point + * @param point2 Stopping point + * @param line Line to slice + * @returns Sliced line + */ + function lineSlice(point1: GeoJSON.Feature, point2: GeoJSON.Feature, line: GeoJSON.Feature): GeoJSON.Feature; + /** * Takes a Point and a LineString and calculates the closest Point on the LineString. * @param line Line to snap to From 4d5969f17cd9411d5bf5d87eba035ecd012a48d2 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 24 Nov 2015 22:47:30 -0500 Subject: [PATCH 07/15] Completed the Helper definitions. --- turf/turf-test.ts | 30 ++++++++++++++++++++++++++++++ turf/turf.d.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index b053e761e..296542868 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -313,6 +313,36 @@ var snapped = turf.pointOnLine(line, point1); // Tests Helper ////////////////////////////////////////////////////////////////////////// +// -- Test featurecollection -- +var fc = turf.featurecollection([point1, point2]); + +// -- Test linestring -- +var linestring1 = turf.linestring([ + [-21.964416, 64.148203], + [-21.956176, 64.141316], + [-21.93901, 64.135924], + [-21.927337, 64.136673] +]); +var linestring2 = turf.linestring([ + [-21.929054, 64.127985], + [-21.912918, 64.134726], + [-21.916007, 64.141016], + [-21.930084, 64.14446] +], {name: 'line 1', distance: 145}); + +// -- Test point -- +var pt1 = turf.point([-75.343, 39.984]); +var pt2 = turf.point([-75.343, 39.984], {name: 'point 1', distance: 145}); + +// -- Test polygon -- +var polygon = turf.polygon([[ + [-2.275543, 53.464547], + [-2.275543, 53.489271], + [-2.215118, 53.489271], + [-2.215118, 53.464547], + [-2.275543, 53.464547] +]], { name: 'poly1', population: 400}); + ////////////////////////////////////////////////////////////////////////// // Tests Data ////////////////////////////////////////////////////////////////////////// diff --git a/turf/turf.d.ts b/turf/turf.d.ts index fb479ae34..0f6868630 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -261,6 +261,37 @@ declare module turf { // Helper ////////////////////////////////////////////////////// + /** + * Takes one or more Features and creates a FeatureCollection. + * @param features Input features + * @returns A FeatureCollection of input features + */ + function featurecollection(features: Array): GeoJSON.FeatureCollection; + + /** + * Creates a LineString based on a coordinate array. Properties can be added optionally. + * @param coordinates An array of Positions + * @param [properties] An Object of key-value pairs to add as properties + * @returns A LineString feature + */ + function linestring(coordinates: Array>, properties?: any): GeoJSON.Feature; + + /** + * Takes coordinates and properties (optional) and returns a new Point feature. + * @param coordinates Longitude, latitude position (each in decimal degrees) + * @param [properties] An Object of key-value pairs to add as properties + * @returns A Point feature + */ + function point(coordinates: Array, properties?: any): GeoJSON.Feature; + + /** + * Takes an array of LinearRings and optionally an Object with properties and returns a Polygon feature. + * @param rings An array of LinearRings + * @param [properties] An Object of key-value pairs to add as properties + * @returns A Polygon feature + */ + function polygon(rings: Array>>, properties?: any): GeoJSON.Feature; + ////////////////////////////////////////////////////// // Data ////////////////////////////////////////////////////// From b5b7ec2ce07a810c2d19a111196e2753197e733e Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 24 Nov 2015 23:44:45 -0500 Subject: [PATCH 08/15] Added filter definition. --- turf/turf-test.ts | 5 +++++ turf/turf.d.ts | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index 296542868..4d89f140e 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -347,6 +347,11 @@ var polygon = turf.polygon([[ // Tests Data ////////////////////////////////////////////////////////////////////////// +// -- Test filter -- +var key = "species"; +var value = "oak"; +var filtered = turf.filter(features, key, value); + ////////////////////////////////////////////////////////////////////////// // Tests Interpolation //////////////////////////////////////////////////////////////////////////; diff --git a/turf/turf.d.ts b/turf/turf.d.ts index 0f6868630..85d7b25f9 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -296,6 +296,15 @@ declare module turf { // Data ////////////////////////////////////////////////////// + /** + * Takes a FeatureCollection and filters it by a given property and value. + * @param features Input features + * @param key The property on which to filter + * @param value The value of that property on which to filter + * @returns A filtered collection with only features that match input key and value + */ + function filter(features: GeoJSON.FeatureCollection, key: string, value: string): GeoJSON.FeatureCollection; + ////////////////////////////////////////////////////// // Interpolation ////////////////////////////////////////////////////// From 9ce1cff36fceab03314c25d0513f23b7d8453cb7 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Wed, 25 Nov 2015 17:08:37 -0500 Subject: [PATCH 09/15] Completed Data definitions. --- turf/turf-test.ts | 18 ++++++++++++++++++ turf/turf.d.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index 4d89f140e..a69d36d47 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -352,6 +352,24 @@ var key = "species"; var value = "oak"; var filtered = turf.filter(features, key, value); +// -- Test random -- +var points = turf.random('points', 100, { + bbox: [-70, 40, -60, 60] +}); + +var points = turf.random('points', 100, { + bbox: [-70, 40, -60, 60], + num_vertices: 2, + max_radial_length: 10 +}); + +// -- Test remove -- +var filtered = turf.remove(points, 'marker-color', '#00f'); + +// -- Test sample -- +var points = turf.random('points', 1000); +var sample = turf.sample(points, 10); + ////////////////////////////////////////////////////////////////////////// // Tests Interpolation //////////////////////////////////////////////////////////////////////////; diff --git a/turf/turf.d.ts b/turf/turf.d.ts index 85d7b25f9..c10fe2ac9 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -305,6 +305,35 @@ declare module turf { */ function filter(features: GeoJSON.FeatureCollection, key: string, value: string): GeoJSON.FeatureCollection; + /** + * Generates random GeoJSON data, including Points and Polygons, for testing and experimentation. + * @param [type='point'] Type of features desired: 'points' or 'polygons' + * @param [count=1] How many geometries should be generated. + * @param [options] Options relevant to the feature desired. Can include: + * - A bounding box inside of which geometries are placed. In the case of Point features, they are guaranteed to be within this bounds, while Polygon features have their centroid within the bounds. + * - The number of vertices added to polygon features. Default is 10; + * - The total number of decimal degrees longitude or latitude that a polygon can extent outwards to from its center. Default is 10. + * @returns Generated random features + */ + function random(type?: string, count?: number, options?: {bbox?: Array; num_vertices?: number; max_radial_length?: number;}): GeoJSON.FeatureCollection; + + /** + * Takes a FeatureCollection of any type, a property, and a value and returns a FeatureCollection with features matching that property-value pair removed. + * @param features Set of input features + * @param property The property to remove + * @param value The value to remove + * @returns The resulting FeatureCollection without features that match the property-value pair + */ + function remove(features: GeoJSON.FeatureCollection, property: string, value: string): GeoJSON.FeatureCollection; + + /** + * Takes a FeatureCollection and returns a FeatureCollection with given number of features at random. + * @param features Set of input features + * @param n Number of features to select + * @returns A FeatureCollection with n features + */ + function sample(features: GeoJSON.FeatureCollection, n: number): GeoJSON.FeatureCollection; + ////////////////////////////////////////////////////// // Interpolation ////////////////////////////////////////////////////// From ab3aa41ff74a961268a1ebe7aee2218e099bb4d9 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Wed, 25 Nov 2015 19:47:29 -0500 Subject: [PATCH 10/15] Added Interpolation definitions to turf.d.ts. --- turf/turf-test.ts | 43 +++++++++++++++++++++++++++++++ turf/turf.d.ts | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index a69d36d47..e8aaec708 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -197,6 +197,24 @@ var features = { ] }; +var triangle = { + "type": "Feature", + "properties": { + "a": 11, + "b": 122, + "c": 44 + }, + "geometry": { + "type": "Polygon", + "coordinates": [[ + [-75.1221, 39.57], + [-75.58, 39.18], + [-75.97, 39.86], + [-75.1221, 39.57] + ]] + } +}; + ////////////////////////////////////////////////////////////////////////// // Tests Aggregation ////////////////////////////////////////////////////////////////////////// @@ -374,6 +392,31 @@ var sample = turf.sample(points, 10); // Tests Interpolation //////////////////////////////////////////////////////////////////////////; +// -- Test hexGrid -- +var cellWidth = 50; +var hexgrid = turf.hexGrid(bbox, cellWidth, units); + +// -- Test isolines -- +var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +var isolined = turf.isolines(points, 'z', 15, breaks); + +// -- Test planepoint -- +var zValue = turf.planepoint(point1, triangle); + +// -- Test pointGrid -- +var extent = [-70.823364, -33.553984, -70.473175, -33.302986]; +var cellWidth = 3; +var grid = turf.pointGrid(extent, cellWidth, units); + +// -- Test squareGrid -- +var squareGrid = turf.squareGrid(extent, cellWidth, units); + +// -- Test tin -- +var tin = turf.tin(points, 'z'); + +// -- Test triangleGrid -- +var triangleGrid = turf.triangleGrid(extent, cellWidth, units); + ////////////////////////////////////////////////////////////////////////// // Tests Joins ////////////////////////////////////////////////////////////////////////// diff --git a/turf/turf.d.ts b/turf/turf.d.ts index c10fe2ac9..b74681a89 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -338,6 +338,70 @@ declare module turf { // Interpolation ////////////////////////////////////////////////////// + /** + * Takes a bounding box and a cell size in degrees and returns a FeatureCollection of flat-topped hexagons (Polygon features) aligned in an "odd-q" vertical grid as described in Hexagonal Grids. + * @param bbox Bounding box in [minX, minY, maxX, maxY] order + * @param cellWidth Width of cell in specified units + * @param units Used in calculating cellWidth ('miles' or 'kilometers') + * @returns A hexagonal grid + */ + function hexGrid(bbox: Array, cellWidth: number, units: string): GeoJSON.FeatureCollection; + + /** + * Takes points with z-values and an array of value breaks and generates isolines. + * @param points Input points + * @param z The property name in points from which z-values will be pulled + * @param resolution Resolution of the underlying grid + * @param breaks Where to draw contours + * @returns Isolines + */ + function isolines(points: GeoJSON.FeatureCollection, z: string, resolution: number, breaks: Array): GeoJSON.FeatureCollection; + + /** + * Takes a triangular plane as a Polygon and a Point within that triangle and returns the z-value at that point. The Polygon needs to have properties a, b, and c that define the values at its three corners. + * @param interpolatedPoint The Point for which a z-value will be calculated + * @param triangle A Polygon feature with three vertices + * @returns The z-value for interpolatedPoint + */ + function planepoint(interpolatedPoint: GeoJSON.Feature, triangle: GeoJSON.Feature): number; + + /** + * Takes a bounding box and a cell depth and returns a set of points in a grid. + * @param extent Extent in [minX, minY, maxX, maxY] order + * @param cellWidth The distance across each cell + * @param units Used in calculating cellWidth ('miles' or 'kilometers') + * @returns Grid of points + */ + function pointGrid(extent: Array, cellWidth: number, units: string): GeoJSON.FeatureCollection; + + /** + * Takes a bounding box and a cell depth and returns a set of square polygons in a grid. + * @param extent Extent in [minX, minY, maxX, maxY] order + * @param cellWidth Width of each cell + * @param units Used in calculating cellWidth ('miles' or 'kilometers') + * @returns Grid of polygons + */ + function squareGrid(extent: Array, cellWidth: number, units: string): GeoJSON.FeatureCollection; + + /** + * Takes a set of points and the name of a z-value property and creates a Triangulated Irregular Network, or a TIN for short, returned as a collection of Polygons. + * These are often used for developing elevation contour maps or stepped heat visualizations. + * This triangulates the points, as well as adds properties called a, b, and c representing the value of the given propertyName at each of the points that represent the corners of the triangle. + * @param points Input points + * @param [propertyName] Name of the property from which to pull z values This is optional: if not given, then there will be no extra data added to the derived triangles. + * @returns TIN output + */ + function tin(points: GeoJSON.FeatureCollection, propertyName?: string): GeoJSON.FeatureCollection; + + /** + * Takes a bounding box and a cell depth and returns a set of triangular polygons in a grid. + * @param extent Extent in [minX, minY, maxX, maxY] order + * @param cellWidth Width of each cell + * @param units Used in calculating cellWidth ('miles' or 'kilometers') + * @returns Grid of triangles + */ + function triangleGrid(extent: Array, cellWidth: number, units: string): GeoJSON.FeatureCollection; + ////////////////////////////////////////////////////// // Joins ////////////////////////////////////////////////////// From 1c55a6117750de906e301ff3a39204844f3a1069 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Wed, 25 Nov 2015 19:56:27 -0500 Subject: [PATCH 11/15] Added Joins definitions to turf.d.ts. --- turf/turf-test.ts | 9 +++++++++ turf/turf.d.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index e8aaec708..d46bf2e81 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -421,6 +421,15 @@ var triangleGrid = turf.triangleGrid(extent, cellWidth, units); // Tests Joins ////////////////////////////////////////////////////////////////////////// +// -- Test inside -- +var isInside1 = turf.inside(point1, polygon); + +// -- Test tag -- +var tagged = turf.tag(points, triangleGrid, 'fill', 'marker-color'); + +// -- Test within -- +var ptsWithin = turf.within(points, polygons); + ////////////////////////////////////////////////////////////////////////// // Tests Classification ////////////////////////////////////////////////////////////////////////// diff --git a/turf/turf.d.ts b/turf/turf.d.ts index b74681a89..f90668b4f 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -406,6 +406,32 @@ declare module turf { // Joins ////////////////////////////////////////////////////// + /** + * Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon. The polygon can be convex or concave. The function accounts for holes. + * @param point Input point + * @param polygon Input polygon or multipolygon + * @returns true if the Point is inside the Polygon; false if the Point is not inside the Polygon + */ + function inside(point: GeoJSON.Feature, polygon: GeoJSON.Feature): boolean; + + /** + * Takes a set of points and a set of polygons and performs a spatial join. + * @param points Input points + * @param polygons Input polygons + * @param polyId Property in polygons to add to joined Point features + * @param containingPolyId Property in points in which to store joined property from polygons + * @returns Points with containingPolyId property containing values from polyId + */ + function tag(points: GeoJSON.FeatureCollection, polygons: GeoJSON.FeatureCollection, polyId: string, containingPolyId: string): GeoJSON.FeatureCollection; + + /** + * Takes a set of points and a set of polygons and returns the points that fall within the polygons. + * @param points Input points + * @param polygons Input polygons + * @returns Points that land within at least one polygon + */ + function within(points: GeoJSON.FeatureCollection, polygons: GeoJSON.FeatureCollection): GeoJSON.FeatureCollection; + ////////////////////////////////////////////////////// // Classification ////////////////////////////////////////////////////// From 77365e3b156e2230e735a1aeec914bcfe9281dd4 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Wed, 25 Nov 2015 20:11:13 -0500 Subject: [PATCH 12/15] Added Classification definitions to turf.d.ts. --- turf/turf-test.ts | 17 +++++++++++++++++ turf/turf.d.ts | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index d46bf2e81..5a8501c74 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -433,3 +433,20 @@ var ptsWithin = turf.within(points, polygons); ////////////////////////////////////////////////////////////////////////// // Tests Classification ////////////////////////////////////////////////////////////////////////// + +// -- Test jenks -- +var breaks = turf.jenks(points, 'population', 3); + +// -- Test nearest -- +var nearest = turf.nearest(point1, points); + +// -- Test quantile -- +var breaks = turf.quantile(points, 'population', [25, 50, 75, 99]); + +// -- Test reclass -- +var translations = [ + [0, 200, "small"], + [200, 400, "medium"], + [400, 600, "large"] +]; +var reclassed = turf.reclass(points, 'population', 'size', translations); diff --git a/turf/turf.d.ts b/turf/turf.d.ts index f90668b4f..0da75e33b 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -436,7 +436,39 @@ declare module turf { // Classification ////////////////////////////////////////////////////// - ////////////////////////////////////////////////////// - // Types - ////////////////////////////////////////////////////// + /** + * Takes a set of features and returns an array of the Jenks Natural breaks for a given property. + * @param input Input features + * @param field The property in input on which to calculate Jenks natural breaks + * @param numberOfBreaks Number of classes in which to group the data + * @returns The break number for each class plus the minimum and maximum values + */ + function jenks(input: GeoJSON.FeatureCollection, field: string, numberOfBreaks: number): Array; + + /** + * Takes a reference point and a set of points and returns the point from the set closest to the reference. + * @param point The reference point + * @param against Input point set + * @returns The closest point in the set to the reference point + */ + function nearest(point: GeoJSON.Feature, against: GeoJSON.FeatureCollection): GeoJSON.Feature; + + /** + * Takes a FeatureCollection, a property name, and a set of percentiles and returns a quantile array. + * @param input Set of features + * @param field The property in input from which to retrieve quantile values + * @param percentiles An Array of percentiles on which to calculate quantile values + * @returns An array of the break values + */ + function quantile(input: GeoJSON.FeatureCollection, field: string, percentiles: Array): Array; + + /** + * Takes a FeatureCollection, an input field, an output field, and an array of translations and outputs an identical FeatureCollection with the output field property populated. + * @param input Set of input features + * @param inField The field to translate + * @param outField The field in which to store translated results + * @param translations An array of translations + * @returns A FeatureCollection with identical geometries to input but with outField populated. + */ + function reclass(input: GeoJSON.FeatureCollection, inField: string, outField: string, translations: Array): GeoJSON.FeatureCollection; } From 36b68a0023358c4c39ff49a9b330639e5c662a0f Mon Sep 17 00:00:00 2001 From: gcroteau Date: Thu, 26 Nov 2015 23:28:51 -0500 Subject: [PATCH 13/15] Added Aggregation definitions to turf.d.ts. --- turf/turf-test.ts | 70 +++++++++++++++++++++++++++++++++++++ turf/turf.d.ts | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index 5a8501c74..a6bb7e2ac 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -215,10 +215,80 @@ var triangle = { } }; +var aggregations = [ + { + aggregation: 'sum', + inField: 'population', + outField: 'pop_sum' + }, + { + aggregation: 'average', + inField: 'population', + outField: 'pop_avg' + }, + { + aggregation: 'median', + inField: 'population', + outField: 'pop_median' + }, + { + aggregation: 'min', + inField: 'population', + outField: 'pop_min' + }, + { + aggregation: 'max', + inField: 'population', + outField: 'pop_max' + }, + { + aggregation: 'deviation', + inField: 'population', + outField: 'pop_deviation' + }, + { + aggregation: 'variance', + inField: 'population', + outField: 'pop_variance' + }, + { + aggregation: 'count', + inField: '', + outField: 'point_count' + } +]; + ////////////////////////////////////////////////////////////////////////// // Tests Aggregation ////////////////////////////////////////////////////////////////////////// +// -- Test aggregate -- +var aggregated = turf.aggregate(polygons, points, aggregations); + +// -- Test average -- +var averaged = turf.average(polygons, points, 'population', 'pop_avg'); + +// -- Test count -- +var counted = turf.count(polygons, points, 'pt_count'); + +// -- Test deviation -- +var deviated = turf.deviation(polygons, points, 'population', 'pop_deviation'); + +// -- Test max -- +var aggregated = turf.max(polygons, points, 'population', 'max'); + +// -- Test median -- +var medians = turf.median(polygons, points, 'population', 'median'); + +// -- Test min -- +var minimums = turf.min(polygons, points, 'population', 'min'); + +// -- Test sum -- +var summed = turf.sum(polygons, points, 'population', 'sum'); + +// -- Test variance -- +var varianced = turf.variance(polygons, points, 'population', 'variance'); + ////////////////////////////////////////////////////////////////////////// // Tests Measurement ////////////////////////////////////////////////////////////////////////// diff --git a/turf/turf.d.ts b/turf/turf.d.ts index 0da75e33b..bbc84eac3 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -10,6 +10,94 @@ declare module turf { // Aggregation ////////////////////////////////////////////////////// + /** + * Calculates a series of aggregations for a set of points within a set of polygons. Sum, average, count, min, max, and deviation are supported. + * @param polygons Polygons with values on which to aggregate + * @param points Points to be aggregated + * @param aggregations An array of aggregation objects + * @returns Polygons with properties listed based on outField values in aggregations + */ + function aggregate(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, aggregations: Array<{aggregation: string, inField: string, outField: string}>): GeoJSON.FeatureCollection; + + /** + * Calculates the average value of a field for a set of points within a set of polygons. + * @param polygons Polygons with values on which to average + * @param points Points from which to calculate the average + * @param field The field in the points features from which to pull values to average + * @param outField The field in polygons to put results of the averages + * @returns Polygons with the value of outField set to the calculated averages + */ + function average(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, field: string, outField: string): GeoJSON.FeatureCollection; + + /** + * Takes a set of points and a set of polygons and calculates the number of points that fall within the set of polygons. + * @param polygons Input polygons + * @param points Input points + * @param countField A field to append to the attributes of the Polygon features representing Point counts + * @returns Polygons with countField appended + */ + function count(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, countField: string): GeoJSON.FeatureCollection; + + /** + * Calculates the standard deviation value of a field for a set of points within a set of polygons. + * @param polygons Input polygons + * @param points Input points + * @param inField The field in points from which to aggregate + * @param outField The field to append to polygons representing deviation + * @returns Polygons with appended field representing deviation + */ + function deviation(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, inField: string, outField: string): GeoJSON.FeatureCollection; + + /** + * Calculates the maximum value of a field for a set of points within a set of polygons. + * @param polygons Input polygons + * @param points Input points + * @param inField The field in input data to analyze + * @param outField The field in which to store results + * @returns Polygons with properties listed as outField values + */ + function max(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, inField: string, outField: string): GeoJSON.FeatureCollection; + + /** + * Calculates the median value of a field for a set of points within a set of polygons. + * @param polygons Input polygons + * @param points Input points + * @param inField The field in input data to analyze + * @param outField The field in which to store results + * @returns Polygons with properties listed as outField values + */ + function median(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, inField: string, outField: string): GeoJSON.FeatureCollection; + + /** + * Calculates the minimum value of a field for a set of points within a set of polygons. + * @param polygons Input polygons + * @param points Input points + * @param inField The field in input data to analyze + * @param outField The field in which to store results + * @returns Polygons with properties listed as outField values + */ + function min(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, inField: string, outField: string): GeoJSON.FeatureCollection; + + /** + * Calculates the sum of a field for a set of points within a set of polygons. + * @param polygons Input polygons + * @param points Input points + * @param inField The field in input data to analyze + * @param outField The field in which to store results + * @returns Polygons with properties listed as outField + */ + function sum(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, inField: string, outField: string): GeoJSON.FeatureCollection; + + /** + * Calculates the variance value of a field for a set of points within a set of polygons. + * @param polygons Input polygons + * @param points Input points + * @param inField The field in input data to analyze + * @param outField The field in which to store results + * @returns Polygons with properties listed as outField + */ + function variance(polygons: GeoJSON.FeatureCollection, points: GeoJSON.FeatureCollection, inField: string, outField: string): GeoJSON.FeatureCollection; + ////////////////////////////////////////////////////// // Measurement ////////////////////////////////////////////////////// From 25f513d3bf40cc952819d0d6839f4b10bccffef9 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Mon, 30 Nov 2015 10:17:44 -0500 Subject: [PATCH 14/15] Cleaned up the definition file. --- turf/turf-test.ts | 40 ++++++++++++++++++------------------ turf/turf.d.ts | 52 ++++++++++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 39 deletions(-) diff --git a/turf/turf-test.ts b/turf/turf-test.ts index a6bb7e2ac..a262b359e 100644 --- a/turf/turf-test.ts +++ b/turf/turf-test.ts @@ -1,8 +1,8 @@ /// -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests data initialisation -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// var point1 = { "type": "Feature", @@ -258,9 +258,9 @@ var aggregations = [ } ]; -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Aggregation -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test aggregate -- var aggregated = turf.aggregate(polygons, points, aggregations); @@ -289,9 +289,9 @@ var summed = turf.sum(polygons, points, 'population', 'sum'); // -- Test variance -- var varianced = turf.variance(polygons, points, 'population', 'variance'); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Measurement -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test along -- var along = turf.along(line, 1, 'miles'); @@ -343,9 +343,9 @@ var resized = turf.size(bbox, 2); // -- Test square -- var squared = turf.square(bbox); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Transformation -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test bezier -- var curved = turf.bezier(line); @@ -375,9 +375,9 @@ var simplified = turf.simplify(polygon1, tolerance, false); // -- Test union -- var union = turf.union(polygon1, polygon2); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Misc -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test combine -- var combined = turf.combine(features); @@ -397,9 +397,9 @@ var sliced = turf.lineSlice(point1, point2, line); // -- Test pointOnLine -- var snapped = turf.pointOnLine(line, point1); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Helper -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test featurecollection -- var fc = turf.featurecollection([point1, point2]); @@ -431,9 +431,9 @@ var polygon = turf.polygon([[ [-2.275543, 53.464547] ]], { name: 'poly1', population: 400}); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Data -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test filter -- var key = "species"; @@ -458,9 +458,9 @@ var filtered = turf.remove(points, 'marker-color', '#00f'); var points = turf.random('points', 1000); var sample = turf.sample(points, 10); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Interpolation -//////////////////////////////////////////////////////////////////////////; +/////////////////////////////////////////// // -- Test hexGrid -- var cellWidth = 50; @@ -487,9 +487,9 @@ var tin = turf.tin(points, 'z'); // -- Test triangleGrid -- var triangleGrid = turf.triangleGrid(extent, cellWidth, units); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Joins -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test inside -- var isInside1 = turf.inside(point1, polygon); @@ -500,9 +500,9 @@ var tagged = turf.tag(points, triangleGrid, 'fill', 'marker-color'); // -- Test within -- var ptsWithin = turf.within(points, polygons); -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // Tests Classification -////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////// // -- Test jenks -- var breaks = turf.jenks(points, 'population', 3); diff --git a/turf/turf.d.ts b/turf/turf.d.ts index bbc84eac3..07299e766 100644 --- a/turf/turf.d.ts +++ b/turf/turf.d.ts @@ -11,7 +11,8 @@ declare module turf { ////////////////////////////////////////////////////// /** - * Calculates a series of aggregations for a set of points within a set of polygons. Sum, average, count, min, max, and deviation are supported. + * Calculates a series of aggregations for a set of points within a set of polygons. + * Sum, average, count, min, max, and deviation are supported. * @param polygons Polygons with values on which to aggregate * @param points Points to be aggregated * @param aggregations An array of aggregation objects @@ -106,7 +107,7 @@ declare module turf { * Takes a line and returns a point at a specified distance along the line. * @param line Input line * @param distance Distance along the line - * @param [units=miles] 'miles', 'feet', 'kilometers', 'meters', or 'degrees' + * @param [units=miles] 'miles', 'kilometers', 'radians' or 'degrees' * @returns Point along the line */ function along(line: GeoJSON.Feature, distance: number, units?: string): GeoJSON.Feature; @@ -141,27 +142,30 @@ declare module turf { function center(features: GeoJSON.FeatureCollection): GeoJSON.Feature; /** - * Takes one or more features and calculates the centroid using the arithmetic mean of all vertices. This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons. + * Takes one or more features and calculates the centroid using the arithmetic mean of all vertices. + * This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons. * @param features Input features * @returns The centroid of the input features */ function centroid(features: GeoJSON.Feature | GeoJSON.FeatureCollection): GeoJSON.Feature; /** - * Takes a Point and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers; and bearing in degrees. This uses the Haversine formula to account for global curvature. + * Takes a Point and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers; and bearing in degrees. + * This uses the Haversine formula to account for global curvature. * @param start Starting point * @param distance Distance from the starting point * @param bearing Ranging from -180 and 180 - * @param units 'miles', 'feet', 'kilometers', 'meters', or 'degrees' + * @param units 'miles', 'kilometers', 'radians', or 'degrees' * @returns Destination point */ function destination(start: GeoJSON.Feature, distance: number, bearing: number, units: string): GeoJSON.Feature; /** - * Calculates the distance between two points in degress, radians, miles, or kilometers. This uses the Haversine formula to account for global curvature. + * Calculates the distance between two points in degress, radians, miles, or kilometers. + * This uses the Haversine formula to account for global curvature. * @param from Origin point * @param to Destination point - * @param [units=kilometers] 'miles', 'feet', 'kilometers', 'meters', or 'degrees' + * @param [units=kilometers] 'miles', 'kilometers', 'radians', or 'degrees' * @returns Distance between the two points */ function distance(from: GeoJSON.Feature, to: GeoJSON.Feature, units?: string): number; @@ -183,7 +187,7 @@ declare module turf { /** * Takes a line and measures its length in the specified units. * @param line Line to measure - * @param units 'miles', 'feet', 'kilometers', 'meters', or 'degrees' + * @param units 'miles', 'kilometers', 'radians', or 'degrees' * @returns Length of the input line */ function lineDistance(line: GeoJSON.Feature, units: string): number; @@ -197,7 +201,8 @@ declare module turf { function midpoint(pt1: GeoJSON.Feature, pt2: GeoJSON.Feature): GeoJSON.Feature; /** - * Takes a feature and returns a Point guaranteed to be on the surface of the feature. Given a Polygon, the point will be in the area of the polygon. Given a LineString, the point will be along the string. Given a Point, the point will the same as the input. + * Takes a feature and returns a Point guaranteed to be on the surface of the feature. Given a Polygon, the point will be in the area of the polygon. + * Given a LineString, the point will be along the string. Given a Point, the point will the same as the input. * @param input Any feature or set of features * @returns A point on the surface of input */ @@ -223,7 +228,8 @@ declare module turf { ////////////////////////////////////////////////////// /** - * Takes a line and returns a curved version by applying a Bezier spline algorithm. The bezier spline implementation is by Leszek Rybicki. + * Takes a line and returns a curved version by applying a Bezier spline algorithm. + * The bezier spline implementation is by Leszek Rybicki. * @param line Input LineString * @param [resolution=10000] Time in milliseconds between points * @param [sharpness=0.85] A measure of how curvy the path should be between splines @@ -235,7 +241,7 @@ declare module turf { * Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees. * @param feature Input to be buffered * @param distance Distance to draw the buffer - * @param units 'miles', 'feet', 'kilometers', 'meters', or 'degrees' + * @param units 'miles', 'kilometers', 'radians', or 'degrees' * @returns Buffered features */ function buffer(feature: GeoJSON.Feature | GeoJSON.FeatureCollection, distance: number, units: string): GeoJSON.Feature | GeoJSON.FeatureCollection; @@ -254,7 +260,7 @@ declare module turf { * @param input Input points * @returns A convex hull */ - function convex(points: GeoJSON.FeatureCollection): GeoJSON.Feature; + function convex(input: GeoJSON.FeatureCollection): GeoJSON.Feature; /** * Finds the difference between two polygons by clipping the second polygon from the first. @@ -265,22 +271,27 @@ declare module turf { function difference(poly1: GeoJSON.Feature, poly2: GeoJSON.Feature): GeoJSON.Feature; /** - * Takes two polygons and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. + * Takes two polygons and finds their intersection. + * If they share a border, returns the border; if they don't intersect, returns undefined. * @param poly1 The first polygon * @param poly2 The second polygon - * @returns If poly1 and poly2 overlap, returns a Polygon feature representing the area they overlap; if poly1 and poly2 do not overlap, returns undefined; if poly1 and poly2 share a border, a MultiLineString of the locations where their borders are shared + * @returns If poly1 and poly2 overlap, returns a Polygon feature representing the area they overlap; + * if poly1 and poly2 do not overlap, returns undefined; + * if poly1 and poly2 share a border, a MultiLineString of the locations where their borders are shared */ function intersect(poly1: GeoJSON.Feature, poly2: GeoJSON.Feature): GeoJSON.Feature; /** - * Takes a set of polygons and returns a single merged polygon feature. If the input polygon features are not contiguous, this function returns a MultiPolygon feature. + * Takes a set of polygons and returns a single merged polygon feature. + * If the input polygon features are not contiguous, this function returns a MultiPolygon feature. * @param fc Input polygons * @returns Merged polygon or multipolygon */ function merge(fc: GeoJSON.FeatureCollection): GeoJSON.Feature; /** - * Takes a LineString or Polygon and returns a simplified version. Internally uses simplify-js to perform simplification. + * Takes a LineString or Polygon and returns a simplified version. + * Internally uses simplify-js to perform simplification. * @param feature Feature to be simplified * @param tolerance Simplification tolerance * @param highQuality Whether or not to spend more time to create a higher-quality simplification with a different algorithm @@ -289,7 +300,8 @@ declare module turf { function simplify(feature: GeoJSON.Feature | GeoJSON.FeatureCollection | GeoJSON.GeometryCollection, tolerance: number, highQuality: boolean): GeoJSON.Feature | GeoJSON.FeatureCollection | GeoJSON.GeometryCollection; /** - * Takes two polygons and returns a combined polygon. If the input polygons are not contiguous, this function returns a MultiPolygon feature. + * Takes two polygons and returns a combined polygon. + * If the input polygons are not contiguous, this function returns a MultiPolygon feature. * @param poly1 Input polygon * @param poly2 Another input polygon * @returns A combined Polygon or MultiPolygon feature @@ -446,7 +458,8 @@ declare module turf { function isolines(points: GeoJSON.FeatureCollection, z: string, resolution: number, breaks: Array): GeoJSON.FeatureCollection; /** - * Takes a triangular plane as a Polygon and a Point within that triangle and returns the z-value at that point. The Polygon needs to have properties a, b, and c that define the values at its three corners. + * Takes a triangular plane as a Polygon and a Point within that triangle and returns the z-value at that point. + * The Polygon needs to have properties a, b, and c that define the values at its three corners. * @param interpolatedPoint The Point for which a z-value will be calculated * @param triangle A Polygon feature with three vertices * @returns The z-value for interpolatedPoint @@ -495,7 +508,8 @@ declare module turf { ////////////////////////////////////////////////////// /** - * Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon. The polygon can be convex or concave. The function accounts for holes. + * Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon. + * The polygon can be convex or concave. The function accounts for holes. * @param point Input point * @param polygon Input polygon or multipolygon * @returns true if the Point is inside the Polygon; false if the Point is not inside the Polygon From 8888d55a20ff2c5db1a575a98196f3e926898808 Mon Sep 17 00:00:00 2001 From: gcroteau Date: Tue, 1 Dec 2015 10:04:25 -0500 Subject: [PATCH 15/15] Renamed turf/turf-test.ts to turf/turf-tests.ts --- turf/{turf-test.ts => turf-tests.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename turf/{turf-test.ts => turf-tests.ts} (100%) diff --git a/turf/turf-test.ts b/turf/turf-tests.ts similarity index 100% rename from turf/turf-test.ts rename to turf/turf-tests.ts