geojson_to_topojson
Convert GeoJSON to TopoJSON format to reduce file size by sharing boundaries between adjacent features, making geographic data more compact for web applications.
Instructions
Convert GeoJSON to TopoJSON format (more compact with shared boundaries)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| geojson | Yes | GeoJSON object to convert | |
| objectName | No | Name of the TopoJSON object to create | data |
| quantization | No | Quantization parameter for simplification (0 to disable) |
Implementation Reference
- src/index.ts:504-539 (handler)The main handler function that executes the geojson_to_topojson tool. Converts GeoJSON to TopoJSON using topojsonServer.topology() and optionally applies quantization with topojsonClient.quantize().async geojsonToTopoJSON(args: any): Promise<ToolResponse> { const { geojson, objectName = 'data', quantization = 1e4 } = args; if (!geojson) { throw new McpError( ErrorCode.InvalidParams, 'Missing required parameter: geojson' ); } try { console.error('[Converting] GeoJSON to TopoJSON'); // Create a topology object from the GeoJSON const objectsMap: Record<string, any> = {}; objectsMap[objectName] = geojson; // Generate the topology const topology = topojsonServer.topology(objectsMap); // Apply quantization if specified let result = topology; if (quantization > 0) { // Use type assertion to work around TypeScript type incompatibility result = topojsonClient.quantize(topology as any, quantization); } return this.formatToolResponse(JSON.stringify(result, null, 2)); } catch (error) { console.error('[Error] GeoJSON to TopoJSON conversion failed:', error); throw new McpError( ErrorCode.InternalError, `GeoJSON to TopoJSON conversion failed: ${error instanceof Error ? error.message : String(error)}` ); } }
- src/index.ts:168-186 (schema)Input schema definition for the geojson_to_topojson tool, specifying required geojson object and optional objectName and quantization parameters.inputSchema: { type: 'object', properties: { geojson: { type: 'object', description: 'GeoJSON object to convert', }, objectName: { type: 'string', description: 'Name of the TopoJSON object to create', default: 'data', }, quantization: { type: 'number', description: 'Quantization parameter for simplification (0 to disable)', default: 1e4, }, }, required: ['geojson'],
- src/index.ts:165-187 (registration)Registration of the geojson_to_topojson tool in the ListToolsRequestSchema handler, including name, description, and input schema.{ name: 'geojson_to_topojson', description: 'Convert GeoJSON to TopoJSON format (more compact with shared boundaries)', inputSchema: { type: 'object', properties: { geojson: { type: 'object', description: 'GeoJSON object to convert', }, objectName: { type: 'string', description: 'Name of the TopoJSON object to create', default: 'data', }, quantization: { type: 'number', description: 'Quantization parameter for simplification (0 to disable)', default: 1e4, }, }, required: ['geojson'], },