iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 7
1
Modern Web

30天打造我的WebGIS系列 第 7

[Day 7] 常見的GIS資料格式-轉換篇

  • 分享至 

  • twitterImage
  •  

昨天介紹了geojson、shapefile、kml基本概念,今天我們簡單用npm上面的工具寫個簡單轉換,有關node.js的安裝及使用請各位參考其他線上教學資源,這邊我們就直接使用囉。


geojson資料轉換工具:

這邊使用npm上的geojson2,直接安裝:

npm install geojson2

建立test.js,寫一個geojson2.kml的轉換

var geojson2 = require('geojson2');
geojson2.kml('test.geojson', './out.kml', function (err) {
    console.log("finish")
})

執行
node test.js

結果out.kml

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Folder><name>OGRGeoJSON</name>
  <Placemark>
	<name>Dinagat Islands</name>
      <Point><coordinates>125.6,10.1</coordinates></Point>
  </Placemark>
</Folder>
</Document></kml>

geojson2這個模組,包含了四個子功能

  • geojson2csv
  • geojson2shape
  • geojson2kml
  • geojson2topojson

有需要可以自行測試!

其中官方文件提到,若要輸出kml and shp. 須先安裝gdal
Run 'brew install gdal' on OSX,
or 'sudo apt-get install gdal' on linux.


shapefile資料轉換工具:

這邊一樣使用NPM上的shp2json

一樣須先安裝gdal
Run 'brew install gdal' on OSX,
or 'sudo apt-get install gdal' on linux.

安裝
npm install shp2json.

建立test.js

var toJSON = require('shp2json');
var result = toJSON.fromShpFile('out.shp').pipe(process.stdout);
console.log(result);

執行
node test.js

結果

{
    "type": "FeatureCollection", 
    "features": 
    [
        { 
            "type": "Feature", 
            "properties": { "name": "Dinagat Islands" }, 
            "geometry": { "type": "Point", "coordinates": [125.6, 10.1] } 
        }
    ]
}

kml資料轉換工具:

使用NPM上的kml-stream

安裝kml-stream
npm install kml-stream

test.js

var fs = require('fs');
var KmlStream = require("kml-stream");
var data = fs.createReadStream('./out.kml').pipe(new KmlStream()).on('data', function (d) {
    console.log(d);
});

執行
node test.js

結果

{
    type: 'Feature',
    properties: { },
    geometry: { type: 'Point', coordinates: [125.6, 10.1] }
}

後記

基本上只要掌握了這三類資料互轉,大多數webgis線上資源的格式應該幾乎可以掌握了,後續如果真的有其他GIS資料轉換及讀取的問題,建議可進一步研究專業的GIS軟體:QGIS,由於本次鐵人賽的目標是快速上手webgis,比較可惜無法操作太多QGIS,QGIS無論在轉檔、分析及處理等功能都很強大,可以自行試看看唷。


上一篇
[Day 6] 常見的GIS資料格式
下一篇
[Day 8] 自己產製資料-地址定位part1
系列文
30天打造我的WebGIS30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言