Advertisement
bah99

Untitled

Nov 11th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 map, wfsSource
  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 raster= new ol.layer.Tile({
  55.             source: new ol.source.OSM()
  56.           });
  57.           var map = new ol.Map({
  58.            controls: ol.control.defaults().extend([
  59.              new ol.control.MousePosition({
  60.               coordinateFormat: ol.coordinate.createStringXY(4),
  61.               projection: 'EPSG:4326',
  62.               className: 'custom-mouse-position',
  63.               target: document.getElementById('map'),
  64.               undefinedHTML:'&nbsp;'          
  65.              }),
  66.            new ol.control.ScaleLine()
  67.            ]),
  68.            target: 'map',
  69.            layers: [raster, vector ],
  70.            view: new ol.View({
  71.             center: [2.5,46],
  72.             maxZoom:20,
  73.             zoom: 4
  74.            })
  75.           });
  76.           var etat_edition= document.getElementById('mod');
  77.           var etat_creation= document.getElementById('creer');
  78.           var stop_edition= document.getElementById('stop_mod');
  79.           var draw, snap, modify; //declarees globalement pour les activers ou non a la guise
  80.           var formatWFS = new ol.format.WFS();
  81.           var formatGML =  new ol.format.GML3({
  82.             version:'1.1.0',
  83.             srsName:'EPSG:4326',
  84.             url:'http://127.0.0.1/cgi-bin/tinyows.exe',
  85.             featureNS: 'http://www.tinyows.org/',
  86.             featureType:'demo_tyny',
  87.             geometryName: 'geometry',
  88.             SchemaLocation: 'http://127.0.0.1/cgi-bin/tinyows.exe?service=wfs&request=DescribeFeatureType&version=1.1.0&typename=tows:demo_tyny'
  89.           });
  90.           var xs = new XMLSerializer();
  91.           var transactWFS = function (mode, f) {
  92.             var node;
  93.             switch(mode) {
  94.                 case 'insert':
  95.                     node = formatWFS.writeTransaction([f], null, null, formatGML);
  96.                     break;
  97.                 case 'update':
  98.                     node = formatWFS.writeTransaction(null, [f], null, formatGML);
  99.                     break;
  100.                 case 'delete':
  101.                     node = formatWFS.writeTransaction(null, null, [f], formatGML);
  102.                     break;
  103.             }
  104.             var xml_data = xs.serializeToString(node);
  105.             const response = fetch("http://127.0.0.1/cgi-bin/tinyows.exe?service=wfs",{
  106.               method: 'POST',
  107.               body: xml_data
  108.             }).then(function() {
  109.               wfsSource.clear();
  110.             })
  111.           };
  112.           function add_snap() {
  113.             snap = new ol.interaction.Snap({source: wfsSource});
  114.             map.addInteraction(snap);
  115.           };
  116.           function add_modify(){
  117.             stopInteractions();
  118.             add_snap();
  119.             modify = new ol.interaction.Modify({source: wfsSource});
  120.             map.addInteraction(modify)
  121.           };
  122.           function add_creer(){
  123.             stopInteractions();
  124.             add_snap();
  125.             draw = new ol.interaction.Draw({
  126.                 source: wfsSource,
  127.                 type:"MultiPolygon",
  128.                 geometryName:'geometry'
  129.             });
  130.             draw.on('drawend', function(e){
  131.                 code_saisi=prompt("code_commune");
  132.                 e.feature.setProperties({'code_commune':code_saisi});
  133.                 transactWFS('insert',e.feature);
  134.             });
  135.             map.addInteraction(draw);
  136.           };
  137.           function stopInteractions() {
  138.             map.removeInteraction(draw);
  139.             map.removeInteraction(snap);
  140.             map.removeInteraction(modify)
  141.  
  142.           };
  143.           etat_edition.onclick = function() {
  144.             add_modify();
  145.           };
  146.           etat_creation.onclick = function() {
  147.             add_creer();
  148.           };
  149.           stop_edition.onclick = function () {
  150.             stopInteractions();
  151.           };
  152.          
  153.         </script>
  154.     </body>
  155. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement