如何订购路线的状态列表

时间:2012-12-11 16:35:19

标签: gis

我正在尝试按字母顺序列出我目前获得的状态列表,并按路线顺序对它们进行排序。

作为一个例子:一条路线在科罗拉多州开始,在华盛顿结束,我知道它通过俄勒冈州,爱达荷州,犹他州和怀俄明州(例如http://goo.gl/maps/j6tyu

按字母顺序排列的路线是(CO,ID,OR,UT,WA,WY),路线顺序为(CO,WY,UT,ID,OR,WA)

是否可以通过了解哪些状态触及哪些其他状态或我需要具有哪些其他信息来计算路线顺序?是否有建议的阅读链接可以帮助我

如果有帮助,我实际上也会在每个州都行驶里程。 ( CO = 90 ,ID = 275 ,OR = 372 ,UT = 149 ,WA = 8 ,WY = 368 )

1 个答案:

答案 0 :(得分:1)

由于这是一个有趣的问题,我只是为了好玩而写了一个小的JS脚本:http://jsfiddle.net/Ufbbt/

var from = 'CO';
var to = 'WY';
var thru = ['ID', 'OR', 'UT', 'WA'];

var globalCount = 0;

var doStuff = function(currState, states, str) {

    if (states.length == 0) {
        if (inArray(to, statesDesc[currState])) {
            console.log(str + '->' + to);
            globalCount++;
        }
        return;
    }

    for (var i = 0, len = states.length; i < len; i++) {
        if (inArray(states[i], statesDesc[currState])) {
            var newStates = states.slice(0); // clone original array
            var newCurrState = newStates.splice(i, 1)[0];
            doStuff(newCurrState, newStates, str + '->' + newCurrState);
        }
    }
}

var inArray = function(what, where) {
    for (var i = 0, len = where.length; i < len; i++) {
        if (what == where[i]) 
            return true;
    }
    return false;   
}

// Now, initial execute
doStuff(from, thru, from);
console.log(globalCount + ' route(s) found');

var statesDesc = {
    AK: ['WA'],
    AL: ['TN','GA','FL','MS'],
    AR: ['MO','TN','MS','LA','TX','OK'],
    AZ: ['UT','CO','NM','CA','NV'],
    CA: ['OR','NV','AZ','HI'],
    CO: ['WY','NE','KS','OK','NM','AZ','UT'],
    CT: ['MA','RI','NY'],
    DC: ['MD','VA'],
    DE: ['PA','NJ','MD'],
    FL: ['GA','AL'],
    GA: ['NC','SC','FL','AL','TN'],
    HI: ['CA'],
    IA: ['MN','WI','IL','MO','NE','SD'],
    ID: ['MT','WY','UT','NV','OR','WA'],
    IL: ['WI','IN','KY','MO','IA'],
    IN: ['MI','OH','KY','IL'],
    KS: ['NE','MO','OK','CO'],
    KY: ['OH','WV','VA','TN','MO','IL','IN'],
    LA: ['AR','MS','TX'],
    MA: ['NH','RI','CT','NY','VT'],
    MD: ['PA','DE','DC','VA','WV'],
    ME: ['NH'],
    MI: ['OH','IN','WI'],
    MN: ['WI','IA','SD','ND'],
    MO: ['IA','IL','KY','TN','AR','OK','KS','NE'],
    MS: ['TN','AL','LA','AR'],
    MT: ['ND','SD','WY','ID'],
    NC: ['VA','SC','GA','TN'],
    ND: ['MN','SD','MT'],
    NE: ['SD','IA','MO','KS','CO','WY'],
    NH: ['ME','MA','VT'],
    NJ: ['NY','DE','PA'],
    NM: ['CO','OK','TX','AZ','UT'],
    NV: ['ID','UT','AZ','CA','OR'],
    NY: ['VT','MA','CT','NJ','PA'],
    OH: ['PA','WV','KY','IN','MI'],
    OK: ['KS','MO','AR','TX','NM','CO'],
    OR: ['WA','ID','NV','CA'],
    PA: ['NY','NJ','DE','MD','WV','OH'],
    RI: ['MA','CT'],
    SC: ['NC','GA'],
    SD: ['ND','MN','IA','NE','WY','MT'],
    TN: ['KY','VA','NC','GA','AL','MS','AR','MO'],
    TX: ['OK','AR','LA','NM'],
    UT: ['ID','WY','CO','NM','AZ','NV'],
    VA: ['MD','DC','NC','TN','KY','WV'],
    VT: ['NH','MA','NY'],
    WA: ['AK','ID','OR'],
    WI: ['MI','IL','IA','MN'],
    WV: ['PA','MD','VA','KY','OH'],
    WY: ['MT','SD','NE','CO','UT','ID']
}