dartclub / turf_dart

A turf.js-like geospatial analysis library working with GeoJSON, written in pure Dart.
https://pub.dev/packages/turf
MIT License
62 stars 28 forks source link

LineString with 1 point causes ! on null #154

Open leiflinse-trivector opened 5 months ago

leiflinse-trivector commented 5 months ago

Steps to reproduce

Add this to test/components/nearest_point_on_line_test.dart:

  test('nearest_point_on_line -- linestring with one point', () {
    final start = Point(coordinates: Position.of([-122.457175, 37.720033]));
    final line = LineString(coordinates: [
      start.coordinates,
    ]);

    final snapped = nearestPointOnLine(line, start);

    expect(snapped.geometry, start);
    expect(snapped.properties!['dist'], 0);
  });

Then run dart test:

 test\components\nearest_point_on_line_test.dart: nearest_point_on_line -- linestring with one point [E]
  Null check operator used on a null value
  package:turf/src/nearest_point_on_line.dart 151:17      _nearestPointOnLine
  package:turf/src/nearest_point_on_line.dart 206:10      nearestPointOnLine
  test\components\nearest_point_on_line_test.dart 234:21  main.<fn>

Comparing to turf.js

npm install @turf/turf

Create index.js:

const turf = require('@turf/turf');

var line = turf.lineString([[-24, 43]]);
console.log(line);

node index.js:

C:\..\turf\node_modules\@turf\helpers\dist\js\index.js:294
        throw new Error("coordinates must be an array of two or more positions");
        ^

Error: coordinates must be an array of two or more positions
    at Object.lineString (_C:\.._\turf\node_modules\@turf\helpers\dist\js\index.js:294:15)
    at Object.<anonymous> (C:\..\turf\index.js:3:17)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.11.0

Expected / discussion

I expect that either LineString reject creating lines with less than 2 coordinates or that methods like nearset_point_on_line throws or return null.

In turf.js this is rejected when the LineString is created and there is a comment next to the line in turf_dart where it fails saying that LineString should have at least two coordinates.