perliedman / geojson-path-finder

Find shortest path through a network of GeoJSON
https://www.liedman.net/geojson-path-finder/
ISC License
300 stars 86 forks source link

wrong way find. #66

Closed duanshuai007 closed 1 year ago

duanshuai007 commented 4 years ago

hello,i create a geojson map file,and i want get the a->b shortest path,

issues01

but i always get the result like this.

image

my test code:

var PathFinder = require('../'),
    //geojson = require('./network.json'),
    geojson = require('./shenyang1.json'),
    test = require('tap').test,
    point = require('turf-point');
    //distance = require('@turf/distance').default;

let fs = require('fs');
let options = { 
    flags: 'a',     // append模式
    encoding: 'utf8',  // utf8编码
};
let stdout = fs.createWriteStream('./stdout.log', options);
let stderr = fs.createWriteStream('./stderr.log', options);
let logger = new console.Console(stdout, stderr);

test('can find path (complex)', function(t) {
    var pathfinder = new PathFinder(geojson, 
        //path = pathfinder.findPath(point([8.44460166, 59.48947469]), point([8.44651, 59.513920000000006]));
        //path = pathfinder.findPath(point([8.45163096, 59.49259688]), point([8.47169688, 59.50405761]));
        //path = pathfinder.findPath(point([8.45163096, 59.49259688]), point([8.46983216, 59.49492301]));
        //path = pathfinder.findPath(point([123.42682600021364, 41.79379242995082]), point([123.43067765235901, 41.790744790121266]));
        {   
            weightFn: function(a, b) {
                var dx = a[0] - b[0];
                var dy = a[1] - b[1];
                return Math.sqrt(dx * dx + dy * dy);
            },  
            precision:1
        }), 
        //path = pathfinder.findPath(point([11, 13]), point([0, 0]));
        path = pathfinder.findPath(point([0, 0]), point([15, 12]));

        /*  
    var arr = new Array();
    for (var i = 0; i < path.path.length; i++)
        arr.push(path.path[i]);
    console.log(arr.join('], ['));
    */
    console.log(path.path)
    logger.log(path.path);
    console.log(path.weight)
    t.ok(path, 'has path');
    t.ok(path.path, 'path has vertices');
    t.ok(path.weight, 'path has a weight');
    //t.equal(path.path.length, 220, 'path has expected length');
    //t.ok(Math.abs(path.weight - 6.3751) < 5e-5, 'path has expected weight');
    t.end();
});

my geojson file :

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 1,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [0, 0], [12, 0]
        ]
      }
    },
    {
      "type": "Feature",
      "id": 2,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [12, 0], [12, 3]
        ]
      }
    },
    {
      "type": "Feature",
      "id": 3,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [12, 3], [3, 3]
        ]
      }
    },
        {
      "type": "Feature",
          "id": 4,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [3, 3], [3, 6]
        ]
      }
    },
        {
      "type": "Feature",
          "id": 5,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [3, 6], [7, 6]
        ]
      }
    },
        {
      "type": "Feature",
          "id": 6,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [7, 6], [7, 12]
        ]
      }
    },
    {
      "type": "Feature",
      "id": 7,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [7, 12], [3, 14]
        ]
      }
    },
        {
      "type": "Feature",
"id": 8,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [0, 0], [1, 2]
        ]
      }
    },
        {
      "type": "Feature",
          "id": 9,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [1, 2], [3, 3]
        ]
      }
    },
        {
      "type": "Feature",
          "id": 10,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [9, 6], [12, 3]
        ]
      }
    },
    {
      "type": "Feature",
      "id": 13,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [7, 12], [15, 12]
        ]
      }
    },
        {
      "type": "Feature",
          "id": 11,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
           [7, 6], [9, 6]
        ]

      }
    },
        {
      "type": "Feature",
          "id": 12,
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [9, 6], [15, 12]
        ]
      }
    }
  ]
}

i want to know where i am wrong. thank you.

duanshuai007 commented 4 years ago

hello , i find in compactor.js

function compactGraph(vertices, vertexCoords, edgeData, options) {
    options = options || {}; 
    var progress = options.progress;
    var ends = Object.keys(vertices).reduce(function findEnds(es, k, i, vs) {
        var vertex = vertices[k];
        var edges = Object.keys(vertex);
        var numberEdges = edges.length;
        var remove;

        console.log("edges = " + numberEdges);
        if (numberEdges === 1) {
            var other = vertices[edges[0]];
            remove = !other[k];
        } else if (numberEdges === 2) {
            remove = edges.filter(function(n) {
                return vertices[n][k];
            }).length === numberEdges;
        } else {
            remove = false;
        }   

if i modify like this

        if (numberEdges === 1) {
            var other = vertices[edges[0]];
            remove = !other[k];
        } else if (numberEdges === 2) {
           // remove = edges.filter(function(n) {
           //     return vertices[n][k];
           // }).length === numberEdges;
        } else {

i will find the shortest way right. but i can't understand the code do what.

can i delete this code? it will generate a big mistake? thank you .

nickw1 commented 4 years ago

Hi @duanshuai007,

This I think is related to issue #27 and pull request #64.

Try my fork, which incorporates the pull request: https://github.com/nickw1/geojson-path-finder

Does that work for you?

perliedman commented 1 year ago

Thanks for this info, guys, and sorry I did not get to this sooner.

As part of the 2.0 update, I added a test for the test case provided above (with slight modifications, hope I got it right). This test now passes, presumably since #27 was merged.