turf icon indicating copy to clipboard operation
turf copied to clipboard

Error: coordinates must contain numbers

Open Felankia opened this issue 11 months ago • 11 comments

Version: 7.2.0

Code:

let line = {}; //GeoJSON contant in [https://github.com/user-attachments/files/18392267/geojson.txt]
line = turf.cleanCoords(line);
let grid = turf.polygon([[[105.82001,38.30005],[105.87001,38.30005],[105.87001,38.350049999999996],[105.82001,38.350049999999996],[105.82001,38.30005]]])
turf.lineSplit(line, grid);

Stacktrace:

Error: coordinates must contain numbers
    at point (../node_modules/@turf/helpers/dist/cjs/index.cjs:78:11)
    at ../node_modules/@turf/nearest-point-on-line/dist/cjs/index.cjs:69:40
    at../node_modules/@turf/meta/dist/cjs/index.cjs:255:13
    at geomEach (../node_modules/@turf/meta/dist/cjs/index.cjs:197:15)
    at flattenEach (../node_modules/@turf/meta/dist/cjs/index.cjs:248:3)
    at nearestPointOnLine (../node_modules/@turf/nearest-point-on-line/dist/cjs/index.cjs:42:21)
    at ../node_modules/@turf/turf/node_modules/@turf/line-split/dist/cjs/index.cjs:112:53
    at featureEach (../node_modules/@turf/meta/dist/cjs/index.cjs:148:11)
    at findClosestFeature (../node_modules/@turf/turf/node_modules/@turf/line-split/dist/cjs/index.cjs:111:21)
    at ../node_modules/@turf/turf/node_modules/@turf/line-split/dist/cjs/index.cjs:56:27

Felankia avatar Jan 13 '25 02:01 Felankia

Same problem here with the "nearestPointOnLine" function.

petrot avatar Jan 13 '25 08:01 petrot

I discovered that the problem is related to a line with two consecutive equal coordinates

paulomesquita-wsn avatar Jan 13 '25 22:01 paulomesquita-wsn

In case it helps, I've run into a similar issue. I thought it might have to do with the consecutive equal coordinates as well, but if I remove any of the coordinates in this sample file, the problem resolves itself. https://jsfiddle.net/nd48h0r7/

osajus avatar Jan 14 '25 03:01 osajus

Likely to be closely related to #2807

smallsaucepan avatar Jan 22 '25 04:01 smallsaucepan

Same issue for me recently, and I arrived at the same conclusion as @paulomesquita-wsn - this is what happens when two adjacent coords are identical.

Maybe there's a good reason for the function to require deduplicated coords on the input, but in such case it should print a clearer error message, and document this requirement.

Duct-tape-fixed on our end by sanitizing the input geojsons to filter out duplicate coords.

maciejgoscinski-latrace avatar Jan 27 '25 15:01 maciejgoscinski-latrace

I also just experienced this. The last two points in my line string were duplicates. The error message is incredibly unhelpful. I used turf.cleanCoords as recommended in this thread and that resolved the issue for me.

colinperepelken avatar Jan 27 '25 23:01 colinperepelken

Thanks @colinperepelken. What would make the error more helpful to you? Including the value of the invalid coordinate in the message?

smallsaucepan avatar Jan 27 '25 23:01 smallsaucepan

@smallsaucepan Currently the error is "Error: coordinates must contain numbers", which is confusing because when I checked all of my input coordinates to nearestPointOnLine did contain numbers.

Something like "Error: coordinates contain duplicates, use cleanCoords to remove them". Or better yet, can this just not throw an error, or remove the duplicates automatically?

colinperepelken avatar Jan 27 '25 23:01 colinperepelken

Or better yet, can this just not throw an error, or remove the duplicates automatically?

Oh definitely, we'll fix the condition that caused the original problem. The error message that pops up is thrown in a lower level function though, so it's not able to tell why the coordinates weren't numbers.

Will see what we can do to improve both aspects 👍

smallsaucepan avatar Jan 28 '25 03:01 smallsaucepan

Is it possible to add a condition for the following method that if start and end are the same just return one of them: https://github.com/Turfjs/turf/blob/e6d540a4725950f17caeedb89c60ce3793206e63/packages/turf-nearest-point-on-line/index.ts#L192

I think it will resolve some of the issues that were introduced as part of 7.2 and also I think it's correct. The current method returns [NaN, NaN], which in turn is causing this exception I believe.

HarelM avatar Mar 02 '25 12:03 HarelM

Also ran into this problem today, had to fix it by wrapping my feature with cleanCoords before passing it in to lineSplit to fix the duplicate coordinate issue.

Small snippet of my code for when anyone else runs into this:

  const splitLines = lineSplit(
    cleanCoords(feature, { mutate: false }),
    polygon
  );

luc-tielen avatar Oct 22 '25 12:10 luc-tielen