Created
February 1, 2016 14:19
-
-
Save Vanuan/3463a63d3b1f5ca287c3 to your computer and use it in GitHub Desktop.
odesa_aerofoto_proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var Promise = require('promise'); | |
var rp = require('request-promise'); | |
var Jimp = require("jimp"); | |
var Canvas = require('canvas') | |
, Image = Canvas.Image | |
, canvas = new Canvas(200, 200, "png") | |
, ctx = canvas.getContext('2d'); | |
var app = express(); | |
function getImageUrl(z, x, y) { | |
var url = "http://195.138.92.34:6080/arcgis/rest/services/Aerofoto/MapServer/WMTS/tile/1.0.0/Aerofoto/default/default028mm/" + z + '/' + y + '/' + x; | |
console.log(url); | |
return url; | |
} | |
function merge4(leftTop, rightTop, leftBottom, rightBottom) { | |
var lT, rT, lB, rB, a; | |
lt = Jimp.read(leftTop); | |
rt = Jimp.read(rightTop); | |
lb = Jimp.read(leftBottom); | |
rb = Jimp.read(rightBottom); | |
return Promise.all([lt, rt, lb, rb]).then(function (images) { | |
lt = images[0]; | |
rt = images[1]; | |
lb = images[2]; | |
rb = images[3]; | |
a = lt.bitmap.width; | |
return new Jimp(a * 2, a * 2); | |
}).then(function(image) { | |
return image | |
.blit(lt, 0, 0) | |
.blit(rt, a, 0) | |
.blit(lb, 0, a) | |
.blit(rb, a, a); | |
}) | |
} | |
function scale(image, fx, fy) { | |
var w = image.bitmap.width * fx; | |
var h = image.bitmap.height * fy; | |
return image.resize(w, h); | |
} | |
function getBuffer(jimpImage) { | |
return new Promise(function(resolve, reject) { | |
jimpImage.getBuffer(Jimp.MIME_JPEG, function(err, buffer) { | |
if(!err) { | |
resolve(buffer); | |
} else { | |
reject(err); | |
} | |
}); | |
}); | |
} | |
function getTileBytes(z, xOsm, yOsm, scaleFactorX, scaleFactorY) { | |
var osmTileWidth = 256, osmTileHeight = 256; | |
var xOsm0 = 38340.35295573333 * 2 * 2 * 2 * 2; | |
//var xAero0 = 8287.7175 * 5; | |
var xAero0 = 8287.7175 * 20; | |
var xRelOsm = xOsm - xOsm0; | |
var xRelAero = xRelOsm / scaleFactorX; | |
var xAero = xRelAero + xAero0; // fractional tile number | |
console.log("xAero = " + xAero); | |
var yOsm0 = 23149.52 * 2 * 2 * 2 * 2; | |
//var yAero0 = 14742.299 * 5; | |
//var yAero0 = 14742.304 * 5; | |
var yAero0 = 14742.304 * 20; | |
var yRelOsm = yOsm - yOsm0; | |
var yRelAero = yRelOsm / scaleFactorY; | |
var yAero = yRelAero + yAero0; | |
console.log("yAero = " + yAero); | |
var jpg1 = getImageUrl(z, Math.floor(xAero), Math.floor(yAero)); | |
var jpg2 = getImageUrl(z, Math.floor(xAero + 1), Math.floor(yAero)); | |
var jpg3 = getImageUrl(z, Math.floor(xAero), Math.floor(yAero) + 1); | |
var jpg4 = getImageUrl(z, Math.floor(xAero + 1), Math.floor(yAero) + 1); | |
var mergeJpg = merge4(jpg1, jpg2, jpg3, jpg4); | |
return mergeJpg | |
.then(function(image) { | |
return scale(image, scaleFactorX, scaleFactorY); | |
}) | |
.then(function(image) { | |
var tileWidth = image.bitmap.width / 2; | |
var tileHeight = image.bitmap.height / 2; | |
var cropX1 = (xAero % 1) * tileWidth; | |
var cropY1 = (yAero % 1) * tileHeight; | |
return image.crop(Math.floor(cropX1), Math.floor(cropY1), osmTileWidth, osmTileWidth); | |
}) | |
.then(function(image) { | |
ctx.fillStyle = '#f0f'; | |
ctx.fillRect(0, 0, canvas.width, canvas.height); | |
ctx.font = "35px Arial bold"; | |
ctx.fillStyle = '#000'; | |
ctx.fillText(xAero, 10, 50); | |
ctx.fillText(yAero, 10, 100); | |
var slowBuffer = canvas.toBuffer(); | |
var buffer = new Buffer(slowBuffer.length); | |
slowBuffer.copy(buffer); | |
return Jimp.read(buffer).then(function (text) { | |
return image.mask(text, 0, 0); | |
}) | |
}) | |
.then(getBuffer); | |
} | |
app.get('/:z/:y/:x.png', function (req, res) { | |
var z = parseInt(req.params.z); | |
var x = parseInt(req.params.x); | |
var y = parseInt(req.params.y); | |
console.log('Requesting ' + z + '/' + x + '/' + y) | |
if(z == 13) { | |
var base_offset_x = -3135; | |
var base_offset_y = 55; | |
z = 1; | |
x = x + base_offset_x; | |
y = y + base_offset_y; | |
} | |
else if(z == 20) { | |
z = 6; | |
getTileBytes(z, x, y, 1.286, 1.2899/*1.607, 1.6116*/).then(function(buf) { | |
var img = buf; | |
res.writeHead(200, { | |
'Content-Type': Jimp.MIME_JPEG, | |
'Content-Length': img.length | |
}); | |
res.end(img); | |
}).catch(function(e) { | |
console.log(e.message); | |
res.status(404).send('Not found'); | |
}); | |
return; | |
} | |
else { | |
console.log(404); | |
res.status(404).send('Not found'); | |
return; | |
} | |
var redir = getImageUrl(z, x, y); | |
console.log(redir); | |
res.redirect(redir); | |
}); | |
app.listen(8082, function () { | |
console.log('Odessa Aero app listening on port 8082!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment