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

turf.featureCollection() cannot Determine whether Parameter isn't Array #751

Closed
brianbancroft opened this issue May 21, 2017 · 2 comments
Closed
Assignees
Labels
Milestone

Comments

@brianbancroft
Copy link

Hi there,

I've found a bit of an issue with your turf.featureCollection() function with regards to inputs, and I'd like to share as it might be a heartache for beginners of your library. It was for me.

Setup

I'm currently trying determine the number of points within a radius of another point in a node environment where I chain a turf.featureCollection() to a turf.buffer() function. When I do this, my output is not compliant with the geojson spec, and any attempt at carrying out the turf.within() function gets me zero, even though one of the compare points is the point used to generate the buffer. Here is some setup, if someone wishes to replicate:

  • In my package.json file, the version I'm using is current, I believe. `"@turf/turf": "^4.1.0"
  • Here is a single point.

The remainder of this example takes place in the node repl. Go to a node project with turf.
From the shell:

mkdir jsontest && cd jsontest
yarn init
(...)
yarn add @turf/turf
node
const turf = require('@turf/turf')
const point = {"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[-79.419281,43.795368]}}
// using a silly buffer for now
const buffer = turf.buffer(point, 1000, 'kilometers')
const bufferCollection = turf.featuresCollection(buffer)

Here is the output.

The actual problem

When I look at the docs for featureCollection, I find that the desired input is an array, but in my case, I'm laying down an object. But instead of offering a warning or throwing an error, It just carries on producing faulty geoJSON without warning.

Possible solution

The solution may be as easy as either forcing an array if the parameter is a geojson feature, or throwing an error in line 150 if the parameter isn't array. Would using the Array.isArray(arr) fit the bill as a condition to throw an exception? Just in case, here's a compatability table for the JS method.

Thanks for building and maintaining such a lovely library! If you think this is a solution to the problem I have, I would love to contribute.

@brianbancroft brianbancroft changed the title turf.featureCollection() cannot determine if parameter isn't array turf.featureCollection() cannot Determine whether Parameter isn't Array May 21, 2017
@dpmcmlxxvi
Copy link
Collaborator

Why not just call turf.featureCollection with an array as the documentation describes?

const bufferCollection = turf.featuresCollection([buffer])

While there could be an additional validation performed but IMHO I think a function building a collection would clearly expect an array as input and not a single object. Maybe others could chime in and if there's enough support it could be added.

@DenisCarriere
Copy link
Member

👍 @brianbancroft Thanks for reporting this, I've also ran into this problem when I would accidentally add a FeatureCollection with another FeatureCollection.

Added input validation

if (!Array.isArray(features)) throw new Error('features must be an Array');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants