title: Python make an efficient Grid with an extent
body: <p>I make a fonction in Python.
  This function created a grid from a given extent with coordinates. </p>
  
  <p>In enter you give :
  envelope <code>(xMin, xMax, yMin, yMax)</code></p>
  
  <p>xDist distance in X for each mesh</p>
  
  <p>yDist distance in Y for each mesh</p>
  
  <p>In exit the fucntion give 2 lists</p>
  
  <p>xResutl is the X coordinate for each mesh</p>
  
  <p>yResult is the Y coordinate for each mesh</p>
  
  <p>My code is : </p>
  
  <pre><code>from math import *
  
  def distanceEuclidean(x1, y1, x2, y2):
      return sqrt( (x2 - x1)**2 + (y2 - y1)**2 )
  
  def makeGrid(envelope, distX, distY):
      nbMeshX = int(ceil(distanceEuclidean(envelope[0], 0, envelope[1], 0)/distX))
      nbMeshY = int(ceil(distanceEuclidean(envelope[2], 0, envelope[3], 0)/distY))
      nbMesh = nbMeshX*nbMeshY
      xResult, yResult = range(nbMesh), range(nbMesh)
      count = 0
      for x in xrange(nbMeshX):
          for y in xrange(nbMeshY):
              x1 = envelope[0]+x*distX
              x2 = envelope[0]+(x+1)*distX
              y1 = envelope[2]+y*distY
              y2 = envelope[2]+(y+1)*distY
              xResult[count], yResult[count] =[x1,x2,x2,x1,x1], [y1, y1, y2, y2, y1]
      count += 1
  
      return xResult, yResult
  </code></pre>
  
  <p>When I use a little envelope it ok !But when I use a very large envelope the function is very slow, the result's list is very large, very slow and use a lot of memory.</p>
  
  <p>For example :</p>
  
  <p>With a tiny envelope : </p>
  
  <p>Input : </p>
  
  <pre><code>envelope = [-10, 10, -15, 25]
  print makeGrid(envelope, 20, 15)
  </code></pre>
  
  <p>Output :</p>
  
  <pre><code> &gt;&gt;&gt; ([[-10, 10, 10, -10, -10], [-10, 10, 10, -10, -10], [-10, 10, 10, -10, -10]], [[-15, -15, 0, 0, -15], [0, 0, 15, 15, 0], [15, 15, 30, 30, 15]])
  </code></pre>
  
  <p>With a very large envelope :</p>
  
  <p>Input :</p>
  
  <pre><code>envelope = [47300, 1198000, 1617200, 2677500]
  print makeGrid(envelope, 100, 100)
  </code></pre>
  
  <p>Output :</p>
  
  <pre><code>&gt;&gt;&gt; MemoryError
  </code></pre>
  
  <p>Do you have any solution to reduce time computation and/or memory storage ?</p>
  
  <p>Thanks a lot !</p>