Locations
Choose locations by name, constant or coordinates.
Every query accepts a location in three forms. Pick the one that fits:
| Form | Example | Best for |
|---|---|---|
| Name | "臺北市信義區", "左營區" | User input, quick tests |
| Constant | Township.TAIPEI.XINYI, County.HUALIEN | Fixed places in code, with editor autocompletion |
| Coordinates | Coordinates(latitude=25.034, longitude=121.5645) | GPS positions, points on a map |
Names
Use the full Chinese name or any short name that is not ambiguous. 台 and 臺 are treated the same:
import cwa
cwa.weather.get("臺北市信義區") # full name
cwa.weather.get("台北市信義區") # 台 and 臺 both work
cwa.weather.get("左營區") # only Kaohsiung has a 左營區
cwa.weather.get("花蓮市") A name that does not lead to exactly one township raises an exception:
| Exception | When | What to do |
|---|---|---|
AmbiguousLocationError | A short name matches several townships; "中正區" exists in both Taipei and Keelung | error.candidates lists every matching full name, so users can pick one |
UnknownLocationError | The name does not exist | Check the spelling or use a constant |
try:
cwa.weather.get("中正區")
except cwa.AmbiguousLocationError as error:
print(error.candidates) # ('基隆市中正區', '臺北市中正區') Constants
County lists every county and Township lists every township by county. Constants cannot be misspelled, and editors autocomplete them:
from cwa import County, Township
xinyi = Township.TAIPEI.XINYI
print(xinyi.full_name) # 臺北市信義區
print(xinyi.code) # administrative code
print(xinyi.county is County.TAIPEI) # True
for town in County.HUALIEN.townships:
print(town.name) To turn a name string into a constant, use parse(), which follows the same rules as names above:
town = Township.parse("高雄市左營區")
county = County.parse("花蓮縣") Coordinates
Coordinates are WGS84 decimal degrees, the format used by GPS and Google Maps:
from cwa import Coordinates
taipei_101 = Coordinates(latitude=25.034, longitude=121.5645)
reading = cwa.observations.nearest(taipei_101)
alerts = cwa.alerts.active(taipei_101) Not every feature accepts coordinates:
| Feature | Name or constant | Coordinates |
|---|---|---|
| Forecasts, weather snapshots | ✓ | ✗, forecasts are per township |
| Observations, stations | ✓ | ✓ |
| Warnings | ✓ | ✓, needs cwa[geo] |
| Epicentral distance | ✓ | ✓ |
When you query observations or distances by township, the SDK uses the township’s reference point, Township.coordinates, as its position.