Raruto / leaflet-elevation

Leaflet plugin that allows to add elevation profiles using d3js
GNU General Public License v3.0
255 stars 85 forks source link

SyntaxError: JSON.parse: unexpected character #66

Closed pitcock closed 4 years ago

pitcock commented 4 years ago

Hi,

using your example leaflet-elevation_string-data.html with my geojson data gives me this error:

SyntaxError: JSON.parse: unexpected character

I'm using a geojson string which is an automatically generated from a Garmin FIT file. Howerver there is sometimes an empty altitude value like:

[11.74641,47.55261,]

The other values look like:

[11.74652,47.55291,1020]

Is there a way to suppress the SyntaxError and ignore the missing altitude?

Raruto commented 4 years ago

Hi pitcock, if you want to solve the problem programmatically I suggest you to take a look at the following library:

https://github.com/RyanMarcus/dirty-json

if instead it is a question of how to fix just a "single" file, try using this tool:

http://ryanmarcus.github.io/dirty-json/

Have a nice day, Raruto

pitcock commented 4 years ago

Hi Raruto,

thank you very much for the links. I will test the programmatically solution.

I also found this thread:

https://github.com/MrMufflon/Leaflet.Elevation/issues/63#issue-192998443

Would this be a good addition for your solution?

Raruto commented 4 years ago

Currently points without a "z" value are already discarded

https://github.com/Raruto/leaflet-elevation/blob/1d512e5dcc2be5f43eb609227e49c132bdb09f38/src/altitude.js#L25

But your problem is related to a malformed JSON structure:

https://github.com/Raruto/leaflet-elevation/blob/1d512e5dcc2be5f43eb609227e49c132bdb09f38/src/utils.js#L45

Since JSON.parse is a native function, it could also be more difficult to solve (with just a few lines), but if you are good with regex replace you could also achieve it ...

pitcock commented 4 years ago

Hi Raruto,

thank you for the hint.


when I use this method

$array = str_replace( ',]', ',0]', $array );

the total length is correct and I get gaps in the graph

replace-01


when I use this method

$array = str_replace( ',]', ',]', $array );

the total length is incorrect and I get no gaps in the graph

replace-02


Is there a way to get the correct total length and no gaps?

Raruto commented 4 years ago

when I use this method

$array = str_replace( ',]', ',]', $array );

the total length is incorrect and I get no gaps in the graph

maybe you meant this:

$array = str_replace( ',]', ']', $array );

Is there a way to get the correct total length and no gaps?

First of all make sure you are using the latest available version of this library, if you will still have problems please submit a demo file.

pitcock commented 4 years ago

Hi Raruto,

maybe you meant this:

$array = str_replace( ',]', ']', $array );

this was a type you are right, I tried

$array = str_replace( ',]', ']', $array );

First of all make sure you are using the latest available version of this library, if you will still have problems please submit a demo file.

I'm using your latest library. Here is a demo file (data.txt) containing empty altitudes.

[11.74641, 47.55261, ]

For the screenshots I tried:

[11.74641, 47.55261]

and

[11.74641, 47.55261, 0]
Raruto commented 4 years ago

this was a type you are right, I tried

$array = str_replace( ',]', ']', $array );

Your file lacks of many z values, but try this again with version 1.3.1

NB currently, all points (starting from the first one) where the z value is missing are discarded (this is just for the algorithm simplicity, anyway, if you have any suggestions pull requests are welcome)