Tuesday, 17 February 2015

python - Turn a shapefile into a mask and calculate the mean?


I have been using Python to process huge arrays stored as NetCDF files. I would like to calculate the mean of an area defined by a shapefile. I have just installed GDAL but if there are other tools I should use please let me know.


So my main question is how can I turn my shapefile into a mask?



Then use my mask to calculate the NumPy mean of my array within the area of the mask?


shp="country.shp"
array=mynumpyarray
mask=useGDALtochangeshapefiletomask
meanofarea=N.mean(array,limits=mask)

Edit


I have been able to convert my shapefile to a raster using gdal_RasterizeLayer link supplied and then a masked array using


maskarray=mask_ds.GetRasterBand(1).ReadAsArray()


Now it is just a question of matching my NetCDF extent with my raster/maskarray. Here are my extents as requested.


my shapefile/raster extent: x_min, x_max, y_min, y_max 140.962408758 149.974994992 -39.1366533667 -33.9813898583

my netcdf file extent: min longitude: 139.8 max longitude: 150.0 min latitude -39.2 max latitude: -33.6 LAT size 106 LON size 193


1 comment:

  1. All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks
    python代写

    ReplyDelete

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...