1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| var restify = require('restify'); var turf=require("@turf/turf"); var kriging=require("./lib/kriging"); const topojson=require("topojson"); var turf=require("@turf/turf");
const server = restify.createServer({ name: 'PhGIShlgyNode', version: '1.0.0' }); server.use(restify.plugins.acceptParser(server.acceptable)); server.use(restify.plugins.queryParser()); server.use(restify.plugins.bodyParser());
server.use(function(req,res,next){ res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); next(); })
let weight_breaks=[];
let colorSize=20; let maxValue=5000; let minValue=50; let step=(maxValue-minValue)/colorSize-2;
weight_breaks.push(minValue); for(let i=1;i<colorSize-1;i++){ weight_breaks.push(minValue+i*step); } weight_breaks.push(maxValue);
server.post('/api/kriging', function (req, res, next) {
let dataset={"type":"FeatureCollection","features":[{"type":"Feature","geometry": {"type":"Point","coordinates":[119.84338055,28.39903371]},"properties":{"id":40,"value":12},"id":"hlgy_station.1"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84262228,28.39976446]},"properties":{"id":57,"value":28},"id":"hlgy_station.2"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84365151,28.39991672]},"properties":{"id":58,"value":60},"id":"hlgy_station.3"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84268319,28.39863171]},"properties":{"id":59,"value":58},"id":"hlgy_station.4"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84309731,28.39874133]},"properties":{"id":80,"value":47},"id":"hlgy_station.5"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84231169,28.39945996]},"properties":{"id":81,"value":75},"id":"hlgy_station.6"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84238477,28.39940515]},"properties":{"id":85,"value":9},"id":"hlgy_station.7"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84246394,28.39934425]},"properties":{"id":92,"value":46},"id":"hlgy_station.8"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84311558,28.40037956]},"properties":{"id":82,"value":18},"id":"hlgy_station.9"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84299378,28.3994965]},"properties":{"id":83,"value":30},"id":"hlgy_station.10"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84306686,28.39944169]},"properties":{"id":84,"value":19},"id":"hlgy_station.11"}, {"type":"Feature","geometry":{"type":"Point","coordinates":[119.84393165,28.39923463]},"properties":{"id":94,"value":55},"id":"hlgy_station.12"}]}
let values = [], lngs = [], lats = []; dataset.features.forEach(feature => { values.push(feature.properties.value); lngs.push(feature.geometry.coordinates[0]); lats.push(feature.geometry.coordinates[1]); }); let variogram = kriging.train( values, lngs, lats, "exponential", 0, 100 ); let extent=[119.83800000000001, 28.395, 119.848, 28.404999999999998];
let polygons = []; polygons.push([ [extent[0], extent[1]], [extent[0], extent[3]], [extent[2], extent[3]], [extent[2], extent[1]] ]); let grid = kriging.grid(polygons, variogram, (extent[2] - extent[0]) / 500); let fc = gridFeatureCollection(grid, [extent[0], extent[2]], [extent[1], extent[3]]); var collection = turf.featureCollection(fc); var breaks = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]; var isobands = turf.isobands(collection, breaks, { zProperty: 'value' }); function sortArea(a,b) { return turf.area(b)-turf.area(a); } isobands.features.sort(sortArea);
res.send(isobands); return next(); });
server.listen(8799, function () { console.log('%s listening at %s', server.name, server.url); });
const gridFeatureCollection = function (grid, xlim, ylim) { var range =grid.zlim[1] - grid.zlim[0]; var i, j, x, y, z; var n = grid.length; var m = grid[0].length; var pointArray = []; for (i = 0; i < n ; i++) for (j = 0; j < m ; j++) { x = (i) * grid.width + grid.xlim[0]; y = (j) * grid.width + grid.ylim[0]; z = (grid[i][j] - grid.zlim[0]) / range; if (z < 0.0) z = 0.0; if (z > 1.0) z = 1.0; pointArray.push(turf.point([x, y], { value: z })); } return pointArray; }
|