csv_to_geojson
Convert CSV files containing geographic coordinates into GeoJSON format for mapping and spatial analysis by specifying latitude and longitude fields.
Instructions
Convert CSV with geographic data to GeoJSON
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| csv | Yes | CSV string to convert | |
| latfield | Yes | Field name for latitude | |
| lonfield | Yes | Field name for longitude | |
| delimiter | No | CSV delimiter (default is comma) | , |
Implementation Reference
- src/index.ts:375-413 (handler)The handler function csvToGeoJSON that performs the CSV to GeoJSON conversion using the csv2geojson library. It takes CSV string, latitude and longitude field names, optional delimiter, validates inputs, calls the library's csv2geojson function, and returns formatted GeoJSON.async csvToGeoJSON(args: any): Promise<ToolResponse> { const { csv, latfield, lonfield, delimiter = ',' } = args; if (!csv || !latfield || !lonfield) { throw new McpError( ErrorCode.InvalidParams, 'Missing required parameters: csv, latfield, lonfield' ); } return new Promise<ToolResponse>((resolve, reject) => { try { console.error(`[Converting] CSV to GeoJSON using lat field ${latfield} and lon field ${lonfield}`); csv2geojson.csv2geojson(csv, { latfield, lonfield, delimiter }, (err: Error | null, data: any) => { if (err) { console.error('[Error] CSV to GeoJSON conversion failed:', err); reject(new McpError( ErrorCode.InternalError, `CSV to GeoJSON conversion failed: ${err.message}` )); return; } resolve(this.formatToolResponse(JSON.stringify(data, null, 2))); }); } catch (error) { console.error('[Error] CSV to GeoJSON conversion failed:', error); reject(new McpError( ErrorCode.InternalError, `CSV to GeoJSON conversion failed: ${error instanceof Error ? error.message : String(error)}` )); } }); }
- src/index.ts:120-143 (schema)Tool registration in listTools response including name, description, and inputSchema defining parameters: csv (string, required), latfield (string, required), lonfield (string, required), delimiter (string, optional default ',').name: 'csv_to_geojson', description: 'Convert CSV with geographic data to GeoJSON', inputSchema: { type: 'object', properties: { csv: { type: 'string', description: 'CSV string to convert', }, latfield: { type: 'string', description: 'Field name for latitude', }, lonfield: { type: 'string', description: 'Field name for longitude', }, delimiter: { type: 'string', description: 'CSV delimiter (default is comma)', default: ',', }, }, required: ['csv', 'latfield', 'lonfield'],
- src/index.ts:284-285 (registration)Dispatcher switch case in CallToolRequest handler that routes 'csv_to_geojson' calls to the csvToGeoJSON method.case 'csv_to_geojson': return await this.csvToGeoJSON(request.params.arguments);