4

我专注django.contrib.gis.geos.Point于更好地创建和处理具有明确纬度和经度的点。

我当前的代码:

from django.contrib.gis.geos import Point

class LLPoint(Point):
    def __init__(self, *args, **kwargs):
        lat = kwargs.get('latitude')
        lng = kwargs.get('longitude')

        if lat and lng:
            super(LLPoint, self).__init__(lng, lat)
        elif lat or lng:
            raise TypeError(u'You must both declare latitude and longitude, '
                'not just one of them.')
        else:
            super(LLPoint, self).__init__(*args, **kwargs)

    def __unicode__(self):
        c = self.coordinates()
        return u'LLPoint Lat: %.5f Lng: %.5f' % (c['latitude'], c['longitude']) 

    def coordinates(self):
        return {
            'latitude': self.coords[1], 
            'longitude': self.coords[0]
        }

问题是:

>>> LLPoint(latitude=10.0, longitude=20.0)
<Point object at 0xdeadbeef>

为什么它返回一个Point对象?这样我就不能使用我在子类中声明的任何东西。如果我尝试用存根类重现问题,它会起作用。这可能很愚蠢,但我看不到。

4

1 回答 1

6

Point.__init__来电

    super(Point, self).__init__(point, srid=srid)

哪来GEOSGeometry.__init__的,哪来的

    self._post_init(srid)

执行

    self.__class__ = GEOS_CLASSES[self.geom_typeid]

这将类更改selfPoint。邪恶的。


你可以试试:

class LLPoint(Point):
    def __init__(self, *args, **kwargs):
        ...
        super(LLPoint, self).__init__(lng, lat)   # this changes self.__class__ to Point
        ...
        self.__class__ = LLPoint
于 2012-12-03T03:40:30.293 回答