1

I am new to shapely and python. I recently read Splitting Polygon by Linestring in GeoDjango? and below is the resulting answer.

from shapely import wkt
from shapely.ops import linemerge, unary_union, polygonize

POLY = "POLYGON ((34.67491149902344 31.59900710035676, 34.85000610351562 31.59900710035676, 34.85000610351562 31.73867905688433, 34.67491149902344 31.73867905688433, 34.67491149902344 31.59900710035676))"
LINE = "LINESTRING (34.64401245117188 31.63292168314889, 34.80812072753906 31.75911546882192)"

poly = wkt.loads(POLY)
line = wkt.loads(LINE)

merged = linemerge([poly.boundary, line])
borders = unary_union(merged)
polygons = polygonize(borders)
for p in polygons:
    print(p)

The print generates two polygon collections.

How do I access the arrays?

  • Which arrays do you mean? – lynxlynxlynx Nov 21 '17 at 7:54
1

To get xy of polygon's vertices, use .exterior.xy property as demonstrated in the following code:

...
for p in polygons:
    print(p)
    # access arrays of the coordinates of the polygon
    xs, ys = p.exterior.xy[0], p.exterior.xy[1]
    # print all of xy's
    for ix, pp in enumerate(xs):
        print(ix, "(x,y): ", xs[ix], ys[ix])
  • I just did a complete review of the output and while the previous method does give me access to the data it is not formatted in the same manner. See output below. How can all of the coordinates be associated to the appropriate polygon? – sfb Nov 21 '17 at 20:30
  • Print out from new code (0, '(x,y): ', 41.51176297290653, -93.5456) (1, '(x,y): ', 41.51163796057625, -93.54911146369494) (2, '(x,y): ', 41.50564282225992, -93.57068686588043) (3, '(x,y): ', 41.49761926709005, -93.60573262070558) (4, '(x,y): ', 41.49905167857268, -93.6253591194942) (5, '(x,y): ', 41.51345844605223, -93.66085639688252) (6, '(x,y): ', 41.513264462635, -93.678761) (7, '(x,y): ', 41.474608, -93.678761) (8, '(x,y): ', 41.474608, -93.5456) (9, '(x,y): ', 41.51176297290653, -93.5456) – sfb Nov 21 '17 at 20:57
  • POLYGON ((41.513264462635 -93.67876099999999, 41.51345844605223 -93.66085639688252, 41.49905167857268 -93.62535911949421, 41.49761926709005 -93.60573262070558, 41.50564282225992 -93.57068686588043, 41.51163796057625 -93.54911146369494, 41.51176297290653 -93.54559999999999, 41.55434 -93.54559999999999, 41.55434 -93.67876099999999, 41.513264462635 -93.67876099999999)) – sfb Nov 21 '17 at 20:58
  • POLYGON ((41.51176297290653 -93.54559999999999, 41.51163796057625 -93.54911146369494, 41.50564282225992 -93.57068686588043, 41.49761926709005 -93.60573262070558, 41.49905167857268 -93.62535911949421, 41.51345844605223 -93.66085639688252, 41.513264462635 -93.67876099999999, 41.474608 -93.67876099999999, 41.474608 -93.54559999999999, 41.51176297290653 -93.54559999999999)) – sfb Nov 21 '17 at 20:58
  • 1
    Please don't abuse comments for follow-up questions. Either edit your question or ask a new one. It is still not clear what you really want. – bugmenot123 Dec 8 '17 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.