如果能使用node.js直接將postgres的空間數據導出為geojson,然后再使用tippecanoe進行切片,對於前端地圖開發人員將會方便不少。
第一步
先看一下geojson的格式,如下:
1 { 2 "type": "FeatureCollection", 3 "name": "geodata1", 4 "features": [ 5 { 6 "type": "Feature", 7 "properties": { 8 "name": "name1", 9 "type": 1 10 }, 11 "geometry": { 12 "type": "LineString", "coordinates": [[1.1, 1.2], [1.5, 1.3], [1.7, 1.4], [1.8, 1.2]] 13 } 14 } 15 ] 16 }
一個空間數據可導出為一個geojson文件:
1.表名可以對於geojson中對象的name,
2.表中的一行數據生成一個feature,即features數組的一個元素
3.字段對應於feature的properties
4.geometry就是空間字段轉換后的結果
如下,geom是空間字段,使用st_asgeojosn將geom字段轉為geojson
1 // 查詢sql 2 select st_asgeojson(geom) as geometry from "position"; 3 // 結果 4 {"type":"Point","coordinates":[116.789962214781,41.28758603506076]}
第二步
了解geojson的格式后,其實就比較簡單了:
1.查詢數據,表名為table1,字段為: 必要的屬性如name,轉換的geojson
1 select name, st_asgeojson(geom) as geometry from "position";
2.將查詢結果制作成一個個的feature對象,放入數組features中,如下
1 var features = [ 2 { 3 "type": "Feature", 4 "properties": { 5 "name": 1 6 }, 7 "geometry": { 8 "type": "Point", "coordinates": [[1,1]] 9 } 10 }, 11 { 12 "type": "Feature", 13 "properties": { 14 "name": 2 15 }, 16 "geometry": { 17 "type": "Point", "coordinates": [[1, 2]] 18 } 19 } 20 ]
3.將表名和features信息填入一個geojson對象中:
1 var geojson = { 2 "type": "FeatureCollection", 3 "name": table1, 4 "features": features 5 }
最后將geojson寫入文件即可。
主要代碼如下:
1 var fs = require('fs') 2 let config = require('./config') 3 let sql = require('./src/sql') 4 let conStr = config.conStr 5 let selectStr = require('./src/selectStr').selectStr 6 7 selectStr.forEach(item => { 8 sql.init(conStr) 9 sql.select(item.selectStr, function (result) { 10 console.info(item.name + ' start...') 11 let features = [] 12 result.rows.forEach(obj => { 13 let feature = { 14 "type": "Feature", 15 "properties": { 16 // 屬性都放在這 17 }, 18 "geometry": JSON.parse(obj.geometry) 19 } 20 // 掛接屬性 21 for (let key in obj) { 22 if (obj.hasOwnProperty(key) && key != 'geometry') { 23 // console.log(key) 24 // console.log(obj[key] + '') 25 feature.properties[key] = obj[key] + '' 26 } 27 } 28 features.push(feature) 29 }) 30 let geojson = { 31 "type": "FeatureCollection", 32 "name": item.name, 33 "features": features 34 } 35 fs.writeFile('./result/' + item.name + '.json', JSON.stringify(geojson), function () { 36 console.info(item.name + ' ok.'); 37 }) 38 }) 39 })
"select mesh, concat_ws(',', cast(st_x(st_centroid(geom)) as decimal(10,3)), cast(st_x(st_centroid(geom)) as decimal(10,3))) as lngLat, st_asgeojson(geom) as geom from building limit 3;";