From a556bd3e5a974fc9cd552b079991c5a0ae658297 Mon Sep 17 00:00:00 2001 From: zhouhai <850168627@qq.com> Date: Thu, 22 Jul 2021 16:39:49 +0800 Subject: [PATCH] init init --- package.json | 17 +++ server-mysql.js | 304 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 321 insertions(+) create mode 100644 package.json create mode 100644 server-mysql.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..27fb538 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "gisserver", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.19.0", + "express": "^4.17.1", + "mysql": "^2.18.1", + "pg": "^8.6.0" + } +} diff --git a/server-mysql.js b/server-mysql.js new file mode 100644 index 0000000..e44dcb2 --- /dev/null +++ b/server-mysql.js @@ -0,0 +1,304 @@ +const express = require('express'); + +var bodyParser = require("body-parser"); +// var pg = require('pg'); +var mysql = require('mysql'); + +const app = express(); + + const port = 8080 +// var conString = "postgres://postgres:postgres@localhost/gisdb"; +// var conString = { +// host: "139.9.239.248", +// port: "4001", +// user: "root", +// password: "pUxtaIoE!1^s@vDJ", +// database: "air" +// }; +var conString = { + host: "124.71.210.242", + port: "4000", + user: "air", + password: "air123456", + database: "air" +}; +app.use(bodyParser.urlencoded({ extended: false })); +app.use(bodyParser.json()); + +app.all('*', function(req, res, next) { + //设为指定的域 + res.header('Access-Control-Allow-Origin', "*"); + res.header("Access-Control-Allow-Headers", "X-Requested-With"); + res.header('Access-Control-Allow-Headers', 'Content-Type'); + res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); + res.header('Access-Control-Allow-Credentials', true); + res.header("X-Powered-By", ' 3.2.1'); + next(); +}); + +app.get('/', (req, res) => res.send('Hello World!')) + +// 根据定位经纬度范围行政区数据 +// http://localhost:8090/getRegionByLocation?lon=106.5682&lat=29.6267 +app.get('/getRegionByLocation', (req, res) => { + var lon = req.query.lon || ""; + var lat = req.query.lat || ""; + if (lon == "" || lat == "") { + res.send(`参数错误! + 示例: http://localhost:8090/getRegionByLocation?lon=106.5682&lat=29.6267 + `); + return; + } + // var client = mysql.createConnection(conString); + var client = mysql.createConnection(conString); + client.connect(function (err) { + if (err) { + return console.error('连接数据库失败', err); + } else { + console.info('连接数据库成功'); + } + var sql = `select *,st_astext(geom) as geom from xingzhengqu` + sqlQueryPromise(sql, client) + .then((result) => { + // console.log(result) + res.send(result); + client.end(); + }) + .catch((err)=>{ + res.send(err); + client.end(); + }); + }); +}) + +// 根据定位经纬度返回3公里范围内的设施数据 +// http://localhost:8090/getFacilitiesByLocation?lon=106.5682&lat=29.6267 +app.get('/getFacilitiesByLocation', (req, res) => { + var lon = req.query.lon || ""; + var lat = req.query.lat || ""; + var radius = req.query.radius || 3000; + if (lon == "" || lat == "") { + res.send(`参数错误! + 示例: http://localhost:8090/getFacilitiesByLocation?lon=106.5682&lat=29.6267 + `); + return; + } + var client = mysql.createConnection(conString); + client.connect(function (err) { + if (err) { + return console.error('连接数据库失败', err); + } + var sql = `SELECT *,st_astext(geom) as geom1 from facilities where ST_Distance_Sphere(st_geomfromtext('POINT(${lat} ${lon})', 4326), ST_SRID(geom,4326)) < ${radius}` +sqlQueryPromise(sql, client) + .then((result) => { + // console.log(result) + res.send(result); + client.end(); + }) + .catch((err)=>{ + res.send(err); + client.end(); + }); + }); +}) + +// 根据定位经纬度返回5公里范围内的大组团小组团 +// http://localhost:8090/getTypeRegionByLocation?lon=106.5682&lat=29.6267&type=0 +app.get('/getTypeRegionByLocation', (req, res) => { + var lon = req.query.lon || ""; + var lat = req.query.lat || ""; + var type = req.query.type || "0"; + var radius = req.query.radius || 5000000; + if (lon == "" || lat == "") { + res.send(`参数错误! + 示例: http://localhost:8090/getTypeRegionByLocation?lon=106.5682&lat=29.6267&type=0 + `); + return; + } + var tableName = type == "0" ? "xiaozutuan" : "dazutuan" + var client = mysql.createConnection(conString); + client.connect(function (err) { + if (err) { + return console.error('连接数据库失败', err); + } + + var sql = `SELECT *,st_astext(geom) as geom from ${tableName} `; + sqlQueryPromise(sql, client) + .then((result) => { + res.send(result); + client.end(); + }) + .catch((err)=>{ + res.send(err); + client.end(); + }); + }); +}) +// 获取环线数据 +// http://localhost:8090/getLoopLine?lon=106.5682&lat=29.6267&type=0 +app.get('/getLoopLine', (req, res) => { + + var client = mysql.createConnection(conString); + client.connect(function (err) { + if (err) { + return console.error('连接数据库失败', err); + } + + var sql = `SELECT *,st_astext(geom) as geom from huanxian`; + sqlQueryPromise(sql, client) + .then((result) => { + res.send(result); + client.end(); + }) + .catch((err)=>{ + res.send(err); + client.end(); + }); + }); +}) + +// 获取拟挂牌地块数据 +app.get('/getLandToList', (req, res) => { + var lon = req.query.lon || ""; + var lat = req.query.lat || ""; + var radius = req.query.radius || 5000000; + if (lon == "" || lat == "") { + res.send(`参数错误! + 示例: http://localhost:8090/getLandToList?lon=106.5682&lat=29.6267 + `); + return; + } + var client = mysql.createConnection(conString); + client.connect(function (err) { + if (err) { + return console.error('连接数据库失败', err); + } + var sql = `SELECT *,st_astext(st_centroid(geom)) as centerPoint,st_astext(geom) as geom from land_to_list_lon_lat + where geom is not null` + sqlQueryPromise(sql, client) + .then((result) => { + // console.log(result) + res.send(result); + client.end(); + }) + .catch((err)=>{ + res.send(err); + client.end(); + }); + }); +}) + +// 获取挂牌中数据 +app.get('/getLandListing', (req, res) => { + var lon = req.query.lon || ""; + var lat = req.query.lat || ""; + var radius = req.query.radius || 5000000; + if (lon == "" || lat == "") { + res.send(`参数错误! + 示例: http://localhost:8090/getLandListing?lon=106.5682&lat=29.6267 + `); + return; + } + + var client = mysql.createConnection(conString); + client.connect(function (err) { + if (err) { + return console.error('连接数据库失败', err); + } + + var sql = ` + SELECT b.auction_date,a.*,st_astext(st_centroid(a.geom)) as centerPoint,st_astext(a.geom) as geom from land_listed_lon_lat a + left join + land_listed b + on a.land_listed_id = b.land_listed_id + where geom is not null` + sqlQueryPromise(sql, client) + .then((result) => { + // console.log(result) + for(var i = 0; i < result.length; i++) { + var item = result[i] + if (!item.auction_date) item.status = '挂牌中'; + if (item.auction_date && new Date(item.auction_date).getTime() < new Date().getTime()) { + item.status = '已出让'; + result.splice(i, 1); + i--; + } else { + item.status = '挂牌中'; + } + } + res.send(result); + client.end(); + }) + .catch((err)=>{ + res.send(err); + client.end(); + }); + }); +}) + +// 获取出让地块数据 +app.get('/getLandList', (req, res) => { + var lon = req.query.lon || ""; + var lat = req.query.lat || ""; + var radius = req.query.radius || 5000000; + var start = req.query.start || "1900-01-01"; + var end = req.query.end || "9999-01-01"; + if (lon == "" || lat == "") { + res.send(`参数错误! + 示例: http://localhost:8090/getLandList?lon=106.5682&lat=29.6267&type=0 + `); + return; + } + + var client = mysql.createConnection(conString); + client.connect(function (err) { + if (err) { + return console.error('连接数据库失败', err); + } + + + var sql = `SELECT b.auction_date,st_astext(st_centroid(a.geom)) as centerPoint,a.*,st_astext(a.geom) as geom from land_listed_lon_lat a + left join + land_listed b + on a.land_listed_id = b.land_listed_id + where geom is not null` + sqlQueryPromise(sql, client) + .then((result) => { + // console.log(result) + for(var i = 0; i < result.length; i++) { + var item = result[i] + if (item.auction_date && new Date(item.auction_date).getTime() < new Date().getTime()) { + item.status = '已出让'; + if (new Date(item.auction_date).getTime() > new Date(end).getTime() || new Date(item.auction_date).getTime() < new Date(start).getTime()) { + result.splice(i, 1); + i--; + } + } else { + item.status = '挂牌中'; + result.splice(i, 1); + i--; + } + } + res.send(result); + client.end(); + }) + .catch((err)=>{ + res.send(err); + client.end(); + }); + }); +}) + +function sqlQueryPromise(sql, client) { + return new Promise((resolve, reject)=>{ + client.query(sql, function (err, data, fields) { + if (err) { + reject(err); + } else { + resolve(data); + } + }) + }) +} + +app.listen(port, () => console.log(`Example app listening on port ${port}!`)) \ No newline at end of file