如何使用arcgis js api 实现shp文件转为kml文件?是用代码实现哟

已邀请:

许丹石

赞同来自:

在web端 arcgis featureset的json格式对应的桌面端的shp格式。而kml在web端也是json格式。对照着去解析一下即可。示例数据:https://utility.arcgis.com/sharing/kml?url=https%3A%2F%2Fearthquake.usgs.gov%2Ffdsnws%2Fevent%2F1%2Fquery%3Fformat%3Dkml%26minmagnitude%3D5.8&model=simple&folders=&outSR=%7B%22wkid%22%3A4326%7D
 
另外,如果一定要这样实现。
第一步:http://zhihu.geoscene.cn/article/3612 将shp转为geojson
第二步:将geojson转为kml的形式(都是json格式,只是规则不同)。
示例:KML转geojson(反向转换同理,可供参考)
  _getKML: function (url) {
var requestUrl = 'http://utility.arcgis.com/sharing/kml?url=' + url + '&model=simple&folders=&outSR=%7B"wkid"%3A4326%7D';
L.esri.request(requestUrl, {}, function (err, res) {
if (err) {
console.log(err);
} else {
console.log(res);
this._parseFeatureCollection(res.featureCollection);
}
}, this);
},

_parseFeatureCollection: function (featureCollection) {
console.log('_parseFeatureCollection');
var i;
for (i = 0; i < 3; i++) {
if (featureCollection.layers[i].featureSet.features.length > 0) {
console.log(i);
var features = featureCollection.layers[i].featureSet.features;
var objectIdField = featureCollection.layers[i].layerDefinition.objectIdField;

var geojson = this._featureCollectionToGeoJSON(features, objectIdField);

if (featureCollection.layers[i].popupInfo !== undefined) {
this.popupInfo = featureCollection.layers[i].popupInfo;
}
if (featureCollection.layers[i].layerDefinition.drawingInfo.labelingInfo !== undefined) {
this.labelingInfo = featureCollection.layers[i].layerDefinition.drawingInfo.labelingInfo;
}

setRenderer(featureCollection.layers[i].layerDefinition, this);
console.log(geojson);
this.addData(geojson);
}
}
},

_featureCollectionToGeoJSON: function (features, objectIdField) {
var geojsonFeatureCollection = {
type: 'FeatureCollection',
features: []
};
var featuresArray = [];
var i, len;

for (i = 0, len = features.length; i < len; i++) {
var geojson = arcgisToGeoJSON(features[i], objectIdField);
featuresArray.push(geojson);
}

geojsonFeatureCollection.features = featuresArray;

return geojsonFeatureCollection;
}
});

export function kmlLayer (geojson, options) {
return new KMLLayer(geojson, options);
}

要回复问题请先登录注册