Reputation: 674
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:
1: (0,0)
2: (0,2)
3: (2,2)
4: (2,0)
Suppose we wish to divide this rectangle into 4 equal subrectangles so that we have the following:
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)]
The order of the subrectangles is irrelevant.
My approach is to construct latitude and longitude lists and use a double loop to call the items from each list.
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
Unfortunately the output is mostly nonsense. Any suggestions?
Upvotes: 1
Views: 1031
Reputation: 674
I think the error is in the coordinate pair construction. I believe the following works; do you agree?
I changed this line:
table.append([lon_list[i],lat_list[j],lon_list[i+1],lat_list[j],lon_list[i+1],[lat_list[j+1]]])
To the following:
table.append([(lon_list[i],lat_list[j]),(lon_list[i+1],lat_list[j]),(lon_list[i],lat_list[j+1]),(lon_list[i+1],lat_list[j+1])])
Which gives this result:
[[(0, 0), (1.0, 0), (0, 1.0), (1.0, 1.0)], [(0, 1.0), (1.0, 1.0), (0, 2.0), (1.0, 2.0)], [(1.0, 0), (2.0, 0), (1.0, 1.0), (2.0, 1.0)], [(1.0, 1.0), (2.0, 1.0), (1.0, 2.0), (2.0, 2.0)]]
Upvotes: 1