Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding the Polygon class fails due to __new__() #2002

Open
NecPlusIA opened this issue Mar 4, 2024 · 3 comments
Open

Overriding the Polygon class fails due to __new__() #2002

NecPlusIA opened this issue Mar 4, 2024 · 3 comments

Comments

@NecPlusIA
Copy link

The Polygon class builder is defined as:
def __new__(self, shell=None, holes=None):

When I declare a new class inheriting from it, with a few more arguments, an exception occurs:
TypeError: Polygon.__new__() takes from 1 to 3 positional arguments but 4 were given

For instance, my code could be as simple as:

class MyClass(Polygon):
    def __init__(self, shell, holes, my_param_A, my_param_B):
        self.param_A = my_param_A
        self.param_B = my_param_B

I think the polygon builder should begin by:
def __new__(cls, shell=None, holes=None, *args, **kwargs):
but I'm no expert

(shapely v 2.0.1)

@NecPlusIA
Copy link
Author

Update: you can always work around the issue by adding

    def __new__(cls, shell=None, holes=None, *args, **kwargs):
        return Polygon.__new__(cls, shell, holes)

to MyClass

@jorisvandenbossche
Copy link
Member

Starting with shapely 2.0, subclassing indeed became more difficult / less supported. See #1698 and #1233 for related issues.

Note that while you might be able to workaround the __new__ issue, I am not sure that other operations and functions will preserve your custom attributes.

@jerygood870208
Copy link

jerygood870208 commented Apr 10, 2024

I'm designing a Python square class that inherits properties from Shapely's Polygon. Upon initialization, it only requires input of the center point and side length. Here is my code and the error I met, how to deal with it?
shapely 2.0.3 installed via pip

class SquareClass:
    def __init__(self, center, side_length):
        self.center = Point(center)
        self.side_length = side_length
        # Calculate the coordinates of the square's vertices
        half_side = side_length / 2
        x, y = center
        vertices = [(x - half_side, y - half_side),
                    (x + half_side, y - half_side),
                    (x + half_side, y + half_side),
                    (x - half_side, y + half_side)]
        super().__new__(vertices)
    def __new__(cls, shell=None, holes=None, *args, **kwargs):
        return Polygon.__new__(cls, shell, holes)

center = (0, 0)
side_length = 1
square = SquareClass(center, side_length)
print("Square area:", square.area)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

     [16] center = (0, 0)
     [17] side_length = 1
---> [18] SquareClass(center, side_length)
     [19] print("Square area:", square.area)


     [13] def __new__(cls, shell=None, holes=None, *args, **kwargs):
---> [14] return Polygon.__new__(cls, shell, holes)

File  Scripts\StationGA\.venv\lib\site-packages\shapely\geometry\polygon.py:230, in Polygon.__new__(self, shell, holes)
    [228] return shell
    [229] else:
--> [230] shell = LinearRing(shell)
    [232] if holes is not None:
    [233] if len(holes) == 0:
    [234] # shapely constructor cannot handle holes=[]

File Scripts\StationGA\.venv\lib\site-packages\shapely\geometry\polygon.py:93, in LinearRing.__new__(self, coordinates)
     [90] else:
     [91] return [float(c) for c in o]
---> [93] coordinates = np.array([_coords(o) for o in coordinates])
     [94] if not np.issubdtype(coordinates.dtype, np.number):
...
     [89] return o.coords[0]
     [90] else:
---> [91] return [float(c) for c in o]

TypeError: 'int' object is not iterable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants