geo_server_context.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. function GeoServerContext(opt,geoserver_path){
  2. this.local_wms_path = geoserver_path+"/wuhan/wms"
  3. this.local_wmts_path = geoserver_path+"/gwc/service/wmts"
  4. this.local_wfs_path = geoserver_path + "/wuhan/ows"
  5. this.actions = {}
  6. this.clicks = {}
  7. this.currentMap = opt.currentMap || window.map
  8. this.currentView = opt.currentView || window.view
  9. this.projection = opt.projection || window.projection
  10. this.wkt = new ol.format.WKT()
  11. let that = this
  12. this.currentMap.on("click", function(event){
  13. for(let id in that.clicks){
  14. let action = that.clicks[id]
  15. if(action && action.click){
  16. action.click(event, action)
  17. }
  18. }
  19. })
  20. }
  21. GeoServerContext.prototype = {
  22. createAction(key, Action, ext){
  23. if(!this.actions[key]){
  24. this.actions[key] = new Action(this, key, ext)
  25. this.actions[key].create()
  26. }
  27. return this.actions[key]
  28. },
  29. stopActions(currentKey){
  30. for(let key in this.actions){
  31. let always = false;
  32. if(this.actions[key].obj && this.actions[key].obj.layerData){
  33. always = this.actions[key].obj.layerData["always"]
  34. }
  35. if(!always && currentKey != key){
  36. this.actions[key].stop()
  37. }
  38. }
  39. },
  40. getInfo(layerName , coordinate, callback, error) {
  41. // coordinate = ol.proj.transform([coordinate[0],coordinate[1]],"EPSG:4326","EPSG:4526")
  42. console.log(layerName, coordinate)
  43. $.ajax({
  44. type : 'POST',
  45. url : "/crop/getInfo",
  46. data : {layerName: layerName, x : coordinate[0], y : coordinate[1]},
  47. dataType : 'json',
  48. success : function(res){
  49. callback(res)
  50. },error: function(req){
  51. if(error)
  52. error(req)
  53. }
  54. });
  55. },getBuffer(coordinate,m, callback, error) {
  56. // coordinate = ol.proj.transform([coordinate[0],coordinate[1]],"EPSG:4326","EPSG:4526")
  57. $.ajax({
  58. type : 'POST',
  59. url : "/crop/getBuffer",
  60. data : { x : coordinate[0], y : coordinate[1], m:m},
  61. dataType : 'json',
  62. success : function(res){
  63. callback(res)
  64. },error: function(req){
  65. if(error)
  66. error(req)
  67. }
  68. });
  69. },
  70. //获取图例
  71. getSldStyleSign(styleName){
  72. $.ajax({
  73. type : 'POST',
  74. url : "/sld_style/sign",
  75. data : {styleName: styleName},
  76. dataType : 'json',
  77. success : function(res){
  78. let html = ""
  79. let data = JSON.parse(res.msg)
  80. for(let item of data){
  81. html += '<div class="cutline-item">'
  82. html += '<div class="item-color" style="background-color: '+item.fill+'"></div>'
  83. html += '<div class="item-name">'+item.title+'</div>'
  84. html += '</div>'
  85. }
  86. $("#cutline-items").html(html)
  87. console.log($("#cutline-items").html())
  88. }
  89. });
  90. }
  91. }
  92. function MyGeoJsonLayer(option){
  93. this.url = option.url;
  94. this.zIndex = option.zIndex || 0
  95. this.style = option.style
  96. this.source = new ol.source.Vector({
  97. projection: 'EPSG:4326',
  98. url: this.url, //GeoJSON的文件路径,用户可以根据需求而改变
  99. format: new ol.format.GeoJSON()
  100. })
  101. this.vector = new ol.layer.Vector({
  102. style: this.style,
  103. zIndex: this.zIndex,
  104. source: this.source
  105. });
  106. }
  107. MyGeoJsonLayer.prototype = {
  108. }
  109. function TipLayer(option){
  110. this.features = option.features || [];
  111. this.style = option.style
  112. this.zIndex = option.zIndex || 0
  113. this.source = new ol.source.Vector({
  114. projection: 'EPSG:4326'
  115. })
  116. for(let feature of this.features){
  117. let point = new ol.geom.Point(ol.extent.getCenter(feature.getGeometry().getExtent()))
  118. let pointFeature = new ol.Feature({geometry:point})
  119. pointFeature.setId(feature.getId().substring(feature.getId().indexOf(".") + 1))
  120. let keys = feature.getKeys();
  121. for(let key of keys){
  122. if(key === "geometry"){
  123. continue
  124. }
  125. pointFeature.set(key,feature.get(key))
  126. }
  127. this.source.addFeature(pointFeature)
  128. }
  129. this.vector = new ol.layer.Vector({
  130. style: this.style,
  131. zIndex: this.zIndex,
  132. source: this.source,
  133. maxZoom: 14
  134. });
  135. }
  136. String.prototype.format = function(){
  137. let str = this;
  138. if(arguments.length == 0){
  139. return str;
  140. }else{
  141. Object.keys(arguments).forEach((item,index)=>{
  142. str = str.replace(/\!/,arguments[item])
  143. })
  144. return str
  145. }
  146. }