title: Divide a Rectangle into N Equal Subrectangles
body: <p>I have a rectangular grid that is defined by longitude and latitude pairs. I would like to divide this rectangle into N equal subrectangles. For example, suppose we have a rectangle defined by the following four points:</p>
  
  <pre><code>1: (0,0)
  2: (0,2)
  3: (2,2)
  4: (2,0)
  </code></pre>
  
  <p>Suppose we wish to divide this rectangle into 4 equal subrectangles so that we have the following:</p>
  
  <pre><code>First subrectangle:
  1. [(0,0),(0,1),(1,1),(1,1)]
  Second subrectangle:
  2. [(0,1),(0,2),(1,2),(1,1)]
  Third subrectangle:
  3. [(1,1),(1,2),(2,2),(2,1)]
  Fourth subrectangle:
  4. [(1,0),(1,1),(2,1),(2,0)]
  </code></pre>
  
  <p>The order of the subrectangles is irrelevant.</p>
  
  <p>My approach is to construct latitude and longitude lists and use a double loop to call the items from each list.</p>
  
  <pre><code>def sub_rectangles(factor,westlimit=0, southlimit=0, eastlimit=2, northlimit=2):
      table=list()
      #Divide the difference between the limits by the factor
      lat_adj_factor=(northlimit-southlimit)/factor
      lon_adj_factor=(eastlimit-westlimit)/factor
      #Create longitude and latitude lists
      lat_list=[]
      lon_list=[]
      for i in range(factor+1):
          lon_list.append(westlimit)
          westlimit+=lon_adj_factor
      for i in range(factor+1):
          lat_list.append(southlimit)
          southlimit+=lat_adj_factor
      #Build a list of longitude and latitude pairs
      for i in range(0,len(lon_list)-1):
          for j in range(0,len(lat_list)-1):
              table.append([lon_list[i],lat_list[j],lon_list[i+1],lat_list[j],lon_list[i+1],[lat_list[j+1]]]) 
      return table
  </code></pre>
  
  <p>Unfortunately the output is mostly nonsense. Any suggestions?</p>