[Tutor] creating a subclass from superclass without __init__
eryksun
eryksun at gmail.com
Sat Aug 25 06:55:49 CEST 2012
On Fri, Aug 24, 2012 at 4:03 PM, Matt Gregory
<matt.gregory at oregonstate.edu> wrote:
>
> There are two classes of interest in GDAL - a Dataset which typically
> describes a geospatial raster file and Band which described a single band
> from the Dataset. gdal.Band just raises an AttributeError within __init__,
> but a gdal.Band instance can be created using
> gdal.Dataset.GetRasterBand(bandNum).
SWIG! Why'd it have it be SWIG? C++... very dangerous. You go first. ;)
GDAL Usage
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/val_repl.py
Proxy Classes
Driver
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L355
Dataset
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L647
Band
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L851
SWIG Interfaces
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Driver.i#L31
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Dataset.i#L308
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Band.i#L182
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/python/gdal_python.i#L260
_gdal module
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/extensions/gdal_wrap.cpp
Open, OpenShared, GetDriver, GetDriverByName, etc return proxies to
C++ shadow classes. It doesn't look like the SWIG interface was
written for extending the classes in Python (something called
"directors" according to the docs).
The proxies lack a callable __init__ because the C++ shadow classes
have private constructors. Instead, you have calls such as
Dataset.GetRasterBand, which calls _gdal.Dataset_GetRasterBand (i.e.
the C++ function _wrap_Dataset_GetRasterBand), which calls
GDALDatasetShadow_GetRasterBand, which calls GDALGetRasterBand. The
result gets cast to a GDALRasterBandShadow*. Finally, Python gets the
latter wrapped as a Band proxy.
> I had wanted to create additional methods on gdal.Band that would allow some
> GIS type operations (e.g. overloading __add__), thus I thought I would need
> a subclass of gdal.Band. So I was unsure of how to get an instance of my
> subclass when the only way I knew how to create an instance of the
> superclass (gdal.Band) was through the call to GetRasterBand().
I don't see an easy way to do this without modifying the SWIG
interface or monkey patching the Python classes. But my knowledge of
SWIG is superficial. You should ask this on a more appropriate forum,
or Stack Overflow.
More information about the Tutor
mailing list