spatial
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseYou are answering spatial questions using DuckDB's spatial extension and, when needed, Overture Maps as a free global data source.
Question or file:
Additional context:
$0${1:-}您将使用DuckDB的空间扩展,必要时结合Overture Maps这一免费全球数据源来解答空间相关问题。
问题或文件:
额外上下文:
$0${1:-}Step 1 — Understand what the user needs
步骤1 — 理解用户需求
Classify the question:
| Pattern | Data source | Key functions |
|---|---|---|
| "Find X near Y" (no user file) | Overture Maps on S3 | |
| "How far between A and B" | Geocode or user data | |
| "Which points fall inside polygons" | User files | |
| "Analyze this GeoJSON/Shapefile/GPX" | User file | |
| "Show density/hotspots" | User or Overture data | H3 hex binning |
| "Convert to GeoJSON/GeoPackage" | User file | |
| "Count buildings/roads in area" | Overture Maps | bbox filtering + aggregation |
If the question involves real-world places, POIs, buildings, roads, or boundaries and the user hasn't provided a file, use Overture Maps — read for S3 paths and schema.
references/overture.mdFor spatial function syntax, read .
references/functions.md对问题进行分类:
| 模式 | 数据源 | 核心功能 |
|---|---|---|
| “查找Y附近的X”(无用户文件) | S3上的Overture Maps | |
| “A和B之间的距离是多少” | 地理编码或用户数据 | |
| “哪些点落在多边形内” | 用户文件 | |
| “分析此GeoJSON/Shapefile/GPX” | 用户文件 | |
| “展示密度/热点区域” | 用户数据或Overture数据 | H3六边形网格划分 |
| “转换为GeoJSON/GeoPackage” | 用户文件 | |
| “统计区域内的建筑/道路数量” | Overture Maps | 边界框过滤 + 聚合 |
如果问题涉及现实世界地点、兴趣点(POI)、建筑、道路或边界,且用户未提供文件,请使用Overture Maps——查看获取S3路径和模式。
references/overture.md关于空间函数语法,请查看。
references/functions.mdStep 2 — Write and run the query
步骤2 — 编写并运行查询
Always start with:
sql
LOAD spatial;
SET geometry_always_xy = true;Add extensions as needed:
- Overture/remote data:
LOAD httpfs; CREATE SECRET (TYPE S3, PROVIDER config, REGION 'us-west-2'); - H3 hex binning:
INSTALL h3 FROM community; LOAD h3;
始终从以下代码开始:
sql
LOAD spatial;
SET geometry_always_xy = true;根据需要添加扩展:
- Overture/远程数据:
LOAD httpfs; CREATE SECRET (TYPE S3, PROVIDER config, REGION 'us-west-2'); - H3六边形网格划分:
INSTALL h3 FROM community; LOAD h3;
Key principles
核心原则
bbox filtering first — When querying Overture, always filter on before any spatial function. This uses Parquet predicate pushdown and avoids downloading the full dataset.
bbox.xmin/xmax/ymin/ymaxAlways set — This ensures all spatial functions interpret coordinates as longitude, latitude (the standard for Overture, GeoJSON, and most data sources). Without it, spheroid functions assume latitude first and return wrong results.
geometry_always_xy = trueUse spheroid functions for real-world distances — returns meters on the WGS84 ellipsoid. Plain uses planar coordinates and gives meaningless results for lat/lng. Important: spheroid functions (, , etc.) require inputs, not generic . Overture geometry columns are typed and cannot be cast directly. Extract coordinates first:
ST_Distance_SpheroidST_DistanceST_Distance_SpheroidST_Area_SpheroidPOINT_2DGEOMETRYGEOMETRY('OGC:CRS84')sql
ST_Point(ST_X(geometry), ST_Y(geometry))::POINT_2DCSV with lat/lng needs conversion — (longitude first). This is the most common gotcha.
ST_Point(longitude, latitude)Run the query in a single bash call:
bash
duckdb -c "
LOAD spatial;
<ADDITIONAL_SETUP>
<YOUR_QUERY>
"先进行边界框过滤——查询Overture时,务必在调用任何空间函数前先过滤。这会利用Parquet谓词下推,避免下载完整数据集。
bbox.xmin/xmax/ymin/ymax始终设置——这确保所有空间函数将坐标解读为经度、纬度(Overture、GeoJSON及大多数数据源的标准格式)。如果不设置,球体函数会默认先纬度后经度,导致结果错误。
geometry_always_xy = true使用球体函数计算真实世界距离——返回基于WGS84椭球体的米数。普通的使用平面坐标,对经纬度数据来说结果毫无意义。**重要提示:**球体函数(、等)要求输入为类型,而非通用的类型。Overture的几何列类型为,无法直接转换。需先提取坐标:
ST_Distance_SpheroidST_DistanceST_Distance_SpheroidST_Area_SpheroidPOINT_2DGEOMETRYGEOMETRY('OGC:CRS84')sql
ST_Point(ST_X(geometry), ST_Y(geometry))::POINT_2D含经纬度的CSV需要转换——使用(经度在前)。这是最常见的易错点。
ST_Point(longitude, latitude)通过单个bash命令运行查询:
bash
duckdb -c "
LOAD spatial;
<ADDITIONAL_SETUP>
<YOUR_QUERY>
"Step 3 — Present results
步骤3 — 呈现结果
- For tabular results: show the data directly
- For spatial results: consider exporting to GeoJSON for visualization ()
COPY TO 'result.geojson' WITH (FORMAT GDAL, DRIVER 'GeoJSON') - For distance/area results: use human-readable units (km for large distances, m for small)
- For density/hotspot results: describe the pattern and offer to export for visualization
If the query fails:
- → delegate to
duckdb: command not found/duckdb-skills:install-duckdb - Missing extension → or
INSTALL spatial; LOAD spatial;INSTALL h3 FROM community; LOAD h3; - S3 access denied → suggest checking AWS credentials
- No results with Overture → widen the bbox, check the category spelling, or try a broader search
- 对于表格结果:直接展示数据
- 对于空间结果:考虑导出为GeoJSON以便可视化()
COPY TO 'result.geojson' WITH (FORMAT GDAL, DRIVER 'GeoJSON') - 对于距离/面积结果:使用易读的单位(大距离用千米,小距离用米)
- 对于密度/热点区域结果:描述模式并提供导出可视化的选项
如果查询失败:
- → 调用
duckdb: command not found/duckdb-skills:install-duckdb - 缺少扩展 → 执行或
INSTALL spatial; LOAD spatial;INSTALL h3 FROM community; LOAD h3; - S3访问被拒绝 → 建议检查AWS凭证
- 使用Overture无结果 → 扩大边界框、检查类别拼写或尝试更宽泛的搜索