越来越接近美国各州(php)

时间:2010-03-13 18:02:33

标签: php geocoding

HI需要能够选择状态并获得边界状态列表。有没有人知道或已经为此做过代码?

如果我必须自己编写,那么最好的方法是什么 - 可能是边框的mysql数据库(每行两个状态列),然后选择匹配?

编辑:这是一个开始 - 边框列表:http://www.econ.umn.edu/~holmes/data/BORDLIST.html

5 个答案:

答案 0 :(得分:6)

<?php
$borders = array (
  'AL' => 
  array (
    0 => 'FL',
    1 => 'GA',
    2 => 'MS',
    3 => 'TN',
  ),
  'FL' => 
  array (
    0 => 'AL',
    1 => 'GA',
  ),
  'GA' => 
  array (
    0 => 'AL',
    1 => 'FL',
    2 => 'NC',
    3 => 'SC',
    4 => 'TN',
  ),
  'MS' => 
  array (
    0 => 'AL',
    1 => 'AR',
    2 => 'LA',
    3 => 'TN',
  ),
  'TN' => 
  array (
    0 => 'AL',
    1 => 'AR',
    2 => 'GA',
    3 => 'KY',
    4 => 'MO',
    5 => 'MS',
    6 => 'NC',
    7 => 'VA',
  ),
  'AR' => 
  array (
    0 => 'LA',
    1 => 'MO',
    2 => 'MS',
    3 => 'OK',
    4 => 'TN',
    5 => 'TX',
  ),
  'LA' => 
  array (
    0 => 'AR',
    1 => 'MS',
    2 => 'TX',
  ),
  'MO' => 
  array (
    0 => 'AR',
    1 => 'IA',
    2 => 'IL',
    3 => 'KS',
    4 => 'KY',
    5 => 'NE',
    6 => 'OK',
    7 => 'TN',
  ),
  'OK' => 
  array (
    0 => 'AR',
    1 => 'CO',
    2 => 'KS',
    3 => 'MO',
    4 => 'NM',
    5 => 'TX',
  ),
  'TX' => 
  array (
    0 => 'AR',
    1 => 'LA',
    2 => 'NM',
    3 => 'OK',
  ),
  'AZ' => 
  array (
    0 => 'CA',
    1 => 'CO',
    2 => 'NM',
    3 => 'NV',
    4 => 'UT',
  ),
  'CA' => 
  array (
    0 => 'AZ',
    1 => 'NV',
    2 => 'OR',
  ),
  'CO' => 
  array (
    0 => 'AZ',
    1 => 'KS',
    2 => 'NE',
    3 => 'NM',
    4 => 'OK',
    5 => 'UT',
    6 => 'WY',
  ),
  'NM' => 
  array (
    0 => 'AZ',
    1 => 'CO',
    2 => 'OK',
    3 => 'TX',
    4 => 'UT',
  ),
  'NV' => 
  array (
    0 => 'AZ',
    1 => 'CA',
    2 => 'ID',
    3 => 'OR',
    4 => 'UT',
  ),
  'UT' => 
  array (
    0 => 'AZ',
    1 => 'CO',
    2 => 'ID',
    3 => 'NM',
    4 => 'NV',
    5 => 'WY',
  ),
  'OR' => 
  array (
    0 => 'CA',
    1 => 'ID',
    2 => 'NV',
    3 => 'WA',
  ),
  'KS' => 
  array (
    0 => 'CO',
    1 => 'MO',
    2 => 'NE',
    3 => 'OK',
  ),
  'NE' => 
  array (
    0 => 'CO',
    1 => 'IA',
    2 => 'KS',
    3 => 'MO',
    4 => 'SD',
    5 => 'WY',
  ),
  'WY' => 
  array (
    0 => 'CO',
    1 => 'ID',
    2 => 'MT',
    3 => 'NE',
    4 => 'SD',
    5 => 'UT',
  ),
  'CT' => 
  array (
    0 => 'MA',
    1 => 'NY',
    2 => 'RI',
  ),
  'MA' => 
  array (
    0 => 'CT',
    1 => 'NH',
    2 => 'NY',
    3 => 'RI',
    4 => 'VT',
  ),
  'NY' => 
  array (
    0 => 'CT',
    1 => 'MA',
    2 => 'NJ',
    3 => 'PA',
    4 => 'VT',
  ),
  'RI' => 
  array (
    0 => 'CT',
    1 => 'MA',
  ),
  'DC' => 
  array (
    0 => 'MD',
    1 => 'VA',
  ),
  'MD' => 
  array (
    0 => 'DC',
    1 => 'DE',
    2 => 'PA',
    3 => 'VA',
    4 => 'WV',
  ),
  'VA' => 
  array (
    0 => 'DC',
    1 => 'KY',
    2 => 'MD',
    3 => 'NC',
    4 => 'TN',
    5 => 'WV',
  ),
  'DE' => 
  array (
    0 => 'MD',
    1 => 'NJ',
    2 => 'PA',
  ),
  'NJ' => 
  array (
    0 => 'DE',
    1 => 'NY',
    2 => 'PA',
  ),
  'PA' => 
  array (
    0 => 'DE',
    1 => 'MD',
    2 => 'NJ',
    3 => 'NY',
    4 => 'OH',
    5 => 'WV',
  ),
  'NC' => 
  array (
    0 => 'GA',
    1 => 'SC',
    2 => 'TN',
    3 => 'VA',
  ),
  'SC' => 
  array (
    0 => 'GA',
    1 => 'NC',
  ),
  'IA' => 
  array (
    0 => 'MN',
    1 => 'MO',
    2 => 'NE',
    3 => 'SD',
    4 => 'WI',
    5 => 'IL',
  ),
  'MN' => 
  array (
    0 => 'IA',
    1 => 'ND',
    2 => 'SD',
    3 => 'WI',
  ),
  'SD' => 
  array (
    0 => 'IA',
    1 => 'MN',
    2 => 'MT',
    3 => 'ND',
    4 => 'NE',
    5 => 'WY',
  ),
  'WI' => 
  array (
    0 => 'IA',
    1 => 'IL',
    2 => 'MI',
    3 => 'MN',
  ),
  'ID' => 
  array (
    0 => 'MT',
    1 => 'NV',
    2 => 'OR',
    3 => 'UT',
    4 => 'WA',
    5 => 'WY',
  ),
  'MT' => 
  array (
    0 => 'ID',
    1 => 'ND',
    2 => 'SD',
    3 => 'WY',
  ),
  'WA' => 
  array (
    0 => 'ID',
    1 => 'OR',
  ),
  'IL' => 
  array (
    0 => 'IA',
    1 => 'IN',
    2 => 'KY',
    3 => 'MO',
    4 => 'WI',
  ),
  'IN' => 
  array (
    0 => 'IL',
    1 => 'KY',
    2 => 'MI',
    3 => 'OH',
  ),
  'KY' => 
  array (
    0 => 'IL',
    1 => 'IN',
    2 => 'MO',
    3 => 'OH',
    4 => 'TN',
    5 => 'VA',
    6 => 'WV',
  ),
  'MI' => 
  array (
    0 => 'IN',
    1 => 'OH',
    2 => 'WI',
  ),
  'OH' => 
  array (
    0 => 'IN',
    1 => 'KY',
    2 => 'MI',
    3 => 'PA',
    4 => 'WV',
  ),
  'WV' => 
  array (
    0 => 'KY',
    1 => 'MD',
    2 => 'OH',
    3 => 'PA',
    4 => 'VA',
  ),
  'NH' => 
  array (
    0 => 'MA',
    1 => 'ME',
    2 => 'VT',
  ),
  'VT' => 
  array (
    0 => 'MA',
    1 => 'NH',
    2 => 'NY',
  ),
  'ME' => 
  array (
    0 => 'NH',
  ),
  'ND' => 
  array (
    0 => 'MN',
    1 => 'MT',
    2 => 'SD',
  ),
);

echo "Illinois borders: \n";
foreach ($borders['IL'] as $s)
  echo "\t$s\n";

答案 1 :(得分:1)

与许多组合数据集一样,这个数据集有时间/空间权衡。

如果你使用A isNextTo B关系,你有一个在对角线上折叠的关系,即B也是NextTo A.

因此,完整的枚举量是它需要的两倍。

但是,如果只使用关系的一半,则必须双向搜索:A isNextTo X或X isNextT A。

将其保存在SQL数据库中会使存储再次翻倍,因为搜索数据所需的索引将与数据大小相同,那么为什么要这么麻烦呢?

PHP没有本地b-tree所以也许PhpClasses.org的btree类会引起人们的兴趣所以你只需要加载一次数据。

答案 2 :(得分:0)

根据您提供的列表,我会说它已经非常简单了。我会将列表转换为PHP数组,如下所示:

$borders = 
 array("AL-FL",
       "AL-GA",
 ...
 );

然后,查询函数的原始(但可行)示例:

/**
 * @desc Returns an array with the bordering states for a state. 
 * @param string the 2-letter code of the state. Case insensitive. Example: AK
 * @param array the list of borders.
 * @return an array with the 2-letter codes of all neighboring states.
 */

function getBorderingStates($state, $borders)
 {
   $neighbors = array(); 
   $state = strtolower($state);

   foreach ($borders as $border)
    {
      $pair = explode("-", $border)
      if (strtolower($pair[0]) == $state) 
        array_push($neighbors, $pair[1]);

      if (strtolower($pair[1]) == $state) 
        array_push($neighbors, $pair[0]);

     }
     return $neighbors;

 }

答案 3 :(得分:0)

你不必自己写。雅虎有一个很好的地理定位api叫做WOEID,它不仅可以记录位置和一般的地理数据,还可以记录关系; http://developer.yahoo.com/geo/geoplanet/guide/concepts.html#relationships

答案 4 :(得分:0)

我在MySQL DB(DOWNLOAD HERE)中将其全部映射出来。它有lat和long以及州名和缩写。

这将是一个如何查询使用此表按州,缩写和邻居查询的示例:

#Find state by abbrev
SELECT * FROM states WHERE abbrev = "WA" LIMIT 1;

#Find state by name
SELECT * FROM states WHERE name = "Washington" LIMIT 1;

#Find neighboring states simple
SELECT * FROM states WHERE parent_id = 55;

#Find neighboring states expanded
SELECT * FROM states WHERE parent_id = (SELECT id FROM states WHERE `name` = "West Virginia" LIMIT 1);

*请注意,此处使用的相邻数据来自其他帖子的数组数据。