#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include-once #cs ---------------------------------------------------------------------------- AutoIt Version : 3.3.6.1 Auteur: tlem@tuxolem.fr Fonction du Script : Fonctions de traitement de fichiers OV2. #ce ---------------------------------------------------------------------------- ; #CURRENT# ===================================================================================================================== ;_POItoASC() ;_ASCtoOV2() ;_OV2toPOI() ;_POItoOV2() ; =============================================================================================================================== ;=============================================================================== ; ; Function Name: _POItoASC() ; Description: Write a ASC file of the list of POI's coordinate and data ; ; Parameter(s): $FilePath - The ASC file to write. ; $asPOI - An array or a string of POI, where : ; String element have this form : Longitude|Latitude|"Data"|@CRLF ; Array must be a 2 dimention array with this form : ; $ArrayPOI[0][0] Contains the dimention of the array. ; $ArrayPOI[x][x] Contains the POI information on this ; form : $ArrayPOI[x][1] = Logitude ; $ArrayPOI[x][2] = Latitude ; $ArrayPOI[x][3] = Data ; $Comment - 0 if you don't want comments. ; 1 If you want comments. ; ; Requirement(s): #include ; #include ; ; Return Value(s): On Success - Returns 1 ; @Extented = number of POI ; On Failure - Returns 0 ; @Error - 0 = No error. ; |1 = File access error ; |2 = Input datas error ; ; Author(s): tlem@tuxolem.net ; ;=============================================================================== Func _POItoASC($FilePath, ByRef Const $asPOI, $iComment = 0) Local $WLine, $iCount = 0 ; Check if $asPOI is an array or not If IsArray($asPOI) Then ; Check if we have a correct dimention If UBound($asPOI, 2) <> 3 Then Return SetError(2, 0, 0) EndIf ; Convert array in string in the form Longitude, Latitude, "Data" @CRLF For $i = 1 To UBound($asPOI) - 1 $WLine &= $asPOI[$i][0] & ', ' & $asPOI[$i][1] & ', "' & $asPOI[$i][2] & '"' & @CRLF $iCount += 1 Next Else Local $Line[3] ; Convert the string in 1D array Local $aPOI = StringSplit($asPOI, @CRLF, 1) If @error Then Return SetError(2, 0, 0) EndIf For $i = 1 To $aPOI[0] - 1 $Line = StringSplit($aPOI[$i], "|", 0) $WLine &= $Line[1] & ', ' & $Line[2] & ', "' & $Line[3] & '"' & @CRLF $iCount += 1 Next EndIf If $iComment = 1 Then $WLine = '; ' & UBound($asPOI) - 1 & " POI(s)" & @CRLF & _ '; Longitude, Latitude, "Nom"' & @CRLF & _ ';' & @CRLF & $WLine EndIf Local $hFile = FileOpen($FilePath, 2) If $hFile = -1 Then Return SetError(1, 0, 0) EndIf Local $Res = FileWrite($hFile, $WLine) FileClose($hFile) If $Res = 1 Then Return SetError(0, $iCount, 1) Else Return SetError(1, 0, 0) EndIf EndFunc ;==>_POItoASC ;=============================================================================== ; ; Function Name: _ASCtoOV2() ; Description: Convert a ASC file of POI in OV2 file. ; ; Parameter(s): $FilePathIn - The Asc file to read. ; $FilePathOut - The OV2 file to write. ; ; Requirement(s): #include ; #include ; ; Return Value(s): On Success - Returns 1 ; @Extented = number of POI ; On Failure - Returns 0 ; @Error - 0 = No error. ; |1 = File access error ; ; Author(s): tlem@tuxolem.net ; ;=============================================================================== Func _ASCtoOV2($FilePathIn, $FilePathOut = "") Local $Line, $Table, $STW, $Res, $iCount If $FilePathOut = "" Then $FilePathOut = StringTrimRight($FilePathIn, 3) & "ov2" EndIf Local $hFile = FileOpen($FilePathIn, 0) If $hFile = -1 Then Return SetError(1, 0, 0) EndIf While 1 $Line = FileReadLine($hFile) If @error = -1 Then ExitLoop $table = StringSplit($Line, ",") $table[1] = StringStripWS($table[1], 8) If StringIsFloat($table[1]) Then $table[2] = StringStripWS($table[2], 8) $table[3] = StringReplace($table[3], '"', '') Local $lendata = Hex(Binary(BitShift(String(StringLen($table[3]) + 14), 32))) Local $lon = Hex(Binary(BitShift(String($table[1] * 100000), 32))) Local $lat = Hex(Binary(BitShift(String($table[2] * 100000), 32))) Local $data = Hex(StringToBinary($table[3])) $STW &= "02" & $lendata & $lon & $lat & $data & "00" $iCount += 1 EndIf WEnd FileClose($hFile) $hFile = FileOpen($FilePathOut, 2 + 16) If $hFile = -1 Then Return SetError(1, 0, 0) EndIf $Res = FileWrite($hFile, BinaryToString("0x" & $STW)) FileClose($hFile) If $Res = 1 Then Return SetError(0, $iCount, 1) Else Return SetError(1, 0, 0) EndIf EndFunc ;==>_ASCtoOV2 ;=============================================================================== ; ; Function Name: _OV2toPOI() ; Description: Returns a string or an array, filled with POI's ; coordinate and data of the OV2 file. ; ; Parameter(s): $FilePath - The OV2 file to read. ; $iRetType - The return type : ; 1 - 2 dimensions Array on the form : $POI[x][3] (default) ; 2 - String on the form : Longitude|Latitude|Data|@CRLF ; $Data - The description text replacement ; ; Requirement(s): NA ; Return Value(s): On Success - Returns a string or an array with POI's coordinate ; @Extented = number of POI ; On Failure - Returns 0 ; @Error - 0 = No error. ; |1 = File access error ; |3 = POI file damaged ; ; Author(s): tlem@tuxolem.net ; Thanks to siao for his help. ;=============================================================================== Func _OV2toPOI($FilePath, $iRetType = 1, $sData = "") Local $i = 0, $Str = "", $len, $lon, $lat, $Data, $iCount ; Check type choice If $iRetType <> 1 Then $iRetType = 2 EndIf ; Force file opening in binary mode Local $hFile = FileOpen($FilePath, 16) If $hFile = -1 Then Return SetError(1, 0, 0) EndIf ; Loop begin Do ; Read the first record. Local $type = FileRead($hFile, 1) If @error Then ExitLoop $type = Int($type) Switch $type Case 0, 3 ; DELETED RECORD: ; 1 byte T: type (always 0) ; 4 bytes L: length of this record in bytes ; (including the T and L fields) ; L-5 bytes bytes to ignore (content undefined) $len = Dec(Hex(Binary(BitShift(String(FileRead($hFile, 4)), 32)))) - 5 $Data = BinaryToString(FileRead($hFile, $len)) Case 1 ; SKIPPER RECORD: ; 1 byte T: type (always 1) ; 4 bytes Number of bytes in the file, including and starting at this ; record, that contain data for POI enclosed in the given rectangle ; 4 bytes X1: longitude coordinate of the west edge of the rectangle ; 4 bytes Y1: latitude coordinate of the south edge of the rectangle ; 4 bytes X2: longitude coordinate of the east edge of the rectangle ; 4 bytes Y2: latitude coordinate of the north edge of the rectangle $Data = BinaryToString(FileRead($hFile, 20)) Case 2 ; SIMPLE POI RECORD: ; 1 byte T: type (always 2) ; 4 bytes L: length of this record in bytes (including the T and L fields) ; 4 bytes X: longitude coordinate of the POI ; 4 bytes Y: latitude coordinate of the POI ; L-13 bytes Name: zero-terminated ASCII string specifying the name of the POI $len = Dec(Hex(Binary(BitShift(String(FileRead($hFile, 4)), 32)))) - 14 $lon = StringFormat("%.5f", Dec(Hex(Binary(BitShift(String(FileRead($hFile, 4)), 32)))) / 100000) $lat = StringFormat("%.5f", Dec(Hex(Binary(BitShift(String(FileRead($hFile, 4)), 32)))) / 100000) $Data = BinaryToString(FileRead($hFile, $len)) If $sData <> "" Then $data = $sData FileRead($hFile, 1) $Str &= $lon & "|" & $lat & "|" & $data & @CRLF $iCount += 1 Case 3 ; EXTENDED POI RECORD: ; 1 byte T: type (always 3) ; 4 bytes L: length of this record in bytes (including the T and L fields) ; 4 bytes X: longitude coordinate of the POI ; 4 bytes Y: latitude coordinate of the POI ; P bytes Name: zero-terminated ASCII string specifying the name of the POI ; Q bytes Unique ID: zero-terminated string specifying the unique ID of the POI ; L-P-Q-13 bytes Extra data: zero-terminated string, not used yet $len = Dec(Hex(Binary(BitShift(String(FileRead($hFile, 4)), 32)))) - 13 $lon = StringFormat("%.5f", Dec(Hex(Binary(BitShift(String(FileRead($hFile, 4)), 32)))) / 100000) $lat = StringFormat("%.5f", Dec(Hex(Binary(BitShift(String(FileRead($hFile, 4)), 32)))) / 100000) $data = BinaryToString(FileRead($hFile, $len)) If $sData <> "" Then $data = $sData $Str &= $lon & "|" & $lat & "|" & $data & @CRLF $iCount += 1 Case Else ; Error file dammaged. Return SetError(3, 0, 0) ExitLoop EndSwitch Until 0 FileClose($hFile) ; Delete the last @CRLF $Str = StringTrimRight($Str, 2) ; If $iRetType = 1 then we transforme $Str in array If $iRetType = 1 Then ; Transform string $Str in $aPoiTmp1 array Local $aPoiTmp1 = StringSplit($Str, @CRLF, 1) ; Create the destination 2D array based on $aPoiTmp1 dimension Local $aPoi[UBound($aPoiTmp1)][3] ; Transfert datas from $aPoiTmp1 to $aPoi For $i = 1 to UBound($aPoiTmp1) - 1 If $aPoiTmp1[$i] <> "" Then Local $aPoiTmp2 = StringSplit($aPoiTmp1[$i], "|") $aPoi[$i][0] = $aPoiTmp2[1] $aPoi[$i][1] = $aPoiTmp2[2] $aPoi[$i][2] = $aPoiTmp2[3] EndIf Next ; Put the number of elements in first element $aPoi[0][0] = UBound($aPoi) - 1 ; Return the array Return SetError(0, $iCount, $aPoi) Else ; Else return $Str Return SetError(0, $iCount, $Str) EndIf EndFunc ;==>_OV2toPOI ;=============================================================================== ; ; Function Name: _POItoOV2() ; Description: Write a OV2 file of the list of POI's coordinate and data ; ; Parameter(s): $FilePath - The OV2 file to write. ; $asPOI - An array or a string of POI, where : ; String element have this form : Longitude|Latitude|Data|@CRLF ; Array must be a 2 dimention array with this form : ; $ArrayPOI[0][0] Contains the dimention of the array. ; $ArrayPOI[x][x] Contains the POI information on this ; form : $ArrayPOI[x][1] = Logitude ; $ArrayPOI[x][2] = Latitude ; $ArrayPOI[x][3] = Data ; ; Requirement(s): #include ; #include ; ; Return Value(s): On Success - Returns 1 ; @Extented = number of POI ; On Failure - Returns 0 ; @Error - 0 = No error. ; |1 = File access error ; |2 = Input datas error ; ; Author(s): tlem@tuxolem.net ; ;=============================================================================== Func _POItoOV2($FilePath, ByRef Const $asPOI) Local $Line, $Table, $STW, $Res, $aWLine, $hFile, $iCount ; If $asPOI is an array then we check if his dimension is correct. If IsArray($asPOI) Then If UBound($asPOI, 2) <> 3 Then Return SetError(2, 0, 0) EndIf ; Convert 2D array in 1D array For $i = 1 To UBound($asPOI) - 1 $aWLine &= $asPOI[$i][0] & '|' & $asPOI[$i][1] & '|"' & $asPOI[$i][2] & '"' & @CRLF Next $aWLine = StringSplit($asPOI, @CRLF, 1) ; Else if it's a string Else ; Convert in 1D array $aWLine = StringSplit($asPOI, @CRLF, 1) EndIf ; If $aWLine is not an array, then return If Not IsArray($aWLine) Then Return 0 EndIf For $i = 1 To UBound($aWLine) - 1 $Line = $aWLine[$i] $table = StringSplit($Line, "|") $table[1] = StringStripWS($table[1], 8) If StringIsFloat($table[1]) Then $table[2] = StringStripWS($table[2], 8) $table[3] = StringReplace($table[3], '"', '') Local $lendata = Hex(Binary(BitShift(String(StringLen($table[3]) + 14), 32))) Local $lon = Hex(Binary(BitShift(String($table[1] * 100000), 32))) Local $lat = Hex(Binary(BitShift(String($table[2] * 100000), 32))) Local $data = Hex(StringToBinary($table[3])) $STW &= "02" & $lendata & $lon & $lat & $data & "00" $iCount += 1 EndIf Next $hFile = FileOpen($FilePath, 2 + 16) If $hFile = -1 Then Return SetError(1, 0, 0) EndIf $Res = FileWrite($hFile, BinaryToString("0x" & $STW)) FileClose($hFile) If $Res = 1 Then Return SetError(0, $iCount, 1) Else Return SetError(1, 0, 0) EndIf EndFunc ;==>_POItoOV2