var json_script = null; var json_data = null; var nbrEspeces = 0; var i_loop_especes = 0; var j_loop_especes = 0; var json_especes; $(window).load(function() { // action click sur le bouton id=ETAPE_4_GEO $('#ETAPE_4_GEO').click(function() { // Affichage popup wait svp $("#JQUERY_WAIT").dialog({ modal: true }); // appel d'une page qui analyse et retourne les données longitude et latitude (avec le N° unik de la paire) depuis une table mysql $.ajax({ url: 'ma_page_qui_cherche_et_retourne_une stucture_json_des_coordonnées.php', data: 'unik_import_file=' + Mon_numéro_unik_de_fichier_à_traiter, async: false, dataType: 'json', error: function(json) { alert(html); $("#JQUERY_WAIT").dialog("close"); }, success: function(json) { if (json=='"ERREUR":"OUI"') { $("#JQUERY_WAIT").dialog("close"); alert("Cette fonction n'est accessible que si vous avez désignez les colonnes -Longitude- et -Latitude- exprimées en degrés décimaux !"); } else { $("#JQUERY_WAIT").dialog("close"); $("#JQUERY_WAIT").dialog({ modal: true }); $("#TD_JQUERY_WAIT_MSG").html("Géoférencement inverse"); // initilisation de la boucle sur le nombre de paires de coordonnées à traiter json_especes=json; nbrEspeces = json_especes.especes.length; i_loop_especes = 0; j_loop_especes = 0; // premier appel du géoportail Send_Geoportail(); } } }); }); }); function json_get(url) { //alert("json_get"); var head = document.getElementsByTagName("head")[0]; if (json_script) { head.removeChild(json_script) } json_script = document.createElement("script"); json_script.type = "text/javascript"; json_script.src = url + "&output=json&callback=json_callback"; head.appendChild(json_script) } function Send_Geoportail() { //alert("Send_Geoportail"); var apiKEYs = { 'localhost:8080': '1b7k.....ov0876h0o2rx', 'beta.wnat.fr': '82...ioqm4vbmnqs4y', 'wnat.fr': 'y66adhgr76....46x', 'saxrub.fr': 'yb6ux0yvugis...dv6vm', 'www.wnat.fr': 'y66a....72k5rk2v46x', 'www.saxrub.fr': 'yb6ux0....xnvudv6vm' }; apiKEY = apiKEYs[window.location.host]; reponse_div = document.getElementById('reponse'); var myEspece = json_especes.especes[i_loop_especes]; unik = myEspece.espece.unik; longitude = myEspece.espece.longitude.replace(',', '.'); latitude = myEspece.espece.latitude.replace(',', '.'); var maxReponse = 10; query = '%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%3CXLS%20xmlns%3Agml%3D%22http%3A%2F%2Fwww.opengis.net%2Fgml%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fxls%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20version%3D%221.2%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fxls%20http%3A%2F%2Fschemas.opengis.net%2Fols%2F1.2%2FolsAll.xsd%22%3E%3CRequestHeader%2F%3E%3CRequest%20requestID%3D%221%22%20version%3D%221.2%22%20methodName%3D%22GeocodeRequest%22%20maximumResponses%3D%22' + maxReponse + '%22%3E%3CReverseGeocodeRequest%3E%3CPosition%3E%3Cgml%3APoint%3E%3Cgml%3Apos%3E' + latitude + '%20' + longitude + '%3C%2Fgml%3Apos%3E%3C%2Fgml%3APoint%3E%3Cgml%3ACircleByCenterPoint%3E%3Cgml%3Apos%3E' + latitude + '%20' + longitude + '%3C%2Fgml%3Apos%3E%3Cgml%3Aradius%3E1000%3C%2Fgml%3Aradius%3E%3C%2Fgml%3ACircleByCenterPoint%3E%3C%2FPosition%3E%3CReverseGeocodePreference%3EPositionOfInterest,StreetAddress%3C%2FReverseGeocodePreference%3E%3C%2FReverseGeocodeRequest%3E%3C%2FRequest%3E%3C%2FXLS%3E'; var uri_dec = decodeURIComponent(query); url = "http://wxs.ign.fr/" + apiKEY + "/geoportail/ols?xls=" + query; json_get(url); } // retour du geoportail function json_callback(data) { my_i_loop_especes=i_loop_especes+1; $("#TD_JQUERY_WAIT_MSG").html("Géoférencement inverse "+my_i_loop_especes+"/"+nbrEspeces); var F_C_F_UNIK = document.getElementById('F_C_F_UNIK').value; json_data = data; //alert("json_callback"); if (json_data.http.status != 200) { console.log("Erreur ", json_data.http.status); return; } console.log("OK "+i_loop_especes+"/"+nbrEspeces, ""); var myEspece = json_especes.especes[i_loop_especes]; unik = myEspece.espece.unik; longitude = myEspece.espece.longitude.replace(',', '.'); latitude = myEspece.espece.latitude.replace(',', '.'); //alert(unik); var parser = new Geoportal.Format.XLS(); var resp = parser.read(json_data.xml); // doc est le document XML retourné l1 = resp._body[0]._responseParameters.reverseGeocodedLocations.length; ClicMunicipality = ""; ClicDepartement = ""; ClicCommune = ""; ClicINSEE = ""; ClicNature = ""; ClicTerritoire = ""; ClicStreet = ""; ClicStreetNumber = ""; FirstMunicipality = ""; FirstDepartement = ""; FirstCommune = ""; FirstINSEE = ""; FirstNature = ""; FirstTerritoire = ""; FirstStreet = ""; FirstStreetNumber = ""; Municipality = ""; Departement = ""; Commune = ""; INSEE = "?"; Nature = ""; Territoire = ""; Street = ""; StreetNumber = ""; ClicOk = 0; for (i = 0; i < l1; i++) { codePostal = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.postalCode.name; l2 = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places.length; l3 = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.streetAddress.streets.length; if (i == 1) { ClicMunicipality = Municipality; ClicDepartement = Departement; ClicCommune = Commune; ClicINSEE = INSEE; ClicNature = Nature; ClicTerritoire = Territoire; ClicStreet = Street; ClicStreetNumber = StreetNumber; } else { if (i > 1 && Nature != "Commune" && FirstMunicipality == "") { FirstMunicipality = Municipality; FirstDepartement = Departement; FirstCommune = Commune; FirstINSEE = INSEE; FirstNature = Nature; FirstTerritoire = Territoire; FirstStreet = Street; FirstStreetNumber = StreetNumber; } if (INSEE == ClicINSEE) { ClicOk = 1; break; } } Municipality = ""; Departement = ""; Commune = ""; INSEE = ""; Nature = ""; Territoire = ""; Street = ""; StreetNumber = ""; for (j = 0; j < l2; j++) { // alert(i+"/"+j); switch (resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].classification) { case "Municipality": Municipality = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; break; case "Departement": Departement = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; break; case "Commune": Commune = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; break; case "INSEE": INSEE = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; break; case "Nature": Nature = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; break; case "Qualite": Nature = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; break; case "Territoire": Territoire = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; break; // case "Municipality": // Municipality=resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; // break; // case "Municipality": // Municipality=resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.places[j].name; // break; } } for (j = 0; j < l3; j++) { Street = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.streetAddress.streets[j].name; StreetNumber = resp._body[0]._responseParameters.reverseGeocodedLocations[i].address.streetAddress._streetLocation.num; } } if (ClicOk == 1) { // console.log("OK1", ""); $.ajax({ url: 'Include_WNat_ImportFile_Etape_4_AJAX_GEOPORTAIL_UPDATE.php', data: 'INSEE='+INSEE+"&Lieudit="+Municipality+'&unik_import_file='+F_C_F_UNIK+'&unik='+unik+'&Long='+longitude+"&Lat="+latitude, type:'POST', async:true, dataType: 'html', error: function(html) { var xx=json2[0]; alert(xx.aaa); }, success: function(html) { //alert(html); } }); // alert( } else { if (FirstINSEE == '') { // console.log("OK2", ""); $.ajax({ url: 'Include_WNat_ImportFile_Etape_4_AJAX_GEOPORTAIL_UPDATE.php', data: 'INSEE='+INSEE+"&Lieudit="+Municipality+'&unik_import_file='+F_C_F_UNIK+'&unik='+unik+'&Long='+longitude+"&Lat="+latitude, type:'POST', async:true, dataType: 'html', error: function(html) { var xx=json2[0]; alert(xx.aaa); }, success: function(html) { // alert(html); } }); } else { // console.log("OK3", ""); $.ajax({ url: 'Include_WNat_ImportFile_Etape_4_AJAX_GEOPORTAIL_UPDATE.php', data: 'INSEE='+FirstINSEE+"&Lieudit="+FirstMunicipality+'&unik_import_file='+F_C_F_UNIK+'&unik='+unik+'&Long='+longitude+"&Lat="+latitude, type:'POST', async:true, dataType: 'html', error: function(html) { var xx=json2[0]; alert(xx.aaa); }, success: function(html) { // alert(html); } }); } } i_loop_especes += 1; if (i_loop_especes < nbrEspeces) { Send_Geoportail(); } else { $("#TD_JQUERY_WAIT_MSG").html("") $("#JQUERY_WAIT").dialog("close"); } return }