cityland.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. function CityLandAction(context, id){
  2. this.context = context
  3. this.id = id
  4. this.selected = {}
  5. this.tempVectorLayer = null
  6. this.popup = null
  7. this.obj = new CityLand({
  8. url : context.local_wmts_path
  9. })
  10. }
  11. CityLandAction.prototype = {
  12. clickLock:false,
  13. startStatus : false,
  14. createStatus : false,
  15. create(){
  16. let that = this
  17. this.obj.createTileLayer()
  18. this.tempVectorLayer = this.loadTempVectorLayer()
  19. this.drawMultiPolygon = new DrawMultiPolygon({vector: this.tempVectorLayer, map: this.context.currentMap,wkt: this.context.wkt})
  20. this.popup = new Popup({map: this.context.currentMap,
  21. close(){
  22. that.clickLock = false
  23. },
  24. buttons:[
  25. {"name":"发布任务","click":"pubTask"},{"name":"缩放","click":"fit"}
  26. ],
  27. listener: {
  28. info(){
  29. window.location = "/field_point/info"
  30. },
  31. fit(popup, event, ext){
  32. that.context.currentView.fit(ext.getGeometry(),{duration:1000})
  33. },
  34. pubTask(popup, event, ext){
  35. that.saveTask(that, ext)
  36. }
  37. }
  38. });
  39. this.createStatus = true;
  40. },
  41. start(){
  42. if(this.startStatus){
  43. console.log("startStatus is true")
  44. return
  45. }
  46. // this.context.clicks[this.id] = this
  47. this.obj.addTileLayer(this.context.currentMap)
  48. this.context.currentMap.addLayer(this.tempVectorLayer)
  49. this.drawMultiPolygon.show()
  50. this.context.landFilter()
  51. this.context.getSldStyleSign(this.obj.wmsData.params.STYLE)
  52. this.startStatus = true;
  53. // this.showBuffer();
  54. },
  55. stop(){
  56. if(!this.startStatus){
  57. console.log("startStatus is false")
  58. return
  59. }
  60. this.context.currentMap.removeLayer(this.tempVectorLayer)
  61. this.obj.removeTileLayer(this.context.currentMap)
  62. console.log("drawMultiPolygon close")
  63. this.drawMultiPolygon.close()
  64. console.log("tempVectorLayer cleared")
  65. this.context.clicks[this.id] = null
  66. console.log("remove click")
  67. this.popup._close()
  68. console.log("close popup")
  69. this.startStatus = false;
  70. },
  71. click(event, that){
  72. if(that.clickLock){
  73. console.log("clickLock", that.clickLock)
  74. return
  75. }
  76. that.clickLock = true
  77. let layerName = that.obj.wmsData.params.LAYER.substring(that.obj.wmsData.params.LAYER.indexOf(":") + 1)
  78. that.context.getInfo(layerName, event.coordinate, function(res){
  79. try {
  80. if(res.code == -1){
  81. return;
  82. }
  83. console.log(res.data)
  84. let geometry = that.context.wkt.readGeometry(res.data.wkt)
  85. let area = geometry.getArea()
  86. area = (area + area / 2) / 1000
  87. res.data["area"] = area.toFixed(2)
  88. let feature = new ol.Feature({
  89. geometry: geometry
  90. });
  91. for(let key in res.data){
  92. if(key != "wkt"){
  93. feature.set(key, res.data[key])
  94. }
  95. }
  96. that.tempVectorLayer.getSource().clear()
  97. that.tempVectorLayer.getSource().addFeature(feature)
  98. if(res.data.status > 0){
  99. that.addFieldPointOfTempLayer(res.data)
  100. }
  101. that.popup.show(ol.extent.getCenter(feature.getGeometry().getExtent()), that.getInfoHtml(feature), feature)
  102. }catch (e){
  103. console.log(e)
  104. }finally {
  105. that.clickLock = false
  106. }
  107. },function(){
  108. that.clickLock = false
  109. })
  110. },
  111. showBuffer(){
  112. let that = this
  113. that.context.getBuffer([113.26962951874916,23.126323245095218],5000, function(res){
  114. try {
  115. if(res.code == -1){
  116. return;
  117. }
  118. let geometry = that.context.wkt.readGeometry(res.msg)
  119. let feature = new ol.Feature({
  120. geometry: geometry
  121. });
  122. that.tempVectorLayer.getSource().addFeature(feature)
  123. }catch (e){
  124. console.log(e)
  125. }finally {
  126. }
  127. })
  128. },
  129. addFieldPointOfTempLayer(cityLand){
  130. let geometry = this.context.wkt.readGeometry(cityLand.wkt)
  131. let extent = ol.extent.getCenter(geometry.getExtent());
  132. let point = new ol.Feature({
  133. geometry: new ol.geom.Point(extent)
  134. });
  135. point.set("isPoint",1)
  136. point.setId(cityLand.id)
  137. point.set("status",cityLand.status)
  138. let source = this.tempVectorLayer.getSource();
  139. source.addFeature(point)
  140. },
  141. newFieldPoint(data){
  142. let geom = this.context.geojson.readGeometry(data["geojson"])
  143. let point = new ol.Feature({
  144. geometry: geom
  145. });
  146. for(let key in data){
  147. if(key != "geojson"){
  148. point.set(key,data[key])
  149. }
  150. }
  151. point.set("isPoint",1)
  152. point.setId(data["id"])
  153. return point;
  154. }
  155. ,loadTempVectorLayer(){
  156. let that = this
  157. let map = this.context.currentMap
  158. const vectorSource = new ol.source.Vector({
  159. loader: function(extent, resolution, projection, success, failure) {
  160. if(!that.context.vectorSwitch){
  161. return;
  162. }
  163. let typeNames = that.obj.getVisibleLayers()
  164. console.log("typeNames",typeNames)
  165. const xhr = new XMLHttpRequest();
  166. xhr.open('GET', "/crop/getGeoJson?tableNames="+typeNames + "&x1=" + extent[0] + "&y1=" + extent[1]+"&x2=" + extent[2] + "&y2="+extent[3]);
  167. const onError = function() {
  168. vectorSource.removeLoadedExtent(extent);
  169. failure();
  170. }
  171. xhr.onerror = onError;
  172. xhr.onload = function() {
  173. if (xhr.status == 200) {
  174. let res = JSON.parse(xhr.responseText);
  175. if(res.code == 0){
  176. let features = []
  177. let datas = res.data;
  178. for(let data of datas){
  179. let feature = that.newFieldPoint(data)
  180. vectorSource.addFeature(feature)
  181. features.push(feature)
  182. }
  183. success(features);
  184. if(datas.length > 0){
  185. that.context.cacheBbox(extent)
  186. that.context.vectorSwitch = false
  187. }
  188. }
  189. } else {
  190. onError();
  191. }
  192. }
  193. xhr.send();
  194. },
  195. strategy: ol.loadingstrategy.bbox,
  196. });
  197. let vector = new ol.layer.Vector({
  198. source:vectorSource,
  199. style: globalStyle.cropStyle,
  200. minZoom:14,
  201. maxZoom:22,
  202. zIndex: 3,
  203. visible: true
  204. });
  205. that.select = new ol.interaction.Select({
  206. condition: ol.events.condition.singleClick,
  207. layers:[vector],
  208. style: globalStyle.selectedCropStyle
  209. })
  210. map.addInteraction(that.select)
  211. that.select.on("select",function(e){
  212. if(e.selected.length > 0){
  213. that.popup.show(ol.extent.getCenter(e.selected[0].getGeometry().getExtent()), that.getInfoHtml(e.selected[0]), e.selected[0])
  214. }
  215. })
  216. return vector;
  217. },getInfoHtml(feature){
  218. let html = ""
  219. console.log(feature.getKeys())
  220. let data = {
  221. "地块类别":this.landType[feature.get("gridcode")],
  222. "名称":feature.get("name"),
  223. "地址":feature.get("address"),
  224. "作物":cropTypes[feature.get("crop_type")],
  225. "地块图斑编号":feature.get("id") || feature.getId(),
  226. "地块状态":this.taskStatus[feature.get("status")],
  227. "生长期":cropPeriods[feature.get("growing_period")],
  228. "图片信息":"<a style='color: whitesmoke;' href='javascript:layui.tool.open(!,!,!,!)'>点击查看</a>"
  229. .format('"/crop_image/view?id='+feature.get("id")+'"','"60%"','"图片信息"','{area:["90%","90%"],shade:0.1}'),
  230. }
  231. for(let key in data){
  232. html += "<div class='layui-row'><div class='lab' >"+key+"</div><div class='val' >"+data[key]+"</div></div>"
  233. }
  234. return html
  235. },
  236. taskStatus:["未发布","已发布","已领取","已完成"],
  237. landType:{0:"其他", 1:"湿地", 2:"耕地", 3:"种植", 4:"林地", 5:"草地", 6:"水域"}
  238. ,
  239. saveTask(that, feature){
  240. let id = feature.getId()
  241. layui.tool.submit("/crop/publish",{"id":feature.get("id") || id.substring(id.indexOf(".") + 1)},function(res){
  242. if(res.code == 0){
  243. that.addFieldPointOfTempLayer(res.data)
  244. }
  245. })
  246. }
  247. }
  248. function CityLand(opt){
  249. this.name = "CityLand"
  250. var gridsetName = 'EPSG:4326';
  251. var gridNames = ['EPSG:4326:0', 'EPSG:4326:1', 'EPSG:4326:2', 'EPSG:4326:3', 'EPSG:4326:4', 'EPSG:4326:5', 'EPSG:4326:6', 'EPSG:4326:7', 'EPSG:4326:8', 'EPSG:4326:9', 'EPSG:4326:10', 'EPSG:4326:11', 'EPSG:4326:12', 'EPSG:4326:13', 'EPSG:4326:14', 'EPSG:4326:15', 'EPSG:4326:16', 'EPSG:4326:17', 'EPSG:4326:18', 'EPSG:4326:19', 'EPSG:4326:20', 'EPSG:4326:21'];
  252. this.wmsData = {
  253. url: opt.url,
  254. projection: new ol.proj.Projection({
  255. code: 'EPSG:4326',
  256. units: 'degrees',
  257. axisOrientation: 'neu'
  258. }),
  259. params:{
  260. 'VERSION': '1.0.0',
  261. 'STYLE': "wuhan:city_land",
  262. 'TILEMATRIX': gridNames,
  263. 'TILEMATRIXSET': gridsetName,
  264. 'SERVICE': 'WMTS',
  265. 'FORMAT': "image/png"
  266. }
  267. }
  268. this.layerData = {
  269. layer : [],
  270. source : [],
  271. always : false
  272. }
  273. }
  274. CityLand.prototype = {
  275. visibleLayers:[],
  276. getVisibleLayers(){
  277. let names = "";
  278. let i = 0;
  279. for(let layerName of this.visibleLayers){
  280. if(i > 0){
  281. names += ","
  282. }
  283. names += layerName
  284. i++;
  285. }
  286. return names;
  287. },
  288. layerNames:['wuhan:p_land_banan','wuhan:p_land_beibei','wuhan:p_land_bishan','wuhan:p_land_changshou','wuhan:p_land_chengkou','wuhan:p_land_dadukou','wuhan:p_land_dazu','wuhan:p_land_dianjiang','wuhan:p_land_fengdu','wuhan:p_land_fengjie','wuhan:p_land_fuling','wuhan:p_land_hechuan','wuhan:p_land_jiangbei','wuhan:p_land_jiangjin','wuhan:p_land_jiulongpo','wuhan:p_land_kaizhou','wuhan:p_land_liangping','wuhan:p_land_nanan','wuhan:p_land_nanchuan','wuhan:p_land_pengshui','wuhan:p_land_qianjiang','wuhan:p_land_qijiang','wuhan:p_land_qiuyang','wuhan:p_land_rongchang','wuhan:p_land_shapingba','wuhan:p_land_shizhu','wuhan:p_land_tongliang','wuhan:p_land_tongnan','wuhan:p_land_wanzhou','wuhan:p_land_wulong','wuhan:p_land_wushan','wuhan:p_land_wuxi','wuhan:p_land_xiushan','wuhan:p_land_yongchuan','wuhan:p_land_yubei','wuhan:p_land_yunyang','wuhan:p_land_yuzhong','wuhan:p_land_zhong','wuhan:p_crop'],
  289. createTileLayer(){
  290. for(let layer of this.layerNames){
  291. this.getWMTS(layer)
  292. }
  293. },
  294. addTileLayer(map){
  295. for(let layer of this.layerData.layer){
  296. map.addLayer(layer)
  297. }
  298. },
  299. removeTileLayer(map){
  300. for(let layer of this.layerData.layer){
  301. map.removeLayer(layer)
  302. }
  303. }
  304. ,getWMS(typeName){
  305. var resolutions = [0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125, 0.02197265625, 0.010986328125, 0.0054931640625, 0.00274658203125, 0.001373291015625, 6.866455078125E-4, 3.4332275390625E-4, 1.71661376953125E-4, 8.58306884765625E-5, 4.291534423828125E-5, 2.1457672119140625E-5, 1.0728836059570312E-5, 5.364418029785156E-6, 2.682209014892578E-6, 1.341104507446289E-6, 6.705522537231445E-7, 3.3527612686157227E-7];
  306. let source = new ol.source.ImageWMS({
  307. url: 'http://change.zylfsm.com/geoserver/gwc/service/wms?SERVICE=WMS&',
  308. layer: typeName,
  309. matrixSet: this.wmsData.params['TILEMATRIXSET'],
  310. format: this.wmsData.params['FORMAT'],
  311. projection: this.wmsData.projection,
  312. tileGrid: new ol.tilegrid.WMS({
  313. tileSize: [256,256],
  314. extent: [-180.0,-90.0,180.0,90.0],
  315. origin: [-180.0, 90.0],
  316. resolutions: resolutions,
  317. matrixIds: this.wmsData.params['TILEMATRIX']
  318. }),
  319. style: this.wmsData.params['STYLE'],
  320. wrapX: true
  321. })
  322. this.layerData.source.push(source)
  323. let layer = new ol.layer.Image({
  324. source: source,
  325. zIndex: 2,
  326. minZoom: 7,
  327. maxZoom: 30,
  328. visible: true,
  329. });
  330. this.layerData.layer.push(layer)
  331. },getWMTS(typeName){
  332. var resolutions = [0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125, 0.02197265625, 0.010986328125, 0.0054931640625, 0.00274658203125, 0.001373291015625, 6.866455078125E-4, 3.4332275390625E-4, 1.71661376953125E-4, 8.58306884765625E-5, 4.291534423828125E-5, 2.1457672119140625E-5, 1.0728836059570312E-5, 5.364418029785156E-6, 2.682209014892578E-6, 1.341104507446289E-6, 6.705522537231445E-7, 3.3527612686157227E-7];
  333. var baseUrl = this.wmsData.url;
  334. var baseParams = ['VERSION','STYLE','TILEMATRIX','TILEMATRIXSET','SERVICE','FORMAT'];
  335. var url = baseUrl+'?LAYER='+ typeName +'&'
  336. for (var param in this.wmsData.params) {
  337. if (baseParams.indexOf(param.toUpperCase()) < 0) {
  338. url = url + param + '=' + this.wmsData.params[param] + '&';
  339. }
  340. }
  341. url = url.slice(0, -1);
  342. let source = new ol.source.WMTS({
  343. url: url,
  344. layer: typeName,
  345. matrixSet: this.wmsData.params['TILEMATRIXSET'],
  346. format: this.wmsData.params['FORMAT'],
  347. projection: this.wmsData.projection,
  348. tileGrid: new ol.tilegrid.WMTS({
  349. tileSize: [256,256],
  350. extent: [-180.0,-90.0,180.0,90.0],
  351. origin: [-180.0, 90.0],
  352. resolutions: resolutions,
  353. matrixIds: this.wmsData.params['TILEMATRIX']
  354. }),
  355. style: this.wmsData.params['STYLE'],
  356. wrapX: true
  357. })
  358. this.layerData.source.push(source);
  359. let tile = new ol.layer.Tile({
  360. source: source,
  361. zIndex: 2,
  362. minZoom: 7,
  363. maxZoom: 30,
  364. visible: false,
  365. properties:{"name":typeName.substring(typeName.indexOf(":") + 1)}
  366. })
  367. this.layerData.layer.push(tile);
  368. return tile;
  369. },
  370. visibleWMTS(tableNames){
  371. this.visibleLayers = tableNames
  372. for(let layer of this.layerData.layer){
  373. if(tableNames.includes(layer.get("name"))){
  374. layer.setVisible(true)
  375. }else{
  376. layer.setVisible(false)
  377. }
  378. }
  379. }
  380. }
  381. function DrawMultiPolygon(option){
  382. this.vector = option.vector
  383. this.map = option.map
  384. this.wkt = option.wkt
  385. let that = this
  386. let html = "<a href='javascript:void(0)' id='drawMultiPolygon' style='color: white;position: absolute;top: 0px;right: 100px;z-index: 10' >绘制多边形</a>";
  387. $(document.body).append(html)
  388. this.initDraw()
  389. $("#drawMultiPolygon").on("click",function(){
  390. that.vector.getSource().clear()
  391. that.draw.setActive(true)
  392. })
  393. }
  394. DrawMultiPolygon.prototype = {
  395. initDraw(){
  396. let that = this
  397. that.draw = new ol.interaction.Draw({
  398. source : that.vector.getSource(),
  399. type: "MultiPolygon",
  400. freehand: false,//自由绘制
  401. stopClick: true
  402. })
  403. that.draw.setActive(false)
  404. that.draw.on("drawend", function(e){
  405. that.draw.setActive(false)
  406. that.submit(e.feature)
  407. })
  408. that.map.addInteraction(that.draw)
  409. },
  410. show(){
  411. $("#drawMultiPolygon").show()
  412. },
  413. close(){
  414. this.draw.setActive(false)
  415. $("#drawMultiPolygon").hide()
  416. },
  417. submit(feature){
  418. let geometry = feature.getGeometry()
  419. layui.tool.post("/crop/addTest",{"wkt":this.wkt.writeGeometry(geometry)})
  420. }
  421. }