Explanation: I currently have 3 1D numpy arrays, each having a length of over 100000 points. The first contains the X coordinates of an object, the second contains the Y coordinates, and the third one contains the speed of the object at the corresponding coordinates.The 3 arrays are in sequence from the start to end. I am trying to generate a raster file that will match another raster file that I produced using certain images.For simplicity's sake, the output raster size is 400 meters x 400 meters, and each cell has dimensions of 20 X 2. How exactly do I go about generating a raster using my 3 arrays ?
Ideally I would like to do something as simple as:
#xm = x-cooridnate array #ym = Y-coordinate array #speed = speed array xmin,ymin,xmax,ymax = [xm.min(),ym.min(),xm.max(),ym.max()] #nrows,ncols = np.shape(mining_durations) xres = (xmax-xmin)/float(2) yres = (ymax-ymin)/float(20) geotransform=(xmin,xres,0,ymax,0, -yres) output_raster = gdal.GetDriverByName('GTiff').Create(outputFileName,400/20, 400/2, 1 ,gdal.GDT_Float32) # Open the file output_raster.SetGeoTransform(geotransform) output_raster.GetRasterBand(1).WriteArray(speed) output_raster.FlushCache()
But this of course doesn't work.I know that many points will coincide in one cell, but I assume that it will automatically take the average of all the coinciding points.
Do I need to first create some sort of grid for the speed array?
Edit: I have added sample data, as well as the code I am using.
Sample data: The coordinates are just float values. For example, the first column here is X, the second is Y and the last is some other variable (speed):
52039,57 -54169,66 7,68667678522605 52027,68 -54180,01 6,8999999999869 52039,55 -54169,70 2,12132034354421 52039,59 -54169,71 1,34164078652916 52039,61 -54169,64 0,670820393288984 52039,63 -54169,51 0,600000000013097 52039,69 -54169,49 0,750000000043656 52039,70 -54169,46 0,150000000030559 52039,67 -54169,58 0,75
Each sample sample/row of data is taken every 4 seconds, although this sometimes changes. The X and Y coordinates don't necessarily follow a fixed pattern. The code I am using is shown below.Note that this doesn't produce an output of 400x400 which I originally had in mind, the size is based on the coordinates.
def GenerateRaster(xdata, ydata, zdata): binWidth = 2.0 binLength = 20.0 xMin = min(xdata) xMax = max(xdata) yMin = min(ydata) yMax = max(ydata) statistic,x_edge,y_edge,binnumber = stats.binned_statistic_2d(xdata, ydata, zdata, 'mean',bins = [np.arange(xMin, xMax, binWidth), np.arange(yMin, yMax, binLength)] ) # statistic = np.ma.masked_invalid(statistic) nrows,ncols = np.shape(statistic) xres = (xMax-xMin)/y_edge.shape yres = (yMax-yMin)/x_edge.shape geotransform=(xMin,xres,0,yMax,0, -yres) output_raster = gdal.GetDriverByName('GTiff').Create('Test_Raster.tif',ncols, nrows, 1 ,gdal.GDT_Float32) # Open the file output_raster.SetGeoTransform(geotransform) # Specify its coordinates output_raster.GetRasterBand(1).WriteArray(statistic) # Writes array to the raster output_raster.FlushCache()
Although I do produce a raster file, I am not sure if this is correct or accurate. I noticed that the cell sizes are not exactly 20 x 2 which is strange: the dimensions are slightly longer.