2

I have a list containing coordinates of a polygon inside a list .How do I access x and y points present in this list? Basically, I have to use these points and plot. I have to extract all_points_x and all_points_y from:

[{'shape_attributes':
    {'name': 'polygon',
     'all_points_x': [35, 28, 27, 31, 40, 51, 62, 72, 74, 71, 65, 57, 41],
     'all_points_y': [74, 55, 32, 16, 4, 6, 12, 35, 56, 74, 83, 86, 81]},
    'region_attributes': {}},
 None,
 {'shape_attributes':
    {'name': 'polygon',
     'all_points_x': [6, 16, 24, 44, 69, 77, 81, 82, 80, 76, 69, 62, 51, 26, 9, 7],
     'all_points_y': [85, 77, 78, 83, 92, 100, 106, 115, 118, 120, 122, 125, 126, 112, 98, 92]},
    'region_attributes': {}}]
Angus
  • 3,680
  • 1
  • 12
  • 27
mohit guru
  • 35
  • 7

2 Answers2

1

You may iterate through your list, within which each element is a nested dictionary. Then, you can first get the key "shape_attributes", followed by "all_points_x" and "all_points_y". Since it looks like your list can also contain None elements, you need to check for this before trying to get keys from the dictionary.

shapes = [{'shape_attributes': {'name': 'polygon', 'all_points_x': [35, 28, 27, 31, 40, 51, 62, 72, 74, 71, 65, 57, 41], 'all_points_y': [74, 55, 32, 16, 4, 6, 12, 35, 56, 74, 83, 86, 81]}, 'region_attributes': {}}, None, {'shape_attributes': {'name': 'polygon', 'all_points_x': [6, 16, 24, 44, 69, 77, 81, 82, 80, 76, 69, 62, 51, 26, 9, 7], 'all_points_y': [85, 77, 78, 83, 92, 100, 106, 115, 118, 120, 122, 125, 126, 112, 98, 92]}, 'region_attributes': {}}]

for shape in shapes:
    if shape is not None:
        attributes = shape["shape_attributes"]
        print(attributes["all_points_x"])
        print(attributes["all_points_y"])
dspencer
  • 4,297
  • 4
  • 22
  • 43
  • @dispencer Thank you for your answer . Any suggestions on plotting points as polygons.? – mohit guru Apr 04 '20 at 10:18
  • This is a very different question; I suggest you do some research on this, then make a new question if your solution isn't working. – dspencer Apr 04 '20 at 10:19
1

One way is to iterate over your list which contains dictionary of objects with list of x and y cordinates, you can extract cordinates and put them in a list in tuple form like (x,y):

shapesCordinates = []
for shape in shapes:
    if shape is not None:
        x_cor = shape['shape_attributes']['all_points_x']
        y_cor = shape['shape_attributes']['all_points_y']
        for x, y in zip(x_cor, y_cor):
            shapesCordinates.append((x, y))
print(shapesCordinates)

Ouput:

[(35, 74), (28, 55), (27, 32), (31, 16), (40, 4), (51, 6), (62, 12), (72, 35), (74, 56), (71, 74), (65, 83), (57, 86), (41, 81), (6, 85), (16, 77), (24, 78), (44, 83), (69, 92), (77, 100), (81, 106), (82, 115), (80, 118), (76, 120), (69, 122), (62, 125), (51, 126), (26, 112), (9, 98), (7, 92)]
Sheri
  • 1,383
  • 3
  • 10
  • 26
  • Thank you @Sheri any suggestions on plotting it as polygon.I did plot x and y as polygon using the below code but here there are multiple points . – mohit guru Apr 04 '20 at 10:14
  • x1 =[42,43,44,59,70,83,93,101,109,113,117,122,123,124,124,120,120,114,112,92,84,75,64,58] y1=[190,202,203,192,178,162,148,139,125,113,102,88,79,71,65,60,56,57,60,72,80,84,94,97] coord2 =list(zip(x1,y1)) coord2.append(coord2[0]) xs1, ys1 = zip(*coord2) plt.figure() plt.plot(xs1,ys1) plt.show() – mohit guru Apr 04 '20 at 10:15
  • @mohitguru you should open new question for this, but for refrence see this may help you https://stackoverflow.com/questions/26935701/ploting-filled-polygons-in-python – Sheri Apr 04 '20 at 10:54