0

so I have a huge .txt-file with about 4 000 000 coordinates in the following format:

315987 5587999 718.80
315988 5587999 718.87
315989 5587999 719.03
315990 5587999 718.94
315991 5587999 718.97
...

So my task now is to read these coordinates in from this txt-file with a python script and store the data for each axis in a tuple (np-array) so I can use methods like np.flatten on the tuples.

For this tasks I found many answers on stackoverflow but somehow it was a little bit confusing and I couldnt manage to implement it how I wanted.

Here is my code:

def get_stl():

with open('test.txt') as f:
    for line in f:
        x, y, z = (float(a) for a in line.split())

    X, Y = np.meshgrid(x, y, z)
    Z = z;
    return X, Y, Z

x, y, z = get_stl()

xyz = np.zeros(shape=(x.size, 3))
xyz[:, 0] = x.flatten()
xyz[:, 1] = y.flatten()
xyz[:, 2] = z.flatten()

So obviously z.flatten() cant be executed because I didn't read in z as a np.array.

My question would be how to read in that large txt.file and especially the z-coordinate properly.

Thank you!

EDIT:

I don't want to use CSV because of its memory-usage

Samuel Dressel
  • 1,181
  • 2
  • 13
  • 27
  • 1
    Does this answer your question? [How do I read CSV data into a record array in NumPy?](https://stackoverflow.com/questions/3518778/how-do-i-read-csv-data-into-a-record-array-in-numpy) – Daniel GL Jan 16 '20 at 16:16
  • 1
    Consider your txt as as csv file (with a space as a separator), and check SO again. Or you could check this: https://riptutorial.com/numpy/example/22990/reading-csv-files – Daniel GL Jan 16 '20 at 16:20
  • _for each axis in a tuple (np-array) so I can use methods like np.flatten on the tuples._ Can you expand on this, I'm not sure I understand correctly. – AMC Jan 16 '20 at 16:22
  • @AMC as you can see I use the np-method `flatten()` on each tuple (last three lines) – Samuel Dressel Jan 16 '20 at 16:26
  • @SamuelDressel I meant what are you trying to do, what's your desired output, what is it for? – AMC Jan 16 '20 at 16:30
  • @AMC I want to use it to create an STL-File with a Python-Script - https://uuvsimulator.github.io/packages/uuv_simulator/docs/tutorials/seabed_world/ – Samuel Dressel Jan 16 '20 at 17:25
  • I used the script on the webpage above but their use calculated values from a library (get_test_data() from `mpl_toolkits.mplot3d.axes3d` – Samuel Dressel Jan 16 '20 at 17:26

1 Answers1

2
import numpy as np
data = np.genfromtxt('test.txt', delimiter=' ')

array([[3.159870e+05, 5.587999e+06, 7.188000e+02],
       [3.159880e+05, 5.587999e+06, 7.188700e+02],
       [3.159890e+05, 5.587999e+06, 7.190300e+02],
       [3.159900e+05, 5.587999e+06, 7.189400e+02],
       [3.159910e+05, 5.587999e+06, 7.189700e+02]])
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55