Home > Software engineering >  How to iterate a coordinate conversion trough several text files
How to iterate a coordinate conversion trough several text files

Time:06-30

I am quite new to python. I need to transform the coordinate system of a set of squares from WGS84 to JDG2000. I could solve how to change the coordinates. However, my problem is the iteration of the operation among thousands of .txt files. The coordinates XY are located in columns 7 and 8 of my files (scroll down). I tried to use glob and os libraries to do it, but it did not work.

I am not quite sure what method I should use.

Could you please help? This is my code:


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')

This is the structure of the original file:

 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

This is the structure of the output after transforming the coordinates (which is working):

 20.0  20.0  5.0  193.0   3.0   90.0  2.264  -13545.150580 300831.041674   2

I need to repeat the operation thousands of times in my files named as: 'Square0001.txt', 'Square0002.txt'... path='D:/Documents/samples'

Thank you!!!

CodePudding user response:

Assuming your problem is only Iterating over files. You can use os.listdir(), as show in below code. You change end PRINT statement with your np.savetext, also in below example files are in same directory as of code, if that is not the case please update directory in 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')

Output (Using your same example file multiple times to show the iteration):

enter image description here

CodePudding user response:

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')

Output:

           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
  • Related