我一直在尋找一個德爾福函數或任何可以解析空格分隔的TStringList(從* .asc文件加載)到整數的動態二維數組的信息。如何將分隔的TStringlist解析爲整數的動態多維數組?
我所擁有的是一個帶有網格格式高程的ASCII網格文件。首先我將文件讀入一個TStringlist,在這裏我將頭數據提取到變量,然後從TStringlist中刪除頭數據。我剩下的是一個TStringlist,它只有一個3601 x 3601的網格中的高程數據,每個值用一個空格分隔。有了這個Stringlist仍然在內存中,我想用高程數據填充一個二維整數數組,然後釋放TStringList。
從我的搜索我偶然發現了一種代碼here這種工作,但不完全。
我所擁有的是:
var sourceData, tmpGrid, tmpRow : TStringList;
val : String;
Pos, i, j, k, l : Integer;
nCols, nRows, noData : Integer;
xllCorner, yllCorner, cellSize : Extended;
Dem : array of array of Integer;
begin
val := '';
sourceData := TStringList.Create;
tmpGrid := TStringList.Create;
tmpRow := TStringList.Create;
tmpRow.StrictDelimiter := True;
tmpRow.Delimiter := ' ';
sourceData.LoadFromFile('D:\ZA_DEM_DATA\Bulk_Order_466486\ARCASCII\s30_e026_1arc_v3.asc');
nCols := StrToInt(StripNonConforming(sourceData.Strings[0], ['0'..'9', '+', '-', '.']));
nRows := StrToInt(StripNonConforming(sourceData.Strings[1], ['0'..'9', '+', '-', '.']));
xllCorner := StrToFloat(StripNonConforming(sourceData.Strings[2], ['0'..'9', '+', '-', '.']));
yllCorner := StrToFloat(StripNonConforming(sourceData.Strings[3], ['0'..'9', '+', '-', '.']));
cellSize := StrToFloat(StripNonConforming(sourceData.Strings[4], ['0'..'9', '+', '-', '.']));
noData := StrToInt(StripNonConforming(sourceData.Strings[5], ['0'..'9', '+', '-', '.']));
Edit1.Text := IntToStr(nCols);
Edit2.Text := IntToStr(nRows);
Edit3.Text := FloatToStr(xllCorner);
Edit4.Text := FloatToStr(yllCorner);
Edit5.Text := FloatToStr(cellSize);
Edit6.Text := IntToStr(noData);
for I := 0 to 5 do
begin
sourceData.Delete(0);
end;
//This is where I start parsing my TStrinList
tmpGrid.DelimitedText := sourceData.Text;
SetLength(Dem, nCols, nRows);
Edit7.Text := IntToStr(sourceData.Count);
try
for i := 0 to sourceData.Count -1 do
for j := 0 to sourceData.Count -1 do
begin
Dem[i][j] := StrToInt(tmpGrid.Strings[i]);
end;
finally
sourceData.Free;
end;
StringGrid1.ColCount := nCols;
StringGrid1.RowCount := nRows;
//This is mainly to visualise the data while testing the code
for i := 0 to nCols -1 do
for j := 0 to nRows -1 do
begin
StringGrid1.Cells[i, j] := IntToStr(dem[i, j]);
end;
end;
結果我得到的是顯示在原始文件,但複製到陣列中的所有行數據的第一個「行」。例如:
原始數據:
1. 1245 1268 1232 1258 2. 1354 1321 1578 1689 3. 1461 1423 1475 1427 4. 1598 1541 1562 1550
我得到的是:
1. 1245 1268 1232 1258 2. 1245 1268 1232 1258 3. 1245 1268 1232 1258 4. 1245 1268 1232 1258
任何和所有幫助將不勝感激。
好吧,看起來像'[debug-this-code-for-me]'應該是這裏的一個主要標籤。 – 2015-04-02 12:23:06