I want to do something like the following:

class SubClassOfSpatialReference(arcpy_spatial_reference_object):
    def __init__(self, input_data):

... etc.

Can I even do this? If so, what exactly is the parent object that I need to provide in the class definition? If you type() a spatial reference object, you get arcpy.arcobjects.arcobjects.SpatialReference. I tried that and got AttributeError: 'module' object has no attribute 'arcobjects'. No surprise, really. I imagine that there is also a problem with the fact that the arcpy SpatialReference class invocation takes an argument of a WKID, spatial reference name, or feature dataset from which to copy the spatial reference; there isn't any way to create a new, blank spatial reference object.

Here's another thought: can one subclass an instance, as in:

class SubClassOfSpatialReferenceInstance(arcpy.SpatialReference(54004):

starting in this case with the World_Mercator spatial reference?

I am obviously out of my immediate sphere of knowledge about such things, so I thank anyone who can point me in the right direction or give advice. What I am ultimately after is a convenient interface for a modifiable spatial reference. Contrary to ESRI documentation of the arcpy SpatialReference class, a majority of the properties are read-only. I figured that, if I could subclass the arcpy Spatial Reference class, then I could override the name(), standardParallel1(), etc. attributes and use WKT string manipulation to make the spatial reference modifiable. I suppose I could make a generic class that deals only with the WKT and implements a CreateSR() method that uses the loadFromString() method to make a spatial reference after all the properties have been set, but it just wouldn't be as slick.

  • Are those statments still valid? I tried with Arcpy Desktop on ArcGIS 10.2.2 and python 2.7 and it doesn't work. ArcGIS gives me and error: >>> sr = WeirdSpatialReferenceSubclass() Traceback (most recent call last): File "<pyshell#39>", line 1, in <module> sr = WeirdSpatialReferenceSubclass() File "<pyshell#38>", line 4, in init super(WeirdSpatialReferenceSubclass, self).__init__(54004) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 925, in init _BaseArcObject.__init__(self) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\ – Gabriel Asato Jun 10 '16 at 15:07
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – John Powell Jun 10 '16 at 15:32

You normally get to the SpatialReference class through arcpy, so you'd probably want to do that. And you can't subclass in that way, you have to put the overridden constructor call in your own init:

class WeirdSpatialReferenceSubclass(arcpy.SpatialReference):
    def __init__(self):
        # Force initialize to 54004
        super(WeirdSpatialReferenceSubclass, self).__init__(54004)
    def myPropertyX(self):
        s = self.exportToString()
        # Find some interesting property in s and assign to value
        return value
    def myPropertyX(self, new_value):
        s = self.exportToString()
        # Do some interesting edit of s and assign to new_string
  • Exactly what I was after. Very cool! BTW, I was also getting 'can't set attribute' errors with the initializing of instance attributes, because I was using the older Python 2.5 property decorator syntax with fget and fset as inner functions, which doesn't use the newer setattr, getattr, and delattr attributes. I guess I have some catching up to do. – celticflute Nov 18 '13 at 15:20
  • ... that is, of course, the setter, getter, and deleter attributes. Sorry. – celticflute Nov 18 '13 at 15:27

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.