Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ https://github.com/user-attachments/assets/8b1b8ef2-9fba-4951-bc9a-beaed4f6aff6
- [GeoJSON Preview tool (Beta)](#geojson-preview-tool-beta)
- [Coordinate Conversion tool](#coordinate-conversion-tool)
- [Bounding Box tool](#bounding-box-tool)
- [Style Optimization tool](#style-optimization-tool)
- [Resources](#resources)
- [Observability \& Tracing](#observability--tracing)
- [Features](#features)
Expand Down Expand Up @@ -453,6 +454,70 @@ An array of four numbers representing the bounding box: `[minX, minY, maxX, maxY
- "Calculate the bounding box of this GeoJSON file" (then upload a .geojson file)
- "What's the bounding box for the coordinates in the uploaded parks.geojson file?"

#### Style Optimization tool

Optimizes Mapbox styles by removing redundancies, simplifying expressions, and reducing file size.

**Parameters:**

- `style` (string or object, required): Mapbox style to optimize (JSON string or style object)
- `optimizations` (array, optional): Specific optimizations to apply. If not specified, all optimizations are applied. Available optimizations:
- `remove-unused-sources`: Remove sources not referenced by any layer
- `remove-duplicate-layers`: Remove layers that are exact duplicates
- `simplify-expressions`: Simplify boolean expressions (e.g., `["all", true]` → `true`)
- `remove-empty-layers`: Remove layers with no visible properties (excluding background layers)
- `consolidate-filters`: Identify layers with identical filters that could be consolidated

**Optimizations performed:**

- **Remove unused sources**: Identifies and removes source definitions that aren't referenced by any layer
- **Remove duplicate layers**: Detects layers with identical properties (excluding ID) and removes duplicates
- **Simplify expressions**: Simplifies boolean logic in filters and property expressions:
- `["all", true]` → `true`
- `["any", false]` → `false`
- `["!", false]` → `true`
- `["!", true]` → `false`
- **Remove empty layers**: Removes layers with no paint or layout properties (background layers are preserved)
- **Consolidate filters**: Identifies groups of layers with identical filter expressions

**Returns:**

A JSON object with:

- `optimizedStyle`: The optimized Mapbox style
- `optimizations`: Array of optimizations applied
- `summary`: Statistics including size savings and percent reduction

**Example:**

```json
{
"optimizedStyle": { "version": 8, "sources": {}, "layers": [] },
"optimizations": [
{
"type": "remove-unused-sources",
"description": "Removed 2 unused source(s): unused-source1, unused-source2",
"count": 2
}
],
"summary": {
"totalOptimizations": 2,
"originalSize": 1234,
"optimizedSize": 890,
"sizeSaved": 344,
"percentReduction": 27.88
}
}
```

**Example prompts:**

- "Optimize this Mapbox style to reduce its file size"
- "Remove unused sources from my style"
- "Simplify the expressions in this style"
- "Find and remove duplicate layers in my map style"
- "Optimize my style but only remove unused sources and empty layers"

## Agent Skills

This repository includes [Agent Skills](https://agentskills.io) that provide domain expertise for building maps with Mapbox. Skills teach AI assistants about map design, security best practices, and common implementation patterns.
Expand Down
26 changes: 26 additions & 0 deletions src/tools/optimize-style-tool/OptimizeStyleTool.input.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.

import { z } from 'zod';

export const OptimizeStyleInputSchema = z.object({
style: z
.union([z.string(), z.record(z.unknown())])
.describe('Mapbox style to optimize (JSON string or style object)'),
optimizations: z
.array(
z.enum([
'remove-unused-sources',
'remove-duplicate-layers',
'simplify-expressions',
'remove-empty-layers',
'consolidate-filters'
])
)
.optional()
.describe(
'Specific optimizations to apply (if not specified, all optimizations are applied)'
)
});

export type OptimizeStyleInput = z.infer<typeof OptimizeStyleInputSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.

import { z } from 'zod';

const OptimizationSchema = z.object({
type: z.string().describe('Type of optimization applied'),
description: z.string().describe('Description of what was optimized'),
count: z.number().describe('Number of items affected by this optimization')
});

export const OptimizeStyleOutputSchema = z.object({
optimizedStyle: z.record(z.unknown()).describe('The optimized Mapbox style'),
optimizations: z
.array(OptimizationSchema)
.describe('List of optimizations that were applied'),
summary: z.object({
totalOptimizations: z
.number()
.describe('Total number of optimization operations performed'),
originalSize: z.number().describe('Original style size in bytes'),
optimizedSize: z.number().describe('Optimized style size in bytes'),
sizeSaved: z.number().describe('Bytes saved through optimization'),
percentReduction: z.number().describe('Percentage reduction in size')
})
});

export type OptimizeStyleOutput = z.infer<typeof OptimizeStyleOutputSchema>;
Loading
Loading