Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add negative buffer tests to @turf/buffer #739

Merged
merged 4 commits into from
May 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions packages/turf-buffer/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/// <reference types="geojson" />

import {
Units,
Feature, Features,
Point, Points, MultiPoint, MultiPoints,
LineString, LineStrings, MultiLineString, MultiLineStrings,
Polygon, Polygons, MultiPolygon, MultiPolygons} from '@turf/helpers';
import {Units, FeatureGeometryCollection} from '@turf/helpers';

type Point = GeoJSON.Point;
type LineString = GeoJSON.LineString;
type Polygon = GeoJSON.Polygon;
type MultiPoint = GeoJSON.MultiPoint;
type MultiLineString = GeoJSON.MultiLineString;
type MultiPolygon = GeoJSON.MultiPolygon;
type GeometryObject = GeoJSON.GeometryObject;
type GeometryCollection = GeoJSON.GeometryCollection;
type Feature<Geom extends GeometryObject> = GeoJSON.Feature<Geom>;
type FeatureCollection<Geom extends GeometryObject> = GeoJSON.FeatureCollection<Geom>;
type Geoms = Point|LineString|Polygon|MultiPoint|MultiLineString|MultiPolygon;

interface Buffer {
/**
* http://turfjs.org/docs/#buffer
*/
(feature: Point | LineString | Polygon, radius?: number, unit?: Units, steps?: number): Polygon;
(feature: Points | LineStrings | Polygons | MultiPoint | MultiPoints, radius?: number, unit?: Units, steps?: number): Polygons;
(feature: MultiLineString | MultiPolygon, radius?: number, unit?: Units, steps?: number): MultiPolygon;
(feature: MultiLineStrings | MultiPolygons, radius?: number, unit?: Units, steps?: number): MultiPolygons;
(feature: Feature<any>, radius?: number, unit?: Units, steps?: number): Polygon | Polygons | MultiPolygon;
(feature: Features<any>, radius?: number, unit?: Units, steps?: number): Polygons | MultiPolygons;
(feature: GeoJSON.GeometryObject, radius?: number, unit?: Units, steps?: number): Polygon | Polygons | MultiPolygon;
(feature: GeoJSON.GeometryCollection, radius?: number, unit?: Units, steps?: number): Polygons | MultiPolygons;
<Geom extends Point|LineString|Polygon>(feature: Feature<Geom>|Geom, radius?: number, unit?: Units, steps?: number): Feature<Polygon>;
<Geom extends MultiPoint|MultiLineString|MultiPolygon>(feature: Feature<Geom>|Geom, radius?: number, unit?: Units, steps?: number): Feature<MultiPolygon>;
<Geom extends Point|LineString|Polygon>(feature: FeatureCollection<Geom>, radius?: number, unit?: Units, steps?: number): FeatureCollection<Polygon>;
<Geom extends MultiPoint|MultiLineString|MultiPolygon>(feature: FeatureCollection<Geom>, radius?: number, unit?: Units, steps?: number): FeatureCollection<MultiPolygon>;
(feature: FeatureCollection<any>|FeatureGeometryCollection|GeometryCollection, radius?: number, unit?: Units, steps?: number): FeatureCollection<Polygon|MultiPolygon>;
(feature: Feature<any>|GeometryObject, radius?: number, unit?: Units, steps?: number): Feature<Polygon|MultiPolygon>;
}
declare const buffer: Buffer;
declare namespace buffer {}
Expand Down
10 changes: 9 additions & 1 deletion packages/turf-buffer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ module.exports = function (geojson, radius, units, steps) {
return featureCollection(results);
case 'FeatureCollection':
featureEach(geojson, function (feature) {
results.push(buffer(feature, radius, units, steps));
featureEach(buffer(feature, radius, units, steps), function (buffered) {
results.push(buffered);
});
});
return featureCollection(results);
}
Expand All @@ -83,6 +85,12 @@ function buffer(geojson, radius, units, steps) {
switch (geometry.type) {
case 'Point':
return circle(geometry.coordinates, radius, steps, units, properties);
case 'GeometryCollection':
var results = [];
geomEach(geojson, function (geometry) {
results.push(buffer(geometry, radius, units, steps));
});
return featureCollection(results);
}

// Project GeoJSON to Transverse Mercator projection (convert to Meters)
Expand Down
34 changes: 27 additions & 7 deletions packages/turf-buffer/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const load = require('load-json-file');
const write = require('write-json-file');
const truncate = require('@turf/truncate');
const {featureEach} = require('@turf/meta');
const {featureCollection, point, polygon} = require('@turf/helpers');
const {featureCollection, point, polygon, geometryCollection} = require('@turf/helpers');
const buffer = require('./');

const directories = {
Expand All @@ -24,29 +24,39 @@ let fixtures = fs.readdirSync(directories.in).map(filename => {

test('turf-buffer', t => {
for (const {filename, name, geojson} of fixtures) {
let {radius, units, padding} = geojson.properties || {};
let {radius, units, steps} = geojson.properties || {};
radius = radius || 50;
units = units || 'miles';

const buffered = truncate(buffer(geojson, radius, units, padding));
const buffered = truncate(buffer(geojson, radius, units, steps));

// Add Results to FeatureCollection
const results = featureCollection([]);
featureEach(buffered, feature => results.features.push(feature));
featureEach(geojson, feature => results.features.push(feature));
featureEach(buffered, feature => results.features.push(colorize(feature, '#F00')));
featureEach(geojson, feature => results.features.push(colorize(feature, '#00F')));

if (process.env.REGEN) write.sync(directories.out + filename, results);
t.deepEqual(results, load.sync(directories.out + filename), name);
}
t.end();
});

// https:/Turfjs/turf/pull/736
test('turf-buffer - Support Negative Buffer', t => {
const poly = polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);

t.assert(buffer(poly, -50), 'allow negative buffer param');
t.end();
});

test('turf-buffer - Support Geometry Objects', t => {
const pt = point([61, 5]);
const poly = polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
const gc = geometryCollection([pt.geometry, poly.geometry]);

buffer(pt.geometry, 10);
buffer(poly.geometry, 10);
t.assert(buffer(gc, 10), 'support Geometry Collection');
t.assert(buffer(pt.geometry, 10), 'support Point Geometry');
t.assert(buffer(poly.geometry, 10), 'support Polygon Geometry');
t.end();
});

Expand All @@ -68,3 +78,13 @@ test('turf-buffer - Prevent Input Mutation', t => {
t.deepEqual(collection, beforeCollection, 'collection should not mutate');
t.end();
});

function colorize(feature, color = '#F00') {
if (feature.properties) {
feature.properties.stroke = color;
feature.properties.fill = color;
feature.properties['marker-color'] = color;
feature.properties['fill-opacity'] = 0.3;
}
return feature;
}
46 changes: 46 additions & 0 deletions packages/turf-buffer/test/in/negative-buffer.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"type": "Feature",
"properties": {
"radius": -200,
"units": "miles"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
133.90136718749997,
-14.647368383896618
],
[
126.73828125,
-19.352610894378625
],
[
128.49609375,
-27.371767300523032
],
[
134.1650390625,
-30.789036751261136
],
[
143.7890625,
-30.789036751261136
],
[
144.9755859375,
-26.391869671769022
],
[
144.31640625,
-17.14079039331664
],
[
133.90136718749997,
-14.647368383896618
]
]
]
}
}
42 changes: 36 additions & 6 deletions packages/turf-buffer/test/out/feature-collection-points.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"features": [
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
Expand Down Expand Up @@ -492,7 +497,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "Polygon",
"coordinates": [
Expand Down Expand Up @@ -763,7 +773,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "Polygon",
"coordinates": [
Expand Down Expand Up @@ -1034,7 +1049,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#00F",
"fill": "#00F",
"marker-color": "#00F",
"fill-opacity": 0.3
},
"geometry": {
"type": "MultiPoint",
"coordinates": [
Expand All @@ -1059,7 +1079,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#00F",
"fill": "#00F",
"marker-color": "#00F",
"fill-opacity": 0.3
},
"geometry": {
"type": "Point",
"coordinates": [
Expand All @@ -1070,7 +1095,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#00F",
"fill": "#00F",
"marker-color": "#00F",
"fill-opacity": 0.3
},
"geometry": {
"type": "Point",
"coordinates": [
Expand Down
14 changes: 12 additions & 2 deletions packages/turf-buffer/test/out/geometry-collection-points.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"features": [
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
Expand Down Expand Up @@ -420,7 +425,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "Polygon",
"coordinates": [
Expand Down
14 changes: 12 additions & 2 deletions packages/turf-buffer/test/out/linestring.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"features": [
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "Polygon",
"coordinates": [
Expand Down Expand Up @@ -250,7 +255,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#00F",
"fill": "#00F",
"marker-color": "#00F",
"fill-opacity": 0.3
},
"geometry": {
"type": "LineString",
"coordinates": [
Expand Down
14 changes: 12 additions & 2 deletions packages/turf-buffer/test/out/multi-linestring.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"features": [
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
Expand Down Expand Up @@ -776,7 +781,12 @@
},
{
"type": "Feature",
"properties": {},
"properties": {
"stroke": "#00F",
"fill": "#00F",
"marker-color": "#00F",
"fill-opacity": 0.3
},
"geometry": {
"type": "MultiLineString",
"coordinates": [
Expand Down
12 changes: 10 additions & 2 deletions packages/turf-buffer/test/out/multi-point.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
{
"type": "Feature",
"properties": {
"radius": 300
"radius": 300,
"stroke": "#F00",
"fill": "#F00",
"marker-color": "#F00",
"fill-opacity": 0.3
},
"geometry": {
"type": "MultiPolygon",
Expand Down Expand Up @@ -383,7 +387,11 @@
{
"type": "Feature",
"properties": {
"radius": 300
"radius": 300,
"stroke": "#00F",
"fill": "#00F",
"marker-color": "#00F",
"fill-opacity": 0.3
},
"geometry": {
"type": "MultiPoint",
Expand Down
Loading