1

I am attempting to create a function that creates a shapely Polygon object from either a list of point tuples (e.g. point_list = [(100,2), (32, 22), (70, 10.2)]) or Point objects (e.g. point_list = [point1, point2, point3], where point1 is defined as point1 = Point(100, 2), etc)

I understand that Polygon([[p.x, p.y] for p in point_list])) will create a Polygon object, which is why I can't figure out why the below code is giving >"TypeError: object of type 'Point' has no len()"

point_list = [point1, point2, point3]

def create_poly_geom(coords):
    assert type(coords) == type(list()), "Input should be a list"
    #assert len(coords) >= 3, "Polygon object requires at least 3 points"
    for element in coords:
        assert type(element) == type(Point()) or type(element) == type(tuple()), 
    "All list values should be Shapely Point objects"
    if type(coords) == type(list()):
        return Polygon(coords)
    else:
        return Polygon([[p.x, p.y] for p in coords])

print(create_poly_geom(point_list))

I get:

TypeError: object of type 'Point' has no len() when I should be getting a Polygon object

  • 1
    Which line are you getting the TypeError? I don't see any call to len() in above code. Also this condition "if type(coords) == type(list()):" is suspicious, you have already asserted this in line1 so else part will never get invoked. – Ashwinee K Jha Aug 02 '19 at 18:00
  • I will admit I am a relative beginner and the point of the first line is to check that the input argument is a list. The TypeError is coming from the package itself. File "shapely/speedups/_speedups.pyx", line 319, in shapely.speedups._speedups.geos_linearring_from_py TypeError: object of type 'Point' has no len() – geologist2010 Aug 02 '19 at 18:37
  • @AshwineeKJha TypeError is due to a bug. See this post for details: [How to create a Polygon given its Point vertices?](https://stackoverflow.com/q/30457089). From the OP's code I can see that they are aware of it and know how to deal with it. The problem is, as you already noticed, with the `if type(coords) == type(list())` line. It should check *elements* of the input list, but instead it checks the type of a *list itself*. I doubt this question will be useful for future reader, hence I vote to close it as "a problem that can no longer be reproduced". – Georgy Aug 02 '19 at 19:50
  • @geologist2010 Please, see here how to check the type of elements of a list: [Test type of elements python tuple/list](https://stackoverflow.com/questions/8964191/test-type-of-elements-python-tuple-list) – Georgy Aug 02 '19 at 19:51

0 Answers0