Skip to content

Instantly share code, notes, and snippets.

@dwtkns
Last active October 1, 2017 20:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwtkns/71bda78d532002ba11d8 to your computer and use it in GitHub Desktop.
Save dwtkns/71bda78d532002ba11d8 to your computer and use it in GitHub Desktop.
OSAscript to output the footprint of what's visible in the current Google Earth Pro window as a geojson file
#!/usr/bin/env osascript -l JavaScript
// usage: google-earth-perspective-to-geojson.js xSamples ySamples > currentPerspective.geojson
function run(argv) {
var xSamples = argv[0] || 50;
var ySamples = argv[1] || 50;
var ge = Application('Google Earth Pro');
var points = [];
// kinda dumb but keeps things in correct order for geojson
for (var x=0; x<xSamples; x++) {
var xn = normalize(x, xSamples);
var loc = ge.getpointonterrain([ xn, -1]);
points.push([loc[1],loc[0]])
}
for (var y=0; y<ySamples; y++) {
var yn = normalize(y, ySamples);
var loc = ge.getpointonterrain([ 1, yn]);
points.push([loc[1],loc[0]])
}
for (var x=xSamples; x>0; x--) {
var xn = normalize(x, xSamples);
var loc = ge.getpointonterrain([ xn, 1]);
points.push([loc[1],loc[0]])
}
for (var y=xSamples; y>0; y--) {
var yn = normalize(y, ySamples);
var loc = ge.getpointonterrain([ -1, yn]);
points.push([loc[1],loc[0]])
}
points.push(points[0]); // last point has to be same as first
var output = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": []
}
}
]
};
output.features[0].geometry.coordinates[0]=points;
return JSON.stringify(output);
}
function normalize(n,outOf) {
return (n/(outOf-1))*2-1;
}
@dwtkns
Copy link
Author

dwtkns commented Nov 12, 2015

coords default to 0,0 at infinity; could implement a scan to find horizon line when that happens

@dwtkns
Copy link
Author

dwtkns commented Nov 12, 2015

Use with rio clip input.tif output.tif --bounds $(fio info currentPerspective.json --bounds) to crop a raster to the smallest size that will cover the current perspective.

You can then convert that to a KML overlay with gdal_translate -of KMLSUPEROVERLAY input.tif output.kmz -co FORMAT=JPEG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment