10.10.2010

[PHP]simplexml

Yahoo! Weather の RSS Feed をsimplexmlでパースする。

まずは、
取得したXMLをsimplexml_load_string()でsimplexmlオブジェクトに展開する。
$xml = file_get_contents($rss);
  $w = simplexml_load_string($xml);
後は、
default namespaceの要素を取得するなら、
引数なしでchildren()と実行し、
$ch = $w->channel->children();
namespaceの指定がある要素を取得するなら、
children($namespace) と引数ありで実行するだけ。
$ch = $w->channel->children($namespace);
全体はこんな感じ。
$ cat weather.php
<?php

  $rss = "http://weather.yahooapis.com/forecastrss?w=2348079&u=c";
  $xml = file_get_contents($rss);
  if(!$xml) exit(1);
  $w = simplexml_load_string($xml);

  $yw  = "http://xml.weather.yahoo.com/ns/rss/1.0";
  $geo = "http://www.w3.org/2003/01/geo/wgs84_pos#"

  // default namespace <channel><title>
  $ch = $w->channel->children();
  foreach($ch as $k => $v){
    if($k === "lastBuildDate") $weather[$k] = $v;
  }

  // <channel><yweather>
  $ch = $w->channel->children($yw);
  foreach($ch as $category => $kv){
    foreach($kv->attributes() as $k => $v){ 
      $category = (string)$category;
      $k = (string)$k;
      $v = (string)$v;
      $weather[$category][$k]=$v;
    }
  }

  // <channel><item><geo>
  $ch = $w->channel->item->children($geo);
  foreach($ch as $k => $v){
    $k = (string)$k;
    $v = (string)$v;
    $weather["location"][$k] = $v;
  }

  // <channel><item><yweather>
  $item = $w->channel->item->children($yw);
  foreach($item as $category => $kv){
    foreach($kv->attributes() as $k => $v){
      $category = (string)$category;
      $k = (string)$k;
      $v = (string)$v;
      if($k === "day" && $category === "forecast") $day = $v;
      if($category === "forecast"){
        $weather[$category][$day][$k] = $v;
      }
      else{
        $weather[$category][$k] = $v;
      }
    }
  }

  print_r($weather);
?>
実行すると
$ php weather.php
Array
(
    [location] => Array
        (  
            [city] => Auckland
            [region] =>
            [country] => New Zealand
            [lat] => -36.88
            [long] => 174.77
        )

    [units] => Array
        (  
            [temperature] => C
            [distance] => km
            [pressure] => mb
            [speed] => km/h
        )

    [wind] => Array
        (  
            [chill] => 11
            [direction] => 270
            [speed] => 24.14
        )

    [atmosphere] => Array
        (  
            [humidity] => 94
            [visibility] => 9.99
            [pressure] => 1015.92
            [rising] => 0
        )

    [astronomy] => Array
        (  
            [sunrise] => 6:43 am
            [sunset] => 7:32 pm
        )

    [condition] => Array
        (
            [text] => Rain Shower
            [code] => 11
            [temp] => 11
            [date] => Mon, 11 Oct 2010 2:00 am NZDT
        )

    [forecast] => Array
        (
            [Mon] => Array
                (
                    [day] => Mon
                    [date] => 11 Oct 2010
                    [low] => 8
                    [high] => 13
                    [text] => Showers
                    [code] => 11
                )

            [Tue] => Array
                (
                    [day] => Tue
                    [date] => 12 Oct 2010
                    [low] => 12
                    [high] => 16
                    [text] => Partly Cloudy
                    [code] => 30
                )

        )

)


便利だね、simplexml!!

10.03.2010

Yahoo! Weather RSS Feed

Yahoo! Weather RSS Feed
The base URL for the Weather RSS feed is

http://weather.yahooapis.com/forecastrss

For the Weather RSS feed there are two parameters:

* w for WOEID.
* u for degrees units (Fahrenheit or Celsius).

WOEIDは
Where On Earth Identifiers

Spatial entities provided by Yahoo! GeoPlanet are referenced by a 32-bit identifier: the Where On Earth ID (WOEID). WOEIDs are unique and non-repetitive, and are assigned to all entities within the system. A WOEID, once assigned, is never changed or recycled. If a WOEID is deprecated it is mapped to its successor or parent WOEID, so that requests to the service using a deprecated WOEID are served transparently.

のこと。 詳しくはこちら

http://weather.yahoo.com/ の検索を使い、Auckland, NZ のWOEIDを調べてみると2348079。

気温の単位(parameter 'u')は、
f = Fahrenheit = 華氏
c = Celsius = 摂氏

Aucklandの天気情報(℃)を取得するには
http://weather.yahooapis.com/forecastrss?w=2348079&u=c
結果はこんな感じ。




あとは、
  1. 天気を表示する都市を決め、WOEIDリストを作成する
  2. WOEIDリストを元にcronで天気情報を取得する
  3. Latitude and Longitudeのお勉強
  4. Google Codesを活用して、ニュージーランドお天気マップを作る

9.20.2010

天気情報

天気情報について調査。
  • Google
    • 4日分の天気が分かる
    • かなりの都市をカバーしている e.g. Napier, Rotorua
    • データ自体は公開していない
    • iGoogleのコメントを見ると精度は低い模様
  • tenki.jp(Yahoo!Japan)
    • 7日分の天気が分かる
    • 主要都市をカバーしているが、Googleに比べると網羅性は劣る
    • データ自体は公開していない
  • 天気予報API
    • 7日分の天気が分かる
    • カバーしているのは国内のみ
    • データを公開しているが、有料(初期費用:105,000円 基本料:31,500円~)
  • livedoor 天気情報
    • 7日分の天気が分かる
    • カバーしているのは国内のみ
    • データを無料公開している
  • Yahoo!
    • 10日分の天気が分かる
    • かなりの都市をカバーしている
    • データを無料公開している(2日分)
    • 精度は不明

ニュージーランドの天気情報を手に入れるなら、Yahoo!だね。

「利用条件」
  • 個人もしくは非営利団体が、個人向けもしくは非営利目的で利用する場合に限る
  • 利用を明記する("Yahoo! Weather." in text or Yahoo! Weather logo)

9.19.2010

ニュージーランドの地域

ニュージーランドの地域(region)についてお勉強。


大きな地図で見る

英語の勉強も兼ねてWikipediaを読むと・・・

まずニュージーランド領(Realm of New Zealand)という概念が最上位にあり、
この下にいわゆるニュージーランドが存在する。ほかにもトケラウ(Tokelau)、クック諸島(Cook Islands)、ニウエ(Niue)、ロス属領(Ross Dependency)が同列にある。

ちなみにロス属領は南極の一部で、一般的にはニュージーランド領とされるが、南極条約では領内とするニュージーランドの主張は保留になっている模様。

ニュージーランドに戻り、
この下には12の非単一自治体、4つの単一自治体、チャタム諸島(Chatham Islands)、ケルマデク諸島(Kermadec Islands)、亜南極諸島(sub-Antarctic islands)が存在する。

非単一自治体は、日本で言う都道府県に当たり、その下に市/区(2階層/3階層)が存在する。
一方、単一自治体は1階層からなる自治体のことで、市/区は存在しない。

12の非単一自治体には、16の都市、57の区が存在する。

「ノースアイランドの自治体」
  1. ノースランド
  2. オークランド
  3. ワイカト
  4. ベイ・オブ・プレンティ
  5. ギズボーン*
  6. ホークスベイ
  7. タラナキ
  8. マナワツ・ワンガヌイ
  9. ウェリントン
「サウスアイランドの自治体」
  1. マールボロ*
  2. タスマン*
  3. ネルソン*
  4. ウェストコースト
  5. カンタベリー
  6. オタゴ
  7. サウスランド
* 単一自治体

これで知りたいことは大体分かったかな。