Python class is instantiated with a @classmethod but it is erroring that too many arguments are being passed to the classes __init__ method

As stated in the title, I have a class called TestLandCover which won’t instantiate using the from_geotiff @classmethod because it supposedly only takes 1 positional argument. However, the init method takes 6 arguments.

Here is the class:
`
import rasterio
from rasterio.mask import mask

class TestLandCover():
def init(self, raster, transform, crs, name, nodata):
self.raster = raster
self.transform = transform
self.crs = crs
self.name = name
self.nodata = nodata

@classmethod
def from_geotiff(cls, geotiff_path, name=None, nodata=np.nan, mask_gdf=None):
    
    with rasterio.open(geotiff_path) as src:
        crs = src.crs
        print(f'CRS line 418: {crs}')
        transform = src.transform

        if mask_gdf is not None:
            if 'geometry' in mask_gdf:
                geometries = mask_gdf.geometry.tolist()
            else:
                raise ValueError("The provided GeoDataFrame does not have a 'geometry' column.")
            
            # Mask the raster data using the provided geometries
            data, out_trans = mask(src, geometries, crop=True, nodata=nodata)
            
            # Ensure data is in a 2D array format (first band)
            data = data[0]
        else:
            data = src.read(1)
            out_trans = transform

    return cls(data, out_trans, crs, name, nodata)`

However when I instantiate the class like this:

landcover_test = TestLandCover.from_geotiff(geotiff_path='input_data/NELandcover/ukregion-northeastengland.tif', mask_gdf=area_of_interst.gdf, nodata=0, name='landcover_test')

I am receiving this error:

Traceback (most recent call last):
File “c:projecosys-oppo-modelmodel.py”, line 432, in
landcover_test = TestLandCover.from_geotiff(geotiff_path=’input_data/NELandcover/ukregion-northeastengland.tif’, mask_gdf=area_of_interst.gdf, nodata=0, name=’landcover_test’)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “c:projecosys-oppo-modelclasses.py”, line 670, in from_geotiff
return cls(data, out_trans, crs, name, nodata)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: TestLandCover.init() takes 1 positional argument but 6 were given

This problem arose after I refactored some code and interestingly, the old parent class which TestLandCover used to inherit from which has the same from_geotiff @classmethod does instatntiate correctly:

`class RasterData(DataLayer):
def init(self, raster=None, transform=None, crs=None, nodata=None, name=None):
print(f’Initializing RasterData with raster={raster}, transform={transform}, crs={crs}, nodata={nodata}, name={name}’)
#super().init()
self.raster = raster
self.transform = transform
self.crs = crs
self.nodata = nodata
self.name = name

@classmethod
def from_geotiff(cls, geotiff_path, name=None, nodata=np.nan, mask_gdf=None):
    
    with rasterio.open(geotiff_path) as src:
        crs = src.crs
        print(f'CRS line 418: {crs}')
        transform = src.transform
        print(f'Transform line 420: {transform}')

        if mask_gdf is not None:
            if 'geometry' in mask_gdf:
                geometries = mask_gdf.geometry.tolist()
            else:
                raise ValueError("The provided GeoDataFrame does not have a 'geometry' column.")
            
            # Mask the raster data using the provided geometries
            data, out_trans = mask(src, geometries, crop=True, nodata=nodata)
            print(f'out_trans: {out_trans}')
            
            # Ensure data is in a 2D array format (first band)
            data = data[0]
        else:
            data = src.read(1)
            out_trans = transform

    return cls(raster=data, transform=out_trans, crs=crs, name=name, nodata=nodata)

`

I can’t see any reason why that would work and yet the TestLandCover wouldn’t. To instantiate it I am just swapping out the TestLandCover for RasterData. RasterData works, TestLandCover doesn’t.

I’m beginning to think this is a bug but would appreciate anyone else’s input.

Also I have discovered that if I just define the lass in the Python 2 style like

class TestLandCover: def __init__(self, raster, transform, crs, name, nodata): self.raster = raster self.transform = transform self.crs = crs self.name = name self.nodata = nodata

It works but I don’t see why and I do want the class to inherit from RasterData which If I set up it doesn’t work again.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật