Using geonames and importing location enabled nodes

Last modified: November 17, 2008 - 23:14

function xmlfromplace($name, $country, $type) {
  $str  = 'http://ws.geonames.org/search?q=';
  $str .= $name;
  if ($type == 'city') {
      $str .= '&featureClass=P&country=';
  } elseif ($type == 'province') {
      $str .= '&featureCode=ADM1&country=';
  } elseif ($type == 'country') {
      $str .= '&featureClass=A&country=';
  } else {
    $str .= '&country=';
  }
  $str .= $country;
  $str .= '&maxRows=1';
  $xml = simplexml_load_file($str);
  return $xml;
}

function insertlocation($node, $city, $postal_code, $province, $country, $xml) {
  $lat = $xml->geoname->lat;
  $lon = $xml->geoname->lng;
  $l = array();
  $l['city'] = $city;
  $l['province'] = $province;
  $l['country'] = $country;
  $l['lat'] = $lat;
  $l['lon'] = $lon;
  $l['source'] = 3;
  _location_save($l, $node, 'node');
}

Parameters for insertlocation

  • node: full node object
  • province: 2 letter abbreviation
  • country: 2 letter abbreviation
  • xml: result from xmlfromplace()

Location table

For each location enabled node that is saved, a row is saved into the location table, where location.eid = node.vid

mysql> describe location;
+-------------+------------------+------+-----+---------+-------+
| Field       | Type             | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| eid         | int(10) unsigned | NO   |     | 0       |       |
| lid         | int(10) unsigned | NO   | PRI | 0       |       |
| type        | varchar(6)       | NO   |     |         |       |
| name        | varchar(255)     | YES  |     | NULL    |       |
| street      | varchar(255)     | YES  |     | NULL    |       |
| additional  | varchar(255)     | YES  |     | NULL    |       |
| city        | varchar(255)     | YES  |     | NULL    |       |
| province    | varchar(16)      | YES  |     | NULL    |       |
| postal_code | varchar(16)      | YES  |     | NULL    |       |
| country     | varchar(2)       | YES  |     | NULL    |       |
| latitude    | decimal(10,6)    | YES  |     | NULL    |       |
| longitude   | decimal(10,6)    | YES  |     | NULL    |       |
| source      | tinyint(4)       | YES  |     | 0       |       |
| is_primary  | tinyint(4)       | NO   |     | 0       |       |
+-------------+------------------+------+-----+---------+-------+

+-----+-----+------+------+--------+------------+-----------+----------+-------------+---------+-----------+-------------+--------+------------+
| eid | lid | type | name | street | additional | city      | province | postal_code | country | latitude  | longitude   | source | is_primary |
+-----+-----+------+------+--------+------------+-----------+----------+-------------+---------+-----------+-------------+--------+------------+
|   9 |   1 | node |      |        |            | Vancouver | BC       |             | ca      | 49.263588 | -123.138565 |      3 |          0 |
+-----+-----+------+------+--------+------------+-----------+----------+-------------+---------+-----------+-------------+--------+------------+

 
 

Drupal is a registered trademark of Dries Buytaert.