1

I have a xyz file containing a lot of 3D coordinates like so:

 370373.771    6535261.431      2.908       
 370373.788    6535261.441      2.911       
 370373.787    6535261.442      2.909       
 370373.809    6535261.449      2.908       
 370373.810    6535261.439      2.909       
 370373.743    6535261.466      2.922       
 370373.584    6535261.455      2.915       
 370373.559    6535261.471      2.898       
 370373.544    6535261.559      2.887       
 370373.552    6535261.538      2.866       
 370373.797    6535261.486      2.876       
 370373.795    6535261.557      2.892       
 ..........    ...........      .....

This file is quite dense and irregular, and I would like to interpolate these coordinates on a regular grid with a point each 5m for example.

Here is what I tried so far:

import numpy as np
from scipy.interpolate import griddata

coord_x = []
coord_y = []
coord_z = []
coord_xy = []

xyzfile = open("xyzfile.txt")
for line in xyzfile:
    x,y,z = line.split()
    coord_x.append(float(x))
    coord_y.append(float(y))
    coord_xy.append([float(x),float(y)])
    coord_z.append(float(z))
xyzfile.close()

lon = np.linspace(min(coord_x), max(coord_x), 200)
lat = np.linspace(min(coord_y), max(coord_y), 200)
X, Y = np.meshgrid(lon, lat)

grid = griddata(np.array(coord_xy), coord_z, (X, Y), method='nearest')

Unfortunately I get an error :

TypeError: only integer arrays with one element can be converted to an index

I'm also not sure how to get a 5m spacing between each point in the regular grid. How can I do this ?

Second question, say I have 6 known points on this grid, how to extract the z value on this regular 5m spaced grid for these 6 points.

Thank you

GeoffreyB
  • 536
  • 1
  • 7
  • 17

1 Answers1

4

The coord_z argument passed in must also be an array:

grid = griddata(np.array(coord_xy), np.array(coord_z), (X, Y), method='nearest')
pv.
  • 33,875
  • 8
  • 55
  • 49