YOLP Yahoo!スタティックマップAPI で地図情報 XML ファイルを取得する

## 概要

- Python で YOLP Yahoo!スタティックマップAPI をコールして地図画像と地図情報XMLを取得する
- YOLP Yahoo!スタティックマップAPI の output パラメータに xml を指定すると、output=xml ではないときに取得する地図画像についての情報を XML で取得できる
- 動作確認環境: macOS Catalina + Python 3.8.5

## Python によるプログラム

```py
from urllib.request import Request, urlopen
import xml.dom.minidom as MD

appid = 'YOUR APPLICATION ID' # アプリケーションIDを指定
headers = {'User-Agent': 'Yahoo AppID: {0}'.format(appid)}

# lat=中心の緯度, lon=中心の経度, z=縮尺レベル, output=出力形式
# 今回は名古屋駅の緯度・経度を指定
png_url = 'https://map.yahooapis.jp/map/V1/static?lat=35.170476&lon=136.882250&z=17'
xml_url = 'https://map.yahooapis.jp/map/V1/static?lat=35.170476&lon=136.882250&z=17&output=xml'

# 地図の画像をダウンロードして保存
req = Request(png_url, headers=headers)
with urlopen(req) as res:
  with open ('map.png', mode='wb') as file:
    file.write(res.read())

# 地図情報のXMLを出力
req = Request(xml_url, headers=headers)
with urlopen(req) as res:
  body = res.read().decode('utf-8')
  with open ('map.xml', mode='w') as file:
    # XML を整形して出力
    dom = MD.parseString(body)
    dom.writexml(file, addindent='  ', newl='\n', encoding='utf-8')
```

## 出力結果

### 地図画像

取得した地図画像。

![map.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139365/3af6f280-63c1-5f48-a074-8c3964f00a97.png)

### 地図情報XML

主な要素と属性の意味。

- Coordinates: 地図の中心座標
- Coordinate-UL: 地図左上の座標
- Coordinate-UR: 地図右上の座標
- Coordinate-DL: 地図左下の座標
- Coordinate-DR: 地図右下の座標
- Image: 地図のサイズ (単位: pixel)
- Scales: 地図の中心座標にて地図が表示可能な縮尺値のリスト
- mode: 地図の種類
- Scale: 縮尺値
- zlevel: 縮尺レベル
- sc: 縮尺ID

取得した XML ファイル。

```map.xml
<?xml version="1.0" encoding="utf-8"?>
<ResultSet xmlns="urn:yahoo:jp:olp:static" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:yahoo:jp:olp:static http://olp.yahooapis.jp/OpenLocalPlatform/V1/staticResponse.xsd">
  <Result>
    <Coordinates datum="WGS84" format="lon,lat">136.88225,35.170476</Coordinates>
    <Coordinate-UL>
      <Coordinates datum="WGS84" format="lon,lat">136.8768855819702,35.1748609813922</Coordinates>
    </Coordinate-UL>
    <Coordinate-UR>
      <Coordinates datum="WGS84" format="lon,lat">136.8876144180298,35.1748609813922</Coordinates>
    </Coordinate-UR>
    <Coordinate-DL>
      <Coordinates datum="WGS84" format="lon,lat">136.8768855819702,35.1660907821191</Coordinates>
    </Coordinate-DL>
    <Coordinate-DR>
      <Coordinates datum="WGS84" format="lon,lat">136.8876144180298,35.1660907821191</Coordinates>
    </Coordinate-DR>
    <Scale zlevel="17" sc="4">23842</Scale>
    <Image>
      <Width>500</Width>
      <Height>500</Height>
    </Image>
    <Scales mode="map">
      <Scale zlevel="1" sc="20">1562498438</Scale>
      <Scale zlevel="2" sc="19">781249219</Scale>
      <Scale zlevel="3" sc="18">390624609</Scale>
      <Scale zlevel="4" sc="17">195312305</Scale>
      <Scale zlevel="5" sc="16">97656152</Scale>
      <Scale zlevel="6" sc="15">48828076</Scale>
      <Scale zlevel="7" sc="14">24414038</Scale>
      <Scale zlevel="8" sc="13">12207019</Scale>
      <Scale zlevel="9" sc="12">6103510</Scale>
      <Scale zlevel="10" sc="11">3051755</Scale>
      <Scale zlevel="11" sc="10">1525877</Scale>
      <Scale zlevel="12" sc="9">762939</Scale>
      <Scale zlevel="13" sc="8">381469</Scale>
      <Scale zlevel="14" sc="7">190735</Scale>
      <Scale zlevel="15" sc="6">95367</Scale>
      <Scale zlevel="16" sc="5">47684</Scale>
      <Scale zlevel="17" sc="4">23842</Scale>
      <Scale zlevel="18" sc="3">11921</Scale>
      <Scale zlevel="19" sc="2">5960</Scale>
      <Scale zlevel="20" sc="1">2980</Scale>
    </Scales>
    <Scales mode="map-mobile">
      <Scale zlevel="1" sc="20">1562498438</Scale>
      <Scale zlevel="2" sc="19">781249219</Scale>
      <Scale zlevel="3" sc="18">390624609</Scale>
      <Scale zlevel="4" sc="17">195312305</Scale>
      <Scale zlevel="5" sc="16">97656152</Scale>
      <Scale zlevel="6" sc="15">48828076</Scale>
      <Scale zlevel="7" sc="14">24414038</Scale>
      <Scale zlevel="8" sc="13">12207019</Scale>
      <Scale zlevel="9" sc="12">6103510</Scale>
      <Scale zlevel="10" sc="11">3051755</Scale>
      <Scale zlevel="11" sc="10">1525877</Scale>
      <Scale zlevel="12" sc="9">762939</Scale>
      <Scale zlevel="13" sc="8">381469</Scale>
      <Scale zlevel="14" sc="7">190735</Scale>
      <Scale zlevel="15" sc="6">95367</Scale>
      <Scale zlevel="16" sc="5">47684</Scale>
      <Scale zlevel="17" sc="4">23842</Scale>
      <Scale zlevel="18" sc="3">11921</Scale>
      <Scale zlevel="19" sc="2">5960</Scale>
      <Scale zlevel="20" sc="1">2980</Scale>
    </Scales>
    <Scales mode="photo">
      <Scale zlevel="1" sc="20">1562498438</Scale>
      <Scale zlevel="2" sc="19">781249219</Scale>
      <Scale zlevel="3" sc="18">390624609</Scale>
      <Scale zlevel="4" sc="17">195312305</Scale>
      <Scale zlevel="5" sc="16">97656152</Scale>
      <Scale zlevel="6" sc="15">48828076</Scale>
      <Scale zlevel="7" sc="14">24414038</Scale>
      <Scale zlevel="8" sc="13">12207019</Scale>
      <Scale zlevel="9" sc="12">6103510</Scale>
      <Scale zlevel="10" sc="11">3051755</Scale>
      <Scale zlevel="11" sc="10">1525877</Scale>
      <Scale zlevel="12" sc="9">762939</Scale>
      <Scale zlevel="13" sc="8">381469</Scale>
      <Scale zlevel="14" sc="7">190735</Scale>
      <Scale zlevel="15" sc="6">95367</Scale>
      <Scale zlevel="16" sc="5">47684</Scale>
      <Scale zlevel="17" sc="4">23842</Scale>
      <Scale zlevel="18" sc="3">11921</Scale>
      <Scale zlevel="19" sc="2">5960</Scale>
      <Scale zlevel="20" sc="1">2980</Scale>
      <Scale zlevel="21" sc="1">1490</Scale>
    </Scales>
    <Scales mode="hybrid">
      <Scale zlevel="1" sc="20">1562498438</Scale>
      <Scale zlevel="2" sc="19">781249219</Scale>
      <Scale zlevel="3" sc="18">390624609</Scale>
      <Scale zlevel="4" sc="17">195312305</Scale>
      <Scale zlevel="5" sc="16">97656152</Scale>
      <Scale zlevel="6" sc="15">48828076</Scale>
      <Scale zlevel="7" sc="14">24414038</Scale>
      <Scale zlevel="8" sc="13">12207019</Scale>
      <Scale zlevel="9" sc="12">6103510</Scale>
      <Scale zlevel="10" sc="11">3051755</Scale>
      <Scale zlevel="11" sc="10">1525877</Scale>
      <Scale zlevel="12" sc="9">762939</Scale>
      <Scale zlevel="13" sc="8">381469</Scale>
      <Scale zlevel="14" sc="7">190735</Scale>
      <Scale zlevel="15" sc="6">95367</Scale>
      <Scale zlevel="16" sc="5">47684</Scale>
      <Scale zlevel="17" sc="4">23842</Scale>
      <Scale zlevel="18" sc="3">11921</Scale>
      <Scale zlevel="19" sc="2">5960</Scale>
      <Scale zlevel="20" sc="1">2980</Scale>
      <Scale zlevel="21" sc="1">1490</Scale>
    </Scales>
    <Scales mode="map-b1">
      <Scale zlevel="19" sc="3">5960</Scale>
      <Scale zlevel="20" sc="2">2980</Scale>
      <Scale zlevel="21" sc="1">1490</Scale>
    </Scales>
    <Scales mode="hd">
      <Scale zlevel="1" sc="20">1562498438</Scale>
      <Scale zlevel="2" sc="19">781249219</Scale>
      <Scale zlevel="3" sc="18">390624609</Scale>
      <Scale zlevel="4" sc="17">195312305</Scale>
      <Scale zlevel="5" sc="16">97656152</Scale>
      <Scale zlevel="6" sc="15">48828076</Scale>
      <Scale zlevel="7" sc="14">24414038</Scale>
      <Scale zlevel="8" sc="13">12207019</Scale>
      <Scale zlevel="9" sc="12">6103510</Scale>
      <Scale zlevel="10" sc="11">3051755</Scale>
      <Scale zlevel="11" sc="10">1525877</Scale>
      <Scale zlevel="12" sc="9">762939</Scale>
      <Scale zlevel="13" sc="8">381469</Scale>
      <Scale zlevel="14" sc="7">190735</Scale>
      <Scale zlevel="15" sc="6">95367</Scale>
      <Scale zlevel="16" sc="5">47684</Scale>
      <Scale zlevel="17" sc="4">23842</Scale>
      <Scale zlevel="18" sc="3">11921</Scale>
      <Scale zlevel="19" sc="2">5960</Scale>
      <Scale zlevel="20" sc="1">2980</Scale>
    </Scales>
    <Scales mode="hd-mobile">
      <Scale zlevel="1" sc="20">1562498438</Scale>
      <Scale zlevel="2" sc="19">781249219</Scale>
      <Scale zlevel="3" sc="18">390624609</Scale>
      <Scale zlevel="4" sc="17">195312305</Scale>
      <Scale zlevel="5" sc="16">97656152</Scale>
      <Scale zlevel="6" sc="15">48828076</Scale>
      <Scale zlevel="7" sc="14">24414038</Scale>
      <Scale zlevel="8" sc="13">12207019</Scale>
      <Scale zlevel="9" sc="12">6103510</Scale>
      <Scale zlevel="10" sc="11">3051755</Scale>
      <Scale zlevel="11" sc="10">1525877</Scale>
      <Scale zlevel="12" sc="9">762939</Scale>
      <Scale zlevel="13" sc="8">381469</Scale>
      <Scale zlevel="14" sc="7">190735</Scale>
      <Scale zlevel="15" sc="6">95367</Scale>
      <Scale zlevel="16" sc="5">47684</Scale>
      <Scale zlevel="17" sc="4">23842</Scale>
      <Scale zlevel="18" sc="3">11921</Scale>
      <Scale zlevel="19" sc="2">5960</Scale>
      <Scale zlevel="20" sc="1">2980</Scale>
    </Scales>
    <Scales mode="loco">
      <Scale zlevel="1" sc="20">1562498438</Scale>
      <Scale zlevel="2" sc="19">781249219</Scale>
      <Scale zlevel="3" sc="18">390624609</Scale>
      <Scale zlevel="4" sc="17">195312305</Scale>
      <Scale zlevel="5" sc="16">97656152</Scale>
      <Scale zlevel="6" sc="15">48828076</Scale>
      <Scale zlevel="7" sc="14">24414038</Scale>
      <Scale zlevel="8" sc="13">12207019</Scale>
      <Scale zlevel="9" sc="12">6103510</Scale>
      <Scale zlevel="10" sc="11">3051755</Scale>
      <Scale zlevel="11" sc="10">1525877</Scale>
      <Scale zlevel="12" sc="9">762939</Scale>
      <Scale zlevel="13" sc="8">381469</Scale>
      <Scale zlevel="14" sc="7">190735</Scale>
      <Scale zlevel="15" sc="6">95367</Scale>
      <Scale zlevel="16" sc="5">47684</Scale>
      <Scale zlevel="17" sc="4">23842</Scale>
      <Scale zlevel="18" sc="3">11921</Scale>
      <Scale zlevel="19" sc="2">5960</Scale>
      <Scale zlevel="20" sc="1">2980</Scale>
    </Scales>
    <Positions/>
    <Copyright>(C)Yahoo Japan,(C)ZENRIN</Copyright>
  </Result>
</ResultSet>
```

## 参考資料

- [YOLP\(地図\):Yahoo\!スタティックマップAPI \- Yahoo\!デベロッパーネットワーク](https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/static.html)
- [urllib\.request \-\-\- URL を開くための拡張可能なライブラリ — Python 3\.8\.5 ドキュメント](https://docs.python.org/ja/3.8/library/urllib.request.html)
- [xml\.dom\.minidom \-\-\- 最小限の DOM の実装 — Python 3\.8\.5 ドキュメント](https://docs.python.org/ja/3.8/library/xml.dom.minidom.html)