1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

gis: gdal: cleaned up the error module; got rid of relative imports in __init__; added the utility functions get_fields() and get_geoms() to Layer.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6059 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2007-09-07 04:52:02 +00:00
parent 85ea950759
commit ceac9c9645
3 changed files with 45 additions and 33 deletions

View File

@ -1,8 +1,8 @@
from driver import Driver
from envelope import Envelope
from datasource import DataSource
from srs import SpatialReference, CoordTransform
from geometries import OGRGeometry
from geomtype import OGRGeomType
from error import check_err, OGRException, SRSException
from django.contrib.gis.gdal.driver import Driver
from django.contrib.gis.gdal.envelope import Envelope
from django.contrib.gis.gdal.datasource import DataSource
from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
from django.contrib.gis.gdal.geometries import OGRGeometry
from django.contrib.gis.gdal.geomtype import OGRGeomType
from django.contrib.gis.gdal.error import check_err, OGRException, SRSException

View File

@ -1,35 +1,31 @@
# OGR Error Codes
OGRERR_NONE = 0
OGRERR_NOT_ENOUGH_DATA = 1
OGRERR_NOT_ENOUGH_MEMORY = 2
OGRERR_UNSUPPORTED_GEOMETRY_TYPE = 3
OGRERR_UNSUPPORTED_OPERATION = 4
OGRERR_CORRUPT_DATA = 5
OGRERR_FAILURE = 6
OGRERR_UNSUPPORTED_SRS = 7
"""
This module houses the OGR & SRS Exception objects, and the
check_err() routine which checks the status code returned by
OGR methods.
"""
# OGR & SRS Exceptions
class OGRException(Exception): pass
class SRSException(Exception): pass
def check_err(code, msg=False):
# OGR Error Codes
OGRERR_DICT = { 1 : (OGRException, 'Not enough data.'),
2 : (OGRException, 'Not enough memory.'),
3 : (OGRException, 'Unsupported geometry type.'),
4 : (OGRException, 'Unsupported operation.'),
5 : (OGRException, 'Corrupt data.'),
6 : (OGRException, 'OGR failure.'),
7 : (SRSException, 'Unsupported SRS.'),
}
OGRERR_NONE = 0
def check_err(code):
"Checks the given OGRERR, and raises an exception where appropriate."
if code == OGRERR_NONE:
return
elif code == OGRERR_NOT_ENOUGH_DATA:
raise OGRException, 'Not enough data!'
elif code == OGRERR_NOT_ENOUGH_MEMORY:
raise OGRException, 'Not enough memory!'
elif code == OGRERR_UNSUPPORTED_GEOMETRY_TYPE:
raise OGRException, 'Unsupported Geometry Type!'
elif code == OGRERR_UNSUPPORTED_OPERATION:
raise OGRException, 'Unsupported Operation!'
elif code == OGRERR_CORRUPT_DATA:
raise OGRException, 'Corrupt Data!'
elif code == OGRERR_FAILURE:
raise OGRException, 'OGR Failure!'
elif code == OGRERR_UNSUPPORTED_SRS:
raise SRSException, 'Unsupported SRS!'
elif code in OGRERR_DICT:
e, msg = OGRERR_DICT[code]
raise e, msg
else:
raise OGRException, 'Unknown error code: "%s"' % str(code)
raise OGRException, 'Unknown error code: "%s"' % code

View File

@ -112,3 +112,19 @@ class Layer(object):
return [ string_at(lgdal.OGR_Fld_GetNameRef(lgdal.OGR_FD_GetFieldDefn(self._ldefn, i)))
for i in xrange(self.num_fields) ]
#### Layer Methods ####
def get_fields(self, field_name):
"""Returns a list containing the given field name for every Feature
in the Layer."""
if not field_name in self.fields:
raise OGRException, 'invalid field name: %s' % field_name
return [feat.get(field_name) for feat in self]
def get_geoms(self, geos=False):
"""Returns a list containing the OGRGeometry for every Feature in
the Layer."""
if geos:
from django.contrib.gis.geos import fromstr
return [fromstr(feat.geom.wkt) for feat in self]
else:
return [feat.geom for feat in self]