1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

gis: gdal: fixed area function, place field pointer within class, integer and float fields return None when empty.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5754 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2007-07-24 03:20:42 +00:00
parent 8f644283ee
commit 179a2c45c3
2 changed files with 14 additions and 13 deletions

View File

@ -10,11 +10,11 @@ from django.contrib.gis.gdal.error import OGRException
class Field(object): class Field(object):
"A class that wraps an OGR Field, needs to be instantiated from a Feature object." "A class that wraps an OGR Field, needs to be instantiated from a Feature object."
_fld = 0 # Initially NULL
#### Python 'magic' routines #### #### Python 'magic' routines ####
def __init__(self, fld, val=''): def __init__(self, fld, val=''):
"Needs a C pointer (Python integer in ctypes) in order to initialize." "Needs a C pointer (Python integer in ctypes) in order to initialize."
self._fld = 0 # Initially NULL
if not fld: if not fld:
raise OGRException, 'Cannot create OGR Field, invalid pointer given.' raise OGRException, 'Cannot create OGR Field, invalid pointer given.'
self._fld = fld self._fld = fld
@ -51,23 +51,19 @@ class OFTInteger(Field):
try: try:
return int(self._val) return int(self._val)
except ValueError: except ValueError:
return 0 return None
class OFTIntegerList(Field): pass class OFTIntegerList(Field): pass
class OFTReal(Field): class OFTReal(Field):
@property @property
def value(self): def value(self):
"Returns a float contained in this field." "Returns a float contained in this field."
try: try:
return float(self._val) return float(self._val)
except ValueError: except ValueError:
#FIXME: 0? None? return None
return 0
class OFTRealList(Field): pass class OFTRealList(Field): pass
class OFTString(Field): pass class OFTString(Field): pass
class OFTStringList(Field): pass class OFTStringList(Field): pass
class OFTWideString(Field): pass class OFTWideString(Field): pass

View File

@ -55,16 +55,22 @@ from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
# #
# The OGR_G_* routines are relevant here. # The OGR_G_* routines are relevant here.
#### ctypes prototypes #### #### ctypes prototypes for functions that return double values ####
def pnt_func(f): def pnt_func(f):
"For accessing point information." "For accessing point information."
f.restype = c_double f.restype = c_double
f.argtypes = [c_void_p, c_int] f.argtypes = [c_void_p, c_int]
return f return f
# GetX, GetY, GetZ all return doubles.
getx = pnt_func(lgdal.OGR_G_GetX) getx = pnt_func(lgdal.OGR_G_GetX)
gety = pnt_func(lgdal.OGR_G_GetY) gety = pnt_func(lgdal.OGR_G_GetY)
getz = pnt_func(lgdal.OGR_G_GetZ) getz = pnt_func(lgdal.OGR_G_GetZ)
# GetArea returns a double.
get_area = lgdal.OGR_G_GetArea
get_area.restype = c_double
get_area.argtypes = [c_void_p]
#### OGRGeometry Class #### #### OGRGeometry Class ####
class OGRGeometryIndexError(OGRException, KeyError): class OGRGeometryIndexError(OGRException, KeyError):
"""This exception is raised when an invalid index is encountered, and has """This exception is raised when an invalid index is encountered, and has
@ -211,8 +217,7 @@ class OGRGeometry(object):
@property @property
def area(self): def area(self):
"Returns the area for a LinearRing, Polygon, or MultiPolygon; 0 otherwise." "Returns the area for a LinearRing, Polygon, or MultiPolygon; 0 otherwise."
a = lgdal.OGR_G_GetArea(self._g) return get_area(self._g)
return a.value
@property @property
def envelope(self): def envelope(self):