I am creating IDF Curves for work using python and 30-minute rainfall rate rasters. There are 24 rasters per day and about two decades worth of data. I basically input lat/lon coordinates and my python script grabs the pixel value from each raster throughout the period and stores it in a DataFrame to use for statistical analysis. This process currently takes about 5 hours to run per lat/lon coordinate with the majority of the time coming from opening the raster and extracting the pixel value. Are there any ways to optimize this process for speed? I am completely stuck and can't find if there is any speed efficiencies native to GDAL. I've submitted the function I use for extracting the pixel value. Please let me know if you have any questions or suggestions, thanks!
def extract_point_values_from_raster(raster_filepath, lat_lon_str):
lat_lon = lat_lon_str.split(',')
lat = float(lat_lon[0])
lon = float(lat_lon[1])
try:
src_ds = gdal.Open(raster_filepath)
gt = src_ds.GetGeoTransform()
rb = src_ds.GetRasterBand(1)
if (lat < gt[3] and lat > (gt[4] - gt[3]) and
lon > gt[0] and lon < (gt[2] - gt[0])):
px = int((lon-gt[0])/gt[1])
py = int((lat-gt[3])/gt[5])
intval = rb.ReadAsArray(px,py,1,1)
if intval == None:
raster_val = None
else:
raster_val = round(intval[0][0],2)
del src_ds
except:
raster_val = None
return(raster_val)
numpysolution that might be helpful if that is the case. – Marcelo Villa Mar 25 '20 at 21:11