[SciPy-User] match extent of arrays

questions anon questions.anon@gmail....
Wed Nov 16 20:56:58 CST 2011


I have had part of this question answered elsewhere so thought I would post
the result.


import random
from osgeo import gdal, ogr
import numpy as N
from netCDF4 import Dataset
from numpy import ma as MA
from osgeo import gdal, gdalnumeric, ogr, osr

shapefile=r"E:/GIS_layers/test/Vic_dissolve.shp"
xmin,ymin,xmax,ymax=[139.8,-39.2,150.0,-33.6] #Your extents as given above
ncols,nrows=[193,106] #Your rows/cols as given above
maskvalue = 1

xres=(xmax-xmin)/float(ncols)
yres=(ymax-ymin)/float(nrows)
geotransform=(xmin,xres,0,ymax,0, -yres)

src_ds = ogr.Open(shapefile)
src_lyr=src_ds.GetLayer()

dst_ds = gdal.GetDriverByName('MEM').Create('', ncols, nrows, 1
,gdal.GDT_Byte)
dst_rb = dst_ds.GetRasterBand(1)
dst_rb.Fill(0) #initialise raster with zeros
dst_rb.SetNoDataValue(0)
dst_ds.SetGeoTransform(geotransform)

err = gdal.RasterizeLayer(dst_ds, [maskvalue], src_lyr)
dst_ds.FlushCache()

mask_arr=dst_ds.GetRasterBand(1).ReadAsArray()




On Thu, Nov 17, 2011 at 11:46 AM, questions anon
<questions.anon@gmail.com>wrote:

> Hello All,
> I have an array that I created from a shapefile using gdal.RasterizeLayer
> and then ReadAsArray().
> I would like the array I created from a shapefile to match my array from a
> netcdf file.
> I am not sure how I go about this.
>
> The extents of the shapefile/raster/array are:
>
> x_min, x_max, y_min, y_max 140.962408758 149.974994992 -39.1366533667 -33.9813898583
>
>
> The extents and size for my array from netcdf files are:
>
> 139.8 max longitude: 150.0 min latitude -39.2 max latitude: -33.6
>
> LAT size 106 LON size 193
>
>
> Any ideas on how I might achieve this?
> The overal goal is to then use the array from a shapefile as a mask for my
> netcdf files.
> Thanks
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20111117/aeef748f/attachment.html 


More information about the SciPy-User mailing list