node.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. let fs = require('fs');
  2. let params = {
  3. type: "FeatureCollection",
  4. totalFeatures: 544,
  5. features: [
  6. ]
  7. }
  8. function writeJson(params){
  9. //现将json文件读出来
  10. fs.readFile('./map_bf.json',function(err,data){
  11. console.log('读取文件')
  12. if(err){
  13. return console.error(err);
  14. }
  15. console.log('读取文件成功')
  16. let bangladesh = data.toString();//将二进制的数据转换为字符串
  17. bangladesh = JSON.parse(bangladesh);//将字符串转换为json对象
  18. let arr2 = [];
  19. console.log('开始计算')
  20. let i = 0;
  21. // let cityName = [];
  22. let provinceNameArr = ["Barisal", "Chittagong", "Rajshahi", "Rangpur", "Dhaka", "Sylhet", "Mymensingh", "Khulna"];
  23. let provinceNameArrJSON = {
  24. Barisal:{
  25. type: "FeatureCollection",
  26. totalFeatures: 544,
  27. features: [
  28. ],
  29. },
  30. Chittagong:{
  31. type: "FeatureCollection",
  32. totalFeatures: 544,
  33. features: [
  34. ],
  35. },
  36. Rajshahi:{
  37. type: "FeatureCollection",
  38. totalFeatures: 544,
  39. features: [
  40. ],
  41. },
  42. Rangpur:{
  43. type: "FeatureCollection",
  44. totalFeatures: 544,
  45. features: [
  46. ],
  47. },
  48. Dhaka:{
  49. type: "FeatureCollection",
  50. totalFeatures: 544,
  51. features: [
  52. ],
  53. },
  54. Sylhet:{
  55. type: "FeatureCollection",
  56. totalFeatures: 544,
  57. features: [
  58. ],
  59. },
  60. Mymensingh:{
  61. type: "FeatureCollection",
  62. totalFeatures: 544,
  63. features: [
  64. ],
  65. },
  66. Khulna:{
  67. type: "FeatureCollection",
  68. totalFeatures: 544,
  69. features: [
  70. ],
  71. },
  72. };
  73. let cityName = {
  74. Barisal:['Barguna','Barisal','Bhola','Jhalokati','Patuakhali','Pirojpur'],
  75. Chittagong:['Bandarban','Brahmanbaria','Chandpur','Chittagong','Khagrachhari','Lakshmipur','Noakhali','Rangamati','Comilla','Cox\'s Bazar','Feni'],
  76. Rajshahi:['Naogaon','Natore','Chapai Nawabganj','Pabna','Rajshahi','Sirajganj','Bogra','Joypurhat'],
  77. Rangpur:['Dinajpur','Gaibandha','Kurigram','Lalmonirhat','Nilphamari','Panchagarh','Rangpur','Thakurgaon'],
  78. Dhaka:[ 'Dhaka','Kishoreganj','Madaripur','Manikganj','Munshiganj','Narayanganj','Narsingdi','Rajbari','Shariatpur','Tangail','Faridpur','Gazipur','Gopalganj'],
  79. Sylhet:['Habiganj', 'Maulvibazar', 'Sunamganj', 'Sylhet'],
  80. Mymensingh:[ 'Mymensingh', 'Netrakona', 'Sherpur', 'Jamalpur'],
  81. Khulna:[ 'Bagerhat','Chuadanga','Jessore','Jhenaidah','Khulna','Kushtia','Magura','Meherpur','Narail','Satkhira']
  82. };
  83. let arr3Length = 0;
  84. let index = 0;
  85. let number = 0;
  86. // Pirojpur
  87. let fun = function(arr,provinceName,name,item,cfName){
  88. let arr3 = [];
  89. // console.log('计算第'+i+'轮开始');
  90. // console.log(item)
  91. for(let d of arr){
  92. // d.properties.name = d.properties.Divi_name+'-'+ d.properties.Dist_name+'-'+ d.properties.Upaz_name;
  93. // d.properties.name = d.properties.Divi_name;
  94. // d.properties.name = d.properties.Dist_name;
  95. // d.properties.name = d.properties.Upaz_name;
  96. // console.log(d.properties.Dist_name+' '+name,'----8888888888888888-----')
  97. if(d.properties.Divi_name == provinceName){
  98. if(d.properties.Dist_name == name){
  99. if(d.properties.Upaz_name=='Kutubdia Upazila'||d.properties.Upaz_name=='Maheshkhali Upazila'||d.properties.Upaz_name=='Phulbari Sadar Upazila'||d.properties.Upaz_name=='Bhurungamari Upazila'){
  100. continue
  101. }
  102. if(cfName == d.properties.Upaz_name){
  103. continue
  104. }
  105. item.properties.name = d.properties.Dist_name+' Division';
  106. if(item.geometry.coordinates.length == 0){
  107. number++;
  108. item.geometry.coordinates = JSON.parse(JSON.stringify(d.geometry.coordinates));
  109. console.log('已筛选 '+number+' '+d.properties.Upaz_name+'-------------1---------');
  110. }else{
  111. let xArr = [],xIndex = 0;
  112. let yArr = [],yIndex = 0;
  113. let x1 = 0;
  114. let x2 = 0;
  115. let y1 = 0;
  116. let y2 = 0;
  117. let xk = false;
  118. let xkk = false;
  119. let yk = false;
  120. let ykk = false;
  121. for(let j in d.geometry.coordinates[0][0]){
  122. for(let key in item.geometry.coordinates[0][0]){
  123. if(Math.abs(item.geometry.coordinates[0][0][key][0] - d.geometry.coordinates[0][0][j][0])+Math.abs(item.geometry.coordinates[0][0][key][1] - d.geometry.coordinates[0][0][j][1]) < 0.0000001){
  124. if(!x1){
  125. x1 = Number(key);
  126. xArr[xIndex] = Number(key);
  127. }else{
  128. if(Math.abs(x2-Number(key)) > 20){
  129. xIndex++;
  130. xArr[xIndex] = x2;
  131. xIndex++;
  132. xArr[xIndex] = Number(key);
  133. }
  134. }
  135. if(!y1){
  136. y1 = Number(j);
  137. yArr[yIndex] = Number(j);
  138. }else{
  139. if(Math.abs(y2-Number(j)) > 20){
  140. yIndex++;
  141. yArr[yIndex] = y2;
  142. yIndex++;
  143. yArr[yIndex] = Number(j);
  144. }
  145. }
  146. x2 = Number(key);
  147. y2 = Number(j);
  148. if(!xk&&x1!=x2){
  149. xk = x2-x1>0?'+':'-'
  150. xkk = x2;
  151. }
  152. if(!yk&&y1!=y2){
  153. yk = y2-y1>0?'+':'-';
  154. ykk = y2;
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. xIndex++;
  161. xArr[xIndex] = Number(x2);
  162. yIndex++;
  163. yArr[yIndex] = Number(y2);
  164. if(x1||x2||y1||y2){
  165. console.log(xArr,item.geometry.coordinates[0][0].length,yArr,d.geometry.coordinates[0][0].length,'-------')
  166. if(yArr.length == 4){
  167. y1 = yArr[2]
  168. y2 = yArr[1]
  169. }
  170. if(y2>y1){
  171. arr2 = d.geometry.coordinates[0][0].slice(y2,d.geometry.coordinates[0][0].length)
  172. arr2.push(...(d.geometry.coordinates[0][0].slice(0,y1)))
  173. }else{
  174. arr2 = d.geometry.coordinates[0][0].slice(y2,y1)
  175. }
  176. if(x2>x1){
  177. item.geometry.coordinates[0][0].splice(x2,item.geometry.coordinates[0][0].length-1-x2)
  178. item.geometry.coordinates[0][0].splice(0,x1,...arr2)
  179. }else if(x2 == x1){
  180. x1 = Math.max(...xArr)
  181. x2 = Math.min(...xArr)
  182. console.log(x1,x2)
  183. item.geometry.coordinates[0][0].splice(x2,x1-x2,...arr2)
  184. }else{
  185. item.geometry.coordinates[0][0].splice(x2,x1-x2,...arr2)
  186. }
  187. if(d.geometry.coordinates.length>1){
  188. item.geometry.coordinates.push(...d.geometry.coordinates.slice(1,d.geometry.coordinates.length))
  189. }
  190. number++;
  191. console.log('已筛选 '+number+' '+d.properties.Upaz_name+'-------------2---------');
  192. }else{
  193. arr3.push(d)
  194. }
  195. console.log(provinceName+'-'+name+'-'+d.properties.Upaz_name+' '+arr3.length+'_'+arr3Length+'-'+index);
  196. // d.geometry.coordinates[0][0] = arr1
  197. }
  198. }
  199. }
  200. }
  201. if(arr3Length == arr3.length){
  202. index++;
  203. }else{
  204. index = 0;
  205. arr3Length = 0;
  206. }
  207. if(arr3.length>0&&index<2){
  208. arr3Length = arr3.length;
  209. fun(arr3,provinceName,name,item)
  210. }else{
  211. if(index>=2){
  212. item = {
  213. type: "Feature",
  214. geometry: {
  215. type: "MultiPolygon",
  216. coordinates: arr3[0].geometry.coordinates
  217. },
  218. properties: {
  219. name:'',
  220. }
  221. }
  222. index = 0;
  223. number = 1;
  224. arr3Length = 0;
  225. console.log('已筛选 '+number+' '+arr3[0].properties.Upaz_name+'-------------3---------');
  226. fun(bangladesh.features,'Barisal',name,item,arr3[0].properties.Upaz_name)
  227. }else{
  228. console.log(item.geometry.coordinates[0][0].length+'-------------')
  229. provinceNameArrJSON[provinceName].features.push(item)
  230. if(provinceNameArrJSON[provinceName].features.length>0){
  231. console.log(provinceName+' '+name+'已计算');
  232. console.log(provinceNameArrJSON[provinceName].features.length+'-'+cityName[provinceName].length)
  233. if(provinceNameArrJSON[provinceName].features.length == cityName[provinceName].length){
  234. let str = JSON.stringify(provinceNameArrJSON[provinceName]);//因为nodejs的写入文件只认识字符串或者二进制数,所以把json对象转换成字符串重新写入json文件中
  235. // console.log(str)
  236. fs.writeFile('./'+provinceName+'.json',str,function(err){
  237. if(err){
  238. console.log(err);
  239. }
  240. console.log('----------'+provinceName+'计算完成-------------');
  241. })
  242. }
  243. }
  244. }
  245. }
  246. }
  247. // for(let provinceName of provinceNameArr){
  248. for(let name of cityName['Barisal']){
  249. let item = {
  250. type: "Feature",
  251. geometry: {
  252. type: "MultiPolygon",
  253. coordinates: [[[]]]
  254. },
  255. properties: {
  256. name:'',
  257. }
  258. }
  259. index = 0;
  260. number = 0;
  261. arr3Length = 0;
  262. fun(bangladesh.features,'Barisal',name,item)
  263. }
  264. // fun(bangladesh.features,'Barisal','Bhola',item)
  265. // }
  266. // fun(bangladesh.features,'Khulna')
  267. // console.log(cityName)
  268. // params.features.push(item);//将传来的对象push进数组对象中
  269. // var str = JSON.stringify(params);//因为nodejs的写入文件只认识字符串或者二进制数,所以把json对象转换成字符串重新写入json文件中
  270. // fs.writeFile('./person.json',str,function(err){
  271. // if(err){
  272. // console.error(err);
  273. // }
  274. // console.log('----------计算完成-------------');
  275. // })
  276. })
  277. }
  278. writeJson(params)//执行一下;