Steps for assigning each element in the array to a separate variable

My array is dynamic and the length varies, but it looks like this:

var coord_list = [[-7.84 , 12.32],[-8.30 , 10.42],[-11.84 , 12.32]....]

I am trying to incorporate each element (coordinates) "[-7.84 , 12.32]" into a turf module which is done like this:

turf.lineString([[-7.42,9.125],[-8.43,9.800]]...(all of them), {name: 'line 2'});
.

When attempting to use the var coord_list array directly, I encountered issues despite it having the same structure internally.

turf.lineString([coord_list], {name: 'line 2'}); --> Error: coordinates must be an array of two or more positions
turf.lineString(coord_list, {name: 'line 2'}); --> Error: coordinates must contain numbers       

How can I properly integrate each coordinate into the turf formula?

Answer №1

As per the guidelines provided in the turf documentation, an illustration of the syntax would be:

var linestring1 = turf.lineString([[-24, 63], [-23, 60], [-25, 65], [-20, 69]], {name: 'line 1'});

The primary argument consists of an array of positions.

Hence, your second line appears to be accurate:

var coord_list = [[-7.84 , 12.32],[-8.30 , 10.42],[-11.84 , 12.32]....] // is an array of positions
turf.lineString(coord_list, {name: 'line 2'}); --> Throws an Error: coordinates must contain numbers

The error suggests that the dynamically generated coord_list contains strings instead of numbers.

To verify this, you can output the typeof each position value.

In order to convert each value into a number, you need to pass the x,y value of each position through:

let xNum = parseFloat(x);
let yNum = parseFloat(y);

prior to supplying them to

turf.lineString([[xNum, yNum],[xNum2, yNum2],...],{name: 'line 2'})

If there is no control over how coord_list is generated, it can be rectified as follows:

let arr = [['-7.84' , '12.32'],['-8.30' , '10.42'],['-11.84' , '12.32']];

let coord_list = arr.map(position => {
    return [parseFloat(position[0]), parseFloat(position[1])];
}) 

Answer №2

Based on your recent revision highlighting the mistakes, it seems like your coordinates may be mistakenly included as strings instead of numerical values while forming the coord_list.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

function does not return a specified object

My function looks like this const person = function(){ return { id : 1, name : 'John', email : 'john@example.com' } } However, after calling console.log(person()); 'undefined' is returned. What could I ha ...

Set up a targeted version of the winston software package for installation

Can I download a specific version of winston through npm by using the command: npm install winston=2.2.0 The reason I am asking is because I am encountering an issue with my existing code written in version 2.2.0 when I download the latest version. The ...

Control the number of documents fetched in the $lookup operation with the specified limit

I have encountered an issue with this query resulting in the following output: { "_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"), "search" : "flarize", "name" : "flarize", "color" : 0, "profil" : "", "banner" : "", "desc" : "", ...

Having trouble with my custom AngularJs service created using the factory method

Discovering a challenge with my current setup. I have implemented two separate controllers that handle adding 'country name' and 'price' to a list. Everything functions as expected when each button is clicked individually. However, when ...

Tips for parsing URLs to handle page navigation on a single-page website with AJAX when users click the back button

I am currently working on a project for the website . This particular website utilizes ajax and .htaccess to update a single index.php page. However, I seem to be facing an issue with the back button functionality. When users click on different top header ...

Navigate the scroll bar horizontally one by one instead of all at once, due to a width problem

The issue I'm facing involves determining the correct width for scrolling left or right. This results in the navigation tabs not scrolling correctly one by one. Each time I click the scroll right button, it adds more width than necessary and causes th ...

Stop jQuery from submitting the form in case of validation errors

Hey there, I'm currently working on preventing the AJAX form submission function from happening if one of the inputs fails validation. Edit: Specifically, I'm looking for guidance on what changes need to be made in //Adult age validation and var ...

"Enhance Your Website with Customized Background Sound Effects

Currently, I have been developing an application. Now, I am in the stage where I need to incorporate some background music into it. I am planning to utilize jQuery for playing the music and looping it once it finishes. Here is the specific background tr ...

Express callback delaying with setTimeout

I'm working on a route that involves setting a data attribute called "active" to true initially, but then switching it to false after an hour. I'm curious if it's considered bad practice or feasible to use a setTimeout function within the ex ...

Dismiss MUI Snackbar notification and execute action upon pressing a key

A custom notification system has been developed, utilizing the material ui Snackbar with both an action button and a close button. The objective is to implement a listener event for the "enter" key in order to trigger the specific notification's actio ...

The Cloudflare KV namespace remains unbound

After running wrangler dev, it appears that Worker KV is not being bound properly: ERROR in /src/handler.ts ./src/handler.ts 16:8-17 [tsl] ERROR in /src/handler.ts(16,9) TS2304: Cannot find name 'generalKV'. This is the content of handler. ...

Detecting slopes in THREE.js using three axes

Can someone please guide me on how to navigate a car up a slope in a game? I am currently developing a game using the JavaScript THREE.js API and need to figure out how to detect the slope. After conducting some research, I learned that slope can be calcul ...

Conceal all columns apart from the first when expanding/collapsing a table using

I have a table with a single header and multiple columns, and I am trying to implement an expand/collapse feature based on header clicks using the code snippet below: $(this).toggleClass('expand').nextUntil('tr.header').slideToggle(100 ...

A button paired with a selection menu for a seamless user experience

One interesting feature I have implemented is a dropdown that filters records on an HTML table without the need for a button. The filter functionality works flawlessly even without a button to confirm the selection. public selectedBrand: any; public onCha ...

What is the method to create a number using a textbox through javascript?

Whenever a number is entered into the main_textbox, a JavaScript function is called on the onblur event of the textbox using the following code: function generate_bale(){ var bale=document.getElementById("number_of_bale").value; var boxes = "< ...

javascript Try again with async await

I am working with multiple asynchronous functions that send requests to a server. If an error occurs, they catch it and retry the function. These functions depend on data from the previous one, so they need to be executed sequentially. The issue I am facin ...

What is the best approach for extracting JSON data (using jQuery) from Google Geocoding services?

Obtaining the city name using latitude and longitude is my current challenge. Google Geocoding offers a helpful solution known as "Reverse geocoding". According to the documentation, a GET request needs to be made to the following link: http://maps.googl ...

The modal is nowhere to be found, only the backdrop is visible

Upon clicking the edit button in my HTML code, I expected a modal to appear but it did not pop up. <div class="modal fade" id="editModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> &l ...

Develop a function that takes in another function and outputs a new function that includes timestamps

Develop a function called dateStamp which takes another function as input and returns a new function. This returned function will accept the same arguments as the passed-in function and produce an object with two keys: date, representing today's date ...

Utilize Vue.JS to showcase JSON information from an external file

Currently, I have a View.JS app that displays a conversation thread from a JSON file. The existing code appears as follows: const app = new Vue({ el: "#app", data: { messages:[ { name: "Support", message: "Hey! Welcome to suppo ...