Fixed #21270 -- Fixed E701 pep8 warnings

This commit is contained in:
Alasdair Nicol 2013-10-17 09:17:41 +01:00
parent 650b6fd90e
commit eb214452c3
78 changed files with 625 additions and 285 deletions

View File

@ -198,8 +198,10 @@ class ChangeList(six.with_metaclass(RenameChangeListMethods)):
six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2]) six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2])
def get_query_string(self, new_params=None, remove=None): def get_query_string(self, new_params=None, remove=None):
if new_params is None: new_params = {} if new_params is None:
if remove is None: remove = [] new_params = {}
if remove is None:
remove = []
p = self.params.copy() p = self.params.copy()
for r in remove: for r in remove:
for k in list(p): for k in list(p):

View File

@ -94,15 +94,19 @@ ROLES = {
def create_reference_role(rolename, urlbase): def create_reference_role(rolename, urlbase):
def _role(name, rawtext, text, lineno, inliner, options=None, content=None): def _role(name, rawtext, text, lineno, inliner, options=None, content=None):
if options is None: options = {} if options is None:
if content is None: content = [] options = {}
if content is None:
content = []
node = docutils.nodes.reference(rawtext, text, refuri=(urlbase % (inliner.document.settings.link_base, text.lower())), **options) node = docutils.nodes.reference(rawtext, text, refuri=(urlbase % (inliner.document.settings.link_base, text.lower())), **options)
return [node], [] return [node], []
docutils.parsers.rst.roles.register_canonical_role(rolename, _role) docutils.parsers.rst.roles.register_canonical_role(rolename, _role)
def default_reference_role(name, rawtext, text, lineno, inliner, options=None, content=None): def default_reference_role(name, rawtext, text, lineno, inliner, options=None, content=None):
if options is None: options = {} if options is None:
if content is None: content = [] options = {}
if content is None:
content = []
context = inliner.document.settings.default_reference_context context = inliner.document.settings.default_reference_context
node = docutils.nodes.reference(rawtext, text, refuri=(ROLES[context] % (inliner.document.settings.link_base, text.lower())), **options) node = docutils.nodes.reference(rawtext, text, refuri=(ROLES[context] % (inliner.document.settings.link_base, text.lower())), **options)
return [node], [] return [node], []

View File

@ -70,8 +70,10 @@ class GeoModelAdmin(ModelAdmin):
""" """
is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION') is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION')
if is_collection: if is_collection:
if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any' if db_field.geom_type == 'GEOMETRYCOLLECTION':
else: collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', '')) collection_type = 'Any'
else:
collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', ''))
else: else:
collection_type = 'None' collection_type = 'None'

View File

@ -20,7 +20,8 @@ class OpenLayersWidget(Textarea):
""" """
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
# Update the template parameters with any attributes passed in. # Update the template parameters with any attributes passed in.
if attrs: self.params.update(attrs) if attrs:
self.params.update(attrs)
# Defaulting the WKT value to a blank string -- this # Defaulting the WKT value to a blank string -- this
# will be tested in the JavaScript and the appropriate # will be tested in the JavaScript and the appropriate

View File

@ -201,8 +201,10 @@ class SpatialRefSysMixin(object):
return self.srs.ellipsoid return self.srs.ellipsoid
else: else:
m = self.spheroid_regex.match(self.wkt) m = self.spheroid_regex.match(self.wkt)
if m: return (float(m.group('major')), float(m.group('flattening'))) if m:
else: return None return (float(m.group('major')), float(m.group('flattening')))
else:
return None
@property @property
def name(self): def name(self):

View File

@ -72,7 +72,8 @@ class PostGISIntrospection(DatabaseIntrospection):
'WHERE "f_table_name"=%s AND "f_geometry_column"=%s', 'WHERE "f_table_name"=%s AND "f_geometry_column"=%s',
(table_name, geo_col)) (table_name, geo_col))
row = cursor.fetchone() row = cursor.fetchone()
if not row: raise GeoIntrospectionError if not row:
raise GeoIntrospectionError
except GeoIntrospectionError: except GeoIntrospectionError:
if self.connection.ops.geography: if self.connection.ops.geography:
cursor.execute('SELECT "coord_dimension", "srid", "type" ' cursor.execute('SELECT "coord_dimension", "srid", "type" '

View File

@ -453,8 +453,10 @@ class PostGISOperations(DatabaseOperations, BaseSpatialOperations):
Helper routine that returns a boolean indicating whether the number of Helper routine that returns a boolean indicating whether the number of
parameters is correct for the lookup type. parameters is correct for the lookup type.
""" """
def exactly_two(np): return np == 2 def exactly_two(np):
def two_to_three(np): return np >= 2 and np <=3 return np == 2
def two_to_three(np):
return np >= 2 and np <=3
if (lookup_type in self.distance_functions and if (lookup_type in self.distance_functions and
lookup_type != 'dwithin'): lookup_type != 'dwithin'):
return two_to_three(num_param) return two_to_three(num_param)

View File

@ -275,12 +275,14 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
version = None version = None
try: try:
tmp = self._get_spatialite_func("X(GeomFromText('POINT(1 1)'))") tmp = self._get_spatialite_func("X(GeomFromText('POINT(1 1)'))")
if tmp == 1.0: version = '2.3.0' if tmp == 1.0:
version = '2.3.0'
except DatabaseError: except DatabaseError:
pass pass
# If no version string defined, then just re-raise the original # If no version string defined, then just re-raise the original
# exception. # exception.
if version is None: raise if version is None:
raise
m = self.version_regex.match(version) m = self.version_regex.match(version)
if m: if m:
@ -301,7 +303,8 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
if not self.check_aggregate_support(agg): if not self.check_aggregate_support(agg):
raise NotImplementedError('%s spatial aggregate is not implmented for this backend.' % agg_name) raise NotImplementedError('%s spatial aggregate is not implmented for this backend.' % agg_name)
agg_name = agg_name.lower() agg_name = agg_name.lower()
if agg_name == 'union': agg_name += 'agg' if agg_name == 'union':
agg_name += 'agg'
sql_template = self.select % '%(function)s(%(field)s)' sql_template = self.select % '%(function)s(%(field)s)'
sql_function = getattr(self, agg_name) sql_function = getattr(self, agg_name)
return sql_template, sql_function return sql_template, sql_function

View File

@ -54,7 +54,8 @@ class GeometryProxy(object):
# general GeometryField is used. # general GeometryField is used.
if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'): if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
# Assigning the SRID to the geometry. # Assigning the SRID to the geometry.
if value.srid is None: value.srid = self._field.srid if value.srid is None:
value.srid = self._field.srid
elif value is None or isinstance(value, six.string_types + (memoryview,)): elif value is None or isinstance(value, six.string_types + (memoryview,)):
# Set with None, WKT, HEX, or WKB # Set with None, WKT, HEX, or WKB
pass pass

View File

@ -156,14 +156,20 @@ class GeoQuerySet(QuerySet):
# PostGIS we're using. SpatiaLite only uses the first group of options. # PostGIS we're using. SpatiaLite only uses the first group of options.
if backend.spatial_version >= (1, 4, 0): if backend.spatial_version >= (1, 4, 0):
options = 0 options = 0
if crs and bbox: options = 3 if crs and bbox:
elif bbox: options = 1 options = 3
elif crs: options = 2 elif bbox:
options = 1
elif crs:
options = 2
else: else:
options = 0 options = 0
if crs and bbox: options = 3 if crs and bbox:
elif crs: options = 1 options = 3
elif bbox: options = 2 elif crs:
options = 1
elif bbox:
options = 2
s = {'desc' : 'GeoJSON', s = {'desc' : 'GeoJSON',
'procedure_args' : {'precision' : precision, 'options' : options}, 'procedure_args' : {'precision' : precision, 'options' : options},
'procedure_fmt' : '%(geo_col)s,%(precision)s,%(options)s', 'procedure_fmt' : '%(geo_col)s,%(precision)s,%(options)s',
@ -441,7 +447,8 @@ class GeoQuerySet(QuerySet):
# Does the spatial backend support this? # Does the spatial backend support this?
connection = connections[self.db] connection = connections[self.db]
func = getattr(connection.ops, att, False) func = getattr(connection.ops, att, False)
if desc is None: desc = att if desc is None:
desc = att
if not func: if not func:
raise NotImplementedError('%s stored procedure not available on ' raise NotImplementedError('%s stored procedure not available on '
'the %s backend.' % 'the %s backend.' %
@ -489,7 +496,8 @@ class GeoQuerySet(QuerySet):
# Adding any keyword parameters for the Aggregate object. Oracle backends # Adding any keyword parameters for the Aggregate object. Oracle backends
# in particular need an additional `tolerance` parameter. # in particular need an additional `tolerance` parameter.
agg_kwargs = {} agg_kwargs = {}
if connections[self.db].ops.oracle: agg_kwargs['tolerance'] = tolerance if connections[self.db].ops.oracle:
agg_kwargs['tolerance'] = tolerance
# Calling the QuerySet.aggregate, and returning only the value of the aggregate. # Calling the QuerySet.aggregate, and returning only the value of the aggregate.
return self.aggregate(geoagg=aggregate(agg_col, **agg_kwargs))['geoagg'] return self.aggregate(geoagg=aggregate(agg_col, **agg_kwargs))['geoagg']
@ -533,12 +541,14 @@ class GeoQuerySet(QuerySet):
if settings.get('setup', True): if settings.get('setup', True):
default_args, geo_field = self._spatial_setup(att, desc=settings['desc'], field_name=field_name, default_args, geo_field = self._spatial_setup(att, desc=settings['desc'], field_name=field_name,
geo_field_type=settings.get('geo_field_type', None)) geo_field_type=settings.get('geo_field_type', None))
for k, v in six.iteritems(default_args): settings['procedure_args'].setdefault(k, v) for k, v in six.iteritems(default_args):
settings['procedure_args'].setdefault(k, v)
else: else:
geo_field = settings['geo_field'] geo_field = settings['geo_field']
# The attribute to attach to the model. # The attribute to attach to the model.
if not isinstance(model_att, six.string_types): model_att = att if not isinstance(model_att, six.string_types):
model_att = att
# Special handling for any argument that is a geometry. # Special handling for any argument that is a geometry.
for name in settings['geom_args']: for name in settings['geom_args']:

View File

@ -166,7 +166,8 @@ class GeoSQLCompiler(compiler.SQLCompiler):
# doing pagination with Oracle. # doing pagination with Oracle.
rn_offset = 0 rn_offset = 0
if self.connection.ops.oracle: if self.connection.ops.oracle:
if self.query.high_mark is not None or self.query.low_mark: rn_offset = 1 if self.query.high_mark is not None or self.query.low_mark:
rn_offset = 1
index_start = rn_offset + len(aliases) index_start = rn_offset + len(aliases)
# Converting any extra selection values (e.g., geometries and # Converting any extra selection values (e.g., geometries and
@ -243,7 +244,8 @@ class GeoSQLCompiler(compiler.SQLCompiler):
used. If `column` is specified, it will be used instead of the value used. If `column` is specified, it will be used instead of the value
in `field.column`. in `field.column`.
""" """
if table_alias is None: table_alias = self.query.get_meta().db_table if table_alias is None:
table_alias = self.query.get_meta().db_table
return "%s.%s" % (self.quote_name_unless_alias(table_alias), return "%s.%s" % (self.quote_name_unless_alias(table_alias),
self.connection.ops.quote_name(column or field.column)) self.connection.ops.quote_name(column or field.column))

View File

@ -113,7 +113,8 @@ class GeoQuery(sql.Query):
if field_name is None: if field_name is None:
# Incrementing until the first geographic field is found. # Incrementing until the first geographic field is found.
for fld in self.model._meta.fields: for fld in self.model._meta.fields:
if isinstance(fld, GeometryField): return fld if isinstance(fld, GeometryField):
return fld
return False return False
else: else:
# Otherwise, check by the given field name -- which may be # Otherwise, check by the given field name -- which may be

View File

@ -58,7 +58,8 @@ class GeoFeedMixin(object):
raise ValueError('Only should be 2 or 4 numeric elements.') raise ValueError('Only should be 2 or 4 numeric elements.')
# If a GeoRSS box was given via tuple. # If a GeoRSS box was given via tuple.
if not box_coords is None: if not box_coords is None:
if w3c_geo: raise ValueError('Cannot use simple GeoRSS box in W3C Geo feeds.') if w3c_geo:
raise ValueError('Cannot use simple GeoRSS box in W3C Geo feeds.')
handler.addQuickElement('georss:box', self.georss_coords(box_coords)) handler.addQuickElement('georss:box', self.georss_coords(box_coords))
else: else:
# Getting the lower-case geometry type. # Getting the lower-case geometry type.
@ -66,7 +67,8 @@ class GeoFeedMixin(object):
if gtype == 'point': if gtype == 'point':
self.add_georss_point(handler, geom.coords, w3c_geo=w3c_geo) self.add_georss_point(handler, geom.coords, w3c_geo=w3c_geo)
else: else:
if w3c_geo: raise ValueError('W3C Geo only supports Point geometries.') if w3c_geo:
raise ValueError('W3C Geo only supports Point geometries.')
# For formatting consistent w/the GeoRSS simple standard: # For formatting consistent w/the GeoRSS simple standard:
# http://georss.org/1.0#simple # http://georss.org/1.0#simple
if gtype in ('linestring', 'linearring'): if gtype in ('linestring', 'linearring'):

View File

@ -19,8 +19,10 @@ class GDALBase(object):
# Raise an exception if the pointer isn't valid don't # Raise an exception if the pointer isn't valid don't
# want to be passing NULL pointers to routines -- # want to be passing NULL pointers to routines --
# that's very bad. # that's very bad.
if self._ptr: return self._ptr if self._ptr:
else: raise GDALException('GDAL %s pointer no longer valid.' % self.__class__.__name__) return self._ptr
else:
raise GDALException('GDAL %s pointer no longer valid.' % self.__class__.__name__)
def _set_ptr(self, ptr): def _set_ptr(self, ptr):
# Only allow the pointer to be set with pointers of the # Only allow the pointer to be set with pointers of the

View File

@ -95,7 +95,8 @@ class DataSource(GDALBase):
def __del__(self): def __del__(self):
"Destroys this DataStructure object." "Destroys this DataStructure object."
if self._ptr: capi.destroy_ds(self._ptr) if self._ptr:
capi.destroy_ds(self._ptr)
def __iter__(self): def __iter__(self):
"Allows for iteration over the layers in a data source." "Allows for iteration over the layers in a data source."
@ -106,7 +107,8 @@ class DataSource(GDALBase):
"Allows use of the index [] operator to get a layer at the index." "Allows use of the index [] operator to get a layer at the index."
if isinstance(index, six.string_types): if isinstance(index, six.string_types):
l = capi.get_layer_by_name(self.ptr, force_bytes(index)) l = capi.get_layer_by_name(self.ptr, force_bytes(index))
if not l: raise OGRIndexError('invalid OGR Layer name given: "%s"' % index) if not l:
raise OGRIndexError('invalid OGR Layer name given: "%s"' % index)
elif isinstance(index, int): elif isinstance(index, int):
if index < 0 or index >= self.layer_count: if index < 0 or index >= self.layer_count:
raise OGRIndexError('index out of range') raise OGRIndexError('index out of range')

View File

@ -59,7 +59,8 @@ class Driver(GDALBase):
"Attempts to register all the data source drivers." "Attempts to register all the data source drivers."
# Only register all if the driver count is 0 (or else all drivers # Only register all if the driver count is 0 (or else all drivers
# will be registered over and over again) # will be registered over and over again)
if not self.driver_count: capi.register_all() if not self.driver_count:
capi.register_all()
# Driver properties # Driver properties
@property @property

View File

@ -4,9 +4,15 @@
OGR methods. OGR methods.
""" """
#### OGR & SRS Exceptions #### #### OGR & SRS Exceptions ####
class GDALException(Exception): pass class GDALException(Exception):
class OGRException(Exception): pass pass
class SRSException(Exception): pass
class OGRException(Exception):
pass
class SRSException(Exception):
pass
class OGRIndexError(OGRException, KeyError): class OGRIndexError(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

View File

@ -33,7 +33,8 @@ class Feature(GDALBase):
def __del__(self): def __del__(self):
"Releases a reference to this object." "Releases a reference to this object."
if self._ptr: capi.destroy_feature(self._ptr) if self._ptr:
capi.destroy_feature(self._ptr)
def __getitem__(self, index): def __getitem__(self, index):
""" """

View File

@ -132,9 +132,14 @@ class OFTReal(Field):
return self.as_double() return self.as_double()
# String & Binary fields, just subclasses # String & Binary fields, just subclasses
class OFTString(Field): pass class OFTString(Field):
class OFTWideString(Field): pass pass
class OFTBinary(Field): pass
class OFTWideString(Field):
pass
class OFTBinary(Field):
pass
# OFTDate, OFTTime, OFTDateTime fields. # OFTDate, OFTTime, OFTDateTime fields.
class OFTDate(Field): class OFTDate(Field):
@ -172,10 +177,17 @@ class OFTTime(Field):
return None return None
# List fields are also just subclasses # List fields are also just subclasses
class OFTIntegerList(Field): pass class OFTIntegerList(Field):
class OFTRealList(Field): pass pass
class OFTStringList(Field): pass
class OFTWideStringList(Field): pass class OFTRealList(Field):
pass
class OFTStringList(Field):
pass
class OFTWideStringList(Field):
pass
# Class mapping dictionary for OFT Types and reverse mapping. # Class mapping dictionary for OFT Types and reverse mapping.
OGRFieldTypes = { OGRFieldTypes = {

View File

@ -122,14 +122,16 @@ class OGRGeometry(GDALBase):
self.ptr = g self.ptr = g
# Assigning the SpatialReference object to the geometry, if valid. # Assigning the SpatialReference object to the geometry, if valid.
if bool(srs): self.srs = srs if bool(srs):
self.srs = srs
# Setting the class depending upon the OGR Geometry Type # Setting the class depending upon the OGR Geometry Type
self.__class__ = GEO_CLASSES[self.geom_type.num] self.__class__ = GEO_CLASSES[self.geom_type.num]
def __del__(self): def __del__(self):
"Deletes this Geometry." "Deletes this Geometry."
if self._ptr: capi.destroy_geom(self._ptr) if self._ptr:
capi.destroy_geom(self._ptr)
# Pickle routines # Pickle routines
def __getstate__(self): def __getstate__(self):
@ -143,7 +145,8 @@ class OGRGeometry(GDALBase):
def __setstate__(self, state): def __setstate__(self, state):
wkb, srs = state wkb, srs = state
ptr = capi.from_wkb(wkb, None, byref(c_void_p()), len(wkb)) ptr = capi.from_wkb(wkb, None, byref(c_void_p()), len(wkb))
if not ptr: raise OGRException('Invalid OGRGeometry loaded from pickled state.') if not ptr:
raise OGRException('Invalid OGRGeometry loaded from pickled state.')
self.ptr = ptr self.ptr = ptr
self.srs = srs self.srs = srs
@ -285,7 +288,8 @@ class OGRGeometry(GDALBase):
# The SRID property # The SRID property
def _get_srid(self): def _get_srid(self):
srs = self.srs srs = self.srs
if srs: return srs.srid if srs:
return srs.srid
return None return None
def _set_srid(self, srid): def _set_srid(self, srid):
@ -602,7 +606,8 @@ class LineString(OGRGeometry):
return self._listarr(capi.getz) return self._listarr(capi.getz)
# LinearRings are used in Polygons. # LinearRings are used in Polygons.
class LinearRing(LineString): pass class LinearRing(LineString):
pass
class Polygon(OGRGeometry): class Polygon(OGRGeometry):
@ -673,7 +678,8 @@ class GeometryCollection(OGRGeometry):
"Add the geometry to this Geometry Collection." "Add the geometry to this Geometry Collection."
if isinstance(geom, OGRGeometry): if isinstance(geom, OGRGeometry):
if isinstance(geom, self.__class__): if isinstance(geom, self.__class__):
for g in geom: capi.add_geom(self.ptr, g.ptr) for g in geom:
capi.add_geom(self.ptr, g.ptr)
else: else:
capi.add_geom(self.ptr, geom.ptr) capi.add_geom(self.ptr, geom.ptr)
elif isinstance(geom, six.string_types): elif isinstance(geom, six.string_types):
@ -695,9 +701,14 @@ class GeometryCollection(OGRGeometry):
coords = tuple coords = tuple
# Multiple Geometry types. # Multiple Geometry types.
class MultiPoint(GeometryCollection): pass class MultiPoint(GeometryCollection):
class MultiLineString(GeometryCollection): pass pass
class MultiPolygon(GeometryCollection): pass
class MultiLineString(GeometryCollection):
pass
class MultiPolygon(GeometryCollection):
pass
# Class mapping dictionary (using the OGRwkbGeometryType as the key) # Class mapping dictionary (using the OGRwkbGeometryType as the key)
GEO_CLASSES = {1 : Point, GEO_CLASSES = {1 : Point,

View File

@ -36,7 +36,8 @@ class OGRGeomType(object):
num = type_input.num num = type_input.num
elif isinstance(type_input, six.string_types): elif isinstance(type_input, six.string_types):
type_input = type_input.lower() type_input = type_input.lower()
if type_input == 'geometry': type_input='unknown' if type_input == 'geometry':
type_input='unknown'
num = self._str_types.get(type_input, None) num = self._str_types.get(type_input, None)
if num is None: if num is None:
raise OGRException('Invalid OGR String Type "%s"' % type_input) raise OGRException('Invalid OGR String Type "%s"' % type_input)

View File

@ -47,7 +47,8 @@ class Layer(GDALBase):
# An integer index was given -- we cannot do a check based on the # An integer index was given -- we cannot do a check based on the
# number of features because the beginning and ending feature IDs # number of features because the beginning and ending feature IDs
# are not guaranteed to be 0 and len(layer)-1, respectively. # are not guaranteed to be 0 and len(layer)-1, respectively.
if index < 0: raise OGRIndexError('Negative indices are not allowed on OGR Layers.') if index < 0:
raise OGRIndexError('Negative indices are not allowed on OGR Layers.')
return self._make_feature(index) return self._make_feature(index)
elif isinstance(index, slice): elif isinstance(index, slice):
# A slice was given # A slice was given
@ -88,7 +89,8 @@ class Layer(GDALBase):
# Random access isn't supported, have to increment through # Random access isn't supported, have to increment through
# each feature until the given feature ID is encountered. # each feature until the given feature ID is encountered.
for feat in self: for feat in self:
if feat.fid == feat_id: return feat if feat.fid == feat_id:
return feat
# Should have returned a Feature, raise an OGRIndexError. # Should have returned a Feature, raise an OGRIndexError.
raise OGRIndexError('Invalid feature id: %s.' % feat_id) raise OGRIndexError('Invalid feature id: %s.' % feat_id)

View File

@ -36,7 +36,8 @@ else:
if lib_names: if lib_names:
for lib_name in lib_names: for lib_name in lib_names:
lib_path = find_library(lib_name) lib_path = find_library(lib_name)
if not lib_path is None: break if not lib_path is None:
break
if lib_path is None: if lib_path is None:
raise OGRException('Could not find the GDAL library (tried "%s"). ' raise OGRException('Could not find the GDAL library (tried "%s"). '
@ -83,7 +84,8 @@ version_regex = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<subminor>\d+)
def gdal_version_info(): def gdal_version_info():
ver = gdal_version().decode() ver = gdal_version().decode()
m = version_regex.match(ver) m = version_regex.match(ver)
if not m: raise OGRException('Could not parse GDAL version string "%s"' % ver) if not m:
raise OGRException('Could not parse GDAL version string "%s"' % ver)
return dict((key, m.group(key)) for key in ('major', 'minor', 'subminor')) return dict((key, m.group(key)) for key in ('major', 'minor', 'subminor'))
_verinfo = gdal_version_info() _verinfo = gdal_version_info()

View File

@ -19,8 +19,10 @@ def ptr_byref(args, offset=-1):
def check_bool(result, func, cargs): def check_bool(result, func, cargs):
"Returns the boolean evaluation of the value." "Returns the boolean evaluation of the value."
if bool(result): return True if bool(result):
else: return False return True
else:
return False
### String checking Routines ### ### String checking Routines ###
def check_const_string(result, func, cargs, offset=None): def check_const_string(result, func, cargs, offset=None):
@ -46,8 +48,10 @@ def check_string(result, func, cargs, offset=-1, str_result=False):
if str_result: if str_result:
# For routines that return a string. # For routines that return a string.
ptr = result ptr = result
if not ptr: s = None if not ptr:
else: s = string_at(result) s = None
else:
s = string_at(result)
else: else:
# Error-code return specified. # Error-code return specified.
check_err(result) check_err(result)
@ -56,7 +60,8 @@ def check_string(result, func, cargs, offset=-1, str_result=False):
s = ptr.value s = ptr.value
# Correctly freeing the allocated memory beind GDAL pointer # Correctly freeing the allocated memory beind GDAL pointer
# w/the VSIFree routine. # w/the VSIFree routine.
if ptr: lgdal.VSIFree(ptr) if ptr:
lgdal.VSIFree(ptr)
return s return s
### DataSource, Layer error-checking ### ### DataSource, Layer error-checking ###

View File

@ -15,8 +15,10 @@ def double_output(func, argtypes, errcheck=False, strarg=False):
"Generates a ctypes function that returns a double value." "Generates a ctypes function that returns a double value."
func.argtypes = argtypes func.argtypes = argtypes
func.restype = c_double func.restype = c_double
if errcheck: func.errcheck = check_arg_errcode if errcheck:
if strarg: func.errcheck = check_str_arg func.errcheck = check_arg_errcode
if strarg:
func.errcheck = check_str_arg
return func return func
def geom_output(func, argtypes, offset=None): def geom_output(func, argtypes, offset=None):
@ -106,7 +108,8 @@ def void_output(func, argtypes, errcheck=True):
For functions that don't only return an error code that needs to For functions that don't only return an error code that needs to
be examined. be examined.
""" """
if argtypes: func.argtypes = argtypes if argtypes:
func.argtypes = argtypes
if errcheck: if errcheck:
# `errcheck` keyword may be set to False for routines that # `errcheck` keyword may be set to False for routines that
# return void, rather than a status code. # return void, rather than a status code.

View File

@ -97,7 +97,8 @@ class SpatialReference(GDALBase):
def __del__(self): def __del__(self):
"Destroys this spatial reference." "Destroys this spatial reference."
if self._ptr: capi.release_srs(self._ptr) if self._ptr:
capi.release_srs(self._ptr)
def __getitem__(self, target): def __getitem__(self, target):
""" """
@ -176,10 +177,14 @@ class SpatialReference(GDALBase):
@property @property
def name(self): def name(self):
"Returns the name of this Spatial Reference." "Returns the name of this Spatial Reference."
if self.projected: return self.attr_value('PROJCS') if self.projected:
elif self.geographic: return self.attr_value('GEOGCS') return self.attr_value('PROJCS')
elif self.local: return self.attr_value('LOCAL_CS') elif self.geographic:
else: return None return self.attr_value('GEOGCS')
elif self.local:
return self.attr_value('LOCAL_CS')
else:
return None
@property @property
def srid(self): def srid(self):
@ -336,7 +341,8 @@ class CoordTransform(GDALBase):
def __del__(self): def __del__(self):
"Deletes this Coordinate Transformation object." "Deletes this Coordinate Transformation object."
if self._ptr: capi.destroy_ct(self._ptr) if self._ptr:
capi.destroy_ct(self._ptr)
def __str__(self): def __str__(self):
return 'Transform from "%s" to "%s"' % (self._srs1_name, self._srs2_name) return 'Transform from "%s" to "%s"' % (self._srs1_name, self._srs2_name)

View File

@ -93,7 +93,8 @@ class DataSourceTest(unittest.TestCase):
# Now checking the field names. # Now checking the field names.
flds = layer.fields flds = layer.fields
for f in flds: self.assertEqual(True, f in source.fields) for f in flds:
self.assertEqual(True, f in source.fields)
# Negative FIDs are not allowed. # Negative FIDs are not allowed.
self.assertRaises(OGRIndexError, layer.__getitem__, -1) self.assertRaises(OGRIndexError, layer.__getitem__, -1)

View File

@ -418,7 +418,8 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin):
mp1.add(poly) # Adding a geometry at a time mp1.add(poly) # Adding a geometry at a time
mp2.add(poly.wkt) # Adding WKT mp2.add(poly.wkt) # Adding WKT
mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once. mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp) for tmp in (mp1, mp2, mp3):
self.assertEqual(mpoly, tmp)
def test15_extent(self): def test15_extent(self):
"Testing `extent` property." "Testing `extent` property."

View File

@ -89,7 +89,8 @@ class GeoIP(object):
# Getting the GeoIP data path. # Getting the GeoIP data path.
if not path: if not path:
path = GEOIP_SETTINGS.get('GEOIP_PATH', None) path = GEOIP_SETTINGS.get('GEOIP_PATH', None)
if not path: raise GeoIPException('GeoIP path must be provided via parameter or the GEOIP_PATH setting.') if not path:
raise GeoIPException('GeoIP path must be provided via parameter or the GEOIP_PATH setting.')
if not isinstance(path, six.string_types): if not isinstance(path, six.string_types):
raise TypeError('Invalid path type: %s' % type(path).__name__) raise TypeError('Invalid path type: %s' % type(path).__name__)
@ -129,8 +130,10 @@ class GeoIP(object):
# Cleaning any GeoIP file handles lying around. # Cleaning any GeoIP file handles lying around.
if GeoIP_delete is None: if GeoIP_delete is None:
return return
if self._country: GeoIP_delete(self._country) if self._country:
if self._city: GeoIP_delete(self._city) GeoIP_delete(self._country)
if self._city:
GeoIP_delete(self._city)
def _check_query(self, query, country=False, city=False, city_or_country=False): def _check_query(self, query, country=False, city=False, city_or_country=False):
"Helper routine for checking the query and database availability." "Helper routine for checking the query and database availability."
@ -199,8 +202,10 @@ class GeoIP(object):
#### Coordinate retrieval routines #### #### Coordinate retrieval routines ####
def coords(self, query, ordering=('longitude', 'latitude')): def coords(self, query, ordering=('longitude', 'latitude')):
cdict = self.city(query) cdict = self.city(query)
if cdict is None: return None if cdict is None:
else: return tuple(cdict[o] for o in ordering) return None
else:
return tuple(cdict[o] for o in ordering)
def lon_lat(self, query): def lon_lat(self, query):
"Returns a tuple of the (longitude, latitude) for the given query." "Returns a tuple of the (longitude, latitude) for the given query."

View File

@ -18,9 +18,11 @@ else:
lib_name = 'GeoIP' lib_name = 'GeoIP'
# Getting the path to the GeoIP library. # Getting the path to the GeoIP library.
if lib_name: lib_path = find_library(lib_name) if lib_name:
if lib_path is None: raise RuntimeError('Could not find the GeoIP library (tried "%s"). ' lib_path = find_library(lib_name)
'Try setting GEOIP_LIBRARY_PATH in your settings.' % lib_name) if lib_path is None:
raise RuntimeError('Could not find the GeoIP library (tried "%s"). '
'Try setting GEOIP_LIBRARY_PATH in your settings.' % lib_name)
lgeoip = CDLL(lib_path) lgeoip = CDLL(lib_path)
# Getting the C `free` for the platform. # Getting the C `free` for the platform.

View File

@ -27,7 +27,8 @@ geoip_encodings = {
1: 'utf8', 1: 'utf8',
} }
class GeoIPTag(Structure): pass class GeoIPTag(Structure):
pass
RECTYPE = POINTER(GeoIPRecord) RECTYPE = POINTER(GeoIPRecord)
DBTYPE = POINTER(GeoIPTag) DBTYPE = POINTER(GeoIPTag)

View File

@ -34,8 +34,10 @@ class GEOSBase(object):
# Raise an exception if the pointer isn't valid don't # Raise an exception if the pointer isn't valid don't
# want to be passing NULL pointers to routines -- # want to be passing NULL pointers to routines --
# that's very bad. # that's very bad.
if self._ptr: return self._ptr if self._ptr:
else: raise GEOSException('NULL GEOS %s pointer encountered.' % self.__class__.__name__) return self._ptr
else:
raise GEOSException('NULL GEOS %s pointer encountered.' % self.__class__.__name__)
def _set_ptr(self, ptr): def _set_ptr(self, ptr):
# Only allow the pointer to be set with pointers of the # Only allow the pointer to be set with pointers of the

View File

@ -73,7 +73,8 @@ class GeometryCollection(GEOSGeometry):
prev_ptr = self.ptr prev_ptr = self.ptr
srid = self.srid srid = self.srid
self.ptr = self._create_collection(length, items) self.ptr = self._create_collection(length, items)
if srid: self.srid = srid if srid:
self.srid = srid
capi.destroy_geom(prev_ptr) capi.destroy_geom(prev_ptr)
_set_single = GEOSGeometry._set_single_rebuild _set_single = GEOSGeometry._set_single_rebuild

View File

@ -64,7 +64,8 @@ class GEOSCoordSeq(GEOSBase):
# Setting the X, Y, Z # Setting the X, Y, Z
self.setX(index, value[0]) self.setX(index, value[0])
self.setY(index, value[1]) self.setY(index, value[1])
if set_3d: self.setZ(index, value[2]) if set_3d:
self.setZ(index, value[2])
#### Internal Routines #### #### Internal Routines ####
def _checkindex(self, index): def _checkindex(self, index):
@ -144,8 +145,10 @@ class GEOSCoordSeq(GEOSBase):
"Returns the KML representation for the coordinates." "Returns the KML representation for the coordinates."
# Getting the substitution string depending on whether the coordinates have # Getting the substitution string depending on whether the coordinates have
# a Z dimension. # a Z dimension.
if self.hasz: substr = '%s,%s,%s ' if self.hasz:
else: substr = '%s,%s,0 ' substr = '%s,%s,%s '
else:
substr = '%s,%s,0 '
return '<coordinates>%s</coordinates>' % \ return '<coordinates>%s</coordinates>' % \
''.join(substr % self[i] for i in xrange(len(self))).strip() ''.join(substr % self[i] for i in xrange(len(self))).strip()
@ -153,5 +156,7 @@ class GEOSCoordSeq(GEOSBase):
def tuple(self): def tuple(self):
"Returns a tuple version of this coordinate sequence." "Returns a tuple version of this coordinate sequence."
n = self.size n = self.size
if n == 1: return self[0] if n == 1:
else: return tuple(self[i] for i in xrange(n)) return self[0]
else:
return tuple(self[i] for i in xrange(n))

View File

@ -66,7 +66,8 @@ class GEOSGeometry(GEOSBase, ListMixin):
wkt_m = wkt_regex.match(geo_input) wkt_m = wkt_regex.match(geo_input)
if wkt_m: if wkt_m:
# Handling WKT input. # Handling WKT input.
if wkt_m.group('srid'): srid = int(wkt_m.group('srid')) if wkt_m.group('srid'):
srid = int(wkt_m.group('srid'))
g = wkt_r().read(force_bytes(wkt_m.group('wkt'))) g = wkt_r().read(force_bytes(wkt_m.group('wkt')))
elif hex_regex.match(geo_input): elif hex_regex.match(geo_input):
# Handling HEXEWKB input. # Handling HEXEWKB input.
@ -100,7 +101,8 @@ class GEOSGeometry(GEOSBase, ListMixin):
def _post_init(self, srid): def _post_init(self, srid):
"Helper routine for performing post-initialization setup." "Helper routine for performing post-initialization setup."
# Setting the SRID, if given. # Setting the SRID, if given.
if srid and isinstance(srid, int): self.srid = srid if srid and isinstance(srid, int):
self.srid = srid
# Setting the class type (e.g., Point, Polygon, etc.) # Setting the class type (e.g., Point, Polygon, etc.)
self.__class__ = GEOS_CLASSES[self.geom_typeid] self.__class__ = GEOS_CLASSES[self.geom_typeid]
@ -114,7 +116,8 @@ class GEOSGeometry(GEOSBase, ListMixin):
Destroys this Geometry; in other words, frees the memory used by the Destroys this Geometry; in other words, frees the memory used by the
GEOS C++ object. GEOS C++ object.
""" """
if self._ptr: capi.destroy_geom(self._ptr) if self._ptr:
capi.destroy_geom(self._ptr)
def __copy__(self): def __copy__(self):
""" """
@ -149,7 +152,8 @@ class GEOSGeometry(GEOSBase, ListMixin):
# Instantiating from the tuple state that was pickled. # Instantiating from the tuple state that was pickled.
wkb, srid = state wkb, srid = state
ptr = wkb_r().read(memoryview(wkb)) ptr = wkb_r().read(memoryview(wkb))
if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.') if not ptr:
raise GEOSException('Invalid Geometry loaded from pickled state.')
self.ptr = ptr self.ptr = ptr
self._post_init(srid) self._post_init(srid)
@ -361,8 +365,10 @@ class GEOSGeometry(GEOSBase, ListMixin):
def get_srid(self): def get_srid(self):
"Gets the SRID for the geometry, returns None if no SRID is set." "Gets the SRID for the geometry, returns None if no SRID is set."
s = capi.geos_get_srid(self.ptr) s = capi.geos_get_srid(self.ptr)
if s == 0: return None if s == 0:
else: return s return None
else:
return s
def set_srid(self, srid): def set_srid(self, srid):
"Sets the SRID for the geometry." "Sets the SRID for the geometry."
@ -377,8 +383,10 @@ class GEOSGeometry(GEOSBase, ListMixin):
are *not* included in this representation because GEOS does not yet are *not* included in this representation because GEOS does not yet
support serializing them. support serializing them.
""" """
if self.get_srid(): return 'SRID=%s;%s' % (self.srid, self.wkt) if self.get_srid():
else: return self.wkt return 'SRID=%s;%s' % (self.srid, self.wkt)
else:
return self.wkt
@property @property
def wkt(self): def wkt(self):

View File

@ -43,7 +43,8 @@ else:
if lib_names: if lib_names:
for lib_name in lib_names: for lib_name in lib_names:
lib_path = find_library(lib_name) lib_path = find_library(lib_name)
if not lib_path is None: break if not lib_path is None:
break
# No GEOS library could be found. # No GEOS library could be found.
if lib_path is None: if lib_path is None:
@ -83,10 +84,17 @@ error_h = ERRORFUNC(error_h)
#### GEOS Geometry C data structures, and utility functions. #### #### GEOS Geometry C data structures, and utility functions. ####
# Opaque GEOS geometry structures, used for GEOM_PTR and CS_PTR # Opaque GEOS geometry structures, used for GEOM_PTR and CS_PTR
class GEOSGeom_t(Structure): pass class GEOSGeom_t(Structure):
class GEOSPrepGeom_t(Structure): pass pass
class GEOSCoordSeq_t(Structure): pass
class GEOSContextHandle_t(Structure): pass class GEOSPrepGeom_t(Structure):
pass
class GEOSCoordSeq_t(Structure):
pass
class GEOSContextHandle_t(Structure):
pass
# Pointers to opaque GEOS geometry structures. # Pointers to opaque GEOS geometry structures.
GEOM_PTR = POINTER(GEOSGeom_t) GEOM_PTR = POINTER(GEOSGeom_t)

View File

@ -24,25 +24,31 @@ class LineString(GEOSGeometry):
ls = LineString(Point(1, 1), Point(2, 2)) ls = LineString(Point(1, 1), Point(2, 2))
""" """
# If only one argument provided, set the coords array appropriately # If only one argument provided, set the coords array appropriately
if len(args) == 1: coords = args[0] if len(args) == 1:
else: coords = args coords = args[0]
else:
coords = args
if isinstance(coords, (tuple, list)): if isinstance(coords, (tuple, list)):
# Getting the number of coords and the number of dimensions -- which # Getting the number of coords and the number of dimensions -- which
# must stay the same, e.g., no LineString((1, 2), (1, 2, 3)). # must stay the same, e.g., no LineString((1, 2), (1, 2, 3)).
ncoords = len(coords) ncoords = len(coords)
if coords: ndim = len(coords[0]) if coords:
else: raise TypeError('Cannot initialize on empty sequence.') ndim = len(coords[0])
else:
raise TypeError('Cannot initialize on empty sequence.')
self._checkdim(ndim) self._checkdim(ndim)
# Incrementing through each of the coordinates and verifying # Incrementing through each of the coordinates and verifying
for i in xrange(1, ncoords): for i in xrange(1, ncoords):
if not isinstance(coords[i], (tuple, list, Point)): if not isinstance(coords[i], (tuple, list, Point)):
raise TypeError('each coordinate should be a sequence (list or tuple)') raise TypeError('each coordinate should be a sequence (list or tuple)')
if len(coords[i]) != ndim: raise TypeError('Dimension mismatch.') if len(coords[i]) != ndim:
raise TypeError('Dimension mismatch.')
numpy_coords = False numpy_coords = False
elif numpy and isinstance(coords, numpy.ndarray): elif numpy and isinstance(coords, numpy.ndarray):
shape = coords.shape # Using numpy's shape. shape = coords.shape # Using numpy's shape.
if len(shape) != 2: raise TypeError('Too many dimensions.') if len(shape) != 2:
raise TypeError('Too many dimensions.')
self._checkdim(shape[1]) self._checkdim(shape[1])
ncoords = shape[0] ncoords = shape[0]
ndim = shape[1] ndim = shape[1]
@ -55,9 +61,12 @@ class LineString(GEOSGeometry):
cs = GEOSCoordSeq(capi.create_cs(ncoords, ndim), z=bool(ndim==3)) cs = GEOSCoordSeq(capi.create_cs(ncoords, ndim), z=bool(ndim==3))
for i in xrange(ncoords): for i in xrange(ncoords):
if numpy_coords: cs[i] = coords[i,:] if numpy_coords:
elif isinstance(coords[i], Point): cs[i] = coords[i].tuple cs[i] = coords[i,:]
else: cs[i] = coords[i] elif isinstance(coords[i], Point):
cs[i] = coords[i].tuple
else:
cs[i] = coords[i]
# If SRID was passed in with the keyword arguments # If SRID was passed in with the keyword arguments
srid = kwargs.get('srid', None) srid = kwargs.get('srid', None)
@ -103,7 +112,8 @@ class LineString(GEOSGeometry):
self._cs[index] = value self._cs[index] = value
def _checkdim(self, dim): def _checkdim(self, dim):
if dim not in (2, 3): raise TypeError('Dimension mismatch.') if dim not in (2, 3):
raise TypeError('Dimension mismatch.')
#### Sequence Properties #### #### Sequence Properties ####
@property @property
@ -118,8 +128,10 @@ class LineString(GEOSGeometry):
the given function. Will return a numpy array if possible. the given function. Will return a numpy array if possible.
""" """
lst = [func(i) for i in xrange(len(self))] lst = [func(i) for i in xrange(len(self))]
if numpy: return numpy.array(lst) # ARRRR! if numpy:
else: return lst return numpy.array(lst) # ARRRR!
else:
return lst
@property @property
def array(self): def array(self):
@ -144,8 +156,10 @@ class LineString(GEOSGeometry):
@property @property
def z(self): def z(self):
"Returns a list or numpy array of the Z variable." "Returns a list or numpy array of the Z variable."
if not self.hasz: return None if not self.hasz:
else: return self._listarr(self._cs.getZ) return None
else:
return self._listarr(self._cs.getZ)
# LinearRings are LineStrings used within Polygons. # LinearRings are LineStrings used within Polygons.
class LinearRing(LineString): class LinearRing(LineString):

View File

@ -180,13 +180,15 @@ class ListMixin(object):
"Standard list count method" "Standard list count method"
count = 0 count = 0
for i in self: for i in self:
if val == i: count += 1 if val == i:
count += 1
return count return count
def index(self, val): def index(self, val):
"Standard list index method" "Standard list index method"
for i in xrange(0, len(self)): for i in xrange(0, len(self)):
if self[i] == val: return i if self[i] == val:
return i
raise ValueError('%s not found in object' % str(val)) raise ValueError('%s not found in object' % str(val))
## Mutating ## ## Mutating ##

View File

@ -50,7 +50,8 @@ class Point(GEOSGeometry):
i = iter(coords) i = iter(coords)
capi.cs_setx(cs, 0, next(i)) capi.cs_setx(cs, 0, next(i))
capi.cs_sety(cs, 0, next(i)) capi.cs_sety(cs, 0, next(i))
if ndim == 3: capi.cs_setz(cs, 0, next(i)) if ndim == 3:
capi.cs_setz(cs, 0, next(i))
return capi.create_point(cs) return capi.create_point(cs)
@ -74,9 +75,12 @@ class Point(GEOSGeometry):
def __len__(self): def __len__(self):
"Returns the number of dimensions for this Point (either 0, 2 or 3)." "Returns the number of dimensions for this Point (either 0, 2 or 3)."
if self.empty: return 0 if self.empty:
if self.hasz: return 3 return 0
else: return 2 if self.hasz:
return 3
else:
return 2
def _get_single_external(self, index): def _get_single_external(self, index):
if index == 0: if index == 0:

View File

@ -99,7 +99,8 @@ class Polygon(GEOSGeometry):
def _construct_ring(self, param, msg='Parameter must be a sequence of LinearRings or objects that can initialize to LinearRings'): def _construct_ring(self, param, msg='Parameter must be a sequence of LinearRings or objects that can initialize to LinearRings'):
"Helper routine for trying to construct a ring from the given parameter." "Helper routine for trying to construct a ring from the given parameter."
if isinstance(param, LinearRing): return param if isinstance(param, LinearRing):
return param
try: try:
ring = LinearRing(param) ring = LinearRing(param)
return ring return ring
@ -112,7 +113,8 @@ class Polygon(GEOSGeometry):
prev_ptr = self.ptr prev_ptr = self.ptr
srid = self.srid srid = self.srid
self.ptr = self._create_polygon(length, items) self.ptr = self._create_polygon(length, items)
if srid: self.srid = srid if srid:
self.srid = srid
capi.destroy_geom(prev_ptr) capi.destroy_geom(prev_ptr)
def _get_single_internal(self, index): def _get_single_internal(self, index):

View File

@ -11,11 +11,13 @@ class PreparedGeometry(GEOSBase):
ptr_type = capi.PREPGEOM_PTR ptr_type = capi.PREPGEOM_PTR
def __init__(self, geom): def __init__(self, geom):
if not isinstance(geom, GEOSGeometry): raise TypeError if not isinstance(geom, GEOSGeometry):
raise TypeError
self.ptr = capi.geos_prepare(geom.ptr) self.ptr = capi.geos_prepare(geom.ptr)
def __del__(self): def __del__(self):
if self._ptr: capi.prepared_destroy(self._ptr) if self._ptr:
capi.prepared_destroy(self._ptr)
def contains(self, other): def contains(self, other):
return capi.prepared_contains(self.ptr, other.ptr) return capi.prepared_contains(self.ptr, other.ptr)

View File

@ -33,7 +33,8 @@ def last_arg_byref(args):
def check_dbl(result, func, cargs): def check_dbl(result, func, cargs):
"Checks the status code and returns the double value passed in by reference." "Checks the status code and returns the double value passed in by reference."
# Checking the status code # Checking the status code
if result != 1: return None if result != 1:
return None
# Double passed in by reference, return its value. # Double passed in by reference, return its value.
return last_arg_byref(cargs) return last_arg_byref(cargs)
@ -53,8 +54,10 @@ def check_minus_one(result, func, cargs):
def check_predicate(result, func, cargs): def check_predicate(result, func, cargs):
"Error checking for unary/binary predicate functions." "Error checking for unary/binary predicate functions."
val = ord(result) # getting the ordinal from the character val = ord(result) # getting the ordinal from the character
if val == 1: return True if val == 1:
elif val == 0: return False return True
elif val == 0:
return False
else: else:
raise GEOSException('Error encountered on GEOS C predicate function "%s".' % func.__name__) raise GEOSException('Error encountered on GEOS C predicate function "%s".' % func.__name__)
@ -80,7 +83,8 @@ def check_string(result, func, cargs):
This frees the memory allocated by GEOS at the result pointer. This frees the memory allocated by GEOS at the result pointer.
""" """
if not result: raise GEOSException('Error encountered checking string return value in GEOS C function "%s".' % func.__name__) if not result:
raise GEOSException('Error encountered checking string return value in GEOS C function "%s".' % func.__name__)
# Getting the string value at the pointer address. # Getting the string value at the pointer address.
s = string_at(result) s = string_at(result)
# Freeing the memory allocated within GEOS # Freeing the memory allocated within GEOS

View File

@ -35,7 +35,8 @@ def bin_output(func):
def geom_output(func, argtypes): def geom_output(func, argtypes):
"For GEOS routines that return a geometry." "For GEOS routines that return a geometry."
if argtypes: func.argtypes = argtypes if argtypes:
func.argtypes = argtypes
func.restype = GEOM_PTR func.restype = GEOM_PTR
func.errcheck = check_geom func.errcheck = check_geom
return func return func

View File

@ -11,10 +11,17 @@ from django.utils import six
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
### The WKB/WKT Reader/Writer structures and pointers ### ### The WKB/WKT Reader/Writer structures and pointers ###
class WKTReader_st(Structure): pass class WKTReader_st(Structure):
class WKTWriter_st(Structure): pass pass
class WKBReader_st(Structure): pass
class WKBWriter_st(Structure): pass class WKTWriter_st(Structure):
pass
class WKBReader_st(Structure):
pass
class WKBWriter_st(Structure):
pass
WKT_READ_PTR = POINTER(WKTReader_st) WKT_READ_PTR = POINTER(WKTReader_st)
WKT_WRITE_PTR = POINTER(WKTWriter_st) WKT_WRITE_PTR = POINTER(WKTWriter_st)
@ -121,7 +128,8 @@ class IOBase(GEOSBase):
def __del__(self): def __del__(self):
# Cleaning up with the appropriate destructor. # Cleaning up with the appropriate destructor.
if self._ptr: self._destructor(self._ptr) if self._ptr:
self._destructor(self._ptr)
### Base WKB/WKT Reading and Writing objects ### ### Base WKB/WKT Reading and Writing objects ###
@ -194,7 +202,8 @@ class WKBWriter(IOBase):
return wkb_writer_get_byteorder(self.ptr) return wkb_writer_get_byteorder(self.ptr)
def _set_byteorder(self, order): def _set_byteorder(self, order):
if not order in (0, 1): raise ValueError('Byte order parameter must be 0 (Big Endian) or 1 (Little Endian).') if not order in (0, 1):
raise ValueError('Byte order parameter must be 0 (Big Endian) or 1 (Little Endian).')
wkb_writer_set_byteorder(self.ptr, order) wkb_writer_set_byteorder(self.ptr, order)
byteorder = property(_get_byteorder, _set_byteorder) byteorder = property(_get_byteorder, _set_byteorder)
@ -204,7 +213,8 @@ class WKBWriter(IOBase):
return wkb_writer_get_outdim(self.ptr) return wkb_writer_get_outdim(self.ptr)
def _set_outdim(self, new_dim): def _set_outdim(self, new_dim):
if not new_dim in (2, 3): raise ValueError('WKB output dimension must be 2 or 3') if not new_dim in (2, 3):
raise ValueError('WKB output dimension must be 2 or 3')
wkb_writer_set_outdim(self.ptr, new_dim) wkb_writer_set_outdim(self.ptr, new_dim)
outdim = property(_get_outdim, _set_outdim) outdim = property(_get_outdim, _set_outdim)
@ -214,8 +224,10 @@ class WKBWriter(IOBase):
return bool(ord(wkb_writer_get_include_srid(self.ptr))) return bool(ord(wkb_writer_get_include_srid(self.ptr)))
def _set_include_srid(self, include): def _set_include_srid(self, include):
if bool(include): flag = b'\x01' if bool(include):
else: flag = b'\x00' flag = b'\x01'
else:
flag = b'\x00'
wkb_writer_set_include_srid(self.ptr, flag) wkb_writer_set_include_srid(self.ptr, flag)
srid = property(_get_include_srid, _set_include_srid) srid = property(_get_include_srid, _set_include_srid)

View File

@ -11,7 +11,8 @@ from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc
def binary_predicate(func, *args): def binary_predicate(func, *args):
"For GEOS binary predicate functions." "For GEOS binary predicate functions."
argtypes = [GEOM_PTR, GEOM_PTR] argtypes = [GEOM_PTR, GEOM_PTR]
if args: argtypes += args if args:
argtypes += args
func.argtypes = argtypes func.argtypes = argtypes
func.restype = c_char func.restype = c_char
func.errcheck = check_predicate func.errcheck = check_predicate

View File

@ -11,7 +11,8 @@ class GEOSContextHandle(object):
self.ptr = lgeos.initGEOS_r(notice_h, error_h) self.ptr = lgeos.initGEOS_r(notice_h, error_h)
def __del__(self): def __del__(self):
if self.ptr: lgeos.finishGEOS_r(self.ptr) if self.ptr:
lgeos.finishGEOS_r(self.ptr)
# Defining a thread-local object and creating an instance # Defining a thread-local object and creating an instance
# to hold a reference to GEOSContextHandle for this thread. # to hold a reference to GEOSContextHandle for this thread.

View File

@ -16,7 +16,8 @@ from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc
def topology(func, *args, **kwargs): def topology(func, *args, **kwargs):
"For GEOS unary topology functions." "For GEOS unary topology functions."
argtypes = [GEOM_PTR] argtypes = [GEOM_PTR]
if args: argtypes += args if args:
argtypes += args
func.argtypes = argtypes func.argtypes = argtypes
func.restype = kwargs.get('restype', GEOM_PTR) func.restype = kwargs.get('restype', GEOM_PTR)
func.errcheck = kwargs.get('errcheck', check_geom) func.errcheck = kwargs.get('errcheck', check_geom)

View File

@ -143,7 +143,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
for tg in self.geometries.wkt_out: for tg in self.geometries.wkt_out:
geom = fromstr(tg.wkt) geom = fromstr(tg.wkt)
kml = getattr(tg, 'kml', False) kml = getattr(tg, 'kml', False)
if kml: self.assertEqual(kml, geom.kml) if kml:
self.assertEqual(kml, geom.kml)
def test_errors(self): def test_errors(self):
"Testing the Error handlers." "Testing the Error handlers."
@ -338,7 +339,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments
self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list
self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt) # Point individual arguments self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt) # Point individual arguments
if numpy: self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array if numpy:
self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array
def test_multilinestring(self): def test_multilinestring(self):
"Testing MultiLineString objects." "Testing MultiLineString objects."
@ -378,7 +380,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
self.assertEqual(lr, LinearRing(lr.tuple)) self.assertEqual(lr, LinearRing(lr.tuple))
self.assertEqual(lr, LinearRing(*lr.tuple)) self.assertEqual(lr, LinearRing(*lr.tuple))
self.assertEqual(lr, LinearRing([list(tup) for tup in lr.tuple])) self.assertEqual(lr, LinearRing([list(tup) for tup in lr.tuple]))
if numpy: self.assertEqual(lr, LinearRing(numpy.array(lr.tuple))) if numpy:
self.assertEqual(lr, LinearRing(numpy.array(lr.tuple)))
def test_polygons_from_bbox(self): def test_polygons_from_bbox(self):
"Testing `from_bbox` class method." "Testing `from_bbox` class method."
@ -528,8 +531,10 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
self.assertEqual(c1, c2) self.assertEqual(c1, c2)
# Constructing the test value to set the coordinate sequence with # Constructing the test value to set the coordinate sequence with
if len(c1) == 2: tset = (5, 23) if len(c1) == 2:
else: tset = (5, 23, 8) tset = (5, 23)
else:
tset = (5, 23, 8)
cs[i] = tset cs[i] = tset
# Making sure every set point matches what we expect # Making sure every set point matches what we expect
@ -636,14 +641,16 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
# Testing SRID keyword on fromstr(), and on Polygon rings. # Testing SRID keyword on fromstr(), and on Polygon rings.
poly = fromstr(self.geometries.polygons[1].wkt, srid=4269) poly = fromstr(self.geometries.polygons[1].wkt, srid=4269)
self.assertEqual(4269, poly.srid) self.assertEqual(4269, poly.srid)
for ring in poly: self.assertEqual(4269, ring.srid) for ring in poly:
self.assertEqual(4269, ring.srid)
poly.srid = 4326 poly.srid = 4326
self.assertEqual(4326, poly.shell.srid) self.assertEqual(4326, poly.shell.srid)
# Testing SRID keyword on GeometryCollection # Testing SRID keyword on GeometryCollection
gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021) gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021)
self.assertEqual(32021, gc.srid) self.assertEqual(32021, gc.srid)
for i in range(len(gc)): self.assertEqual(32021, gc[i].srid) for i in range(len(gc)):
self.assertEqual(32021, gc[i].srid)
# GEOS may get the SRID from HEXEWKB # GEOS may get the SRID from HEXEWKB
# 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS # 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS
@ -691,7 +698,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
# Constructing the new shell by adding 500 to every point in the old shell. # Constructing the new shell by adding 500 to every point in the old shell.
shell_tup = poly.shell.tuple shell_tup = poly.shell.tuple
new_coords = [] new_coords = []
for point in shell_tup: new_coords.append((point[0] + 500., point[1] + 500.)) for point in shell_tup:
new_coords.append((point[0] + 500., point[1] + 500.))
new_shell = LinearRing(*tuple(new_coords)) new_shell = LinearRing(*tuple(new_coords))
# Assigning polygon's exterior ring w/the new shell # Assigning polygon's exterior ring w/the new shell
@ -724,7 +732,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
# Offsetting the each ring in the polygon by 500. # Offsetting the each ring in the polygon by 500.
for j in xrange(len(poly)): for j in xrange(len(poly)):
r = poly[j] r = poly[j]
for k in xrange(len(r)): r[k] = (r[k][0] + 500., r[k][1] + 500.) for k in xrange(len(r)):
r[k] = (r[k][0] + 500., r[k][1] + 500.)
poly[j] = r poly[j] = r
self.assertNotEqual(mpoly[i], poly) self.assertNotEqual(mpoly[i], poly)
@ -1017,7 +1026,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
g1, g2 = cPickle.loads(s1), pickle.loads(s2) g1, g2 = cPickle.loads(s1), pickle.loads(s2)
for tmpg in (g1, g2): for tmpg in (g1, g2):
self.assertEqual(geom, tmpg) self.assertEqual(geom, tmpg)
if not no_srid: self.assertEqual(geom.srid, tmpg.srid) if not no_srid:
self.assertEqual(geom.srid, tmpg.srid)
@skipUnless(HAS_GEOS and GEOS_PREPARE, "geos >= 3.1.0 is required") @skipUnless(HAS_GEOS and GEOS_PREPARE, "geos >= 3.1.0 is required")
def test_prepared(self): def test_prepared(self):

View File

@ -12,26 +12,57 @@ if HAS_GEOS:
from ..error import GEOSIndexError from ..error import GEOSIndexError
def getItem(o,i): return o[i] def getItem(o,i):
def delItem(o,i): del o[i] return o[i]
def setItem(o,i,v): o[i] = v
def delItem(o,i):
del o[i]
def setItem(o,i,v):
o[i] = v
if HAS_GEOS: if HAS_GEOS:
def api_get_distance(x): return x.distance(Point(-200,-200)) def api_get_distance(x):
return x.distance(Point(-200,-200))
def api_get_buffer(x): return x.buffer(10) def api_get_buffer(x):
def api_get_geom_typeid(x): return x.geom_typeid return x.buffer(10)
def api_get_num_coords(x): return x.num_coords
def api_get_centroid(x): return x.centroid def api_get_geom_typeid(x):
def api_get_empty(x): return x.empty return x.geom_typeid
def api_get_valid(x): return x.valid
def api_get_simple(x): return x.simple def api_get_num_coords(x):
def api_get_ring(x): return x.ring return x.num_coords
def api_get_boundary(x): return x.boundary
def api_get_convex_hull(x): return x.convex_hull def api_get_centroid(x):
def api_get_extent(x): return x.extent return x.centroid
def api_get_area(x): return x.area
def api_get_length(x): return x.length def api_get_empty(x):
return x.empty
def api_get_valid(x):
return x.valid
def api_get_simple(x):
return x.simple
def api_get_ring(x):
return x.ring
def api_get_boundary(x):
return x.boundary
def api_get_convex_hull(x):
return x.convex_hull
def api_get_extent(x):
return x.extent
def api_get_area(x):
return x.area
def api_get_length(x):
return x.length
geos_function_tests = [val for name, val in vars().items() geos_function_tests = [val for name, val in vars().items()
if hasattr(val, '__call__') if hasattr(val, '__call__')
@ -48,7 +79,8 @@ class GEOSMutationTest(unittest.TestCase):
def test00_GEOSIndexException(self): def test00_GEOSIndexException(self):
'Testing Geometry GEOSIndexError' 'Testing Geometry GEOSIndexError'
p = Point(1,2) p = Point(1,2)
for i in range(-2,2): p._checkindex(i) for i in range(-2,2):
p._checkindex(i)
self.assertRaises(GEOSIndexError, p._checkindex, 2) self.assertRaises(GEOSIndexError, p._checkindex, 2)
self.assertRaises(GEOSIndexError, p._checkindex, -3) self.assertRaises(GEOSIndexError, p._checkindex, -3)

View File

@ -59,7 +59,8 @@ class ListMixinTest(unittest.TestCase):
listType = UserListA listType = UserListA
def lists_of_len(self, length=None): def lists_of_len(self, length=None):
if length is None: length = self.limit if length is None:
length = self.limit
pl = list(range(length)) pl = list(range(length))
return pl, self.listType(pl) return pl, self.listType(pl)
@ -90,7 +91,8 @@ class ListMixinTest(unittest.TestCase):
def test02_setslice(self): def test02_setslice(self):
'Slice assignment' 'Slice assignment'
def setfcn(x,i,j,k,L): x[i:j:k] = range(L) def setfcn(x,i,j,k,L):
x[i:j:k] = range(L)
pl, ul = self.lists_of_len() pl, ul = self.lists_of_len()
for slen in range(self.limit + 1): for slen in range(self.limit + 1):
ssl = nextRange(slen) ssl = nextRange(slen)
@ -208,9 +210,12 @@ class ListMixinTest(unittest.TestCase):
def test05_out_of_range_exceptions(self): def test05_out_of_range_exceptions(self):
'Out of range exceptions' 'Out of range exceptions'
def setfcn(x, i): x[i] = 20 def setfcn(x, i):
def getfcn(x, i): return x[i] x[i] = 20
def delfcn(x, i): del x[i] def getfcn(x, i):
return x[i]
def delfcn(x, i):
del x[i]
pl, ul = self.lists_of_len() pl, ul = self.lists_of_len()
for i in (-1 - self.limit, self.limit): for i in (-1 - self.limit, self.limit):
self.assertRaises(IndexError, setfcn, ul, i) # 'set index %d' % i) self.assertRaises(IndexError, setfcn, ul, i) # 'set index %d' % i)
@ -248,7 +253,8 @@ class ListMixinTest(unittest.TestCase):
self.assertEqual(pl[:], ul[:], 'after pop') self.assertEqual(pl[:], ul[:], 'after pop')
pl, ul = self.lists_of_len() pl, ul = self.lists_of_len()
def popfcn(x, i): x.pop(i) def popfcn(x, i):
x.pop(i)
self.assertRaises(IndexError, popfcn, ul, self.limit) self.assertRaises(IndexError, popfcn, ul, self.limit)
self.assertRaises(IndexError, popfcn, ul, -1 - self.limit) self.assertRaises(IndexError, popfcn, ul, -1 - self.limit)
@ -265,8 +271,10 @@ class ListMixinTest(unittest.TestCase):
ul.remove(val) ul.remove(val)
self.assertEqual(pl[:], ul[:], 'after remove val %d' % val) self.assertEqual(pl[:], ul[:], 'after remove val %d' % val)
def indexfcn(x, v): return x.index(v) def indexfcn(x, v):
def removefcn(x, v): return x.remove(v) return x.index(v)
def removefcn(x, v):
return x.remove(v)
self.assertRaises(ValueError, indexfcn, ul, 40) self.assertRaises(ValueError, indexfcn, ul, 40)
self.assertRaises(ValueError, removefcn, ul, 40) self.assertRaises(ValueError, removefcn, ul, 40)
@ -276,7 +284,8 @@ class ListMixinTest(unittest.TestCase):
ul._allowed = six.integer_types ul._allowed = six.integer_types
ul[1] = 50 ul[1] = 50
ul[:2] = [60, 70, 80] ul[:2] = [60, 70, 80]
def setfcn(x, i, v): x[i] = v def setfcn(x, i, v):
x[i] = v
self.assertRaises(TypeError, setfcn, ul, 2, 'hello') self.assertRaises(TypeError, setfcn, ul, 2, 'hello')
self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), ('hello','goodbye')) self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), ('hello','goodbye'))
@ -284,8 +293,10 @@ class ListMixinTest(unittest.TestCase):
'Length limits' 'Length limits'
pl, ul = self.lists_of_len() pl, ul = self.lists_of_len()
ul._minlength = 1 ul._minlength = 1
def delfcn(x,i): del x[:i] def delfcn(x,i):
def setfcn(x,i): x[:i] = [] del x[:i]
def setfcn(x,i):
x[:i] = []
for i in range(self.limit - ul._minlength + 1, self.limit + 1): for i in range(self.limit - ul._minlength + 1, self.limit + 1):
self.assertRaises(ValueError, delfcn, ul, i) self.assertRaises(ValueError, delfcn, ul, i)
self.assertRaises(ValueError, setfcn, ul, i) self.assertRaises(ValueError, setfcn, ul, i)
@ -299,7 +310,8 @@ class ListMixinTest(unittest.TestCase):
def test09_iterable_check(self): def test09_iterable_check(self):
'Error on assigning non-iterable to slice' 'Error on assigning non-iterable to slice'
pl, ul = self.lists_of_len(self.limit + 1) pl, ul = self.lists_of_len(self.limit + 1)
def setfcn(x, i, v): x[i] = v def setfcn(x, i, v):
x[i] = v
self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), 2) self.assertRaises(TypeError, setfcn, ul, slice(0,3,2), 2)
def test10_checkindex(self): def test10_checkindex(self):

View File

@ -87,9 +87,11 @@ class GoogleMap(object):
# Defaults for the zoom level and center coordinates if the zoom # Defaults for the zoom level and center coordinates if the zoom
# is not automatically calculated. # is not automatically calculated.
if zoom is None: zoom = 4 if zoom is None:
zoom = 4
self.zoom = zoom self.zoom = zoom
if center is None: center = (0, 0) if center is None:
center = (0, 0)
self.center = center self.center = center
def render(self): def render(self):
@ -229,5 +231,6 @@ class GoogleMapSet(GoogleMap):
def icons(self): def icons(self):
"Returns a sequence of all icons in each map of the set." "Returns a sequence of all icons in each map of the set."
icons = set() icons = set()
for map in self.maps: icons |= map.icons for map in self.maps:
icons |= map.icons
return icons return icons

View File

@ -102,8 +102,10 @@ class GPolygon(GOverlayBase):
fill_opacity: fill_opacity:
The opacity of the polygon fill. Defaults to 0.4. The opacity of the polygon fill. Defaults to 0.4.
""" """
if isinstance(poly, six.string_types): poly = fromstr(poly) if isinstance(poly, six.string_types):
if isinstance(poly, (tuple, list)): poly = Polygon(poly) poly = fromstr(poly)
if isinstance(poly, (tuple, list)):
poly = Polygon(poly)
if not isinstance(poly, Polygon): if not isinstance(poly, Polygon):
raise TypeError('GPolygon may only initialize on GEOS Polygons.') raise TypeError('GPolygon may only initialize on GEOS Polygons.')
@ -152,8 +154,10 @@ class GPolyline(GOverlayBase):
The opacity of the polyline, between 0 and 1. Defaults to 1. The opacity of the polyline, between 0 and 1. Defaults to 1.
""" """
# If a GEOS geometry isn't passed in, try to contsruct one. # If a GEOS geometry isn't passed in, try to contsruct one.
if isinstance(geom, six.string_types): geom = fromstr(geom) if isinstance(geom, six.string_types):
if isinstance(geom, (tuple, list)): geom = Polygon(geom) geom = fromstr(geom)
if isinstance(geom, (tuple, list)):
geom = Polygon(geom)
# Generating the lat/lng coordinate pairs. # Generating the lat/lng coordinate pairs.
if isinstance(geom, (LineString, LinearRing)): if isinstance(geom, (LineString, LinearRing)):
self.latlngs = self.latlng_from_coords(geom.coords) self.latlngs = self.latlng_from_coords(geom.coords)
@ -282,8 +286,10 @@ class GMarker(GOverlayBase):
Draggable option for GMarker, disabled by default. Draggable option for GMarker, disabled by default.
""" """
# If a GEOS geometry isn't passed in, try to construct one. # If a GEOS geometry isn't passed in, try to construct one.
if isinstance(geom, six.string_types): geom = fromstr(geom) if isinstance(geom, six.string_types):
if isinstance(geom, (tuple, list)): geom = Point(geom) geom = fromstr(geom)
if isinstance(geom, (tuple, list)):
geom = Point(geom)
if isinstance(geom, Point): if isinstance(geom, Point):
self.latlng = self.latlng_from_coords(geom.coords) self.latlng = self.latlng_from_coords(geom.coords)
else: else:
@ -301,9 +307,12 @@ class GMarker(GOverlayBase):
def options(self): def options(self):
result = [] result = []
if self.title: result.append('title: "%s"' % self.title) if self.title:
if self.icon: result.append('icon: %s' % self.icon.varname) result.append('title: "%s"' % self.title)
if self.draggable: result.append('draggable: true') if self.icon:
result.append('icon: %s' % self.icon.varname)
if self.draggable:
result.append('draggable: true')
return '{%s}' % ','.join(result) return '{%s}' % ','.join(result)
@property @property

View File

@ -179,7 +179,8 @@ class MeasureBase(object):
val = 0.0 val = 0.0
default_unit = self.STANDARD_UNIT default_unit = self.STANDARD_UNIT
for unit, value in six.iteritems(kwargs): for unit, value in six.iteritems(kwargs):
if not isinstance(value, float): value = float(value) if not isinstance(value, float):
value = float(value)
if unit in self.UNITS: if unit in self.UNITS:
val += self.UNITS[unit] * value val += self.UNITS[unit] * value
default_unit = unit default_unit = unit

View File

@ -27,7 +27,8 @@ class GeoRSSSitemap(Sitemap):
# Setting up. # Setting up.
self.feed_dict = feed_dict self.feed_dict = feed_dict
self.locations = [] self.locations = []
if slug_dict is None: slug_dict = {} if slug_dict is None:
slug_dict = {}
# Getting the feed locations. # Getting the feed locations.
for section in feed_dict.keys(): for section in feed_dict.keys():
if slug_dict.get(section, False): if slug_dict.get(section, False):
@ -42,7 +43,8 @@ class GeoRSSSitemap(Sitemap):
is placed on each URL element. is placed on each URL element.
""" """
urls = Sitemap.get_urls(self, page=page, site=site) urls = Sitemap.get_urls(self, page=page, site=site)
for url in urls: url['geo_format'] = 'georss' for url in urls:
url['geo_format'] = 'georss'
return urls return urls
def items(self): def items(self):

View File

@ -46,7 +46,8 @@ class KMLSitemap(Sitemap):
is placed on each URL element. is placed on each URL element.
""" """
urls = Sitemap.get_urls(self, page=page, site=site) urls = Sitemap.get_urls(self, page=page, site=site)
for url in urls: url['geo_format'] = self.geo_format for url in urls:
url['geo_format'] = self.geo_format
return urls return urls
def items(self): def items(self):

View File

@ -7,7 +7,9 @@ class SouthTexasCity(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
point = models.PointField(srid=32140) point = models.PointField(srid=32140)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class SouthTexasCityFt(models.Model): class SouthTexasCityFt(models.Model):
@ -15,7 +17,9 @@ class SouthTexasCityFt(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
point = models.PointField(srid=2278) point = models.PointField(srid=2278)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class AustraliaCity(models.Model): class AustraliaCity(models.Model):
@ -23,7 +27,9 @@ class AustraliaCity(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
point = models.PointField() point = models.PointField()
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class CensusZipcode(models.Model): class CensusZipcode(models.Model):
@ -31,7 +37,9 @@ class CensusZipcode(models.Model):
name = models.CharField(max_length=5) name = models.CharField(max_length=5)
poly = models.PolygonField(srid=4269) poly = models.PolygonField(srid=4269)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class SouthTexasZipcode(models.Model): class SouthTexasZipcode(models.Model):
@ -39,7 +47,9 @@ class SouthTexasZipcode(models.Model):
name = models.CharField(max_length=5) name = models.CharField(max_length=5)
poly = models.PolygonField(srid=32140, null=True) poly = models.PolygonField(srid=32140, null=True)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class Interstate(models.Model): class Interstate(models.Model):
@ -47,7 +57,9 @@ class Interstate(models.Model):
name = models.CharField(max_length=10) name = models.CharField(max_length=10)
path = models.LineStringField() path = models.LineStringField()
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class SouthTexasInterstate(models.Model): class SouthTexasInterstate(models.Model):
@ -55,4 +67,6 @@ class SouthTexasInterstate(models.Model):
name = models.CharField(max_length=10) name = models.CharField(max_length=10)
path = models.LineStringField(srid=32140) path = models.LineStringField(srid=32140)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name

View File

@ -66,8 +66,10 @@ class DistanceTest(TestCase):
# Performing distance queries on two projected coordinate systems one # Performing distance queries on two projected coordinate systems one
# with units in meters and the other in units of U.S. survey feet. # with units in meters and the other in units of U.S. survey feet.
for dist in tx_dists: for dist in tx_dists:
if isinstance(dist, tuple): dist1, dist2 = dist if isinstance(dist, tuple):
else: dist1 = dist2 = dist dist1, dist2 = dist
else:
dist1 = dist2 = dist
qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt, dist1)) qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt, dist1))
qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt, dist2)) qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt, dist2))
for qs in qs1, qs2: for qs in qs1, qs2:
@ -75,12 +77,16 @@ class DistanceTest(TestCase):
# Now performing the `dwithin` queries on a geodetic coordinate system. # Now performing the `dwithin` queries on a geodetic coordinate system.
for dist in au_dists: for dist in au_dists:
if isinstance(dist, D) and not oracle: type_error = True if isinstance(dist, D) and not oracle:
else: type_error = False type_error = True
else:
type_error = False
if isinstance(dist, tuple): if isinstance(dist, tuple):
if oracle: dist = dist[1] if oracle:
else: dist = dist[0] dist = dist[1]
else:
dist = dist[0]
# Creating the query set. # Creating the query set.
qs = AustraliaCity.objects.order_by('name') qs = AustraliaCity.objects.order_by('name')

View File

@ -7,6 +7,8 @@ class City(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
point = models.PointField() point = models.PointField()
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
admin.site.register(City, admin.OSMGeoAdmin) admin.site.register(City, admin.OSMGeoAdmin)

View File

@ -10,14 +10,18 @@ class Country(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
mpoly = models.MultiPolygonField() # SRID, by default, is 4326 mpoly = models.MultiPolygonField() # SRID, by default, is 4326
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class City(models.Model): class City(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
point = models.PointField() point = models.PointField()
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
# This is an inherited model from City # This is an inherited model from City
class PennsylvaniaCity(City): class PennsylvaniaCity(City):
@ -30,14 +34,18 @@ class State(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
poly = models.PolygonField(null=null_flag) # Allowing NULL geometries here. poly = models.PolygonField(null=null_flag) # Allowing NULL geometries here.
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class Track(models.Model): class Track(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
line = models.LineStringField() line = models.LineStringField()
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
class Truth(models.Model): class Truth(models.Model):
val = models.BooleanField(default=False) val = models.BooleanField(default=False)
@ -49,7 +57,9 @@ if not spatialite:
name = models.CharField(max_length=20) name = models.CharField(max_length=20)
geom = models.GeometryField() geom = models.GeometryField()
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
class MinusOneSRID(models.Model): class MinusOneSRID(models.Model):
geom = models.PointField(srid=-1) # Minus one SRID. geom = models.PointField(srid=-1) # Minus one SRID.

View File

@ -190,7 +190,8 @@ class GeoModelTest(TestCase):
qs = PennsylvaniaCity.objects.transform(32128) qs = PennsylvaniaCity.objects.transform(32128)
self.assertEqual(1, qs.count()) self.assertEqual(1, qs.count())
for pc in qs: self.assertEqual(32128, pc.point.srid) for pc in qs:
self.assertEqual(32128, pc.point.srid)
def test_raw_sql_query(self): def test_raw_sql_query(self):
"Testing raw SQL query." "Testing raw SQL query."
@ -229,7 +230,8 @@ class GeoLookupTest(TestCase):
qs = City.objects.filter(point__contained=texas.mpoly) qs = City.objects.filter(point__contained=texas.mpoly)
self.assertEqual(3, qs.count()) self.assertEqual(3, qs.count())
cities = ['Houston', 'Dallas', 'Oklahoma City'] cities = ['Houston', 'Dallas', 'Oklahoma City']
for c in qs: self.assertEqual(True, c.name in cities) for c in qs:
self.assertEqual(True, c.name in cities)
# Pulling out some cities. # Pulling out some cities.
houston = City.objects.get(name='Houston') houston = City.objects.get(name='Houston')
@ -284,13 +286,15 @@ class GeoLookupTest(TestCase):
'Lawrence', 'Chicago', 'Wellington'] 'Lawrence', 'Chicago', 'Wellington']
qs = City.objects.filter(point__right=co_border) qs = City.objects.filter(point__right=co_border)
self.assertEqual(6, len(qs)) self.assertEqual(6, len(qs))
for c in qs: self.assertEqual(True, c.name in cities) for c in qs:
self.assertEqual(True, c.name in cities)
# These cities should be strictly to the right of the KS border. # These cities should be strictly to the right of the KS border.
cities = ['Chicago', 'Wellington'] cities = ['Chicago', 'Wellington']
qs = City.objects.filter(point__right=ks_border) qs = City.objects.filter(point__right=ks_border)
self.assertEqual(2, len(qs)) self.assertEqual(2, len(qs))
for c in qs: self.assertEqual(True, c.name in cities) for c in qs:
self.assertEqual(True, c.name in cities)
# Note: Wellington has an 'X' value of 174, so it will not be considered # Note: Wellington has an 'X' value of 174, so it will not be considered
# to the left of CO. # to the left of CO.
@ -300,7 +304,8 @@ class GeoLookupTest(TestCase):
cities = ['Pueblo', 'Victoria'] cities = ['Pueblo', 'Victoria']
qs = City.objects.filter(point__left=ks_border) qs = City.objects.filter(point__left=ks_border)
self.assertEqual(2, len(qs)) self.assertEqual(2, len(qs))
for c in qs: self.assertEqual(True, c.name in cities) for c in qs:
self.assertEqual(True, c.name in cities)
# The left/right lookup tests are known failures on PostGIS 2.0/2.0.1 # The left/right lookup tests are known failures on PostGIS 2.0/2.0.1
# http://trac.osgeo.org/postgis/ticket/2035 # http://trac.osgeo.org/postgis/ticket/2035
@ -313,7 +318,8 @@ class GeoLookupTest(TestCase):
c1 = City.objects.get(point=pnt) c1 = City.objects.get(point=pnt)
c2 = City.objects.get(point__same_as=pnt) c2 = City.objects.get(point__same_as=pnt)
c3 = City.objects.get(point__equals=pnt) c3 = City.objects.get(point__equals=pnt)
for c in [c1, c2, c3]: self.assertEqual('Houston', c.name) for c in [c1, c2, c3]:
self.assertEqual('Houston', c.name)
@no_mysql @no_mysql
def test_null_geometries(self): def test_null_geometries(self):
@ -476,7 +482,8 @@ class GeoQuerySetTest(TestCase):
@no_spatialite @no_spatialite
def test_geohash(self): def test_geohash(self):
"Testing GeoQuerySet.geohash()." "Testing GeoQuerySet.geohash()."
if not connection.ops.geohash: return if not connection.ops.geohash:
return
# Reference query: # Reference query:
# SELECT ST_GeoHash(point) FROM geoapp_city WHERE name='Houston'; # SELECT ST_GeoHash(point) FROM geoapp_city WHERE name='Houston';
# SELECT ST_GeoHash(point, 5) FROM geoapp_city WHERE name='Houston'; # SELECT ST_GeoHash(point, 5) FROM geoapp_city WHERE name='Houston';
@ -619,7 +626,8 @@ class GeoQuerySetTest(TestCase):
if not oracle: if not oracle:
# Oracle cannot count vertices in Point geometries. # Oracle cannot count vertices in Point geometries.
for c in City.objects.num_points(): self.assertEqual(1, c.num_points) for c in City.objects.num_points():
self.assertEqual(1, c.num_points)
@no_mysql @no_mysql
def test_point_on_surface(self): def test_point_on_surface(self):

View File

@ -6,14 +6,18 @@ class City(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)
point = models.PointField(geography=True) point = models.PointField(geography=True)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
@python_2_unicode_compatible @python_2_unicode_compatible
class Zipcode(models.Model): class Zipcode(models.Model):
code = models.CharField(max_length=10) code = models.CharField(max_length=10)
poly = models.PolygonField(geography=True) poly = models.PolygonField(geography=True)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.code
def __str__(self):
return self.code
@python_2_unicode_compatible @python_2_unicode_compatible
class County(models.Model): class County(models.Model):
@ -21,4 +25,6 @@ class County(models.Model):
state = models.CharField(max_length=20) state = models.CharField(max_length=20)
mpoly = models.MultiPolygonField(geography=True) mpoly = models.MultiPolygonField(geography=True)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return ' County, '.join([self.name, self.state])
def __str__(self):
return ' County, '.join([self.name, self.state])

View File

@ -208,7 +208,8 @@ class LayerMapTest(TestCase):
def test_test_fid_range_step(self): def test_test_fid_range_step(self):
"Tests the `fid_range` keyword and the `step` keyword of .save()." "Tests the `fid_range` keyword and the `step` keyword of .save()."
# Function for clearing out all the counties before testing. # Function for clearing out all the counties before testing.
def clear_counties(): County.objects.all().delete() def clear_counties():
County.objects.all().delete()
State.objects.bulk_create([ State.objects.bulk_create([
State(name='Colorado'), State(name='Hawaii'), State(name='Texas') State(name='Colorado'), State(name='Hawaii'), State(name='Texas')

View File

@ -5,7 +5,9 @@ from django.utils.encoding import python_2_unicode_compatible
class Location(models.Model): class Location(models.Model):
point = models.PointField() point = models.PointField()
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.point.wkt
def __str__(self):
return self.point.wkt
@python_2_unicode_compatible @python_2_unicode_compatible
class City(models.Model): class City(models.Model):
@ -13,7 +15,9 @@ class City(models.Model):
state = models.CharField(max_length=2) state = models.CharField(max_length=2)
location = models.ForeignKey(Location) location = models.ForeignKey(Location)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
class AugmentedLocation(Location): class AugmentedLocation(Location):
extra_text = models.TextField(blank=True) extra_text = models.TextField(blank=True)
@ -34,7 +38,9 @@ class Parcel(models.Model):
border1 = models.PolygonField() border1 = models.PolygonField()
border2 = models.PolygonField(srid=2276) border2 = models.PolygonField(srid=2276)
objects = models.GeoManager() objects = models.GeoManager()
def __str__(self): return self.name
def __str__(self):
return self.name
# These use the GeoManager but do not have any geographic fields. # These use the GeoManager but do not have any geographic fields.
class Author(models.Model): class Author(models.Model):

View File

@ -79,7 +79,8 @@ class RelatedGeoModelTest(TestCase):
# between the Oracle and PostGIS spatial backends on the extent calculation. # between the Oracle and PostGIS spatial backends on the extent calculation.
tol = 4 tol = 4
for ref, e in [(all_extent, e1), (txpa_extent, e2), (all_extent, e3)]: for ref, e in [(all_extent, e1), (txpa_extent, e2), (all_extent, e3)]:
for ref_val, e_val in zip(ref, e): self.assertAlmostEqual(ref_val, e_val, tol) for ref_val, e_val in zip(ref, e):
self.assertAlmostEqual(ref_val, e_val, tol)
@no_mysql @no_mysql
def test04b_related_union_aggregate(self): def test04b_related_union_aggregate(self):

View File

@ -16,10 +16,17 @@ def no_backend(test_func, backend):
# Decorators to disable entire test functions for specific # Decorators to disable entire test functions for specific
# spatial backends. # spatial backends.
def no_oracle(func): return no_backend(func, 'oracle') def no_oracle(func):
def no_postgis(func): return no_backend(func, 'postgis') return no_backend(func, 'oracle')
def no_mysql(func): return no_backend(func, 'mysql')
def no_spatialite(func): return no_backend(func, 'spatialite') def no_postgis(func):
return no_backend(func, 'postgis')
def no_mysql(func):
return no_backend(func, 'mysql')
def no_spatialite(func):
return no_backend(func, 'spatialite')
# Shortcut booleans to omit only portions of tests. # Shortcut booleans to omit only portions of tests.
_default_db = settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'].rsplit('.')[-1] _default_db = settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'].rsplit('.')[-1]

View File

@ -21,11 +21,20 @@ from django.utils.encoding import force_text
# LayerMapping exceptions. # LayerMapping exceptions.
class LayerMapError(Exception): pass class LayerMapError(Exception):
class InvalidString(LayerMapError): pass pass
class InvalidDecimal(LayerMapError): pass
class InvalidInteger(LayerMapError): pass class InvalidString(LayerMapError):
class MissingForeignKey(LayerMapError): pass pass
class InvalidDecimal(LayerMapError):
pass
class InvalidInteger(LayerMapError):
pass
class MissingForeignKey(LayerMapError):
pass
class LayerMapping(object): class LayerMapping(object):
"A class that maps OGR Layers to GeoDjango Models." "A class that maps OGR Layers to GeoDjango Models."
@ -263,10 +272,12 @@ class LayerMapping(object):
if isinstance(unique, (list, tuple)): if isinstance(unique, (list, tuple)):
# List of fields to determine uniqueness with # List of fields to determine uniqueness with
for attr in unique: for attr in unique:
if not attr in self.mapping: raise ValueError if not attr in self.mapping:
raise ValueError
elif isinstance(unique, six.string_types): elif isinstance(unique, six.string_types):
# Only a single field passed in. # Only a single field passed in.
if unique not in self.mapping: raise ValueError if unique not in self.mapping:
raise ValueError
else: else:
raise TypeError('Unique keyword argument must be set with a tuple, list, or string.') raise TypeError('Unique keyword argument must be set with a tuple, list, or string.')
@ -413,7 +424,8 @@ class LayerMapping(object):
# Transforming the geometry with our Coordinate Transformation object, # Transforming the geometry with our Coordinate Transformation object,
# but only if the class variable `transform` is set w/a CoordTransform # but only if the class variable `transform` is set w/a CoordTransform
# object. # object.
if self.transform: g.transform(self.transform) if self.transform:
g.transform(self.transform)
# Returning the WKT of the geometry. # Returning the WKT of the geometry.
return g.wkt return g.wkt
@ -512,7 +524,8 @@ class LayerMapping(object):
kwargs = self.feature_kwargs(feat) kwargs = self.feature_kwargs(feat)
except LayerMapError as msg: except LayerMapError as msg:
# Something borked the validation # Something borked the validation
if strict: raise if strict:
raise
elif not silent: elif not silent:
stream.write('Ignoring Feature ID %s because: %s\n' % (feat.fid, msg)) stream.write('Ignoring Feature ID %s because: %s\n' % (feat.fid, msg))
else: else:
@ -534,7 +547,8 @@ class LayerMapping(object):
# just-updated geometry WKT. # just-updated geometry WKT.
geom = getattr(m, self.geom_field).ogr geom = getattr(m, self.geom_field).ogr
new = OGRGeometry(kwargs[self.geom_field]) new = OGRGeometry(kwargs[self.geom_field])
for g in new: geom.add(g) for g in new:
geom.add(g)
setattr(m, self.geom_field, geom.wkt) setattr(m, self.geom_field, geom.wkt)
except ObjectDoesNotExist: except ObjectDoesNotExist:
# No unique model exists yet, create. # No unique model exists yet, create.
@ -546,7 +560,8 @@ class LayerMapping(object):
# Attempting to save. # Attempting to save.
m.save(using=self.using) m.save(using=self.using)
num_saved += 1 num_saved += 1
if verbose: stream.write('%s: %s\n' % ('Updated' if is_update else 'Saved', m)) if verbose:
stream.write('%s: %s\n' % ('Updated' if is_update else 'Saved', m))
except Exception as msg: except Exception as msg:
if strict: if strict:
# Bailing out if the `strict` keyword is set. # Bailing out if the `strict` keyword is set.
@ -580,8 +595,10 @@ class LayerMapping(object):
for i, end in enumerate(indices): for i, end in enumerate(indices):
# Constructing the slice to use for this step; the last slice is # Constructing the slice to use for this step; the last slice is
# special (e.g, [100:] instead of [90:100]). # special (e.g, [100:] instead of [90:100]).
if i+1 == n_i: step_slice = slice(beg, None) if i+1 == n_i:
else: step_slice = slice(beg, end) step_slice = slice(beg, None)
else:
step_slice = slice(beg, end)
try: try:
num_feat, num_saved = _save(step_slice, num_feat, num_saved) num_feat, num_saved = _save(step_slice, num_feat, num_saved)

View File

@ -37,11 +37,14 @@ def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
# Generating the field name for each field in the layer. # Generating the field name for each field in the layer.
for field in data_source[layer_key].fields: for field in data_source[layer_key].fields:
mfield = field.lower() mfield = field.lower()
if mfield[-1:] == '_': mfield += 'field' if mfield[-1:] == '_':
mfield += 'field'
_mapping[mfield] = field _mapping[mfield] = field
gtype = data_source[layer_key].geom_type gtype = data_source[layer_key].geom_type
if multi_geom and gtype.num in (1, 2, 3): prefix = 'MULTI' if multi_geom and gtype.num in (1, 2, 3):
else: prefix = '' prefix = 'MULTI'
else:
prefix = ''
_mapping[geom_name] = prefix + str(gtype).upper() _mapping[geom_name] = prefix + str(gtype).upper()
return _mapping return _mapping
@ -151,10 +154,14 @@ def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=Non
# Gets the `null` and `blank` keywords for the given field name. # Gets the `null` and `blank` keywords for the given field name.
def get_kwargs_str(field_name): def get_kwargs_str(field_name):
kwlist = [] kwlist = []
if field_name.lower() in null_fields: kwlist.append('null=True') if field_name.lower() in null_fields:
if field_name.lower() in blank_fields: kwlist.append('blank=True') kwlist.append('null=True')
if kwlist: return ', ' + ', '.join(kwlist) if field_name.lower() in blank_fields:
else: return '' kwlist.append('blank=True')
if kwlist:
return ', ' + ', '.join(kwlist)
else:
return ''
# For those wishing to disable the imports. # For those wishing to disable the imports.
if imports: if imports:
@ -167,7 +174,8 @@ def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=Non
for field_name, width, precision, field_type in zip(ogr_fields, layer.field_widths, layer.field_precisions, layer.field_types): for field_name, width, precision, field_type in zip(ogr_fields, layer.field_widths, layer.field_precisions, layer.field_types):
# The model field name. # The model field name.
mfield = field_name.lower() mfield = field_name.lower()
if mfield[-1:] == '_': mfield += 'field' if mfield[-1:] == '_':
mfield += 'field'
# Getting the keyword args string. # Getting the keyword args string.
kwargs_str = get_kwargs_str(field_name) kwargs_str = get_kwargs_str(field_name)

View File

@ -27,7 +27,8 @@ def color_style():
DJANGO_COLORS = os.environ.get('DJANGO_COLORS', '') DJANGO_COLORS = os.environ.get('DJANGO_COLORS', '')
color_settings = termcolors.parse_color_setting(DJANGO_COLORS) color_settings = termcolors.parse_color_setting(DJANGO_COLORS)
if color_settings: if color_settings:
class dummy: pass class dummy:
pass
style = dummy() style = dummy()
# The nocolor palette has all available roles. # The nocolor palette has all available roles.
# Use that palette as the basis for populating # Use that palette as the basis for populating

View File

@ -48,7 +48,8 @@ class ErrorDict(dict):
return self.as_ul() return self.as_ul()
def as_ul(self): def as_ul(self):
if not self: return '' if not self:
return ''
return format_html('<ul class="errorlist">{0}</ul>', return format_html('<ul class="errorlist">{0}</ul>',
format_html_join('', '<li>{0}{1}</li>', format_html_join('', '<li>{0}{1}</li>',
((k, force_text(v)) ((k, force_text(v))
@ -67,7 +68,8 @@ class ErrorList(list):
return self.as_ul() return self.as_ul()
def as_ul(self): def as_ul(self):
if not self: return '' if not self:
return ''
return format_html('<ul class="errorlist">{0}</ul>', return format_html('<ul class="errorlist">{0}</ul>',
format_html_join('', '<li>{0}</li>', format_html_join('', '<li>{0}</li>',
((force_text(e),) for e in self) ((force_text(e),) for e in self)
@ -75,7 +77,8 @@ class ErrorList(list):
) )
def as_text(self): def as_text(self):
if not self: return '' if not self:
return ''
return '\n'.join('* %s' % force_text(e) for e in self) return '\n'.join('* %s' % force_text(e) for e in self)
def __repr__(self): def __repr__(self):

View File

@ -273,7 +273,8 @@ class PasswordInput(TextInput):
self.render_value = render_value self.render_value = render_value
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
if not self.render_value: value=None if not self.render_value:
value=None
return super(PasswordInput, self).render(name, value, attrs) return super(PasswordInput, self).render(name, value, attrs)
class HiddenInput(Input): class HiddenInput(Input):
@ -291,7 +292,8 @@ class MultipleHiddenInput(HiddenInput):
self.choices = choices self.choices = choices
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None, choices=()):
if value is None: value = [] if value is None:
value = []
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
id_ = final_attrs.get('id', None) id_ = final_attrs.get('id', None)
inputs = [] inputs = []
@ -394,7 +396,8 @@ class Textarea(Widget):
super(Textarea, self).__init__(default_attrs) super(Textarea, self).__init__(default_attrs)
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
if value is None: value = '' if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
return format_html('<textarea{0}>\r\n{1}</textarea>', return format_html('<textarea{0}>\r\n{1}</textarea>',
flatatt(final_attrs), flatatt(final_attrs),
@ -469,7 +472,8 @@ class Select(Widget):
self.choices = list(choices) self.choices = list(choices)
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None, choices=()):
if value is None: value = '' if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select{0}>', flatatt(final_attrs))] output = [format_html('<select{0}>', flatatt(final_attrs))]
options = self.render_options(choices, [value]) options = self.render_options(choices, [value])
@ -539,7 +543,8 @@ class SelectMultiple(Select):
allow_multiple_selected = True allow_multiple_selected = True
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None, choices=()):
if value is None: value = [] if value is None:
value = []
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select multiple="multiple"{0}>', flatatt(final_attrs))] output = [format_html('<select multiple="multiple"{0}>', flatatt(final_attrs))]
options = self.render_options(choices, value) options = self.render_options(choices, value)

View File

@ -234,8 +234,10 @@ def get_text_list(list_, last_word=ugettext_lazy('or')):
>>> get_text_list([]) >>> get_text_list([])
'' ''
""" """
if len(list_) == 0: return '' if len(list_) == 0:
if len(list_) == 1: return force_text(list_[0]) return ''
if len(list_) == 1:
return force_text(list_[0])
return '%s %s %s' % ( return '%s %s %s' % (
# Translators: This string is used as a separator between list elements # Translators: This string is used as a separator between list elements
_(', ').join(force_text(i) for i in list_[:-1]), _(', ').join(force_text(i) for i in list_[:-1]),

View File

@ -7,7 +7,8 @@ from django.utils.encoding import force_text
from django.utils.safestring import mark_safe, SafeData from django.utils.safestring import mark_safe, SafeData
def ngettext(singular, plural, number): def ngettext(singular, plural, number):
if number == 1: return singular if number == 1:
return singular
return plural return plural
ngettext_lazy = ngettext ngettext_lazy = ngettext

View File

@ -7,7 +7,8 @@ from xml.sax.saxutils import XMLGenerator
class SimplerXMLGenerator(XMLGenerator): class SimplerXMLGenerator(XMLGenerator):
def addQuickElement(self, name, contents=None, attrs=None): def addQuickElement(self, name, contents=None, attrs=None):
"Convenience method for adding an element with no children" "Convenience method for adding an element with no children"
if attrs is None: attrs = {} if attrs is None:
attrs = {}
self.startElement(name, attrs) self.startElement(name, attrs)
if contents is not None: if contents is not None:
self.characters(contents) self.characters(contents)

View File

@ -4,7 +4,7 @@ install-script = scripts/rpm-install.sh
[flake8] [flake8]
exclude=./django/utils/dictconfig.py,./django/contrib/comments/*,./django/utils/unittest.py,./tests/comment_tests/*,./django/test/_doctest.py exclude=./django/utils/dictconfig.py,./django/contrib/comments/*,./django/utils/unittest.py,./tests/comment_tests/*,./django/test/_doctest.py
ignore=E123,E124,E125,E126,E127,E128,E225,E226,E241,E251,E302,E501,E121,E122,E203,E221,E227,E231,E261,E301,E303,E502,E701,F401,F403,F841,W601 ignore=E123,E124,E125,E126,E127,E128,E225,E226,E241,E251,E302,E501,E121,E122,E203,E221,E227,E231,E261,E301,E303,E502,F401,F403,F841,W601
[metadata] [metadata]
license-file = LICENSE license-file = LICENSE

View File

@ -94,8 +94,11 @@ class DecoratorsTest(TestCase):
callback = user_passes_test(test1)(callback) callback = user_passes_test(test1)(callback)
callback = user_passes_test(test2)(callback) callback = user_passes_test(test2)(callback)
class DummyUser(object): pass class DummyUser(object):
class DummyRequest(object): pass pass
class DummyRequest(object):
pass
request = DummyRequest() request = DummyRequest()
request.user = DummyUser() request.user = DummyUser()

View File

@ -224,7 +224,8 @@ class FormsErrorMessagesTestCase(TestCase, AssertFormErrorsMixin):
return self.as_divs() return self.as_divs()
def as_divs(self): def as_divs(self):
if not self: return '' if not self:
return ''
return mark_safe('<div class="error">%s</div>' % ''.join('<p>%s</p>' % e for e in self)) return mark_safe('<div class="error">%s</div>' % ''.join('<p>%s</p>' % e for e in self))
# This form should print errors the default way. # This form should print errors the default way.

View File

@ -727,7 +727,8 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin):
return self.as_divs() return self.as_divs()
def as_divs(self): def as_divs(self):
if not self: return '' if not self:
return ''
return '<div class="errorlist">%s</div>' % ''.join('<div class="error">%s</div>' % force_text(e) for e in self) return '<div class="errorlist">%s</div>' % ''.join('<div class="error">%s</div>' % force_text(e) for e in self)
class CommentForm(Form): class CommentForm(Form):

View File

@ -49,7 +49,8 @@ class FormsUtilTestCase(TestCase):
@python_2_unicode_compatible @python_2_unicode_compatible
class VeryBadError: class VeryBadError:
def __str__(self): return "A very bad error." def __str__(self):
return "A very bad error."
# Can take a non-string. # Can take a non-string.
self.assertHTMLEqual(str(ErrorList(ValidationError(VeryBadError()).messages)), self.assertHTMLEqual(str(ErrorList(ValidationError(VeryBadError()).messages)),