From 9d861a65aefa51b086006a6d715e1a3f6924ae3f Mon Sep 17 00:00:00 2001 From: Ben Hsieh Date: Fri, 16 Dec 2016 01:31:23 +0800 Subject: [PATCH] Add viewer and editor functions --- index.js | 115 ++++++++++++++++++++++++++++++++++++++--- package.json | 4 +- public/demo.html | 46 ++++++++++------- public/map-editor.html | 25 ++------- public/style.css | 31 +++++++++++ public/viewer.js | 73 ++++++++++++++++++++++++++ 6 files changed, 249 insertions(+), 45 deletions(-) create mode 100644 public/style.css create mode 100644 public/viewer.js diff --git a/index.js b/index.js index 7d4ad54..c4a54b2 100644 --- a/index.js +++ b/index.js @@ -7,16 +7,71 @@ var fs = require('fs'); var util = require('util'); var mkdirp = require('mkdirp'); var getDirName = require('path').dirname; +var enableCache = true; +var cp = require('child_process').exec; +var bodyParser = require('body-parser'); +var imgCache = {}; var regions = JSON.parse(require('fs').readFileSync('./regions.json')); app.set('port', (process.env.PORT || 5000)); app.use('/public', express.static('public')); + +app.use(bodyParser.urlencoded({extended: true})); +app.use(bodyParser.json()); + app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); }); +app.post('/bounds/:region', function(req, res) { + var region = req.params.region; + console.log(region,req.body) + try { + fs.writeFileSync('./regions/'+region+'.json', JSON.stringify(req.body)); + }catch(err) { + console.log(err.stack); + } + res.sendStatus(200); +}) + +app.get('/bounds/:region', function(req, res) { + + var region = req.params.region; + try { + var data = fs.readFileSync('./regions/' + region + '.json'); + res.send(data); + } catch(err) { + console.log(err.stack); + } + +}) + +app.get('/reset-cache/:region', function(req,res) { + + var region = req.params.region; + try { + deleteFolderRecursive('./regions/'+region+'/debug/', function() { + res.send(200) + }); + } catch(err) { + console.log(err.stack); + } + +}) + +app.get('/cache/:enable', function(req, res) { + + if(req.params.enable == '1') { + enableCache = true; + } + else + enableCache = false; + res.send(200); + +}) + app.get('/lookup/:z/:x/:y', function(req, res) { var x = req.params.x, y = req.params.y, @@ -60,6 +115,24 @@ function handleTileRequest(req, res) { }); } +app.get('/base/:z/:x/:y', function(req,res) { + var request = require('request'); + var x = req.params.x; + var y = req.params.y; + var z = req.params.z; + var pipe = req.pipe(request.post('http://mt1.google.com/vt/lyrs=m@110&hl=pl&x='+x+'&y='+y+'&z='+ z)); + var response = []; + + pipe.on('data',function(chunk) { + response.push(chunk); + }); + + pipe.on('end',function() { + var res2 = Buffer.concat(response); + res.send(res2); + }); +}) + app.get('/exps/:id', function(req, res) { res.send(dummyExperience(req.params.id)); }); @@ -126,7 +199,14 @@ function createTileFromRawImage(region, x, y, z, debug, cb) { tileToLatLng(x+1,y+1,z), tileToLatLng(x,y+1,z) ]; - fs.readFile(`./region-raw-img/${region}.png`, (err, data) => { + if(imgCache[region]) { + render(null, imgCache); + } + else { + fs.readFile(`./region-raw-img/${region}.png`, render); + } + + function render(err, data) { if(err) throw err; let img = new Image; @@ -179,16 +259,24 @@ function createTileFromRawImage(region, x, y, z, debug, cb) { mkdirp(getDirName(dir), function (err) { if (err) console.log(err); - fs.writeFile(dir, bytes, function(err) { - if(err) - console.log(err) + + if(enableCache) { + fs.writeFile(dir, bytes, function(err) { + if(err) + console.log(err) + cb(bytes); + }); + } + else { cb(bytes); - }); + } + }); - }); + } }); + } function drawText(ctx,param, ox,oy, dx, dy, z) { @@ -225,3 +313,18 @@ function generateTile(x,y,z) { var bytes = canvas.toBuffer(undefined, 3, canvas.PNG_FILTER_NONE); return bytes; } + +var deleteFolderRecursive = function(path, cb) { + if( fs.existsSync(path) ) { + fs.readdirSync(path).forEach(function(file,index){ + var curPath = path + "/" + file; + if(fs.lstatSync(curPath).isDirectory()) { // recurse + deleteFolderRecursive(curPath); + } else { // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(path); + } + cb() +}; diff --git a/package.json b/package.json index 1b9c8ff..58d8bda 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,12 @@ "start": "node index.js" }, "dependencies": { + "body-parser": "^1.15.2", "canvas": "^1.6.1", "express": "4.13.3", "mkdirp": "^0.5.1", - "moment": "^2.16.0" + "moment": "^2.16.0", + "request": "^2.79.0" }, "repository": { "type": "git" diff --git a/public/demo.html b/public/demo.html index 7f709af..bf014bb 100644 --- a/public/demo.html +++ b/public/demo.html @@ -3,27 +3,37 @@ - test + Tiles Demo + + + - -
- - - - +
+

Origin

+ + + x + + +
+

Top left

+ + + +
+
+

Bottom right

+ + + +
-
- - - - -
-
- - - - + + + + +
diff --git a/public/map-editor.html b/public/map-editor.html index 2a63f3c..6bab30a 100644 --- a/public/map-editor.html +++ b/public/map-editor.html @@ -3,25 +3,9 @@ - Polygon Arrays + Map Editor + -
@@ -30,10 +14,11 @@