Mapbox Style Quality Skill
This skill provides expert guidance on ensuring Mapbox style quality through validation, accessibility, and optimization tools.
When to Use Quality Tools
Pre-Production Checklist
Before deploying any Mapbox style to production:
- 1. Validate all expressions - Catch syntax errors before runtime
- Check color contrast - Ensure text is readable (WCAG compliance)
- Validate GeoJSON sources - Ensure data integrity
- Optimize style - Reduce file size and improve performance
- Compare versions - Understand what changed
- Remove empty layers - Delete layers with no visible paint properties as a final cleanup step
- Simplify redundant boolean expressions - Clean up filters with unnecessary boolean logic (e.g.,
["all", expr] → expr, ["any", false, expr] → expr)
During Development
When adding GeoJSON data:
- - Always validate external GeoJSON with
validate_geojson_tool before using as a source
When writing expressions:
- - Validate expressions with
validate_expression_tool as you write them - Catch type mismatches early (e.g., using string operator on number)
- Verify operator availability in your Mapbox GL JS version
- Test expressions with expected data types
When styling text/labels:
- - Check foreground/background contrast with INLINECODE6
- Aim for WCAG AA minimum (4.5:1 for normal text, 3:1 for large text)
- Use AAA standard (7:1 for normal text) for better accessibility
- Consider different background scenarios (map tiles, overlays)
Before Committing Changes
Compare style versions:
- - Use
compare_styles_tool to generate a diff report - Review all layer changes, source modifications, and expression updates
- Understand the impact of your changes
- Document significant changes in commit messages
Before Deployment
Optimize the style:
- - Run
optimize_style_tool to reduce file size - Remove unused sources that reference deleted layers
- Eliminate duplicate layers with identical properties
- Simplify redundant boolean expressions in filters (e.g., collapse
["all", expr] to expr, remove tautological conditions) - Remove empty layers (layers with no visible paint properties) as a final cleanup step
Validation Best Practices
GeoJSON Validation
Always validate when:
- - Loading GeoJSON from user uploads
- Fetching GeoJSON from external APIs
- Processing GeoJSON from third-party sources
- Converting between data formats
Common GeoJSON errors:
- - Invalid coordinate ranges (longitude > 180 or < -180)
- Unclosed polygon rings (first and last coordinates must match)
- Wrong coordinate order (should be [longitude, latitude], not [latitude, longitude])
- Missing required properties (type, coordinates, geometry)
- Invalid geometry types or nesting
Example workflow:
CODEBLOCK0
Expression Validation
Validate expressions for:
- - Filter conditions (
filter property on layers) - Data-driven styling (
paint and layout properties) - Feature state expressions
- Dynamic property calculations
Common expression errors:
- - Type mismatches (string operators on numbers)
- Invalid operator names or wrong syntax
- Wrong number of arguments for operators
- Nested expression errors
- Using unavailable operators for your GL JS version
Prevention strategies:
- - Validate as you write expressions, not at runtime
- Test expressions with representative data
- Use type checking (expectedType parameter)
- Validate in context (layer, filter, paint, layout)
Accessibility Validation
WCAG Levels:
- - AA (minimum): 4.5:1 for normal text, 3:1 for large text
- AAA (enhanced): 7:1 for normal text, 4.5:1 for large text
Text size categories:
- - Normal: < 18pt or < 14pt bold
- Large: ≥ 18pt or ≥ 14pt bold
Common scenarios to check:
- - Text labels on map tiles
- POI labels with background colors
- Custom markers with text
- UI overlays on maps
- Legend text and symbols
- Attribution text
Testing strategy:
- - Test against both light and dark map tiles
- Consider overlay backgrounds (popups, modals)
- Test in different lighting conditions (mobile outdoor use)
- Verify contrast at different zoom levels
Quality Workflow Examples
Basic Quality Check
CODEBLOCK1
Full Pre-Production Workflow
CODEBLOCK2
Troubleshooting Workflow
CODEBLOCK3
Common Issues and Solutions
Runtime Expression Errors
Problem: Map throws expression errors at runtime
Solution: Validate expressions with validate_expression_tool during development
Prevention: Add expression validation to pre-commit hooks or CI/CD
Poor Text Readability
Problem: Text labels are hard to read on map
Solution: Check contrast with check_color_contrast_tool, adjust colors to meet WCAG AA
Prevention: Test text on both light and dark backgrounds, check at different zoom levels
Large Style File Size
Problem: Style takes long to load or transfer
Solution: Run optimize_style_tool to remove redundancies and simplify
Prevention: Regularly optimize during development, remove unused sources immediately
Invalid GeoJSON Source
Problem: GeoJSON source fails to load or render
Solution: Validate with validate_geojson_tool, fix coordinate issues, verify structure
Prevention: Validate all external GeoJSON before adding to style
Unexpected Style Changes
Problem: Style changed but unsure what modified
Solution: Use compare_styles_tool to generate diff report
Prevention: Compare before/after for all significant changes, document modifications
Tool Quick Reference
| Tool | Use When | Output |
|---|
| INLINECODE19 | Adding GeoJSON sources | Valid/invalid + error list |
| INLINECODE20 |
Writing expressions | Valid/invalid + error list |
|
check_color_contrast_tool | Styling text labels | Passes/fails + WCAG levels |
|
compare_styles_tool | Reviewing changes | Diff report with paths |
|
optimize_style_tool | Before deployment | Optimized style + savings |
Reference Files
For detailed guidance on specific topics, load the relevant reference:
- -
references/optimization.md — Optimization types, strategies, recommended order, and maintenance best practices references/comparison.md — Style comparison workflows, ignoreMetadata usage, and refactoring workflowreferences/ci-integration.md — Git pre-commit hooks, CI/CD pipeline steps, and code review checklist
Load instruction: Read the reference file when the user needs in-depth guidance on that topic.
Additional Resources
Mapbox 样式质量技能
该技能提供通过验证、可访问性和优化工具确保 Mapbox 样式质量的专家指导。
何时使用质量工具
预生产检查清单
在将任何 Mapbox 样式部署到生产环境之前:
- 1. 验证所有表达式 - 在运行时之前捕获语法错误
- 检查颜色对比度 - 确保文本可读(WCAG 合规性)
- 验证 GeoJSON 源 - 确保数据完整性
- 优化样式 - 减小文件大小并提升性能
- 比较版本 - 了解变更内容
- 移除空图层 - 作为最终清理步骤,删除没有可见绘制属性的图层
- 简化冗余布尔表达式 - 清理包含不必要布尔逻辑的过滤器(例如,[all, expr] → expr,[any, false, expr] → expr)
开发期间
添加 GeoJSON 数据时:
- - 在将外部 GeoJSON 用作源之前,始终使用 validategeojsontool 进行验证
编写表达式时:
- - 在编写表达式时使用 validateexpressiontool 进行验证
- 尽早捕获类型不匹配(例如,对数字使用字符串运算符)
- 验证运算符在您的 Mapbox GL JS 版本中是否可用
- 使用预期数据类型测试表达式
设置文本/标签样式时:
- - 使用 checkcolorcontrast_tool 检查前景/背景对比度
- 目标是 WCAG AA 最低标准(普通文本 4.5:1,大文本 3:1)
- 使用 AAA 标准(普通文本 7:1)以获得更好的可访问性
- 考虑不同的背景场景(地图瓦片、叠加层)
提交更改前
比较样式版本:
- - 使用 comparestylestool 生成差异报告
- 审查所有图层更改、源修改和表达式更新
- 了解更改的影响
- 在提交信息中记录重大更改
部署前
优化样式:
- - 运行 optimizestyletool 以减小文件大小
- 移除引用已删除图层的未使用源
- 消除具有相同属性的重复图层
- 简化过滤器中的冗余布尔表达式(例如,将 [all, expr] 折叠为 expr,移除同义反复条件)
- 作为最终清理步骤,移除空图层(没有可见绘制属性的图层)
验证最佳实践
GeoJSON 验证
始终在以下情况下进行验证:
- - 从用户上传加载 GeoJSON
- 从外部 API 获取 GeoJSON
- 处理来自第三方源的 GeoJSON
- 在数据格式之间转换
常见 GeoJSON 错误:
- - 无效的坐标范围(经度 > 180 或 < -180)
- 未闭合的多边形环(首尾坐标必须匹配)
- 坐标顺序错误(应为 [经度, 纬度],而非 [纬度, 经度])
- 缺少必需属性(类型、坐标、几何)
- 无效的几何类型或嵌套
示例工作流程:
- 1. 接收 GeoJSON 数据
- 使用 validategeojsontool 验证
- 如果有效:作为源添加到样式
- 如果无效:修复错误,重新验证
表达式验证
验证以下内容的表达式:
- - 过滤条件(图层的 filter 属性)
- 数据驱动样式(paint 和 layout 属性)
- 要素状态表达式
- 动态属性计算
常见表达式错误:
- - 类型不匹配(对数字使用字符串运算符)
- 无效的运算符名称或错误语法
- 运算符参数数量错误
- 嵌套表达式错误
- 使用您的 GL JS 版本中不可用的运算符
预防策略:
- - 在编写表达式时进行验证,而非在运行时
- 使用代表性数据测试表达式
- 使用类型检查(expectedType 参数)
- 在上下文中验证(图层、过滤器、绘制、布局)
可访问性验证
WCAG 级别:
- - AA(最低):普通文本 4.5:1,大文本 3:1
- AAA(增强):普通文本 7:1,大文本 4.5:1
文本大小类别:
- - 普通:< 18pt 或 < 14pt 粗体
- 大:≥ 18pt 或 ≥ 14pt 粗体
需要检查的常见场景:
- - 地图瓦片上的文本标签
- 带背景颜色的 POI 标签
- 带文本的自定义标记
- 地图上的 UI 叠加层
- 图例文本和符号
- 归属文本
测试策略:
- - 在浅色和深色地图瓦片上测试
- 考虑叠加背景(弹出窗口、模态框)
- 在不同光照条件下测试(移动户外使用)
- 在不同缩放级别验证对比度
质量工作流程示例
基本质量检查
- 1. 验证样式中的表达式
- 检查文本图层的颜色对比度
- 根据需要优化
完整预生产工作流程
- 1. 验证所有 GeoJSON 源
- 验证所有表达式(过滤器、绘制、布局)
- 检查所有文本图层的颜色对比度
- 与之前的生产版本比较
- 优化样式
- 测试优化后的样式
- 部署
故障排除工作流程
- 1. 比较正常样式与损坏样式
- 识别差异
- 验证可疑表达式
- 如果与源相关,检查 GeoJSON 数据
- 如果是可见性问题,验证颜色对比度
常见问题与解决方案
运行时表达式错误
问题: 地图在运行时抛出表达式错误
解决方案: 在开发期间使用 validateexpressiontool 验证表达式
预防: 在预提交钩子或 CI/CD 中添加表达式验证
文本可读性差
问题: 地图上的文本标签难以阅读
解决方案: 使用 checkcolorcontrast_tool 检查对比度,调整颜色以满足 WCAG AA
预防: 在浅色和深色背景上测试文本,在不同缩放级别检查
样式文件过大
问题: 样式加载或传输耗时较长
解决方案: 运行 optimizestyletool 以移除冗余并简化
预防: 在开发期间定期优化,立即移除未使用的源
无效的 GeoJSON 源
问题: GeoJSON 源无法加载或渲染
解决方案: 使用 validategeojsontool 验证,修复坐标问题,验证结构
预防: 在将外部 GeoJSON 添加到样式之前进行验证
意外样式更改
问题: 样式已更改但不清楚修改了什么
解决方案: 使用 comparestylestool 生成差异报告
预防: 对所有重大更改进行前后比较,记录修改内容
工具快速参考
| 工具 | 使用时机 | 输出 |
|---|
| validategeojsontool | 添加 GeoJSON 源时 | 有效/无效 + 错误列表 |
| validateexpressiontool |
编写表达式时 | 有效/无效 + 错误列表 |
| check
colorcontrast_tool | 设置文本标签样式时 | 通过/失败 + WCAG 级别 |
| compare
stylestool | 审查更改时 | 包含路径的差异报告 |
| optimize
styletool | 部署前 | 优化后的样式 + 节省量 |
参考文件
如需特定主题的详细指导,请加载相关参考:
- - references/optimization.md — 优化类型、策略、推荐顺序和维护最佳实践
- references/comparison.md — 样式比较工作流程、ignoreMetadata 使用和重构工作流程
- references/ci-integration.md — Git 预提交钩子、CI/CD 流水线步骤和代码审查清单
加载说明: 当用户需要该主题的深入指导时,请阅读参考文件。
其他资源