MCPcopy
hub / github.com/django/django / check_layer

Method check_layer

django/contrib/gis/utils/layermapping.py:192–308  ·  view source on GitHub ↗

Check the Layer metadata and ensure that it's compatible with the mapping information and model. Unlike previous revisions, there is no need to increment through each feature in the Layer.

(self)

Source from the content-addressed store, hash-verified

190 return None
191
192 def check_layer(self):
193 """
194 Check the Layer metadata and ensure that it's compatible with the
195 mapping information and model. Unlike previous revisions, there is no
196 need to increment through each feature in the Layer.
197 """
198 # The geometry field of the model is set here.
199 # TODO: Support more than one geometry field / model. However, this
200 # depends on the GDAL Driver in use.
201 self.geom_field = False
202 self.fields = {}
203
204 # Getting lists of the field names and the field types available in
205 # the OGR Layer.
206 ogr_fields = self.layer.fields
207 ogr_field_types = self.layer.field_types
208
209 # Function for determining if the OGR mapping field is in the Layer.
210 def check_ogr_fld(ogr_map_fld):
211 try:
212 idx = ogr_fields.index(ogr_map_fld)
213 except ValueError:
214 raise LayerMapError(
215 'Given mapping OGR field "%s" not found in OGR Layer.' % ogr_map_fld
216 )
217 return idx
218
219 # No need to increment through each feature in the model, simply check
220 # the Layer metadata against what was given in the mapping dictionary.
221 for field_name, ogr_name in self.mapping.items():
222 # Ensuring that a corresponding field exists in the model
223 # for the given field name in the mapping.
224 try:
225 model_field = self.model._meta.get_field(field_name)
226 except FieldDoesNotExist:
227 raise LayerMapError(
228 'Given mapping field "%s" not in given Model fields.' % field_name
229 )
230
231 # Getting the string name for the Django field class (e.g.,
232 # 'PointField').
233 fld_name = model_field.__class__.__name__
234
235 if isinstance(model_field, GeometryField):
236 if self.geom_field:
237 raise LayerMapError(
238 "LayerMapping does not support more than one GeometryField per "
239 "model."
240 )
241
242 # Getting the coordinate dimension of the geometry field.
243 coord_dim = model_field.dim
244
245 try:
246 if coord_dim == 3:
247 gtype = OGRGeomType(ogr_name + "25D")
248 else:
249 gtype = OGRGeomType(ogr_name)

Callers 1

__init__Method · 0.95

Calls 5

make_multiMethod · 0.95
OGRGeomTypeClass · 0.90
LayerMapErrorClass · 0.85
itemsMethod · 0.45
get_fieldMethod · 0.45

Tested by

no test coverage detected