-1

For some purpose, I want to plot an polygon based on several latitude and longitude as endpoints which combined together.

The example data shows like this:

fig=plt.figure()
ax = plt.gca()

x_map1, x_map2 = 114.166,114.996
y_map1, y_map2 = 37.798,38.378

map = Basemap(llcrnrlon=x_map1,llcrnrlat=y_map1,urcrnrlon=x_map2,urcrnrlat=y_map2)
map.drawparallels(np.arange(y_map1+0.102,y_map2,0.2),labels=[1,0,0,1],size=14,linewidth=0,color= '#FFFFFF')
map.drawmeridians(np.arange(x_map1+0.134,x_map2,0.2),labels=[1,0,0,1],size=14,linewidth=0)

bo_x = [114.4390022, 114.3754847, 114.3054522, 114.3038236, 114.2802081, 114.2867228, 114.3378847, 114.3888619, \
    114.6288783,  114.6848733, 114.7206292, 114.7341219]
bo_y = [38.16671389, 38.14472722, 38.14309861, 38.10156778, 38.08853833, 38.06980889, 38.03587472, 37.96409056, \
    37.84975278, 37.84840333, 37.9017, 38.16683306]

x, y = map( bo_x, bo_y )
xy = zip(x,y)
poly = Polygon( xy, facecolor='red', alpha=0.4 )
plt.gca().add_patch(poly)

The figure shows like this:

enter image description here

But when the Lons array and Lats array are not in the anticlockwise order, and the arrays contain many items that hard to adjust manually. The polygon output may show non-conformity.

Here, I disorganize the bo_x and bo_y as an suppositional situation.

bo_x_adjust = [114.4390022, 114.3754847, 114.3054522, 114.3038236, 114.6288783,  114.6848733, 114.7206292, 114.7341219,
        114.2802081, 114.2867228, 114.3378847, 114.3888619,        ]
bo_y_adjust = [38.16671389, 38.14472722, 38.14309861, 38.10156778, 37.84975278, 37.84840333, 37.9017, 38.16683306,
        38.08853833, 38.06980889, 38.03587472, 37.96409056,        ]  

Figure shows like:

enter image description here

So, here is my question. Sometimes, the original endpoints are not in order which can output a closed polygon. Pre-organize the arrays is the way to go.

I think to adjust the order of arrays like bo_x and bo_y must follow two principles:

  • Elements in these two array should be adjust synchronously for the purpose to not break the endpoint pairs(X~Y)

  • The new arrays should be outlined in clockwise or anticlockwise order on 2-D space.

Any advice or guidelines would be appreciate.

Han Zhengzu
  • 3,694
  • 7
  • 44
  • 94

2 Answers2

1

Not an answer yet, but I needed the ability to attach images.

The problem may be ill defined. For example, these two legitimate polygons have the same vertices.

polygon 1 polygon2

Do you want to get either one?

Aguy
  • 7,851
  • 5
  • 31
  • 58
1

Here is a way to solve what you want by linear algebra. Sorry but I am writing just the general guidelines. Nonetheless it should work.

  1. Write a function that accept two edges numbers j and k and check if there is an intersection. Note that you need to handle correctly the last to first vertices edge. You also need to make sure you give 'False' when adjacent edges are called since these always intersect by definition.

Now the way to know if two edges intersect is to follow a little algebra. Extract from each edge its straight line parameters a and b by y = a*x + b. Then solve for the two edges to find the intersection x by equating a1*x+b1==a2*x+b2. If the intersection x for both edges is between the x's of the edge's vertices, then the two edges indeed intersect.

  1. Write a function that goes over all edges pairs and test for intersection. Only when no intersection exist the polygon is legitimate.

Next you can go in two approaches:

  1. Comprehensive approach - Go over all possible permutations of the vertices. Test each permutation polygon for intersections. Note that when permutating you need to permutate x and y together. Note that there are a lot of permutations so this could be very time consuming.

  2. Greedy approach - As long as there are still intersections, go over the edges pairs combinations and whenever there is an intersection simply switch the two last edge coordinates (unwind the intersection). Then restart going over all the edges pairs again . Repeat this until there are no more intersections. This should work pretty fast but will not give the best polygon (e.g. will not optimize the largest polygon area)

Hope this helps...

Aguy
  • 7,851
  • 5
  • 31
  • 58