我已经成功安装了AGStoShape文件,它是一个node.js脚本,可将ESRI Geoservices转换为geojson和shapefile格式。
github存储库在这里https://github.com/tannerjt/AGStoShapefile 我遇到问题的具体.js文件是https://github.com/tannerjt/AGStoShapefile/blob/master/AGStoSHP.js
默认情况下,脚本使用带有服务的输入txt文件并输出.shp,.json文件。此services.txt位于AGStoShapefile目录中,并且在终端中脚本运行完美,直到第154行,此时出现以下错误:
node AGStoSHP.js
info: Number of features for service: 225
info: Getting chunks of 100 features...
info: query -> 0 out of 225
info: query -> 100 out of 225
info: query -> 200 out of 225
info: Number of features for service: 8258
info: Getting chunks of 100 features...
info: query -> 0 out of 8258
info: query -> 100 out of 8258
info: query -> 200 out of 8258
info: query -> 300 out of 8258
info: query -> 400 out of 8258
info: query -> 500 out of 8258
info: query -> 600 out of 8258
info: query -> 700 out of 8258
info: query -> 800 out of 8258
info: query -> 900 out of 8258
它继续......
info: query -> 7100 out of 8258
info: query -> 7200 out of 8258
info: query -> 7300 out of 8258
info: query -> 7400 out of 8258
info: query -> 7500 out of 8258
info: query -> 7600 out of 8258
info: query -> 7700 out of 8258
info: query -> 7800 out of 8258
info: query -> 7900 out of 8258
info: query -> 8000 out of 8258
info: query -> 8100 out of 8258
info: query -> 8200 out of 8258
info: all requests settled
info: creating WildFire json
info: Creating Esri JSON
info: Creating GeoJSON
info: Creating Shapefile
events.js:154
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open './output/WildFire.json'
at Error (native)
知道为什么没有创建输出文件?我对node.js来说相当新。
这是AGStoSHP.js文件:
var ogr2ogr = require('ogr2ogr');
var esri2geo = require('esri2geo');
var q = require('q');
var request = q.nfbind(require('request'));
var objectstream = require('objectstream');
var fs = require('fs');
var queryString = require('query-string');
var winston = require('winston');
// Setup logging with winston
winston.level = 'debug';
// winston.add(winston.transports.File, {filename: './logfile.log'});
// ./mixin.js
// merge user query params with default
var mixin = require('./mixin');
var serviceFile = process.argv[2] || 'services.txt';
var outDir = process.argv[3] || './output/';
if(outDir[outDir.length - 1] !== '/') {
outDir += '/';
}
// Make request to each service
fs.readFile(serviceFile, function (err, data) {
if (err) {
winston.info(err);
throw err;
}
data.toString().split('\n').forEach(function (service) {
var service = service.split('|');
if(service[0].split('').length == 0) return;
var baseUrl = getBaseUrl(service[0].trim()) + '/query';
var reqQS = {
where: '1=1',
returnIdsOnly: true,
f: 'json'
};
var userQS = getUrlVars(service[0].trim());
// mix one obj with another
var qs = mixin(userQS, reqQS);
var qs = queryString.stringify(qs);
var url = decodeURIComponent(getBaseUrl(baseUrl) + '/query/?' + qs);
request({
url : url,
method : 'GET',
json : true
}, function (err, response, body) {
var err = err || body.error;
if(err) {
winston.info(err);
throw err;
}
requestService(service[0].trim(), service[1].trim(), body.objectIds);
});
})
});
// Resquest JSON from AGS
function requestService(serviceUrl, serviceName, objectIds) {
objectIds.sort();
winston.info('Number of features for service: ', objectIds.length);
winston.info('Getting chunks of 100 features...');
var requests = [];
for(var i = 0; i < Math.ceil(objectIds.length / 100); i++) {
var ids = [];
if ( ((i + 1) * 100) < objectIds.length ) {
ids = objectIds.slice(i * 100, (i * 100) + 100);
} else {
ids = objectIds.slice(i * 100, objectIds[objectIds.length]);
}
if(ids[0] !== undefined) {
winston.info('query ->', (i * 100) , 'out of', objectIds.length);
} else {
winston.info('wait for requests to settle...');
continue;
}
// we need these query params
var reqQS = {
objectIds : ids.join(','),
geometryType : 'esriGeometryEnvelope',
returnGeometry : true,
returnIdsOnly : false,
outFields : '*',
outSR : '4326',
f : 'json'
};
// user provided query params
var userQS = getUrlVars(serviceUrl);
// mix one obj with another
var qs = mixin(userQS, reqQS);
var qs = queryString.stringify(qs);
var url = decodeURIComponent(getBaseUrl(serviceUrl) + '/query/?' + qs);
var r = request({
url : url,
method : 'GET',
json : true
});
requests.push(r);
};
q.allSettled(requests).then(function (results) {
winston.info('all requests settled');
var allFeatures;
for(var i = 0; i < results.length; i++) {
if(i == 0) {
allFeatures = results[i].value[0].body;
} else {
allFeatures.features = allFeatures.features.concat(results[i].value[0].body.features);
}
}
winston.info('creating', serviceName, 'json');
var json = allFeatures;
//esri json
winston.info('Creating Esri JSON');
var stream = fs.createWriteStream(outDir + serviceName + '.json');
var objstream = objectstream.createSerializeStream(stream);
objstream.write(json);
objstream.end();
//geojson
winston.info('Creating GeoJSON');
var stream = fs.createWriteStream(outDir + serviceName + '.geojson');
var objstream = objectstream.createSerializeStream(stream);
esri2geo(json, function (err, data) {
if(err) {
throw(err);
winston.info('Error converting esri json to geojson');
return;
}
objstream.write(data);
objstream.end();
winston.info('Creating Shapefile');
//shapefile
var shapefile = ogr2ogr(data)
.format('ESRI Shapefile')
.options(['-nln', serviceName])
.skipfailures();
shapefile.stream().pipe(fs.createWriteStream(outDir + serviceName + '.zip'));
});
}).catch(function (err) {
winston.info(err);
throw err;
});
}
//http://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url
function getUrlVars(url) {
var vars = {}, hash;
var hashes = url.slice(url.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars[hash[0].toString()] = hash[1];
}
return vars;
}
// get base url for query
function getBaseUrl(url) {
// remove any query params
var url = url.split("?")[0];
if((/\/$/ig).test(url)) {
url = url.substring(0, url.length - 1);
}
return url;
}
答案 0 :(得分:2)
我是该GitHub存储库的维护者。该脚本假定已创建输出文件夹。您可以尝试创建一个名为output的文件夹,或手动指定您自己的输出位置和输入服务文本文件,如下所示:
node AGStoSHP.js services.txt ./myotherfolder/