3

If I have a CSV Dataset with Lat, Lon, and Value fields, what is the best approach on generating a raster map using python? The raster Z field can be any column within this table..

L5  L6  L7  L8  L9  L10 L11 L12 L13 L14 LAT LON
3.571732    1.338448    0   9.96921E+36 -3.482845   -1.42944    133.229919  141.246002  67.685631   5.059844    24.335797   -95.088764
3.571732    1.338448    0   9.96921E+36 -3.420345   -1.42944    132.749512  140.641464  67.318848   5.105563    24.335107   -95.060013
3.571732    1.338448    0   9.96921E+36 -3.420345   -1.42944    132.230164  140.047211  67.318848   5.063346    24.334408   -95.031263
3.571732    1.338448    0   9.96921E+36 -3.420345   -1.42944    132.230164  139.463104  67.318848   5.063346    24.333701   -95.002512
3.509232    1.369698    0   9.96921E+36 -3.357845   -1.42944    131.702133  137.82196   66.940475   5.021552    24.332986   -94.973763
3.509232    1.369698    0   9.96921E+36 -3.357845   -1.49194    131.702133  137.26651   66.043732   5.021552    24.332263   -94.945013
3.509232    1.369698    0   9.96921E+36 -3.357845   -1.49194    131.165268  136.72081   66.043732   4.980192    24.331531   -94.916265
3.509232    1.338448    0   9.96921E+36 -3.357845   -1.49194    131.165268  136.184738  66.043732   4.980192    24.330792   -94.887516

Keep in mind, These are numpy arrays

ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
Kyle
  • 51
  • 2
  • 8
  • 1
    Did you look at `Basemap` the matplotlib toolkit for geographic data? see for instance https://jakevdp.github.io/PythonDataScienceHandbook/04.13-geographic-data-with-basemap.html – xdze2 Sep 04 '18 at 12:09
  • Yes, however, I do not use pandas... – Kyle Sep 04 '18 at 12:28

1 Answers1

4

There are two options depending on your Lat, Lon coordinates. When the Lat, Lon coordinates form an equidistant grid you can use this first option, otherwise you can use the second option below.


first option

I create the array below with values in the first column and Lat, Lon in the 2nd and 3rd column:

import numpy as np

lat = np.arange(0, 15, 5)
lon = np.arange(0, 10, 5)
val = np.random.randint(0,10, size =len(lat)*len(lon))
xx, yy = np.meshgrid(lon, lat)
array = np.array([val,  yy.ravel(), xx.ravel()]).T
print(array)

>>> array([[ 7,  0,  0],
           [ 8,  0,  5],
           [ 7,  5,  0],
           [ 3,  5,  5],
           [ 2, 10,  0],
           [ 8, 10,  5]])

When your Lat, Lon coordinates are neatly sorted you can reshape the values to get a grid array like this:

no_lon = len(np.unique(array[:,-1]))
no_lat = len(np.unique(array[:,-2]))
grid_array = array[:,0].reshape((no_lat,no_lon))[::-1]
print(grid_array)
>>> array([[2, 8],
           [7, 3],
           [7, 8]])

second option

When you have a bunch of random Lat, Lon coordinates with values like the one created here:

array = np.random.randint(0,10, size =(6,3))
print(array)
>>> array([[9 6 0]
           [7 8 8]
           [6 0 9]
           [7 7 4]
           [2 4 3]
           [0 2 9]])

you can convert this to a grid by using interpolation like this:

from scipy import interpolate

lon_list = np.arange(3, 6, 1)
lat_list = np.arange(4, 8, 1)

lon_2d, lat_2d = np.meshgrid(lon_list, lat_list)
grid_array = interpolate.griddata((array[:,-1], array[:,-2]), array[:,0],
                                  (lon_2d, lat_2d))[::-1]
print(grid_array)

>>> [[  nan  7.    6.72]
     [ 6.    5.4   5.6 ]
     [ 4.    3.8   4.  ]
     [ 2.    2.2   2.4 ]]

Note that you get nan values if your grid cells are not within the bounds of your points.

You can visualize the results using plt.imshow

import matplotlib.pyplot as plt
plt.imshow(grid_array)
onno
  • 969
  • 5
  • 9