1. 程式人生 > >[Github]shapefile轉json——對Github上shp2json項目的修改

[Github]shapefile轉json——對Github上shp2json項目的修改

res writen json 限制 next() bad 原來 onclick ide

一. 前言

作為編程菜雞,我有事沒事都會上Github上抄代碼。

基本上想寫點東西時,都會先在Github上看有沒有現成的......

又因為寫的代碼往往和GIS相關,GIS又是個炒雞冷門的方向,所以往往搜索結果慘淡,拉下來能用的代碼更是寥寥無幾呀。

這時候就只能在別人代碼基礎上修改了,愁......

該Github項目的地址

https://github.com/substack/shp2json

二. 修改

當初是想實現一個將shapefile文件轉為json的功能,

在Github上類似的代碼還算多。

但是,作為菜雞,那麽多的代碼裏,我能運行的也就那麽一兩個,再從中間挑一個評分高的,那就選中shp2json了。

作為菜雞,真的心累。

在運行完shp2json,發現json數據能正確顯示在控制臺裏。

那麽呵呵呵,怎麽把控制臺的所謂的“流”變成json字符串呢?

這對連json是什麽都不知道的我真是頭大呀。

當時各種搜索,各種關鍵詞都一無所獲,

最後走投無路時只能打開源碼。

其實我連源碼怎麽看都不知道,

但還好,所以就挑了個最長最大的index.js開始瞎幾把改。

最後,就莫名其妙的成功了。

呵呵呵~

下面是代碼修改:

原來的index.js

技術分享圖片
 1 function fromShpFile (file, outStream) {
 2     outStream = outStream || duplex.obj();
3 var shp = gdal.open(file); 4 var layerCount = shp.layers.count(); 5 6 var before = ‘{"type": "FeatureCollection","features": [\n‘; 7 var after = ‘\n]}\n‘; 8 var started = false; 9 var currentLayer, currentFeature, currentTransformation; 10 var nextLayer = 0;
11 12 var to = gdal.SpatialReference.fromEPSG(4326); 13 14 function getNextLayer() { 15 currentLayer = shp.layers.get(nextLayer++); 16 var srs = currentLayer.srs || gdal.SpatialReference.fromEPSG(4326); 17 currentTransformation = new gdal.CoordinateTransformation(srs, to); 18 } 19 20 getNextLayer(); 21 22 var layerStream = from(function(size, next) { 23 var out = ‘‘; 24 writeNextFeature(); 25 26 function writeNextFeature() { 27 var feature = currentLayer.features.next(); 28 if (!feature) { 29 // end stream 30 if (nextLayer === layerCount) { 31 // push remaining output and end 32 layerStream.push(out); 33 layerStream.push(after); 34 return layerStream.push(null); 35 } 36 getNextLayer(); 37 feature = currentLayer.features.next(); 38 } 39 40 try { 41 var geom = feature.getGeometry(); 42 } catch (e) { 43 return writeNextFeature(); 44 } 45 46 geom.transform(currentTransformation); 47 var geojson = geom.toJSON(); 48 var fields = feature.fields.toJSON(); 49 var featStr = ‘{"type": "Feature", "properties": ‘ + fields + ‘,"geometry": ‘ + geojson + ‘}‘; 50 51 if (started) { 52 featStr = ‘,\n‘ + featStr; 53 } else { 54 featStr = before + featStr; 55 } 56 57 started = true; 58 out += featStr; 59 60 if (out.length >= size) { 61 next(null, out); 62 } else { 63 writeNextFeature(); 64 } 65 } 66 67 }) 68 69 outStream.setReadable(layerStream); 70 outStream.end(after); 71 72 return outStream; 73 }
View Code

修改之後的代碼:

技術分享圖片
 1 function fromShpFile (file, outStream) {
 2     var out = ‘‘;
 3     outStream = outStream || duplex.obj();
 4     var shp = gdal.open(file);
 5     var layerCount = shp.layers.count();
 6     
 7     var countLimit = 0;//要素讀取數量限制
 8     
 9     var before = ‘{"type": "FeatureCollection","features": [\n‘;
10     var after = ‘\n]}\n‘;
11     var started = false;
12     var currentLayer, currentFeature, currentTransformation;
13     var nextLayer = 0;
14 
15     var to = gdal.SpatialReference.fromEPSG(4326);
16 
17     function getNextLayer() {
18       currentLayer = shp.layers.get(nextLayer++);
19       var srs = currentLayer.srs || gdal.SpatialReference.fromEPSG(4326);
20       currentTransformation = new gdal.CoordinateTransformation(srs, to);
21     }
22 
23     getNextLayer();
24 
25     var layerStream = from(function(size, next) {
26       writeNextFeature();
27 
28       function writeNextFeature() {
29           var feature = currentLayer.features.next();
30           if (!feature) {
31               // end stream
32               if (nextLayer === layerCount) {
33                   // push remaining output and end
34                   layerStream.push(out);
35                   layerStream.push(after);
36                   return layerStream.push(null);
37               }
38               getNextLayer();
39               feature = currentLayer.features.next();
40           }
41 
42           try {
43               var geom = feature.getGeometry();
44           } catch (e) {
45               return writeNextFeature();
46           }
47 
48           geom.transform(currentTransformation);
49           var geojson = geom.toJSON();
50           var fields = feature.fields.toJSON();
51           var featStr = ‘{"type": "Feature", "properties": ‘ + fields + ‘,"geometry": ‘ + geojson + ‘}‘;
52 
53           if (started) {
54               featStr = ‘,\n‘ + featStr;
55           } else {
56               featStr = before + featStr;
57           }
58 
59           started = true;
60           out += featStr;
61 
62          countLimit++;  //要素讀取數量
63                   
64          if(countLimit < 8000)
65             writeNextFeature();  //這個方法報錯,棧溢出
66 
67       }
68 
69     })
70 
71     outStream.setReadable(layerStream);
72     outStream.end(after);
73 
74     return (out += after) ;  
75 }
View Code

三. 問題

這個包轉為的速率並不理想,甚至很慢......

而且當要素數量過大時,會棧溢出,這和機器的內存有關......

當然我也束手無策,

所以,呵呵噠。

[Github]shapefile轉json——對Github上shp2json項目的修改