我有一个关于澳大利亚郊区边界的大型GeoJSON文件,我想在西澳大利亚提取这些文件。
我有一个简单的节点程序尝试这样做,但我不断得到一个完整的输出文件" [object Object],[object Object],[object Object] ....& #34;
我不明白为什么这不起作用?
/**
* Created by cyork on 7/07/2014.
*/
var fs = require('fs');
var file = __dirname + '/POA_2011_AUST.json';
var fileOut = __dirname + '/WA-only.json';
var waJson = new Array();
var count = 0;
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
data = data.features;
for (var i = 0; i < data.length-1; i++) {
var postCode = data[i].properties['POA_NAME'];
if ((postCode >= 6000) && (postCode < 7000)) {
var temp = JSON.parse(JSON.stringify(data[i]));
console.log(temp);
waJson.push(temp);
}
}
fs.writeFile(fileOut, waJson, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
geoJSON文件的示例是:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "POA_CODE": "0800", "POA_NAME": "0800", "SQKM": 3.123064 }, "geometry": { "type": "Polygon", "coordinates": [
[ [ 130.833947231999986, -12.457410109499998 ], [ 130.83376361599997, -12.457233194 ], [ 130.833355679999983, -12.456840143 ], [ 130.833468, -12.45
6751768499997 ], [ 130.833939871999974, -12.456268252499997 ], [ 130.834299615999981, -12.4558996585 ], [ 130.834758239999985, -12.455456213499998
], [ 130.835050207999984, -12.4557681605 ], [ 130.83586198399999, -12.456378087 ], [ 130.836419295999946, -12.455982908499998 ], [ 130.836350176, -
12.455877773 ], [ 130.836291487999972, -12.455746645 ], [ 130.836250079999985, -12.455582402 ], [ 130.836231711999972, -12.455479893499998 ], [ 130
.836224895999976, -12.455428223 ], [ 130.836219839999984, -12.4553764785 ], [ 130.836217695999977, -12.455350467499997 ], [ 130.83621372799999, -12
.455272527 ], [ 130.836213343999987, -12.455220412499999 ], [ 130.836214079999962, -12.455168409 ], [ 130.836218751999979, -12.4550904685 ], [ 130.
836223455999971, -12.455038613 ], [ 130.836229919999965, -12.454986961 ], [ 130.836237856, -12.454935475499999 ], [ 130.836247551999975, -12.454884
36 ], [ 130.836258719999961, -12.454833411 ], [ 130.836278175999979, -12.45475782 ], [ 130.836292927999978, -12.454707962499997 ], [ 130.8363008319
99978, -12.454683117 ], [ 130.83632713599999, -12.454609598 ], [ 130.836346559999981, -12.4545612575 ], [ 130.836367071999973, -12.454513472 ], [ 1
30.836400927999989, -12.454443098 ], [ 130.836425408, -12.4543970145 ], [ 130.836450975999981, -12.454351745 ], [ 130.83648857599999, -12.454290658
], [ 130.836521151999989, -12.454242021499999 ], [ 130.836803775999982, -12.453776469 ], [ 130.836826815999984, -12.453731458499998 ], [ 130.83684
8415999981, -12.453685745 ], [ 130.836858463999988, -12.4536626015 ], [ 130.836877919999978, -12.453615981499999 ], [ 130.836895903999988, -12.4535
68732499999 ], [ 130.83691212799999, -12.453520947 ], [ 130.836934079999963, -12.453448316 ], [ 130.836952447999977, -12.453374797 ], [ 130.8369671
99999975, -12.453300538 ], [ 130.836978719999962, -12.453225576 ], [ 130.836984127999983, -12.453175348499997 ], [ 130.836988447999971, -12.4531250
47 ], [ 130.836990591999978, -12.453074542 ], [ 130.836991679999983, -12.453024055499997 ], [ 130.836991327999982, -12.452998766 ], [ 130.836989887
999977, -12.452948279499999 ], [ 130.83698700799999, -12.452897793 ], [ 130.836982335999977, -12.452847473 ], [ 130.83697584, -12.4527973565 ], [ 1
30.83696828799998, -12.452747499 ], [ 130.836960671999975, -12.452702969499997 ], [ 130.83694201599999, -12.452623919 ], [ 130.837251231999971, -12
.452548235499998 ], [ 130.837666848, -12.452448391 ], [ 130.837857375999988, -12.452502115 ], [ 130.838536576, -12.452705282 ], [ 130.8391567359999
78, -12.452983115 ], [ 130.839250591999985, -12.453029457499998 ], [ 130.839267967999973, -12.453116851499997 ], [ 130.83930755199998, -12.45326031
9 ], [ 130.839358687999976, -12.453417180499997 ], [ 130.839370559999963, -12.453446077499997 ], [ 130.839494047999978, -12.453729923 ], [ 130.8396
11871999978, -12.454027384499998 ], [ 130.839690239999982, -12.454085437499998 ], [ 130.840776351999978, -12.453031344499998 ], [ 130.8410204479999
66, -12.452587733 ], [ 130.841022751999986, -12.452290863499998 ], [ 130.841612831999981, -12.452292362 ], [ 130.842790207999968, -12.452293287 ],
[ 130.843119231999964, -12.452716622499997 ], [ 130.84358681599997, -12.452878368 ], [ 130.845151807999969, -12.452733346499997 ], [ 130.8466907839
99975, -12.452630338499997 ], [ 130.847082495999985, -12.452638312 ], [ 130.847415839999968, -12.452752808499998 ], [ 130.847780511999986, -12.4530
7284 ], [ 130.847864767999965, -12.452989497499999 ], [ 130.847946847999964, -12.452903916499997 ], [ 130.848000127999967, -12.452845586 ], [ 130.8
48052671999966, -12.4527863675 ], [ 130.848131615999961, -12.452693331 ], [ 130.848296, -12.452702007499997 ], [ 130.850171295999957, -12.453012271
], [ 130.850131327999975, -12.453236731499999 ], [ 130.849742527999979, -12.455466573499997 ], [ 130.848965311999962, -12.455335334499997 ], [ 130
.848914783999987, -12.455354371 ], [ 130.848888799999969, -12.455369374499998 ], [ 130.848862783999977, -12.455388374 ], [ 130.8488248, -12.4554343
64999896 ], [ 130.848817791999977, -12.455457360499997 ], [ 130.848815775999981, -12.455483371499998 ], [ 130.848792991999971, -12.455608061499998
], [ 130.848762783999973, -12.455771361 ], [ 130.848758783999983, -12.455856368499997 ], [ 130.848779807999989, -12.455929369499998 ], [ 130.848822
783999964, -12.455973362499998 ], [ 130.848880799999961, -12.456004368499997 ], [ 130.848913791999962, -12.456012360499997 ], [ 130.849013791999965
, -12.456019372 ], [ 130.849093791999962, -12.456048361499999 ], [ 130.849155807999978, -12.456102363 ], [ 130.850434623999973, -12.456365525499999
], [ 130.850099359999973, -12.458244274499998 ], [ 130.849329855999969, -12.458111426 ], [ 130.849253503999961, -12.458551948 ], [ 130.84922483199
9976, -12.459043400499997 ], [ 130.851038815999971, -12.459033410499998 ], [ 130.85103584, -12.459220408499997 ], [ 130.851314815999956, -12.459222
406499997 ], [ 130.85135436799996, -12.459423557 ], [ 130.851560895999967, -12.460662243 ], [ 130.850902239999982, -12.460646888 ], [ 130.851006911
999974, -12.4612172244999
答案 0 :(得分:0)
事实证明,我的问题的答案有很多部分。
为什么我只在节点中获得“[object Object],[object Object],[object Object] ....”?
这让我感到困惑,因为我会在控制台(来自console.log)和我写的文件中创建一系列[object]项。我可以consol.log JSON的元素很好,但不是整件事。但是,如果我将JSON文件的有效部分复制到JSBin并执行了console.log就可以了。
经过一些研究后,您可以使用console.log('%j“,Object)在节点中向控制台输出扩展的JSON,如本文How can I get the full object in Node.js's console.log(), rather than '[Object]'?
中所述。将JSON对象的提取元素写入文本文件时,我缺少的步骤是使用JSON.stringify(Object)对其进行字符串化。
我的结果代码现在是:
var fs = require('fs');
var file = __dirname + '/POA_2011_AUST.json';
var fileOut = __dirname + '/wa-only.json';
var waJson = new Array();
var i;
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
data = data.features;
console.log(data.length);
for (i = 0; i < data.length-1; i++) {
var postCode = 0;
postCode = data[i].properties.POA_CODE;
if ((postCode >= 6000) && (postCode < 7000)) {
waJson.push(JSON.stringify(data[i]));
}
}
fs.writeFile(fileOut, waJson, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});