Skip to content

Instantly share code, notes, and snippets.

@DamonOehlman
Created October 20, 2012 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DamonOehlman/3923202 to your computer and use it in GitHub Desktop.
Save DamonOehlman/3923202 to your computer and use it in GitHub Desktop.
image conversion to latlon
var image = new Image(),
canvas, context, landSquares = [],
landPixelCount = 0,
rounding = 10^2;
function errorHandler() {
console.log(arguments)
}
function writeData() {
/*
request
.post('http://10.0.5.38:7000/data/')
.set('Accept', 'application/json')
.send({ data: landSquares })
.end(function(err) {
console.log(err);
});
*/
var blob = new Blob(
[ JSON.stringify([[ '1990', landSquares ]]) ],
{ type: 'text/plain' }
);
saveAs(blob, 'data.json');
}
function extractSamples(context) {
// get the image data
var imageData = context.getImageData(0, 0, canvas.width, canvas.height).data,
landPixels = 0,
halfWidth = canvas.width / 2,
halfHeight = canvas.height / 2,
maxLat = -90, minLat = 90, maxLon = -180, minLon = 180,
pos;
// iterate through the image data
for (var ii = 0; ii < imageData.length; ii += 4) {
var rVal = imageData[ii],
bVal = imageData[ii+1],
gVal = imageData[ii+2],
x = ((((ii / 4) % canvas.width) - halfWidth) / halfWidth),
y = ((Math.floor((ii / 4) / canvas.width) - halfHeight) / halfHeight),
xRad = x * Math.PI,
yRad = y * Math.PI;
// if black we are interested in the value
if (rVal + bVal + gVal === 0) {
pos = T5.unproject(xRad, yRad);
pos.lat = Math.floor(pos.lat); // Math.floor(pos.lat * rounding) / rounding;
pos.lon = Math.floor(pos.lon); // Math.floor(pos.lon * rounding) / rounding;
maxLat = Math.max(pos.lat, maxLat);
minLat = Math.min(pos.lat, minLat);
maxLon = Math.max(pos.lon, maxLon);
minLon = Math.min(pos.lon, minLon);
landSquares.push(x, y, 0.001);
landPixelCount++;
}
}
console.log('lat: ', minLat, maxLat);
console.log('lon: ', minLon, maxLon);
writeData();
console.log('found ' + landPixelCount + ' pixels');
}
image.src = 'globe/world.jpg';
image.onload = function() {
canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
// get the canvas context
context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
// extract the samples
extractSamples(context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment