Looking to manipulate a JavaScript Object file with 5.5mb of longitude latitude data? Consider opening it in Perl for applying a detail-reducing algorithm that will generate a new object file with reduced dataset. My approach involves using a for loop to select every 20th long/lat pair.
While I can achieve this in JavaScript, it requires manual copying and pasting each coordinate set to run the script individually.
Initially, I considered storing coordinates in a SQL database but found it inefficient due to excessive data movement.
Opting for Perl seems like a more server-friendly solution. Here's a snippet showcasing how to open the file:
#!/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 structure of the object is defined as:
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 demonstrate the reduction process used in JavaScript, my version loads data 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;
Suggestions have been made to convert data to JSON format, although unsure of necessity. Wanting to avoid manual text manipulation, is there a method to load the file directly as an object?
Due to time constraints, approached the task by:
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;
This method still involves some manual intervention such as copy/pasting and removing the last ,
.