十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
今天就跟大家聊聊有关如何进行geojson图层批量坐标转换,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比垣曲网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式垣曲网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖垣曲地区。费用合理售后完善,十余年实体公司更值得信赖。
//引入fs和coordtransform库 const fs = require('fs'); const coordtransform = require('coordtransform'); //geojson矢量对象处理 function dataByfeaturetype(_feature) { this.datafeature = _feature; this.featuretype = _feature.geometry.type; this.coords = _feature.geometry.coordinates; this.newcoords = []; } //点、线、面中数据坐标遍历处理 dataByfeaturetype.prototype.handlePoint = function () { this.newcoords = coordtransform.wgs84togcj02(this.coords[0], this.coords[1]); } dataByfeaturetype.prototype.handleMultipointOrLinestring = function () { let _tempCoords = []; for (let _udx = 0; _udx < this.coords.length; _udx++) { //这个根据自己的需要,选择坐标的偏移处理函数 _tempCoords.push(coordtransform.wgs84togcj02(this.coords[_udx][0], this.coords[_udx][1])); } this.newcoords = _tempCoords; } dataByfeaturetype.prototype.handleMultiLineStringOrPolygon = function () { let _tempCoords = []; for (let _udx = 0; _udx < this.coords.length; _udx++) { let _evCoords = []; for (let _ndx = 0; _ndx < this.coords[_udx].length; _ndx++) { //这个根据自己的需要,选择坐标的偏移处理函数 _evCoords.push(coordtransform.wgs84togcj02(this.coords[_udx][_ndx][0], this.coords[_udx][_ndx][1])); } _tempCoords.push(_evCoords); } this.newcoords = _tempCoords; } dataByfeaturetype.prototype.handleMultiPolygon = function () { let _tempCoords = []; for (let _udx = 0; _udx < this.coords.length; _udx++) { let _polygons = []; for (let _ndx = 0; _ndx < this.coords[_udx].length; _ndx++) { let _polygon = []; for (let _tdx = 0; _tdx < this.coords[_udx][_ndx].length; _tdx++) { _polygon.push(coordtransform.wgs84togcj02(this.coords[_udx][_ndx][_tdx][0], this.coords[_udx][_ndx][_tdx][1])); } _polygons.push(_polygon); } _tempCoords.push(_polygons); } this.newcoords = _tempCoords; } //根据图层的不同类型,选择处理方法 dataByfeaturetype.prototype.handleByType = function () { switch (this.featuretype) { case "Point": this.handlePoint(); break; case "MultiPoint": case "LineString": this.handleMultipointOrLinestring(); break; case "MultiLineString": case "Polygon": this.handleMultiLineStringOrPolygon(); break; case "MultiPolygon": this.handleMultiPolygon(); break; } } //获取数据处理结果 dataByfeaturetype.prototype.getResult = function () { this.datafeature.geometry.coordinates = this.newcoords; return this.datafeature; }; //输入文件 fs.readFile('输入geojson文件', { encoding: 'utf-8' }, (err, res) => { if (err) return; let _geojsondata = JSON.parse(res); let _newfeatures = []; let _features = _geojsondata.features; for (let _fdx = 0; _fdx < _features.length; _fdx++) { let _feature = _features[_fdx]; let _featurehandle = new dataByfeaturetype(_feature); _featurehandle.handleByType(); _newfeatures.push(_featurehandle.getResult()); } _geojsondata.features = _newfeatures; //输出处理结果 fs.writeFile("输出geojson", JSON.stringify(_geojsondata), (err) => { console.log(err); }); });
看完上述内容,你们对如何进行geojson图层批量坐标转换有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。