title: Get external coordinates of a polygon from a numpy boolean grid
body: <p>I am trying to get the external coordinates of a polygon from a numpy boolean grid. For example, from a (16, 16) ndarray such as the following one</p>
  
  <pre><code>[
      [False False False False False False  True  True  True  True False False False False False False],
      [False False False False False  True  True  True  True  True  True False False False False False],
      [False False False False False False False False False False  True  True False False False False],
      [False False False False False False False False False False  False True False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False],
      [False False False False False False False False False False False False False False False False]
  ]
  </code></pre>
  
  <p>If we plot that <code>ndarray</code> it will be like the following:</p>
  
  <p><a href="https://i.stack.imgur.com/tADCVm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tADCVm.png" alt="Boolean grid as image"></a></p>
  
  <p>I would like to get the following coordinates, in order, such that we could draw the external ring of such polygon, e.g., <code>[(5 1), (6 0), (7 0), (8 0), (9 0), (10 1), (11 2), (11 3), (10 2), (9 1), (8 1), (7 1), (6 1)]</code>. What I have so far is the following:</p>
  
  <pre><code># Consider that the boolean ndarray above is called 'prediction'
  
  import numpy as np
  from shapely.geometry import Polygon, Point
  import matplotlib.pyplot as plt
  
  # Get the coordinates that match the boolean polygon
  (y, x) = np.where(prediction == True)
  
  # Iterate on each of the coordinates, however my problem is that it is not aware of the contour order as it should be :/
  coordinates = [Point(x_coordinate, y_coordinate) for x_coordinate, y_coordinate in itertools.izip(x, y)]
  # Build the polygon out of the points
  polygon = Polygon([[coordinate.x, coordinate.y] for coordinate in coordinates])
  
  exterior_x, exterior_y = polygon.exterior.xy
  
  # Plotting
  
  fig = plt.figure(1, figsize=(5, 5))
  ax = fig.add_subplot(1, 2, 1)
  ax.plot(exterior_x, exterior_y, color='#6699cc')
  ax.invert_yaxis()
  
  plt.subplot(1, 2, 2)
  plt.imshow(prediction)
  plt.show()
  </code></pre>
  
  <p>The problem is that I am building the polygon not considering the order so that the result of <code>polygon.exterior.xy</code> will create the external ring. My approach will create the wrong contour of the polygon such as:</p>
  
  <p><a href="https://i.stack.imgur.com/xsgVhm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xsgVhm.png" alt="enter image description here"></a></p>
  
  <p>However, I am unable to come up with a general approach for this problem. I welcome any suggestion on how to tackle this problem. Thanks in advance.</p>