### Install geo-polyline-tools with npm Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Use npm to install the library. This is the standard package manager for Node.js. ```bash npm install geo-polyline-tools ``` -------------------------------- ### Initialize Project Environment Source: https://github.com/barikoi/geo-polyline-tools/blob/main/DEVELOPMENT.md Commands to clone the repository and install necessary dependencies. ```bash git clone https://github.com/barikoi/geo-polyline-tools.git cd geo-polyline-tools npm install ``` -------------------------------- ### Initialize Development Environment Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Commands to clone the repository and install dependencies. ```bash git clone https://github.com//geo-polyline-tools.git cd geo-polyline-tools npm install git checkout -b feature/my-change ``` ```bash git clone https://github.com/barikoi/geo-polyline-tools.git cd geo-polyline-tools npm install ``` -------------------------------- ### TypeScript Usage Example Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Example of importing and using the library in a TypeScript project, demonstrating type safety. ```typescript import polyline from 'geo-polyline-tools'; const encoded: string = polyline.encode([[38.5, -120.2], [40.7, -120.95]], 5); ``` -------------------------------- ### Install geo-polyline-tools with yarn Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Use yarn to install the library. Yarn is an alternative package manager for Node.js. ```bash yarn add geo-polyline-tools ``` -------------------------------- ### Package Testing with Tarball Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Steps to build, pack, and install the package locally for verification. ```bash # 1. Build the project npm run build # 2. Generate a tarball npm pack # 3. Install the tarball in a test project mkdir /tmp/test-polyline && cd /tmp/test-polyline npm init -y npm install /path/to/geo-polyline-tools/geo-polyline-tools-1.0.6.tgz ``` -------------------------------- ### Conventional Commit Examples Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Examples of commit messages following the conventional commits specification for various project scopes. ```text feat(geojson): add support for MultiLineString geometries fix(decode): handle empty string input without error docs: update coordinate ordering table in README refactor(encode): simplify py2_round implementation test(decode): add edge case tests for high-precision coordinates chore(build): upgrade rollup to v4.60 types: mark precision parameter as optional ``` -------------------------------- ### Browser IIFE Usage Example Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Example of using the library in a browser after including it via an IIFE script tag. The library is accessed via the global `geoPolylineTools` object. ```javascript var encoded = geoPolylineTools.encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]], 5); console.log(encoded); // '_p~iF~ps|U_ulLnnqC_mqNvxq`@' ``` -------------------------------- ### Publishing Workflow Source: https://github.com/barikoi/geo-polyline-tools/blob/main/DEVELOPMENT.md Steps to version, validate, and publish the package to NPM. ```bash # 1. Update version in package.json npm version patch # or minor, or major # 2. Run the full validation npm run lint && npm test && npm run build # 3. Publish to npm npm publish ``` -------------------------------- ### Build Project Source: https://github.com/barikoi/geo-polyline-tools/blob/main/DEVELOPMENT.md Command to trigger the build process using Rollup. ```bash npm run build ``` -------------------------------- ### Package Publishing Workflow Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Commands for versioning, validating, and publishing the package to the npm registry. ```bash # 1. Update the version npm version patch # for bug fixes: 1.0.6 -> 1.0.7 npm version minor # for new features: 1.0.6 -> 1.1.0 npm version major # for breaking changes: 1.0.6 -> 2.0.0 # 2. Run full validation npm run lint && npm test && npm run build # 3. Publish to npm (package is configured as public in publishConfig) npm publish # 4. Push the version tag to GitHub git push origin main --follow-tags ``` -------------------------------- ### Run Test Suite Source: https://github.com/barikoi/geo-polyline-tools/blob/main/DEVELOPMENT.md Command to execute all tests using Jest. ```bash # Run all tests npm test ``` -------------------------------- ### Project Structure Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Overview of the directory layout for the project. ```text geo-polyline-tools/ ├── src/ │ └── index.ts # Library source code (encode, decode, GeoJSON helpers) ├── types/ │ └── index.d.ts # Manual type declarations (NOT auto-generated) ├── __tests__/ │ └── index.test.ts # Jest test suite ├── dist/ # Build output (generated, do not edit) │ ├── cjs/ # CommonJS bundle (index.cjs) │ ├── esm/ # ES Module bundle (index.js) │ └── iife/ # Browser IIFE bundle (geo-polyline-tools.js) ├── package.json ├── tsconfig.json ├── rollup.config.js ├── jest.config.cjs └── .eslintrc.json ``` -------------------------------- ### Package Quality Tools Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Commands to validate package configuration and type resolution. ```bash npx publint # Check package configuration npx @arethetypeswrong/cli --pack . # Verify TypeScript type resolution ``` -------------------------------- ### Project Directory Structure Source: https://github.com/barikoi/geo-polyline-tools/blob/main/DEVELOPMENT.md Visual representation of the project file layout. ```text geo-polyline-tools/ ├── src/ │ └── index.ts # Library source code ├── types/ │ └── index.d.ts # Manual type declarations ├── __tests__/ │ └── index.test.ts # Test suite ├── dist/ # Build output (generated) │ ├── cjs/ # CommonJS bundle │ ├── esm/ # ES Module bundle │ └── iife/ # Browser IIFE bundle ├── package.json ├── tsconfig.json ├── rollup.config.js ├── jest.config.cjs └── .eslintrc.json ``` -------------------------------- ### Run Tests Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Execute the Jest test suite. ```bash npm test ``` -------------------------------- ### Verify Module Formats Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Test the package using CommonJS and ESM imports. ```javascript // CommonJS const polyline = require('geo-polyline-tools'); console.log(polyline.encode([[38.5, -120.2]], 5)); ``` ```javascript // ESM import polyline from 'geo-polyline-tools'; console.log(polyline.decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@', 5)); ``` -------------------------------- ### Include geo-polyline-tools in Browser (IIFE) Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Include the library directly in your HTML using a script tag, either locally or via a CDN. This makes the library available globally. ```html ``` -------------------------------- ### Validate Package Quality Source: https://github.com/barikoi/geo-polyline-tools/blob/main/DEVELOPMENT.md Commands to verify package configuration and TypeScript types before publishing. ```bash npx publint # Check package configuration npx @arethetypeswrong/cli --pack . # Verify TypeScript types npm run build # Build the project npm pack # Generate tarball ``` -------------------------------- ### Lint Source Code Source: https://github.com/barikoi/geo-polyline-tools/blob/main/DEVELOPMENT.md Command to run ESLint for code style validation. ```bash npm run lint ``` -------------------------------- ### Import geo-polyline-tools in ES Module Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Import the library using ES Module syntax. This is common in modern JavaScript projects. ```javascript import polyline from 'geo-polyline-tools'; ``` -------------------------------- ### Import geo-polyline-tools in CommonJS Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Import the library using CommonJS syntax. This is typically used in Node.js environments. ```javascript const polyline = require('geo-polyline-tools'); ``` -------------------------------- ### Conventional Commit Format Source: https://github.com/barikoi/geo-polyline-tools/blob/main/CONTRIBUTING.md Standard structure for commit messages. ```text (): [optional body] [optional footer(s)] ``` -------------------------------- ### Encode Coordinates to Polyline String Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Converts an array of [latitude, longitude] pairs into a compressed polyline string. Supports custom precision; default is 5 decimal places. ```javascript import polyline from 'geo-polyline-tools'; // Encode a route with three waypoints const coordinates = [ [38.5, -120.2], [40.7, -120.95], [43.252, -126.453] ]; const encoded = polyline.encode(coordinates, 5); console.log(encoded); // Output: '_p~iF~ps|U_ulLnnqC_mqNvxq`@' // Encode with higher precision (6 decimal places) const highPrecisionEncoded = polyline.encode(coordinates, 6); console.log(highPrecisionEncoded); // Output: 'o}oia@~bkbK_seK~nskB_ibO`reaE' // Handle empty coordinates gracefully const emptyResult = polyline.encode([], 5); console.log(emptyResult); // Output: '' ``` -------------------------------- ### Decode Polyline String to Coordinates Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Decodes a polyline string back into an array of [latitude, longitude] pairs. The precision parameter must match the encoding precision. ```javascript import polyline from 'geo-polyline-tools'; // Decode a polyline string to coordinates const polylineString = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'; const coordinates = polyline.decode(polylineString, 5); console.log(coordinates); // Output: [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]] // Access individual coordinate points coordinates.forEach((coord, index) => { const [latitude, longitude] = coord; console.log(`Point ${index + 1}: lat=${latitude}, lng=${longitude}`); }); // Output: // Point 1: lat=38.5, lng=-120.2 // Point 2: lat=40.7, lng=-120.95 // Point 3: lat=43.252, lng=-126.453 // Handle empty polyline string const emptyCoords = polyline.decode('', 5); console.log(emptyCoords); // Output: [] ``` -------------------------------- ### polyline.encode Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Converts an array of [latitude, longitude] coordinate pairs into a compressed polyline string. Supports configurable precision. ```APIDOC ## polyline.encode ### Description Converts an array of [latitude, longitude] coordinate pairs into a compressed polyline string using Google's Encoded Polyline Algorithm. The precision parameter controls the number of decimal places preserved in the encoding, with a default of 5 (approximately 1.1 meter accuracy). ### Method `encode(coordinates: Array<[number, number]>, precision?: number): string` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import polyline from 'geo-polyline-tools'; const coordinates = [ [38.5, -120.2], [40.7, -120.95], [43.252, -126.453] ]; const encoded = polyline.encode(coordinates, 5); console.log(encoded); // Output: '_p~iF~ps|U_ulLnnqC_mqNvxq`@' ``` ### Response #### Success Response (200) - **encoded_string** (string) - The compressed polyline string. #### Response Example ```json { "encoded_string": "_p~iF~ps|U_ulLnnqC_mqNvxq`@" } ``` ``` -------------------------------- ### polyline.decode Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Decodes a polyline string back into an array of [latitude, longitude] coordinate pairs. The precision must match the encoding precision. ```APIDOC ## polyline.decode ### Description Decodes a polyline string back into an array of [latitude, longitude] coordinate pairs. The precision parameter must match the precision used during encoding to correctly reconstruct the original coordinates. ### Method `decode(encoded_string: string, precision?: number): Array<[number, number]>` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import polyline from 'geo-polyline-tools'; const polylineString = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'; const coordinates = polyline.decode(polylineString, 5); console.log(coordinates); // Output: [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]] ``` ### Response #### Success Response (200) - **coordinates** (Array<[number, number]>) - An array of [latitude, longitude] coordinate pairs. #### Response Example ```json { "coordinates": [ [38.5, -120.2], [40.7, -120.95], [43.252, -126.453] ] } ``` ``` -------------------------------- ### Convert Polyline String to GeoJSON LineString Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Decode a compressed polyline string into a GeoJSON `LineString` geometry object. The library automatically handles the `[latitude, longitude]` to `[longitude, latitude]` axis conversion. ```javascript const str = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'; const geometry = polyline.toGeoJSON(str, 5); // => { // type: 'LineString', // coordinates: [[-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252]] // } ``` -------------------------------- ### polyline.encode Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Encodes an array of [latitude, longitude] coordinate pairs into a polyline string. ```APIDOC ## polyline.encode(coordinates, precision?) ### Description Encodes an array of [latitude, longitude] coordinate pairs into a polyline string. ### Parameters #### Arguments - **coordinates** (number[][]) - Required - Array of [lat, lng] pairs. - **precision** (number) - Optional - Number of decimal places to preserve (Default: 5). ### Returns - **string** - The encoded polyline. Returns an empty string when coordinates is empty. ``` -------------------------------- ### Convert GeoJSON LineString to Polyline Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Encode a GeoJSON Feature with a `LineString` geometry into a compressed polyline string. The library automatically handles the `[longitude, latitude]` to `[latitude, longitude]` axis conversion. ```javascript const geojson = { type: 'Feature', geometry: { type: 'LineString', coordinates: [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]], }, }; const encoded = polyline.fromGeoJSON(geojson, 5); // => '~ps|U_p~iFnnqC_ulLvxq`@_mqN' ``` -------------------------------- ### Decode polyline to GeoJSON Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Converts a polyline string into a GeoJSON LineString object and demonstrates wrapping it in a FeatureCollection for mapping libraries. ```javascript import polyline from 'geo-polyline-tools'; // Decode polyline to GeoJSON LineString const polylineString = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'; const geoJson = polyline.toGeoJSON(polylineString, 5); console.log(JSON.stringify(geoJson, null, 2)); // Output: // { // "type": "LineString", // "coordinates": [ // [-120.2, 38.5], // [-120.95, 40.7], // [-126.453, 43.252] // ] // } // Use with mapping libraries (example with Leaflet-style usage) const routeData = polyline.toGeoJSON('_p~iF~ps|U_ulLnnqC_mqNvxq`@', 5); const featureCollection = { type: 'FeatureCollection', features: [{ type: 'Feature', properties: { name: 'Decoded Route' }, geometry: routeData }] }; console.log(JSON.stringify(featureCollection, null, 2)); // Ready for use with GeoJSON-compatible mapping libraries ``` -------------------------------- ### polyline.toGeoJSON Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Decodes a polyline string into a GeoJSON LineString geometry object. ```APIDOC ## polyline.toGeoJSON(str, precision?) ### Description Decodes a polyline string into a GeoJSON LineString geometry object. The resulting coordinates follow GeoJSON's [longitude, latitude] ordering. ### Parameters #### Arguments - **str** (string) - Required - The encoded polyline string. - **precision** (number) - Optional - Number of decimal places used during encoding (Default: 5). ### Returns - **{ type: 'LineString', coordinates: number[][] }** - A GeoJSON LineString geometry with [lng, lat] coordinates. ``` -------------------------------- ### polyline.toGeoJSON Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Decodes a polyline string into a GeoJSON LineString geometry object. The resulting coordinates are in GeoJSON standard order [longitude, latitude], ready for use with mapping libraries like Leaflet, Mapbox, or OpenLayers. ```APIDOC ## polyline.toGeoJSON ### Description Decodes a polyline string into a GeoJSON LineString geometry object. The resulting coordinates are in GeoJSON standard order [longitude, latitude], ready for use with mapping libraries like Leaflet, Mapbox, or OpenLayers. ### Method `polyline.toGeoJSON(encodedPolyline, precision)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import polyline from 'geo-polyline-tools'; // Decode polyline to GeoJSON LineString const polylineString = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'; const geoJson = polyline.toGeoJSON(polylineString, 5); console.log(JSON.stringify(geoJson, null, 2)); ``` ### Response #### Success Response (200) - **type** (string) - The geometry type, always "LineString". - **coordinates** (array) - An array of coordinate pairs, where each pair is [longitude, latitude]. #### Response Example ```json { "type": "LineString", "coordinates": [ [-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252] ] } ``` ### Additional Usage Examples ```javascript // Use with mapping libraries (example with Leaflet-style usage) const routeData = polyline.toGeoJSON('_p~iF~ps|U_ulLnnqC_mqNvxq`@', 5); const featureCollection = { type: 'FeatureCollection', features: [{ type: 'Feature', properties: { name: 'Decoded Route' }, geometry: routeData }] }; console.log(JSON.stringify(featureCollection, null, 2)); // Ready for use with GeoJSON-compatible mapping libraries ``` ``` -------------------------------- ### polyline.toGeoJSON Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Decodes a polyline string into a GeoJSON LineString geometry, handling coordinate order conversion. ```APIDOC ## polyline.toGeoJSON ### Description Decodes a polyline string into a GeoJSON LineString geometry. This function automatically handles the coordinate order conversion between polyline format ([latitude, longitude]) and GeoJSON ([longitude, latitude]). ### Method `toGeoJSON(encoded_string: string, precision?: number): object` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import polyline from 'geo-polyline-tools'; const encodedPolyline = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'; const geoJsonLineString = polyline.toGeoJSON(encodedPolyline, 5); console.log(JSON.stringify(geoJsonLineString, null, 2)); /* Output: { "type": "LineString", "coordinates": [ [-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252] ] } */ ``` ### Response #### Success Response (200) - **geoJson** (object) - A GeoJSON LineString geometry object. #### Response Example ```json { "type": "LineString", "coordinates": [ [-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252] ] } ``` ``` -------------------------------- ### polyline.fromGeoJSON Source: https://github.com/barikoi/geo-polyline-tools/blob/main/README.md Encodes a GeoJSON Feature or LineString geometry into a polyline string. ```APIDOC ## polyline.fromGeoJSON(geojson, precision?) ### Description Encodes a GeoJSON Feature or LineString geometry into a polyline string. Coordinates are expected in GeoJSON [longitude, latitude] ordering and are internally flipped to [latitude, longitude] before encoding. ### Parameters #### Arguments - **geojson** (object) - Required - A GeoJSON Feature with LineString geometry, or a LineString geometry directly. - **precision** (number) - Optional - Number of decimal places to preserve (Default: 5). ### Returns - **string** - The encoded polyline. ### Errors - Throws Error('Input must be a GeoJSON LineString') if the input is not a valid GeoJSON LineString or Feature containing one. ``` -------------------------------- ### polyline.fromGeoJSON Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Encodes a GeoJSON Feature or LineString geometry into a polyline string, handling coordinate order conversion. ```APIDOC ## polyline.fromGeoJSON ### Description Encodes a GeoJSON Feature or LineString geometry into a polyline string. This function automatically handles the coordinate order conversion between GeoJSON ([longitude, latitude]) and polyline format ([latitude, longitude]). ### Method `fromGeoJSON(geoJson: object, precision?: number): string` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **geoJson** (object) - Required - A GeoJSON Feature or LineString geometry object. - **precision** (number) - Optional - The precision level for encoding (default is 5). ### Request Example ```javascript import polyline from 'geo-polyline-tools'; const geoJsonFeature = { type: 'Feature', properties: { name: 'Sample Route' }, geometry: { type: 'LineString', coordinates: [ [38.5, -120.2], [40.7, -120.95], [43.252, -126.453] ] } }; const encoded = polyline.fromGeoJSON(geoJsonFeature, 5); console.log(encoded); // Output: '~ps|U_p~iFnnqC_ulLvxq`@_mqN' ``` ### Response #### Success Response (200) - **encoded_string** (string) - The compressed polyline string. #### Response Example ```json { "encoded_string": "~ps|U_p~iFnnqC_ulLvxq`@_mqN" } ``` ``` -------------------------------- ### Encode GeoJSON to Polyline String Source: https://context7.com/barikoi/geo-polyline-tools/llms.txt Encodes a GeoJSON Feature or LineString geometry into a polyline string. Automatically handles coordinate order conversion. ```javascript import polyline from 'geo-polyline-tools'; // Convert a GeoJSON Feature to polyline const geoJsonFeature = { type: 'Feature', properties: { name: 'Sample Route' }, geometry: { type: 'LineString', coordinates: [ [38.5, -120.2], [40.7, -120.95], [43.252, -126.453] ] } }; const encoded = polyline.fromGeoJSON(geoJsonFeature, 5); console.log(encoded); // Output: '~ps|U_p~iFnnqC_ulLvxq`@_mqN' // Convert a GeoJSON LineString geometry directly const lineStringGeometry = { type: 'LineString', coordinates: [ [-122.4194, 37.7749], // San Francisco [-118.2437, 34.0522], // Los Angeles [-117.1611, 32.7157] // San Diego ] }; const routePolyline = polyline.fromGeoJSON(lineStringGeometry, 5); console.log(routePolyline); // Error handling for invalid input try { const invalidGeoJson = { type: 'Feature' }; // Missing geometry polyline.fromGeoJSON(invalidGeoJson, 5); } catch (error) { console.error(error.message); // Output: 'Input must be a GeoJSON LineString' } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.