1

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.

3
  • I suspect that there are many. Are you already using ArcPy or any other common Python libraries for GIS?
    – PolyGeo
    Sep 7, 2016 at 4:30
  • @PolyGeo, I am not using ArcPy, can use gdal or rasterio though.
    – user1186
    Sep 7, 2016 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. Sep 7, 2016 at 11:25

1 Answer 1

1

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
w.field("ID")
id=0

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

w.save('polygon_grid')
1
  • This will not yield a raster but a vector file
    – RutgerH
    Mar 20, 2017 at 16:05

Your Answer

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

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