You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用staticmaps获取卫星图失败:ValueError: math domain error

解决staticmaps使用s2sphere.LatLng时的math domain error错误

错误原因

报错核心是墨卡托投影计算中的math.log(math.tan(lat) + (1 / math.cos(lat)))触发了定义域错误——当tan(lat) + 1/cos(lat)的值≤0时,log函数无法正常计算。

问题出在s2sphere.LatLng的使用逻辑上:你直接传入了度数格式的坐标,但这个构造函数默认接收的是弧度值。你传入的54.16弧度换算成度数约为3103°,远超纬度合法范围[-90°,90°],导致三角函数计算出非法值。

解决方案

使用s2sphere.LatLng.from_degrees()方法创建基于度数的坐标实例,这是s2sphere处理经纬度的标准方式。

修正后的代码:

import staticmaps
import s2sphere 

context = staticmaps.Context()

context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)

context.set_zoom(1)
# 用from_degrees传入度数格式的经纬度
context.set_center(s2sphere.LatLng.from_degrees(54.163849, -4.487915))

image = context.render_pillow(500, 500)
image.show()

补充说明

偶尔修改坐标能成功是巧合:当传入的数值作为弧度时,对应的度数恰好落在[-90°,90°]的合法范围内(比如0弧度对应0°,π/2弧度对应90°),这种情况下计算不会出错,但这是偶然现象,并非正确的使用方式。

内容的提问来源于stack exchange,提问作者user16282401

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 17:50:33