3

Lets say I have several ascii grid files for each time step for a particular location. I would like to spit out the image for each of these ascii grid files and legend on the side bar. I also would like to put the area boundary (County or state) on the top of every image. After that I would like to make a movie from all of these images. How to procede this task in python or R?

NCOLS xxx
NROWS xxx
XLLCENTER xxx | XLLCORNER xxx
YLLCENTER xxx | YLLCORNER xxx
CELLSIZE xxx
NODATA_VALUE xxx
row 1
row 2
...
row n
1
  • As a suggestion (!)... there's a couple of Python based animation tools in QGIS, including Underdark's Time Manager plugin (Github here: github.com/gvb/TimeManager/tree/raster)... which could give you a few ideas. May 25, 2015 at 1:08

2 Answers 2

2

potentially a multi-part question - 1) plotting grids with legends, 2) including shape files on grid, and 3) animate output images. each with multiple opportunities to accomplish the task. here's a quick run-down of at least 2-methods:

using gdal, one should be able to read in the raster - perhaps something like (in a loop to get all rasters).

raster = gdal.Open('inputRaster')
band = raster.GetRasterBand(1)
array = band.ReadAsArray()

apply boundaries using basemap - though i haven't used that one.

using matplotlib, you could then plot the raster with a legend

plt.imshow(array)
plt.colorbar()
plt.savefig('someimagename.png')

once you have images of each grid, they can be stitched together into a video format using ffmpeg. (on my machine that looks something like - )

ffmpeg -framerate 12 -i imageName%04d.png -s:v 1280x720 -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p outputname.mp4

Or another option - for this type of output, i often use VisIt, which can read multiple formats (also uses GDAL) and allows multiple legends and annotation. With the added benefit of allowing you to view the grid in 3D if you choose. It also has python bindings so that the grids/shapefiles can be added, colors changed, legends added, and images exported, all within a script. one example of grids/shapes rendered in visit and another.

2
  • Could you explain what kind of object is plt in this example? Jul 27, 2016 at 11:35
  • sure - plt is a common alias for pyplot - such as import matplotlib.pyplot as plt. some documentation is here Jul 30, 2016 at 19:01
1

In R:

library(raster)
library(animation)
files <- list.files("path/to/asc", pattern = "asc$")
saveHTML({
  for (i in seq_along(files)) {
     r <- raster(files[i])
     r <- plot(r)
     ## include additions like counties here
  }
})

The animation package has other options for different output formats rather than HTML. The raster package has options for the plot etc. There are R packages with maps but you might need to transform them to your grids, and it's likely .asc won't have the projection metadata so that's another detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.