Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to perform batch coordinate conversion of geojson layers

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to carry out batch coordinate conversion of geojson layer. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.

In the absence of regular coordinate offset, such as domestic online map offset and latitude and longitude conversion, it is convenient to convert single coordinates, and the conversion layer needs to be developed by itself.

Here we use coordtransform to convert the offset coordinates.

//introduce fs and coordintransform libraries const fs = require ('fs ');const coordintransform = require ('coordintransform');//geojson vector object processing function dataByfeaturetype(_feature) { this.datafeature = _feature; this.featuretype = _feature.geometry.type; this.coords = _feature.geometry.coordinates; this. newcoordinates = [];}//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); });});

After reading the above, do you have any further understanding of how to perform batch coordinate conversion of geojson layers? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report