Advertisement
bah99

Untitled

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