I have a JavaScript Object file with 5.5mb of longitude latitude data and I would like to open it in Perl so I can apply a crude detail reducing algorithm that would then save a new object file with the reduced dataset. For reducing detail I use a for loop that only takes every 20th long/lat pair.
I can do this in javascript but this requires that I copy/paste each coordinate set and run my JavasSript on it one at a time.
I then thought perhaps I could take each set of coordinates and put them in to a SQL db but that seems like a crude way to do it. And moves a lot of data around.
I settled on Perl being one of the better options, to do it all on the server.
I can open the file with:
#!/usr/bin/perl
# open file
open(FILE, "reduced_object_latlng.js") or die("Unable to open file");
# read file into an array
@data = <FILE>;
# close file
close(FILE);
# print file contents
foreach $line (@data)
{
print $line;
}
The object follows this design:
var paths = {
mayo: {
name: 'Mayo',
colour: 'green',
coordinates: '-9.854892,53.76898 -9.853634,53.769338 -9.85282,53.769387 -9.851981,53.769561 -9.850952,53.769508 -9.850129,53.769371 -9.849136,53.769171 **data**'
},
galway: {
name: 'Galway',
colour: 'purple',
coordinates: '**data**;
}
}; //etc.
To illustrate how I reduce the above data my javascript version loads from a file with one var coords = "*data*"
coords = coords.split(" ");
var path = [];
var output="";
document.getElementById("map_canvas").innerHTML = "";
for (var i = 0; i < coords.length; i++) {
if (i%20==0)
{
var coord = coords[i].split(",");
output += coord[0]+","+coord[1]+" ";
}
}
document.getElementById("map_canvas").innerHTML = output;
I have read some suggesting I convert it to JSON, I'm not sure if I need to do that. And instead of writing a pure text handler is there a way to load the file as an object?
I was stuck for time so I did it this way:
var outputobject = 'var paths = {';
for (property in copypaths) {
outputobject += property + ': { ';
outputobject += "name: '" + copypaths[property].name+"',";
outputobject += "colour: '"+ copypaths[property].colour+"',";
var reducedoutput="";
var coord = copypaths[property].coordinates.split(" ");
for (var i = 0; i < coord.length; i++) {
if (i%20==0)
{
var coords = coord[i].split(",");
reducedoutput += coords[0]+","+coords[1]+" ";
}
}
outputobject += "coordinates: '"+ reducedoutput+"'},";
}
outputobject += "};";
document.getElementById("reduced").innerHTML = outputobject;
it still involves copy/paste and deleting the last ,.
Thank you @Oleg V. Volkov, when I have more time later in the week I'll look at the method you laid out.