0

This doesn't answer my question.

I have a list of coordinates where every 5 consecutive coordinates define the coordinates of a rectangle, e.g.,

mylist=[(0, 7),(4, 7),(4, 12),(0, 12),(0, 7),(7, 1),(10, 1),(10, 8),(7, 8),(7, 1),(4, 8),(10, 8),(10, 12), (4, 12),(4, 8),(0, 0),(7, 0),(7, 7),(0, 7),(0, 0)]

I want to create four rectangles in Shapely with these coordinates shown in the example. The four rectangles should also be uniquely identifiable. Also, the list size can be variable as there can be more or less than the current number of coordinates.

EDIT:

At this point, I have 4 lists:

[[(0, 7), (4, 7), (4, 12), (0, 12), (0, 7)],
 [(7, 1), (10, 1), (10, 8), (7, 8), (7, 1)],
 [(4, 8), (10, 8), (10, 12), (4, 12), (4, 8)],
 [(0, 0), (7, 0), (7, 7), (0, 7), (0, 0)]] 

Now my question is how to pass these 4 sets of coordinates to shapely so I can later draw them in a figure and identify them individually? I am new to shapely.

yoyo
  • 23
  • 7
  • Related: [How do you split a list into evenly sized chunks?](https://stackoverflow.com/q/312443/7851470) – Georgy Mar 08 '21 at 08:56
  • Someone answered and then deleted? – yoyo Mar 08 '21 at 21:21
  • @Georgy I also want to know how to pass these coordinates for each rectangle as shapely objects in a pythonic way. – yoyo Mar 08 '21 at 21:26
  • I think `poly = geometry.Polygon(pointList)` where pointList is one of your sublist should just work for shapely above 1.7a2. Never used shapely thou.. give it a try. c.f. https://stackoverflow.com/questions/30457089/how-to-create-a-shapely-polygon-from-a-list-of-shapely-points – Mathieu Mar 08 '21 at 21:53
  • Thank you. I tried it but in this case, I get only an area of 49.0 when I do **poly.area**. So only the last rectangle is taken. – yoyo Mar 08 '21 at 22:08
  • @yoyo I mean if this is the problem just put a loop and add each polygon? .. – Mathieu Mar 08 '21 at 22:35
  • Thank you. .I am trying to create an image consisting of all the rectangles but I get this- https://ibb.co/RBMBXVB ......any idea what's wrong? – yoyo Mar 09 '21 at 04:54

1 Answers1

0

You could store your Shapely objects in a dictionary and use them whenever your want afterwards, as e.g. below:

from shapely.geometry import Polygon

your_list = [[(0, 7), (4, 7), (4, 12), (0, 12), (0, 7)],
            [(7, 1), (10, 1), (10, 8), (7, 8), (7, 1)],
            [(4, 8), (10, 8), (10, 12), (4, 12), (4, 8)],
            [(0, 0), (7, 0), (7, 7), (0, 7), (0, 0)]] 

your_dict = {}

for i,sublist in enumerate(your_list):
    
    your_dict[i] = Polygon(sublist)

Then plot them you could use your_dict in this way:

plt.figure()

for key,your_polygon in your_dict.items():
    
    plt.plot(*your_polygon.exterior.xy)

which would give:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Hello Adrien...I am trying to create an image consisting of all the rectangles but I get this-https://ibb.co/RBMBXVB ......any idea what's wrong? – yoyo Mar 09 '21 at 04:54
  • If possible, please take a look at this too-https://stackoverflow.com/questions/66539461/draw-circles-and-test-overlap-with-polygons-in-shapely – yoyo Mar 09 '21 at 12:02
  • Hi Adrien, I have updated the picture for the new question for understanding. Please help if possible. https://ibb.co/TvZGLFH – yoyo Mar 09 '21 at 20:02