13 Replies Latest reply on May 22, 2017 12:56 AM by tguo RSS
    tguo Explorer

    How to solve the NaN value in updated location field

    My code is to get a new field of location from available column longitude, latitude, elevation. Built with no error.

     

    // result: INFOTABLE  dataShapeName : "MapData"
    var result = Things["ThingName"].ServiceName(); // Change the ThingName and Service name here
    var newField = new Object(); 
    newField.name = "location"; 
    newField.baseType = 'LOCATION'; 
    result.AddField(newField);
    // location:LOCATION 
    var temp = new Object();
    var tableLength = result.rows.length; 
    for (var x = 0; x < tableLength; x++) { 
        var row = result.rows[x];  
        temp.latitude = row.latitude;//result.rows[x].latitude; 
        temp.longitude = row.longitude;//result.rows[x].longitude; 
        temp.elevation = 0; 
        temp.units = "WGS84"; 
        row.location = temp;
    }
    

     

     

    But the output is like this. The location column is all NaN:NaN. Something is wrong?

     

    WeChat Image_20170519094705.png

      • Re: How to solve the NaN value in updated location field
        yshen-2 Newbie

        Pleas move

        var temp = new Object();

        inside for loop

        other wise you are setting all the location field to one object and do the change on one object


        Thanks

        YUanwu Shen

        • Re: How to solve the NaN value in updated location field
          Ankit Gupta Ninja

          Hi Tiantian Guo,

           

          row.location = temp;  is incorrect

          it should be result.rows[x].location = temp;

          • Re: How to solve the NaN value in updated location field
            qngo Creator

            Hello,

             

            The behavior is weird. Before the end of for loop, if I add "result.AddRow({location: temp}), the new rows are added with correct location. When I log the result, the data structure is different too.

             

            "location" where is Nan: "location={"elevation":1.0,"latitude":1.0,"units":"WGS84","longitude":0.0}"

            Correct "location" added at the end of result: "location=1.0,1.0,0.0"

              • Re: How to solve the NaN value in updated location field
                Ankit Gupta Ninja

                That's Strange because it works fine for me.

                The only difference is I am not using the DataBase. My data is stored in a DataTable.

                • Re: How to solve the NaN value in updated location field
                  qngo Creator

                  The "result" in my test is a new InfoTable of DataShape NamedVTQ, created directly in service. Then I added 10 rows with only "value" before adding the new field "location". The "latitude" and "longitude" are just 0,1,2...

                   

                  var data = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({dataShapeName: "NamedVTQ"});
                  
                  
                  for (var i = 0; i < 10; i++) {
                      data.AddRow({name: "name_" + i})   ;
                  }
                  
                  
                  var newField = new Object();  
                  newField.name = "mylocation";  
                  newField.baseType = 'LOCATION';  
                  data.AddField(newField); 
                  
                  
                  var tableLength = data.length;  
                  
                  
                  for (var x = 0; x < 10; x++) {  
                      var row = data[x]; 
                      var tmp = new Object(); 
                     
                      tmp.latitude = x;
                      tmp.longitude = x * 10;/
                      tmp.elevation = 0;  
                      tmp.units = "WGS84";  
                     
                  // 3 rows below work
                  //    data[x].mylocation = tmp; 
                  //    data.rows[x].mylocation = tmp;
                      row.mylocation = tmp;
                     
                      data.AddRow({mylocation: tmp, value: tmp.latitude});
                     
                      data[x].value = tmp.latitude;
                  }
                  
                  
                  data.AddRow({mylocation: tmp, name: "name_99", value: 99});
                  
                  
                  logger.debug(data.rows);
                  
                  
                  result = data;
                  
                  • Re: How to solve the NaN value in updated location field
                    tguo Explorer

                    Yes. If I add the a new row,

                     

                    var tableLength = result.rows.length; 
                    for (var x = 0; x < tableLength; x++) { 
                        var row = result.rows[x];
                        var temp = new Object();
                     temp.baseType = 'LOCATION';
                    
                        temp.latitude = row.latitude; 
                        temp.longitude = row.longitude;
                        temp.elevation = 0; 
                        temp.units = "WGS84"; 
                        result.rows[x].location = temp;
                    
                        result.AddRow({location: temp});
                    }
                    

                     

                     

                    The result is right then:

                     

                    WeChat Image_20170519180239.png

                  • Re: How to solve the NaN value in updated location field
                    Ankit Gupta Ninja

                    Hi Tiantian Guo,

                     

                    Based on Quang input; I have following suggestion:

                    1. Create a new DataShape which has all the fields you require + location field. DataShape1

                    2. In custom Service create a new Infotable using above DataShape.

                    var params = {
                        infoTableName: "INFOTABLE" /* STRING */,
                        dataShapeName: "DataShape1" /* DATASHAPENAME */
                    };
                    
                    // result: INFOTABLE
                    var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
                    

                    3. Loop though the old infotable (let's say result1) and Add the Row to the new infotable (result).

                     

                    var tableLength = result1.rows.length; 
                    
                    for (var x = 0; x < tableLength; x++) {
                         var temp = new Object(); 
                         temp.elevation = 0;
                         temp.units = "WGS84";
                         temp.latitude = result1.rows[x].latitude; 
                         temp.longitude = result1.rows[x].longitude;
                    
                         var temp1 = new Object();
                         temp1.latitude = result1.rows[x].latitude; 
                         temp1.longitude = result1.rows[x].longitude;
                         temp1.location = temp;
                         result.AddRow(temp1);
                    }
                    

                     

                    I hope it helps.