今天,领导让我帮着后台解析一些数据,本以为是很简单的数据处理,后台发给我一个链接,我请求一看就懵逼了,密密麻麻的文字,复制到文件一看,大小70M,而且还不止一个链接,没办法,只能写一个node请求数据再解析写入文件给后台。 话不多说,上代码。
var http = require("http");
var fs = require("fs");
const tableNames = ['GEOPDDISULINE','GEOPDLINE','GEOPDRIUN','GEOPDRIUNLINE','GEOPTCOND','GEOPTLINE','GEOPTLINE_ORIG']
const primaryKeys = ['FGUID','OID','FGUID','FGUID','OID','FGUID','OID']
const tableName = tableNames[1]
const primaryKey = primaryKeys[1]
const options = {
hostname: '192.168.1.152',
port: 80,
path: `/dldata/shape/getByTableName?tableName=${tableName}&primaryKey=${primaryKey}`,
method: 'GET'
}
var htmlData = ""
var req = http.request(options, function (res) {
res.on("data", function (chunk) {
htmlData += chunk;
});
res.on("end", function () {
let obj = JSON.parse(htmlData)
console.log(obj);
let targetObj = {}
targetObj.type = "FeatureCollection"
targetObj.features = obj[0].map((item,index)=>{
let o = {}
o.type = 'Feature'
o.properties = {}
let itemKeys = Object.keys(item)
itemKeys.forEach(val=>{
val !='wktstr' && (o.properties[val] = item[val]==null?'':item[val])
if(val == 'wktstr'){
o.geometry = {}
o.geometry.type = item.wktstr.indexOf('MULTILINESTRING')>-1?'MULTILINESTRING':'LINESTRING'
o.geometry.coordinates = []
let currPolygonData = []
if(item.wktstr.indexOf('MULTILINESTRING')>-1){
currPolygonData = item.wktstr.match(/\((.+)\)/)[1].split(', (')
currPolygonData.forEach((v,i)=>{
v = v.replace(/(\(|\))/g,'')
v = v.split(', ')
let arr = []
v.forEach((val,ind)=>{
let list = []
list[0] = +val.split(' ')[0]
list[1] = +val.split(' ')[1]
arr[ind] = list
})
o.geometry.coordinates.push(arr)
})
}else{
currPolygonData = item.wktstr.match(/\((.+)\)/)[1].split(', ')
currPolygonData.length==1&&(currPolygonData[1] = currPolygonData[0])
currPolygonData.forEach((v,i)=>{
let arr = []
let [x,y] = v.split(' ')
arr[0] = +x
arr[1] = +y
o.geometry.coordinates.push(arr)
})
}
}
})
return o
})
fs.writeFile(`./${tableName}.txt`, JSON.stringify(targetObj), (err)=>{
console.log(err);
})
});
});
req.end();