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

如何用Python从ArcGIS Online的HostedFeatureLayer筛选当日创建/编辑数据

从ArcGIS Online托管要素图层获取当日创建/编辑的数据

要筛选createdate或editdate为当日的数据,核心是给query()方法添加where条件,通过ArcGIS支持的SQL语法过滤日期。以下是修改后的完整代码:

from arcgis.gis import GIS
import datetime
import sqlalchemy

# 初始化GIS连接(在ArcGIS Pro/Notebook中可直接用"home",外部环境需传入AGOL地址、账号密码或API密钥)
gis = GIS("home")

feature_layer_id = '你的要素图层ID'
Inspform = gis.content.get(feature_layer_id)
layer = Inspform.layers[0]

# 生成当日的时间范围(ArcGIS识别的ISO格式,含UTC标识Z)
today = datetime.date.today()
start_of_day = datetime.datetime(today.year, today.month, today.day, 0, 0, 0).isoformat() + "Z"
end_of_day = datetime.datetime(today.year, today.month, today.day, 23, 59, 59).isoformat() + "Z"

# 构造过滤条件:匹配当日创建或当日编辑的数据
where_clause = f"(createdate >= TIMESTAMP '{start_of_day}' AND createdate <= TIMESTAMP '{end_of_day}') OR (editdate >= TIMESTAMP '{start_of_day}' AND editdate <= TIMESTAMP '{end_of_day}')"

# 执行带条件的查询并转换为SDF
filtered_sdf = layer.query(where=where_clause).sdf

# 将筛选后的数据写入数据库
filtered_sdf.to_sql('testtable', con=engine, schema=None, if_exists='replace', index=False)

关键说明:

  • 日期范围过滤:由于createdate和editdate是带时分秒的时间戳,直接用= '当日日期'会遗漏带时间的数据,因此用当日0点到23:59:59的区间匹配更准确。
  • SQL语法适配:ArcGIS要素图层查询支持标准SQL时间戳语法,需用TIMESTAMP关键字包裹ISO格式的日期字符串,末尾的Z表示UTC时间,若图层使用本地时间可移除该标识。
  • GIS连接配置:如果在本地脚本运行,需将GIS("home")替换为GIS("https://www.arcgis.com", "你的用户名", "你的密码")或使用API密钥认证。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 02:10:26