OpenLayers
Use this skill for practical OpenLayers web map work in browser apps.
OpenLayers is the right choice when the user needs a stronger GIS-style 2D web map stack than Leaflet, especially for projection-aware workflows, tiled and vector layers, custom styling, feature interaction, and more explicit control over map/view/source/layer composition.
Workflow
- 1. Confirm the runtime first:
- plain HTML
- Vite
- React
- Next.js or another SPA framework
- 2. Confirm the actual map job:
- base map only
- markers or selected features
- GeoJSON overlay
- WMS or WMTS layer
- draw or modify interaction
- bug fixing or migration
- 3. Start from the smallest visible map before adding custom sources, interactions, or large datasets.
- Check container size, CSS, and projection assumptions before debugging layer logic.
- Keep coordinate transforms explicit; OpenLayers often needs
fromLonLat(...) or explicit dataProjection and featureProjection.
Implementation Guardrails
- - Ensure the map container has a real height before initializing
Map. - Keep
View projection, incoming data projection, and display projection explicit. - Use
fromLonLat([lng, lat]) for geographic coordinates shown on the default Web Mercator map. - For GeoJSON, set both
dataProjection and featureProjection when the source CRS is known. - Prefer stable layer and source instances instead of recreating the whole map on every state update.
- Clean up the map target in SPA teardown paths.
- Fit bounds from actual features or source extents when the dataset is dynamic.
- Style features deterministically; do not bury business logic inside a style function unless the user needs data-driven styling.
Common Failure Checks
- - Blank map: verify container height, CSS import, map target, and layer source URL.
- Features in the wrong place: verify whether lon/lat was used without
fromLonLat(...), or whether GeoJSON projections were omitted. - Click picking not working: verify layer visibility, z-index ordering, and
forEachFeatureAtPixel(...) usage. - Map looks offset after layout changes: call
map.updateSize() after the container becomes visible or resizes. - Performance issues with many features: simplify data, cluster points, reduce style churn, or move expensive logic out of pointer handlers.
- WMS or WMTS not rendering: verify service URL, layer names, matrix set, CRS, and cross-origin behavior.
Practical Patterns
Minimal map
- - Create one
Map, one View, and one base TileLayer. - Use
OSM or XYZ only after confirming the tile URL and attribution requirements. - Set center using
fromLonLat(...) when starting from geographic coordinates.
GeoJSON overlay
- - Use
VectorSource with a GeoJSON format object. - Set
dataProjection and featureProjection explicitly when precision matters. - Fit the view to
vectorSource.getExtent() after features load.
Interactive picking
- - Use
map.on("singleclick", ...) for primary feature selection. - Use
forEachFeatureAtPixel(...) to identify the top visible feature. - Keep popup or side-panel rendering outside the OpenLayers style layer when possible.
SPA integration
- - Create the map once after the container mounts.
- Reuse
Source and Layer objects across updates. - On teardown, call
map.setTarget(undefined) to release DOM bindings.
Draw and edit
- - Add
Draw, Modify, or Select only when the user explicitly needs editing. - Confirm output format and projection before persisting edited geometries.
Task Boundaries
- - Use this skill for OpenLayers-specific implementation, debugging, and migration patterns.
- For CRS and reprojection reasoning, use
project, wgs84, or cgcs2000 as appropriate. - For file-based GIS conversion or batch reprojection, use
qgis. - For lightweight non-GIS-heavy web maps,
leaflet may be simpler. - For Mapbox GL style expressions or hosted Mapbox platform APIs, use
mapbox. - For globe and 3D scene rendering, use
cesium.
What To Return
- - A minimal working OpenLayers setup or a targeted fix.
- The likely root cause when debugging projection, sizing, or layer visibility issues.
- Clear notes on coordinate order, projection assumptions, and view fitting.
- Framework-specific lifecycle handling when relevant.
- A smaller reproducible example first if the original integration is noisy.
Practical Commands
Generate a minimal OpenLayers page
CODEBLOCK0
Generate a GeoJSON-ready OpenLayers page
CODEBLOCK1
OpenClaw + ClawHub Notes
- - Keep examples generic, portable, and safe to paste into local projects.
- Do not hardcode private tile services, private tokens, or machine-specific paths.
- Prefer standards-based examples and explicit projection handling over hidden magic.
- For clawhub.ai publication, keep examples reproducible and version/changelog updates semver-driven.
Reference Docs In This Skill
- - Read
{baseDir}/references/patterns.md when generating or fixing OpenLayers code. - Read
{baseDir}/references/layer-recipes.md when the task involves XYZ, OSM, GeoJSON, WMS, WMTS, or feature interaction patterns.
OpenLayers
使用此技能在浏览器应用中进行实际的OpenLayers Web地图工作。
当用户需要比Leaflet更强大的GIS风格2D Web地图栈时,OpenLayers是合适的选择,特别适用于投影感知工作流、切片和矢量图层、自定义样式、要素交互,以及对地图/视图/源/图层组合的更显式控制。
工作流程
- 1. 首先确认运行环境:
- 纯HTML
- Vite
- React
- Next.js或其他SPA框架
- 2. 确认实际地图任务:
- 仅底图
- 标记或选定要素
- GeoJSON叠加层
- WMS或WMTS图层
- 绘制或修改交互
- 错误修复或迁移
- 3. 在添加自定义源、交互或大型数据集之前,从最小的可见地图开始。
- 在调试图层逻辑之前,检查容器大小、CSS和投影假设。
- 保持坐标变换显式化;OpenLayers通常需要fromLonLat(...)或显式的dataProjection和featureProjection。
实现约束
- - 在初始化Map之前,确保地图容器具有实际高度。
- 保持View投影、输入数据投影和显示投影的显式性。
- 对于显示在默认Web墨卡托地图上的地理坐标,使用fromLonLat([lng, lat])。
- 对于GeoJSON,当源CRS已知时,同时设置dataProjection和featureProjection。
- 优先使用稳定的图层和源实例,而不是在每次状态更新时重建整个地图。
- 在SPA的销毁路径中清理地图目标。
- 当数据集是动态的时,根据实际要素或源范围适配边界。
- 确定性样式化要素;除非用户需要数据驱动样式,否则不要将业务逻辑隐藏在样式函数中。
常见故障检查
- - 空白地图:检查容器高度、CSS导入、地图目标和图层源URL。
- 要素位置错误:检查是否在没有使用fromLonLat(...)的情况下使用了经纬度,或者是否省略了GeoJSON投影。
- 点击拾取无效:检查图层可见性、z-index排序和forEachFeatureAtPixel(...)的使用。
- 布局更改后地图偏移:在容器变为可见或调整大小后调用map.updateSize()。
- 大量要素的性能问题:简化数据、聚类点、减少样式变化,或将昂贵逻辑移出指针处理程序。
- WMS或WMTS不渲染:检查服务URL、图层名称、矩阵集、CRS和跨域行为。
实用模式
最小地图
- - 创建一个Map、一个View和一个基础TileLayer。
- 仅在确认切片URL和归属要求后使用OSM或XYZ。
- 从地理坐标开始时,使用fromLonLat(...)设置中心点。
GeoJSON叠加层
- - 使用带有GeoJSON格式对象的VectorSource。
- 当精度重要时,显式设置dataProjection和featureProjection。
- 在要素加载后,将视图适配到vectorSource.getExtent()。
交互式拾取
- - 使用map.on(singleclick, ...)进行主要要素选择。
- 使用forEachFeatureAtPixel(...)识别顶部可见要素。
- 尽可能将弹出窗口或侧面板渲染保持在OpenLayers样式层之外。
SPA集成
- - 在容器挂载后创建一次地图。
- 在更新之间重用Source和Layer对象。
- 在销毁时,调用map.setTarget(undefined)释放DOM绑定。
绘制和编辑
- - 仅在用户明确需要编辑时添加Draw、Modify或Select。
- 在持久化编辑后的几何图形之前,确认输出格式和投影。
任务边界
- - 使用此技能处理OpenLayers特定的实现、调试和迁移模式。
- 对于CRS和重投影推理,酌情使用project、wgs84或cgcs2000。
- 对于基于文件的GIS转换或批量重投影,使用qgis。
- 对于轻量级非GIS密集型Web地图,leaflet可能更简单。
- 对于Mapbox GL样式表达式或托管的Mapbox平台API,使用mapbox。
- 对于地球和3D场景渲染,使用cesium。
返回内容
- - 一个最小可用的OpenLayers设置或针对性的修复。
- 调试投影、大小或图层可见性问题时的可能根本原因。
- 关于坐标顺序、投影假设和视图适配的清晰说明。
- 相关时的框架特定生命周期处理。
- 如果原始集成过于复杂,首先提供一个较小的可重现示例。
实用命令
生成最小OpenLayers页面
bash
python3 {baseDir}/scripts/openlayers_scaffold.py minimal --out ./openlayers-demo.html
生成支持GeoJSON的OpenLayers页面
bash
python3 {baseDir}/scripts/openlayers_scaffold.py geojson --out ./openlayers-geojson.html --geojson ./data/sample.geojson
OpenClaw + ClawHub 说明
- - 保持示例通用、可移植,并且可以安全粘贴到本地项目中。
- 不要硬编码私有切片服务、私有令牌或特定机器的路径。
- 优先使用基于标准的示例和显式投影处理,而不是隐藏的魔法。
- 对于clawhub.ai发布,保持示例可重现,版本/变更日志更新遵循语义化版本控制。
此技能中的参考文档
- - 在生成或修复OpenLayers代码时,阅读{baseDir}/references/patterns.md。
- 当任务涉及XYZ、OSM、GeoJSON、WMS、WMTS或要素交互模式时,阅读{baseDir}/references/layer-recipes.md。