01.01.2015 Views

Rapport Bilan Carbone de l'université Paris Diderot.

Rapport Bilan Carbone de l'université Paris Diderot.

Rapport Bilan Carbone de l'université Paris Diderot.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Annexe 5.6 : Scripts du calcul <strong>de</strong>s distances totales via l’API GoogleMap<br />

(Visual Basic sous Excel)<br />

Sub traitement_distance(mws, Pretrait)<br />

Dim val, k As Integer<br />

Dim i, a As Long<br />

Dim address1, address2, nomwb, addr As String<br />

Dim city1, city2<br />

Dim CP1, CP2 As String<br />

Dim sendstring1, sendstring2, Depart, Arrivee As String<br />

Dim oldstatusbar<br />

Dim mywstempdist As Worksheet<br />

oldstatusbar = Application.DisplayStatusBar<br />

nomwb = ThisWorkbook.Name<br />

a = firstligvi<strong>de</strong>(mws, 1, 1)<br />

Set mywstempdist = Workbooks(nomwb).Worksheets("TempDist")<br />

'Prétraite la base pour compatibilité avec<br />

requête Google Maps API<br />

If Pretrait Then<br />

Call pretraitement_adresse(mws, 5, a)<br />

Call pretraitement_adresse(mws, 6, a)<br />

End If<br />

'Adresse <strong>de</strong> base (départ) : Université ici<br />

address1 = mywsbdd.Cells(46, 1).Value<br />

city1 = mywsbdd.Cells(46, 2).Value<br />

CP1 = mywsbdd.Cells(46, 3).Value<br />

address1 = Replace(address1, " ", "+", 1)<br />

sendstring1 = address1 & ",+" & city1 & "+" & CP1<br />

'Parcours <strong>de</strong> toutes les distances à trouver<br />

For i = 862 To a<br />

address2 = mws.Cells(i, 5).Value<br />

city2 = mws.Cells(i, 6).Value<br />

CP2 = mws.Cells(i, 7).Value<br />

address2 = Replace(address2, " ", "+", 1)<br />

If address2 = 0 Then<br />

sendstring2 = city2 & "+" & CP2<br />

ElseIf city2 = 0 Then<br />

sendstring2 = CP2<br />

Else<br />

sendstring2 = address2 & ",+" & city2 & "+" & CP2<br />

End If<br />

Depart = sendstring1<br />

Arrivee = sendstring2<br />

'Calcul <strong>de</strong> la distance<br />

Sheets("TempDist").Cells.Clear<br />

With<br />

Sheets("TempDist").QueryTables.Add(Connection:="URL;http://map<br />

s.googleapis.com/maps/api/distancematrix/jsonorigins=" & Depart &<br />

"&<strong>de</strong>stinations=" & Arrivee & "&mo<strong>de</strong>=car&language=fr-<br />

FR&sensor=false", Destination:=Sheets("TempDist").Range("A1"))<br />

.Name = "itinéraire"<br />

.BackgroundQuery = True<br />

.WebSelectionType = xlEntirePage<br />

.WebFormatting = xlWebFormattingNone<br />

.Refresh BackgroundQuery:=False<br />

For k = 1 To firstligvi<strong>de</strong>(mywstempdist, 1, 1)<br />

If InStr(1, mywstempdist.Cells(k, 1), "value", vbBinaryCompare) ><br />

0 Then<br />

mws.Cells(i, 34).Value = Mid(mywstempdist.Cells(k, 1),<br />

InStr(1, mywstempdist.Cells(k, 1), ":", vbBinaryCompare) + 1) / 1000<br />

Exit For<br />

End If<br />

Next k<br />

End With<br />

If IsEmpty(mws.Cells(i, 34)) Then<br />

mws.Cells(i, 34) = "Itinéraire non trouvé"<br />

End If<br />

'<strong>Rapport</strong> du lien Google Maps utilisé<br />

addr =<br />

"http://maps.googleapis.com/maps/api/distancematrix/jsonorigins="<br />

& Depart & "&<strong>de</strong>stinations=" & Arrivee &<br />

"&mo<strong>de</strong>=car&language=fr-FR&sensor=false"<br />

mywsrapport.Cells(i, 13) = addr<br />

'Geocodage<br />

Sheets("Temp").Cells.Clear<br />

With<br />

Sheets("Temp").QueryTables.Add(Connection:="URL;http://maps.go<br />

ogle.com/maps/geoq=" & Arrivee &<br />

"&output=csv&sensor=false&key=AIzaSyC3kWmzX4QTlgIGqJEcdN<br />

88HB5tNTujl9k", Destination:=Sheets("Temp").Range("A1"))<br />

.Name = "itinéraire"<br />

.BackgroundQuery = True<br />

.WebSelectionType = xlEntirePage<br />

.WebFormatting = xlWebFormattingNone<br />

.Refresh BackgroundQuery:=False<br />

End With<br />

'Renvoie <strong>de</strong> la requête <strong>de</strong> géocodage dans le<br />

rapport <strong>de</strong> traitement<br />

Sheets("<strong>Rapport</strong> <strong>de</strong> Traitement").Range(mywsrapport.Cells(i, 9),<br />

mywsrapport.Cells(i, 12)) = Split(Sheets("Temp").Cells(1, 1).Value,<br />

",", 4)<br />

mywsrapport.Cells(i, 6) = address2<br />

mywsrapport.Cells(i, 7) = city2<br />

mywsrapport.Cells(i, 8) = CP2<br />

'Modif <strong>de</strong> la StatusBar<br />

Application.DisplayStatusBar = True<br />

Application.StatusBar = "Traitement <strong>de</strong>s distances : " & i & "/" & a &<br />

" - " & mws.Cells(i, 34).Value<br />

Next i<br />

'Remise en place <strong>de</strong> l'ancienne StatusBar<br />

Application.StatusBar = False<br />

Application.DisplayStatusBar = oldstatusbar<br />

End Sub<br />

‘Routine Prétraitement pour compatibilité<br />

GoogleMap API<br />

Sub pretraitement_adresse(mws, col, lastlig)<br />

Dim i As Integer<br />

Dim oldstatusbar<br />

oldstatusbar = Application.DisplayStatusBar<br />

For i = 1 To lastlig<br />

mws.Cells(i, col) = Replace(mws.Cells(i, col), "é", "e", 1)<br />

mws.Cells(i, col) = Replace(mws.Cells(i, col), "è", "e", 1)<br />

mws.Cells(i, col) = Replace(mws.Cells(i, col), "ç", "c", 1)<br />

142 Université <strong>Paris</strong> Di<strong>de</strong>rot – <strong>Paris</strong> 7 <strong>Paris</strong> Di<strong>de</strong>rot Développement Durable

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!