### Install csv2geojson Globally
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Install the csv2geojson package globally to use it as a command-line tool. This command allows you to convert CSV files directly from your terminal.
```bash
npm install -g csv2geojson
csv2geojson geodata.csv > geodata.geojson
```
--------------------------------
### Use Command-Line Interface
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Terminal commands for installing and using the csv2geojson tool to convert files.
```bash
# Install globally
npm install -g csv2geojson
# Basic conversion - auto-detects lat/lon columns
csv2geojson locations.csv > locations.geojson
```
--------------------------------
### Install csv2geojson for Node.js
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Install the csv2geojson package as a project dependency for use in your Node.js applications. This command adds the package to your project's package.json.
```bash
npm install --save csv2geojson
```
--------------------------------
### Combine conversion options
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Apply multiple flags simultaneously for complex CSV files.
```bash
csv2geojson --lat latitude --lon longitude --delimiter ';' --numeric-fields "value" data.csv > output.geojson
```
--------------------------------
### Read from stdin
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Pipe data directly into the tool.
```bash
cat data.csv | csv2geojson > output.geojson
```
--------------------------------
### Initialize and Run Mocha Tests
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/test/index.html
Configures the BDD interface and triggers the test suite execution. Handles potential environment differences between standard browsers and PhantomJS.
```javascript
mocha.setup('bdd') (window.mochaPhantomJS || window.mocha).run(); // mocha.globals('getInterface');
```
--------------------------------
### Node.js API with Options
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Configure the csv2geojson conversion in Node.js by providing an options object. Specify latitude and longitude field names, and the delimiter. The callback handles errors and results.
```javascript
csv2geojson.csv2geojson(csvString, {
latfield: 'LATFIELDNAME',
lonfield: 'LONFIELDNAME',
delimiter: ','
}, function(err, data) {
});
```
--------------------------------
### csv2geojson CLI Usage
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Command-line interface for csv2geojson. Specify latitude and longitude column names, whether to output lines, and the delimiter. Defaults are provided for convenience.
```bash
➟ csv2geojson
Usage: csv2geojson --lat [string] --lon [string] --line [boolean] --delimiter [string] FILE
Options:
--lat the name of the latitude column
--lon the name of the longitude column
--line whether or not to output points as a LineString [default: false]
--delimiter the type of delimiter [default: ","]
--numeric-fields comma separated list of fields to convert to numbers
```
--------------------------------
### Convert points to LineString
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Useful for converting GPS tracks into a single line feature.
```bash
csv2geojson --line track.csv > route.geojson
```
--------------------------------
### csv2geojson Binary Usage
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Command-line usage for converting CSV/TSV files to GeoJSON.
```APIDOC
## csv2geojson Binary Usage
### Description
Converts CSV and TSV files into GeoJSON data suitable for maps using the command line.
### Method
Command Line
### Endpoint
N/A
### Parameters
#### Command Line Options
- **--lat** (string) - Required - The name of the latitude column.
- **--lon** (string) - Required - The name of the longitude column.
- **--line** (boolean) - Optional - Whether or not to output points as a LineString. Defaults to false.
- **--delimiter** (string) - Optional - The type of delimiter. Defaults to ",".
- **--numeric-fields** (string) - Optional - Comma-separated list of fields to convert to numbers.
### Usage Example
```bash
npm install -g csv2geojson
csv2geojson geodata.csv > geodata.geojson
```
### Response Example
```
➟ csv2geojson
Usage: csv2geojson --lat [string] --lon [string] --line [boolean] --delimiter [string] FILE
Options:
--lat the name of the latitude column
--lon the name of the longitude column
--line whether or not to output points as a LineString [default: false]
--delimiter the type of delimiter [default: ","]
--numeric-fields comma separated list of fields to convert to numbers
```
```
--------------------------------
### Convert files with custom delimiters
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Use the delimiter flag to handle TSV or pipe-separated files.
```bash
csv2geojson --delimiter '\t' data.tsv > output.geojson
```
```bash
csv2geojson --delimiter '|' data.dsv > output.geojson
```
--------------------------------
### guessLatHeader / guessLonHeader - Header Detection
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Utility functions that analyze row objects to find the best matching latitude and longitude column names.
```APIDOC
## guessLatHeader / guessLonHeader
### Description
Utility functions that analyze row objects to find the best matching latitude and longitude column names using regex patterns.
### Methods
- **guessLatHeader(row)**: Returns the best matching latitude header.
- **guessLonHeader(row)**: Returns the best matching longitude header.
- **isLat(fieldName)**: Returns true if the field name matches latitude patterns.
- **isLon(fieldName)**: Returns true if the field name matches longitude patterns.
```
--------------------------------
### Detect Latitude and Longitude Headers
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Utility functions to identify latitude and longitude columns based on naming conventions and regex patterns.
```javascript
const csv2geojson = require('csv2geojson');
// Detect latitude header
const row1 = { 'platinium': 'xx', 'point latitude': 'yy', 'name': 'zz' };
console.log(csv2geojson.guessLatHeader(row1)); // Output: "point latitude"
const row2 = { 'lat': 'xx', 'Latitude': 'yy' };
console.log(csv2geojson.guessLatHeader(row2)); // Output: "Latitude" (more specific match)
// Detect longitude header
const row3 = { 'this is a long header': 'xx', 'longitude': 'yy' };
console.log(csv2geojson.guessLonHeader(row3)); // Output: "longitude"
const row4 = { 'lng': 'xx', 'data': 'yy' };
console.log(csv2geojson.guessLonHeader(row4)); // Output: "lng"
// Check if a field name matches lat/lon patterns
console.log(csv2geojson.isLat('latitude')); // Output: true
console.log(csv2geojson.isLat('Lat')); // Output: true
console.log(csv2geojson.isLat('is_lat')); // Output: true
console.log(csv2geojson.isLat('platinium')); // Output: false
console.log(csv2geojson.isLon('longitude')); // Output: true
console.log(csv2geojson.isLon('lon')); // Output: true
console.log(csv2geojson.isLon('lng')); // Output: true
```
--------------------------------
### csv2geojson DSV and Auto Parsing
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Utilities for parsing delimited string data using specified or automatically detected delimiters.
```APIDOC
## csv2geojson DSV and Auto Parsing
### Description
Provides functions for parsing delimited string data, either with a specified delimiter using the `dsv` library or automatically detecting the delimiter with `auto`.
### Method
JavaScript Function Call
### Endpoint
N/A
### Functions
#### `csv2geojson.dsv(delimiter).parse(dsvString)`
- **delimiter** (string) - The delimiter to use for parsing (e.g., ',', '\t', '|').
- **dsvString** (string) - The delimited string data to parse.
#### `csv2geojson.auto(dsvString)`
- **dsvString** (string) - The delimited string data to parse.
### Request Example (dsv)
```js
csv2geojson.dsv(delimiter).parse(dsvString);
```
### Request Example (auto)
```js
csv2geojson.auto(dsvString);
```
```
--------------------------------
### Convert numeric fields
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Specify fields to be treated as numbers instead of the default string type.
```bash
csv2geojson --numeric-fields "population,elevation" cities.csv > cities.geojson
```
--------------------------------
### Convert CSV with custom coordinates
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Specify custom latitude and longitude column names for the conversion.
```bash
csv2geojson --lat y_coord --lon x_coord data.csv > output.geojson
```
--------------------------------
### Browser-based CSV conversion
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Use the library in a browser environment to convert user-provided CSV text to GeoJSON.
```html
CSV to GeoJSON Demo
```
--------------------------------
### Basic Node.js Usage
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Use the csv2geojson library in Node.js to convert a CSV string to GeoJSON. The callback function receives an error object and the resulting GeoJSON data.
```javascript
var csv2geojson = require('csv2geojson');
var geoJson = csv2geojson.csv2geojson(csvString, function(err, data) {
// err has any parsing errors
// data is the data.
});
```
--------------------------------
### csv2geojson Node.js Usage
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Programmatic usage of the csv2geojson library within a Node.js application.
```APIDOC
## csv2geojson Node.js Usage
### Description
Use the csv2geojson library in your Node.js projects to convert CSV strings to GeoJSON objects.
### Method
JavaScript Function Call
### Endpoint
N/A
### Parameters
#### `csv2geojson.csv2geojson(csvString, [options], callback)`
- **csvString** (string) - The CSV data as a string.
- **options** (object) - Optional. Configuration object.
- **latfield** (string) - The name of the latitude field.
- **lonfield** (string) - The name of the longitude field.
- **delimiter** (string) - The delimiter used in the CSV. Can be ',', '\t', '|', or 'auto'.
- **callback** (function) - A function to be called with the result.
- **err** - An error object if parsing fails.
- **data** - The resulting GeoJSON object.
### Request Example
```js
var csv2geojson = require('csv2geojson');
var geoJson = csv2geojson.csv2geojson(csvString, function(err, data) {
// err has any parsing errors
// data is the data.
});
```
### Request Example with Options
```js
csv2geojson.csv2geojson(csvString, {
latfield: 'LATFIELDNAME',
lonfield: 'LONFIELDNAME',
delimiter: ','
}, function(err, data) {
});
```
### Response Example
- **data** (object) - A GeoJSON FeatureCollection object.
- **err** (object) - An error object if parsing fails.
```
--------------------------------
### dsv - Direct DSV Parsing Access
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Provides direct access to the underlying d3-dsv library for custom delimiter parsing.
```APIDOC
## dsv
### Description
Provides direct access to the underlying d3-dsv library for custom delimiter parsing. Use dsvFormat to create a parser for any delimiter character.
### Methods
- **dsvFormat(delimiter)**: Creates a parser for the specified delimiter.
- **csv(data)**: Built-in CSV parser.
- **tsv(data)**: Built-in TSV parser.
```
--------------------------------
### toLine - Convert Points to LineString
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Transforms a GeoJSON FeatureCollection of Point features into a single LineString feature, aggregating properties into arrays.
```APIDOC
## toLine
### Description
Transforms a GeoJSON FeatureCollection of Point features into a single LineString feature. The coordinates are ordered as they appear in the input, and all properties from individual points are aggregated into arrays.
### Parameters
#### Request Body
- **pointsGeoJson** (Object) - Required - A GeoJSON FeatureCollection of Point features.
### Response
#### Success Response (200)
- **FeatureCollection** (Object) - A GeoJSON FeatureCollection containing a single LineString feature.
```
--------------------------------
### Detect Delimiters and Parse DSV with auto
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Automatically identifies the delimiter in a string and parses it into an array of objects. This can be chained with the main conversion function for full processing.
```javascript
const csv2geojson = require('csv2geojson');
// Auto-detect comma-separated
const csvString = `lat,lon,name
1,2,PointA
3,4,PointB`;
const parsedCsv = csv2geojson.auto(csvString);
console.log(parsedCsv);
// Output: [{ lat: "1", lon: "2", name: "PointA" }, { lat: "3", lon: "4", name: "PointB" }]
// Auto-detect tab-separated
const tsvString = `lat\tlon\tname
1\t2\tPointA
3\t4\tPointB`;
const parsedTsv = csv2geojson.auto(tsvString);
console.log(parsedTsv);
// Output: [{ lat: "1", lon: "2", name: "PointA" }, { lat: "3", lon: "4", name: "PointB" }]
// Auto-detect pipe-separated
const pipeString = `lat|lon|name
1|2|PointA`;
const parsedPipe = csv2geojson.auto(pipeString);
// Auto-detect semicolon-separated
const semicolonString = `lat;lon;name
1;2;PointA`;
const parsedSemi = csv2geojson.auto(semicolonString);
// Chain with csv2geojson for full conversion
csv2geojson.csv2geojson(csv2geojson.auto(csvString), function(err, geojson) {
console.log(JSON.stringify(geojson, null, 2));
});
// Returns null for invalid/empty input
const invalid = csv2geojson.auto('');
console.log(invalid); // Output: null
```
--------------------------------
### Parse CSV with Auto Delimiter Detection
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Use the `dsv` method with an automatically detected delimiter to parse a string. This is useful when the delimiter is unknown or varies.
```javascript
csv2geojson.dsv(delimiter).parse(dsvString);
```
--------------------------------
### csv2geojson.auto - Automatic Delimiter Detection
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Automatically detects the delimiter used in a DSV (delimiter-separated values) string and parses it into an array of objects. It tests common delimiters like comma, semicolon, tab, and pipe.
```APIDOC
## csv2geojson.auto(dsvString)
### Description
Automatically detects the delimiter in a delimiter-separated values (DSV) string and parses it into an array of objects. It tests comma, semicolon, tab, and pipe delimiters.
### Method
`csv2geojson.auto`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **dsvString** (string) - Required - The DSV data as a string.
### Request Example
```javascript
const csvString = `lat,lon,name\n1,2,PointA\n3,4,PointB`;
const parsedCsv = csv2geojson.auto(csvString);
console.log(parsedCsv);
```
### Response
#### Success Response (200)
- **array** - An array of objects, where each object represents a row in the DSV string.
#### Response Example
```json
[
{ "lat": "1", "lon": "2", "name": "PointA" },
{ "lat": "3", "lon": "4", "name": "PointB" }
]
```
### Error Handling
- Returns `null` for invalid or empty input.
```
--------------------------------
### Access DSV Parsing
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Provides direct access to d3-dsv for custom delimiters and includes built-in CSV and TSV parsing methods.
```javascript
const csv2geojson = require('csv2geojson');
// Parse with specific delimiter using dsv.dsvFormat
const pipeData = `name|value|lat|lon
foo|42|1.5|2.5
bar|17|3.5|4.5`;
const parsed = csv2geojson.dsv.dsvFormat('|').parse(pipeData);
console.log(parsed);
// Output: [{ name: "foo", value: "42", lat: "1.5", lon: "2.5" }, { name: "bar", value: "17", lat: "3.5", lon: "4.5" }]
// Use built-in CSV and TSV parsers
const csvData = `a,b,c
1,2,3`;
console.log(csv2geojson.csv(csvData));
// Output: [{ a: "1", b: "2", c: "3" }]
const tsvData = `a\tb\tc
1\t2\t3`;
console.log(csv2geojson.tsv(tsvData));
// Output: [{ a: "1", b: "2", c: "3" }]
```
--------------------------------
### csv2geojson Polygon and Line Conversion
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Functions to convert GeoJSON point collections into polygons or lines.
```APIDOC
## csv2geojson Polygon and Line Conversion
### Description
These functions take a GeoJSON object consisting of points and derive a new GeoJSON object representing a polygon or a line, using the coordinates of those points in the order they are provided.
### Method
JavaScript Function Call
### Endpoint
N/A
### Functions
#### `csv2geojson.toPolygon(gj)`
- **gj** (object) - A GeoJSON object containing points.
#### `csv2geojson.toLine(gj)`
- **gj** (object) - A GeoJSON object containing points.
### Request Example (toPolygon)
```js
csv2geojson.toPolygon(gj);
```
### Request Example (toLine)
```js
csv2geojson.toLine(gj);
```
```
--------------------------------
### Convert Points to LineString
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Transforms a GeoJSON FeatureCollection of Point features into a single LineString feature, aggregating properties into arrays.
```javascript
const csv2geojson = require('csv2geojson');
const trackCsv = `lat,lon,timestamp,speed
47.6062,-122.3321,2024-01-01T10:00:00Z,5.2
47.6072,-122.3331,2024-01-01T10:05:00Z,6.1
47.6082,-122.3341,2024-01-01T10:10:00Z,5.8
47.6092,-122.3351,2024-01-01T10:15:00Z,6.5`;
csv2geojson.csv2geojson(trackCsv, function(err, pointsGeoJson) {
const lineGeoJson = csv2geojson.toLine(pointsGeoJson);
console.log(JSON.stringify(lineGeoJson, null, 2));
});
```
--------------------------------
### toPolygon - Convert Points to Polygon
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Transforms a GeoJSON FeatureCollection of Point features into a single Polygon feature.
```APIDOC
## toPolygon
### Description
Transforms a GeoJSON FeatureCollection of Point features into a single Polygon feature. The coordinates form the exterior ring of the polygon in the order they appear. Properties from all points are aggregated into arrays.
### Parameters
#### Request Body
- **pointsGeoJson** (Object) - Required - A GeoJSON FeatureCollection of Point features.
### Response
#### Success Response (200)
- **FeatureCollection** (Object) - A GeoJSON FeatureCollection containing a single Polygon feature.
```
--------------------------------
### Convert Points to Polygon
Source: https://context7.com/mapbox/csv2geojson/llms.txt
Transforms a GeoJSON FeatureCollection of Point features into a single Polygon feature, where points form the exterior ring.
```javascript
const csv2geojson = require('csv2geojson');
const boundaryCsv = `lat,lon,marker
40.0,0.0,A
40.0,10.0,B
50.0,10.0,C
50.0,0.0,D`;
csv2geojson.csv2geojson(boundaryCsv, function(err, pointsGeoJson) {
const polygonGeoJson = csv2geojson.toPolygon(pointsGeoJson);
console.log(JSON.stringify(polygonGeoJson, null, 2));
});
```
--------------------------------
### Convert GeoJSON Points to Polygon or LineString
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
Transform a GeoJSON FeatureCollection of points into a single polygon or LineString. The order of points in the input determines the shape of the output.
```javascript
csv2geojson.toPolygon(gj);
csv2geojson.toLine(gj);
```
--------------------------------
### csv2geojson - Main Conversion Function
Source: https://context7.com/mapbox/csv2geojson/llms.txt
The primary function for converting CSV/TSV string data into a GeoJSON FeatureCollection. It handles automatic detection of latitude and longitude columns, supports various delimiters, and can parse sexagesimal coordinates.
```APIDOC
## csv2geojson.csv2geojson(csvData, [options], callback)
### Description
Converts CSV/TSV string data into a GeoJSON FeatureCollection. Automatically detects latitude and longitude columns, supports multiple delimiters, and parses sexagesimal coordinates. Returns errors for invalid coordinate values while processing valid rows.
### Method
`csv2geojson.csv2geojson`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **csvData** (string) - Required - The CSV or TSV data as a string.
- **options** (object) - Optional - Configuration options:
- **latfield** (string) - The name of the latitude column.
- **lonfield** (string) - The name of the longitude column.
- **delimiter** (string) - The delimiter character (e.g., ',', '|', '\t').
- **numericFields** (string or array) - Fields to be parsed as numbers.
- **includeLatLon** (boolean) - If true, latitude and longitude columns are included in the properties.
- **crs** (string) - The Coordinate Reference System identifier (e.g., 'urn:ogc:def:crs:OGC:1.3:CRS84').
- **callback** (function) - Required - A callback function `(err, data)` that receives an error object (if any) and the GeoJSON data.
### Request Example
```javascript
const csvData = `lat,lon,name,population\n40.7128,-74.0060,New York,8336817\n34.0522,-118.2437,Los Angeles,3979576`;
csv2geojson.csv2geojson(csvData, function(err, data) {
if (err) console.error('Errors:', err);
console.log(JSON.stringify(data, null, 2));
});
```
### Response
#### Success Response (200)
- **type** (string) - Always 'FeatureCollection'.
- **features** (array) - An array of GeoJSON features.
- **type** (string) - Always 'Feature'.
- **properties** (object) - Key-value pairs from the CSV row.
- **geometry** (object) - GeoJSON geometry object (e.g., Point).
- **type** (string) - Geometry type (e.g., 'Point').
- **coordinates** (array) - Coordinates of the geometry.
#### Response Example
```json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": { "name": "New York", "population": "8336817" },
"geometry": { "type": "Point", "coordinates": [-74.006, 40.7128] }
}
]
}
```
```
--------------------------------
### Automatically Detect and Parse Delimiter
Source: https://github.com/mapbox/csv2geojson/blob/gh-pages/README.md
The `auto` function in csv2geojson automatically detects the delimiter in a string and parses it. This simplifies handling various delimited file formats.
```javascript
csv2geojson.auto(dsvString);
```
--------------------------------
### Convert CSV to GeoJSON with csv2geojson
Source: https://context7.com/mapbox/csv2geojson/llms.txt
The primary function for converting CSV/TSV strings to GeoJSON FeatureCollections. It supports custom field mapping, delimiter specification, numeric field casting, and coordinate reference system definitions.
```javascript
const csv2geojson = require('csv2geojson');
// Basic usage with automatic lat/lon detection
const csvData = `lat,lon,name,population
40.7128,-74.0060,New York,8336817
34.0522,-118.2437,Los Angeles,3979576
41.8781,-87.6298,Chicago,2693976`;
csv2geojson.csv2geojson(csvData, function(err, data) {
if (err) console.error('Errors:', err);
console.log(JSON.stringify(data, null, 2));
});
// Output:
// {
// "type": "FeatureCollection",
// "features": [
// {
// "type": "Feature",
// "properties": { "name": "New York", "population": "8336817" },
// "geometry": { "type": "Point", "coordinates": [-74.006, 40.7128] }
// },
// ...
// ]
// }
// With custom options: custom field names, delimiter, and numeric fields
const customCsv = `y|x|city|pop
40.7128|-74.0060|New York|8336817
34.0522|-118.2437|Los Angeles|3979576`;
csv2geojson.csv2geojson(customCsv, {
latfield: 'y',
lonfield: 'x',
delimiter: '|',
numericFields: 'pop'
}, function(err, data) {
if (err) console.error('Errors:', err);
console.log(JSON.stringify(data, null, 2));
});
// Output features will have pop as number: { "city": "New York", "pop": 8336817 }
// With sexagesimal (degrees/minutes/seconds) coordinates
const sexagesimalCsv = `lat,lon,name
23°30'15"N,46°15'30"W,Location A
40°26'46"N,79°58'56"W,Pittsburgh`;
csv2geojson.csv2geojson(sexagesimalCsv, function(err, data) {
console.log(JSON.stringify(data, null, 2));
// Coordinates are automatically converted to decimal degrees
});
// With CRS (Coordinate Reference System) option
csv2geojson.csv2geojson(csvData, {
crs: 'urn:ogc:def:crs:OGC:1.3:CRS84'
}, function(err, data) {
console.log(data.crs);
// Output: { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }
});
// Include lat/lon in properties (not removed by default)
csv2geojson.csv2geojson(csvData, {
includeLatLon: true
}, function(err, data) {
// Properties will include: { "lat": "40.7128", "lon": "-74.0060", "name": "New York", ... }
});
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.