Advertisement
bah99

Untitled

Sep 25th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>WFS - GetFeature</title>
  5. <meta charset="utf-8"/>
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/css/ol.css" type="text/css">
  7. <style>
  8. .map {
  9. height: 400px;
  10. width: 100%;
  11. }
  12. </style>
  13. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/build/ol.js"></script>
  14. </head>
  15. <body>
  16. <div id="map" class="map"></div>
  17. <label >Modifier &nbsp;</label>
  18. <button name="modifier" id="mod" value="Edit"></button>
  19. <label >Creer &nbsp;</label>
  20. <button name="Creer" id="creer" value="creer"></button>
  21. <label >Stop Edition &nbsp;</label>
  22. <button name="stop_modif" id="stop_mod" value="stop_Edit"></button>
  23. <script>
  24. var draw, modify, snap;
  25. var maprojection= new ol.proj.Projection({
  26. code:'EPSG:4326',
  27. extent:[-20,30,20,50],
  28. units:'degrees',
  29. axisOrientation:'enu'
  30. });
  31. //surcharge de la projection EPSG:4326
  32. ol.proj.addEquivalentProjections([ol.proj.get('EPSG:4326'), maprojection]);
  33. var wfsSource = new ol.source.Vector({
  34. format: new ol.format.GeoJSON(),
  35. url: function(extent) {
  36. return 'http://127.0.0.1/cgi-bin/tinyows.exe?service=WFS&'+
  37. 'version=1.1.0&request=GetFeature&typename=tows:demo_tyny&'+
  38. 'outputFormat=application/json&srsname=EPSG:4326&'+
  39. 'bbox='+ extent.join(',')+',EPSG:4326';
  40. },
  41. strategy: ol.loadingstrategy.bbox,
  42. projection: maprojection
  43.  
  44. });
  45. var vector = new ol.layer.Vector({
  46. source: wfsSource,
  47. style: new ol.style.Style({
  48. stroke: new ol.style.Stroke({
  49. color: 'rgba(0,0,255,1.0)',
  50. width:4
  51. })
  52. })
  53. });
  54. var map = new ol.Map({
  55. layers:[vector],
  56. target: document.getElementById('map'),
  57. view: new ol.View({
  58. projection: maprojection,
  59. center: [2.5,46],
  60. maxZoom: 20,
  61. zoom: 1
  62. })
  63. });
  64. var etat_edition= document.getElementById('mod');
  65. var etat_creation= document.getElementById('creer');
  66. var stop_edition= document.getElementById('stop_mod');
  67. var draw, snap, modify; //declarees globalement pour les activers ou non a la guise
  68. var formatWFS = new ol.format.WFS();
  69. var xs = new XMLSerializer();
  70. var formatGML = new ol.format.GML3({
  71. version:'1.1.0',
  72. featureNS: 'http://www.mapserver.org/tinyows',
  73. url: 'http://127.0.0.1/cgi-bin/tinyows.exe',
  74. featureType:'demo_tyny',
  75. srsName:'EPSG:4326',
  76. geometryName: 'geom',
  77. SchemaLocation:'http://127.0.0.1/cgi-bin/tinyows.exe?service=wfs&request=DescribeFeatureType&version=1.1.0&typename=tows:demo_tyny'
  78. });
  79. var transactWFS = function (mode, f) {
  80. var node;
  81. switch(mode) {
  82. case 'insert':
  83. node = formatWFS.writeTransaction([f], null, null, formatGML);
  84. break;
  85. case 'update':
  86. node = formatWFS.writeTransaction(null, [f], null, formatGML);
  87. break;
  88. case 'delete':
  89. node = formatWFS.writeTransaction(null, null, [f], formatGML);
  90. break;
  91. }
  92. var xml_data = xs.serializeToString(node);
  93. fetch('http://localhost/cgi-bin/tinyows.exe?service=wfs', {
  94. method:'POST',
  95. body: xml_data
  96. }).then(function() {
  97. wfsSource.clear();
  98. })
  99. };
  100. function add_snap() {
  101. snap = new ol.interaction.Snap({source: vector.getSource()});
  102. map.addInteraction(snap);
  103. }
  104. function add_modify(){
  105. stopInteractions();
  106. add_snap();
  107. modify = new ol.interaction.Modify({source: vector.getSource()});
  108. map.addInteraction(modify)
  109. }
  110. function add_creer(){
  111. stopInteractions();
  112. add_snap();
  113. draw = new ol.interaction.Draw({
  114. source: vector.getSource(),
  115. type:"MultiPolygon",
  116. geometryName:'geom'
  117. });
  118. draw.on('drawend', function(e){
  119. code_saisi=prompt("code_commune");
  120. e.feature.setProperties({'code_commune':code_saisi});
  121. transactWFS('insert',e.feature);
  122. });
  123. map.addInteraction(draw);
  124. }
  125. function stopInteractions() {
  126. map.removeInteraction(draw);
  127. map.removeInteraction(snap);
  128. map.removeInteraction(modify)
  129.  
  130. }
  131. etat_edition.onclick = function() {
  132. add_modify();
  133. };
  134. etat_creation.onclick = function() {
  135. add_creer();
  136. };
  137. stop_edition.onclick = function () {
  138. stopInteractions();
  139. };
  140.  
  141. </script>
  142. </body>
  143. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement