Skip to Content
cwa
Guides

Locations

Choose locations by name, constant or coordinates.

Every query accepts a location in three forms. Pick the one that fits:

FormExampleBest for
Name"臺北市信義區", "左營區"User input, quick tests
ConstantTownship.TAIPEI.XINYI, County.HUALIENFixed places in code, with editor autocompletion
CoordinatesCoordinates(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:

ExceptionWhenWhat to do
AmbiguousLocationErrorA short name matches several townships; "中正區" exists in both Taipei and Keelungerror.candidates lists every matching full name, so users can pick one
UnknownLocationErrorThe name does not existCheck 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:

FeatureName or constantCoordinates
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.