### Install togpx CLI tool
Source: https://github.com/tyrasd/togpx/blob/master/README.md
Instructions for installing the togpx command line interface globally via npm and executing a conversion.
```bash
npm install -g togpx
togpx file.geojson > file.gpx
```
--------------------------------
### Command Line Interface Usage
Source: https://context7.com/tyrasd/togpx/llms.txt
Explains how to install and use the togpx CLI tool to convert GeoJSON files to GPX format via terminal commands.
```bash
npm install -g togpx
togpx input.geojson > output.gpx
cat input.geojson | togpx > output.gpx
```
--------------------------------
### Browser Usage
Source: https://context7.com/tyrasd/togpx/llms.txt
Demonstrates how to use the togpx library directly in a web browser.
```APIDOC
## Browser Usage
### Description
`togpx` can be used directly in browsers by including the bundled script file.
### Method
`togpx(geojson, options)` (global function)
### Parameters
See individual option descriptions above.
### Request Example
```html
GeoJSON to GPX Converter
```
### Response Example
(Generates a downloadable GPX file named 'export.gpx' containing the converted GeoJSON data.)
```
--------------------------------
### Browser Integration and File Download
Source: https://context7.com/tyrasd/togpx/llms.txt
Demonstrates how to include togpx in a browser environment, generate GPX data, and trigger a file download using a Blob and an anchor element.
```html
GeoJSON to GPX Converter
```
--------------------------------
### Configure Custom Creator and Metadata
Source: https://context7.com/tyrasd/togpx/llms.txt
Shows how to customize the GPX creator attribute and add detailed metadata objects like author, copyright, and time.
```javascript
var togpx = require('togpx');
var gpx = togpx(geojson, {
creator: "MyGPSApp v1.0",
metadata: {
name: "My Trip Data",
author: { name: "John Doe" }
}
});
```
--------------------------------
### Convert GeoJSON to GPX using togpx
Source: https://github.com/tyrasd/togpx/blob/master/README.md
Demonstrates how to use the togpx library in both Node.js and browser environments to convert GeoJSON data into a GPX string.
```javascript
var togpx = require('togpx');
togpx(geojson_data);
```
```html
```
--------------------------------
### Convert Polygons to GPX Tracks
Source: https://context7.com/tyrasd/togpx/llms.txt
Demonstrates how Polygon geometries are mapped to GPX track elements representing the boundary of the polygon.
```javascript
var togpx = require('togpx');
var polygon = {
type: "Feature",
properties: { name: "Area Boundary" },
geometry: {
type: "Polygon",
coordinates: [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]
}
};
var gpx = togpx(polygon);
```
--------------------------------
### Convert LineStrings to GPX Tracks
Source: https://context7.com/tyrasd/togpx/llms.txt
Shows how LineString geometries are automatically converted into GPX track elements with segments.
```javascript
var togpx = require('togpx');
var route = {
type: "Feature",
properties: { name: "Morning Run" },
geometry: {
type: "LineString",
coordinates: [
[-122.4194, 37.7749],
[-122.4089, 37.7849],
[-122.3984, 37.7949]
]
}
};
var gpx = togpx(route);
```
--------------------------------
### Custom Feature Description Callback
Source: https://context7.com/tyrasd/togpx/llms.txt
Defines a callback function to construct the GPX element from feature properties.
```APIDOC
## Custom Feature Description Callback
### Description
The `featureDescription` option defines a callback to construct the GPX `` element from feature properties.
### Method
`togpx(geojson, options)`
### Parameters
#### Options
- **featureDescription** (function) - Optional - A callback function that receives feature properties and returns a string for the GPX `` element.
### Request Example
```javascript
var togpx = require('togpx');
var geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
name: "Historic Landmark",
year_built: 1892,
architect: "Famous Builder",
open_hours: "9am-5pm"
},
geometry: {
type: "Point",
coordinates: [-77.0365, 38.8977]
}
}]
};
var gpx = togpx(geojson, {
featureDescription: function(properties) {
return "Built in " + properties.year_built +
" by " + properties.architect +
". Open: " + properties.open_hours;
}
});
// Output waypoint : "Built in 1892 by Famous Builder. Open: 9am-5pm"
```
### Response Example
```xml
Built in 1892 by Famous Builder. Open: 9am-5pm
```
```
--------------------------------
### Convert MultiPoint to GPX Waypoints (JavaScript)
Source: https://context7.com/tyrasd/togpx/llms.txt
Demonstrates how to convert a GeoJSON MultiPoint feature into multiple GPX waypoints using the togpx library. Each point in the MultiPoint geometry becomes a separate element in the GPX output.
```javascript
var togpx = require('togpx');
var multipleLocations = {
type: "Feature",
properties: { name: "Tour Stops" },
geometry: {
type: "MultiPoint",
coordinates: [
[-0.1278, 51.5074], // London
[2.3522, 48.8566], // Paris
[13.4050, 52.5200] // Berlin
]
}
};
var gpx = togpx(multipleLocations);
// Creates three separate elements, one for each point
```
--------------------------------
### Convert GeoJSON to GPX using JavaScript
Source: https://context7.com/tyrasd/togpx/llms.txt
Demonstrates the basic usage of the togpx function to convert a GeoJSON FeatureCollection into a GPX XML string.
```javascript
var togpx = require('togpx');
var geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
name: "Central Park",
type: "park"
},
geometry: {
type: "Point",
coordinates: [-73.965355, 40.782865]
}
}]
};
var gpxString = togpx(geojson);
```
--------------------------------
### Include Elevation Data in GPX
Source: https://context7.com/tyrasd/togpx/llms.txt
Illustrates how to preserve altitude data from GeoJSON coordinates in the resulting GPX output.
```javascript
var togpx = require('togpx');
var hikingTrail = {
type: "Feature",
properties: { name: "Mountain Trail" },
geometry: {
type: "LineString",
coordinates: [
[-105.2705, 40.0150, 1650],
[-105.2710, 40.0160, 1700]
]
}
};
var gpx = togpx(hikingTrail);
```
--------------------------------
### Customize GPX Feature Description with featureDescription
Source: https://context7.com/tyrasd/togpx/llms.txt
The featureDescription callback allows you to construct the GPX element based on specific feature properties. This is useful for including detailed metadata like timestamps or custom attributes.
```javascript
var togpx = require('togpx');
var geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
name: "Historic Landmark",
year_built: 1892,
architect: "Famous Builder",
open_hours: "9am-5pm"
},
geometry: {
type: "Point",
coordinates: [-77.0365, 38.8977]
}
}]
};
var gpx = togpx(geojson, {
featureDescription: function(properties) {
return "Built in " + properties.year_built +
" by " + properties.architect +
". Open: " + properties.open_hours;
}
});
```
--------------------------------
### Convert GeometryCollection to GPX Elements (JavaScript)
Source: https://context7.com/tyrasd/togpx/llms.txt
Illustrates the conversion of a GeoJSON GeometryCollection feature into corresponding GPX elements using the togpx library. Each geometry within the collection is processed individually, resulting in appropriate GPX elements like for Points and for LineStrings.
```javascript
var togpx = require('togpx');
var mixedGeometry = {
type: "Feature",
properties: { name: "Mixed Data" },
geometry: {
type: "GeometryCollection",
geometries: [
{
type: "Point",
coordinates: [100.0, 0.0]
},
{
type: "LineString",
coordinates: [[101.0, 0.0], [102.0, 1.0]]
}
]
}
};
var gpx = togpx(mixedGeometry);
// Creates one and one element from the GeometryCollection
```
--------------------------------
### Add GPX Feature Links with featureLink
Source: https://context7.com/tyrasd/togpx/llms.txt
The featureLink callback enables the creation of URL links within the GPX output for each feature. It takes feature properties as input and returns a string URL.
```javascript
var togpx = require('togpx');
var geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
name: "Museum",
website_id: "natural-history-museum"
},
geometry: {
type: "Point",
coordinates: [-77.026, 38.891]
}
}]
};
var gpx = togpx(geojson, {
featureLink: function(properties) {
return "https://example.com/places/" + properties.website_id;
}
});
```
--------------------------------
### Custom Feature Title Callback
Source: https://context7.com/tyrasd/togpx/llms.txt
Defines a callback function to construct the GPX element from feature properties.
```APIDOC
## Custom Feature Title Callback
### Description
The `featureTitle` option defines a callback function to construct the GPX `` element from feature properties.
### Method
`togpx(geojson, options)`
### Parameters
#### Options
- **featureTitle** (function) - Optional - A callback function that receives feature properties and returns a string for the GPX `` element.
### Request Example
```javascript
var togpx = require('togpx');
var geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
poi_type: "Restaurant",
poi_name: "The Great Diner",
rating: 4.5
},
geometry: {
type: "Point",
coordinates: [-73.985, 40.748]
}
}]
};
var gpx = togpx(geojson, {
featureTitle: function(properties) {
return properties.poi_name + " (" + properties.poi_type + ")";
}
});
// Output waypoint : "The Great Diner (Restaurant)"
```
### Response Example
```xml
The Great Diner (Restaurant)
```
```
--------------------------------
### Custom Feature Link Callback
Source: https://context7.com/tyrasd/togpx/llms.txt
Defines a callback function to construct URL links for each feature.
```APIDOC
## Custom Feature Link Callback
### Description
The `featureLink` option defines a callback to construct URL links for each feature.
### Method
`togpx(geojson, options)`
### Parameters
#### Options
- **featureLink** (function) - Optional - A callback function that receives feature properties and returns a URL string for the GPX `` element.
### Request Example
```javascript
var togpx = require('togpx');
var geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
name: "Museum",
website_id: "natural-history-museum"
},
geometry: {
type: "Point",
coordinates: [-77.026, 38.891]
}
}]
};
var gpx = togpx(geojson, {
featureLink: function(properties) {
return "https://example.com/places/" + properties.website_id;
}
});
// Output:
```
### Response Example
```xml
```
```
--------------------------------
### Custom Timestamp Callback
Source: https://context7.com/tyrasd/togpx/llms.txt
Provides timestamps for coordinates using a custom callback or property name.
```APIDOC
## Custom Timestamp Callback
### Description
The `featureCoordTimes` option allows defining a custom callback to provide timestamps for coordinates.
### Method
`togpx(geojson, options)`
### Parameters
#### Options
- **featureCoordTimes** (function or string) - Optional - A callback function that receives the feature and returns an array of timestamps, or a string specifying the property name containing the timestamps.
### Request Example
```javascript
var togpx = require('togpx');
var trackWithCustomTimes = {
type: "Feature",
properties: {
name: "Activity Track",
recorded_timestamps: [
"2024-07-01T14:00:00Z",
"2024-07-01T14:30:00Z"
]
},
geometry: {
type: "LineString",
coordinates: [
[-118.2437, 34.0522],
[-118.2500, 34.0600]
]
}
};
// Using a callback function
var gpx = togpx(trackWithCustomTimes, {
featureCoordTimes: function(feature) {
return feature.properties.recorded_timestamps;
}
});
// Or using a string to specify the property name
var gpx2 = togpx(trackWithCustomTimes, {
featureCoordTimes: "recorded_timestamps"
});
// Both produce elements with