我對python很陌生。我需要將一組正方形的坐標系從 WGS84 轉換為 JDG2000。我可以解決如何更改坐標。但是,我的問題是數千個 .txt 檔案之間的操作迭代。坐標 XY 位于我的檔案的第 7 列和第 8 列(向下滾動)。我嘗試使用 glob 和 os 庫來執行此操作,但它不起作用。
我不太確定我應該使用什么方法。
能否請你幫忙?這是我的代碼:
import pyproj
import numpy as np
import pandas
file = pandas.read_fwf('test.txt', index_col=False, header=None,usecols = (7,8))
file.head()
conversion = pyproj.Transformer.from_crs("EPSG:4326","EPSG:2452", always_xy = True)
coord = list(zip(file[8],file[7]))
coord = np.array(list(conversion.itransform(coord)))
coord = coord.tolist()
c = pandas.DataFrame(coord)
file[7]=c[1]
file[8]=c[0]
file
output = pandas.read_fwf('test.txt', index_col=False, header=None)
wine = pandas.DataFrame(output)
wine[7] = file[7].map('{:.6f}'.format)
wine[8] = file[8].map('{:.6f}'.format)
wine=pandas.DataFrame(wine)
wine.to_numpy()
np.savetxt('x.txt', wine, fmt='s')
這是原始檔案的結構:
20.0 20.0 5.00 193.0 2.8 90.0 2.264 40.00000000 144.39999390 1
20.0 20.0 5.00 193.0 3.0 90.0 2.264 39.82474518 144.34718323 2
20.0 20.0 5.00 193.0 3.1 90.0 2.264 39.64949036 144.29449463 3
20.0 20.0 5.00 193.0 3.2 90.0 2.264 39.47423553 144.24194336 4
20.0 20.0 5.00 193.0 3.4 90.0 2.264 39.29898071 144.18952942 5
20.0 20.0 5.00 193.0 3.5 90.0 2.264 39.12372589 144.13723755 6
20.0 20.0 8.82 193.0 5.5 90.0 2.264 40.03971863 144.17541504 31
20.0 20.0 8.82 193.0 5.6 90.0 2.264 39.86446381 144.12257385 32
20.0 20.0 8.82 193.0 5.7 90.0 2.264 39.68920898 144.06985474 33
20.0 20.0 8.82 193.0 5.7 90.0 2.264 39.51395416 144.01727295 34
這是轉換坐標后的輸出結構(正在作業):
20.0 20.0 5.0 193.0 3.0 90.0 2.264 -13545.150580 300831.041674 2
我需要在名為“Square0001.txt”、“Square0002.txt”的檔案中重復該運算元千次... path='D:/Documents/samples'
謝謝!!!
uj5u.com熱心網友回復:
假設您的問題只是迭代檔案。您可以使用 os.listdir(),如下面的代碼所示。您使用np.savetext更改 end PRINT陳述句,同樣在下面的示例檔案中,檔案與代碼位于同一目錄中,如果不是這種情況,請更新os.listdir()中的目錄。
import os
import pyproj
import numpy as np
import pandas
for i in os.listdir():
if i.startswith('Square'):
file = pandas.read_fwf(i, index_col=False, header=None,usecols = (7,8))
file.head()
conversion = pyproj.Transformer.from_crs("EPSG:4326","EPSG:2452", always_xy = True)
coord = list(zip(file[8],file[7]))
coord = np.array(list(conversion.itransform(coord)))
coord = coord.tolist()
c = pandas.DataFrame(coord)
file[7]=c[1]
file[8]=c[0]
output = pandas.read_fwf(i, index_col=False, header=None)
wine = pandas.DataFrame(output)
wine[7] = file[7].map('{:.6f}'.format)
wine[8] = file[8].map('{:.6f}'.format)
wine=pandas.DataFrame(wine)
print("File {} = ".format(i),wine.to_numpy(),'\n')
輸出(多次使用相同的示例檔案來顯示迭代):

uj5u.com熱心網友回復:
import pyproj
import numpy as np
data = np.loadtxt('test.txt')
conversion = pyproj.Transformer.from_crs("EPSG:4326","EPSG:2452", always_xy = True)
x[:,8:6:-1] = list(conversion.itransform(x[:,8:6:-1]))
np.savetxt('x.txt', data.round(6), fmt='s')
輸出:
20.0 20.0 5.0 193.0 2.8 90.0 2.264 6097.868995 304574.959957 1.0
20.0 20.0 5.0 193.0 3.0 90.0 2.264 -13545.15058 300831.041674 2.0
20.0 20.0 5.0 193.0 3.1 90.0 2.264 -33184.318982 297071.745425 3.0
20.0 20.0 5.0 193.0 3.2 90.0 2.264 -52819.597495 293298.588191 4.0
20.0 20.0 5.0 193.0 3.4 90.0 2.264 -72450.996884 289511.783092 5.0
20.0 20.0 5.0 193.0 3.5 90.0 2.264 -92078.579913 285710.223126 6.0
20.0 20.0 8.82 193.0 5.5 90.0 2.264 9764.618134 285227.469779 31.0
20.0 20.0 8.82 193.0 5.6 90.0 2.264 -9866.411744 281435.193881 32.0
20.0 20.0 8.82 193.0 5.7 90.0 2.264 -29493.615661 277627.693825 33.0
20.0 20.0 8.82 193.0 5.7 90.0 2.264 -49116.956049 273806.484196 34.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497825.html
