Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gdal_grid in Python with np array

Tags:

python

gdal

I use Python 2.7. The gdal 2.1.2 library is installed.

I have a np array like this:

[[  2.34845824  48.84626174  15.20369   ]
[  2.34966283  48.84910128  13.88528   ]
[  2.35120647  48.85098931  15.76322   ]
...,
[  2.3556567   48.8415611   15.42184   ]
[  2.34394085  48.84248672  15.51128   ]
[  2.34926763  48.85128456  15.0685    ]]

where first and second columns represent my coordinates (specifically, latitude and longitude) and the third one gathers variable values associated to locations. I would like to use gdal_grid directly in Python to obtain an image where at every pixel I have an interpolation of the values of my array using a method like inverse distance weighted.

I found how to do it with gdal_grid, if my data are collected in a csv file (that I will call 'test.csv') and if I create a 'test.vrt' file like this:

<OGRVRTDataSource>
    <OGRVRTLayer name="test">
        <SrcDataSource>test.csv</SrcDataSource>
        <GeometryType>wkbPoint</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <GeometryField separator=" " encoding="PointFromColumns" x="field_1" y="field_2" z="field_3"/>
    </OGRVRTLayer>
</OGRVRTDataSource>

I tried to run it in python with the command:

gdal.Grid('test_python.tiff', 'test.vrt', layers='test', algorithm='invdistnn:max_points=10:min_points=1:radius=0.001', format='GTiff', width=333, height=240)

and it works. The problem is that I would like to avoid the conversion of the np array into a .csv, because I need to replicate this operation several times. How can i do it using directly my np array?

Thank you very much in advance for your help.

like image 724
Paolo Z. Avatar asked Jan 18 '17 17:01

Paolo Z.


1 Answers

You cannot.

The input to gdal_grid must be a GDAL-readable format, of which VRT is one. Alternatively, you can save the numpy array as a Tiff/GeoTiff file.

An other alternative, with GDAL >= 2.1, is to use the python binding rather than the command line utility, along with a 'MEM' dataset populated from the numpy array (which at least avoid saving it to disk).

like image 112
Benjamin Avatar answered Oct 31 '22 19:10

Benjamin