I want to create a numpy array of size 2160 * 4320 with each grid cell containing the size of a 5' grid cell on earth.

Is there a python library or function to do this?

I am using GDAL and rasterio.

  • I suspect that there are many. Are you already using ArcPy or any other common Python libraries for GIS? – PolyGeo Sep 7 '16 at 4:30
  • @PolyGeo, I am not using ArcPy, can use gdal or rasterio though. – user1186 Sep 7 '16 at 4:32
  • 2
    I'm not sure I totally understand what you are asking...do you need to load data into this numpy array or are you just trying to create it? Numpy by itself has no sense of spatial information, so creating a 2160x4320 array is simply that. – Evil Genius Sep 7 '16 at 11:25

Numpy doesn't support any spatial info. You can use pyshp library for the same. https://github.com/GeospatialPython/pyshp

import shapefile as shp
import math

minx,maxx,miny,maxy = xxxxx, xxxxx, xxxxx, xxxxx (specify the extents)
dx = xx (size of grid)
dy = xx (size of grid)

nx = int(math.ceil(abs(maxx - minx)/dx))
ny = int(math.ceil(abs(maxy - miny)/dy))

w = shp.Writer(shp.POLYGON)
w.autoBalance = 1

for i in range(ny):
    for j in range(nx):
        vertices = []
        parts = []
        vertices.append([min(minx+dx*(j+1),maxx),max(maxy- dy*(i+1),miny)])

| improve this answer | |
  • This will not yield a raster but a vector file – RutgerH Mar 20 '17 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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