Skip to content

Instantly share code, notes, and snippets.

@tswicegood
Created June 20, 2009 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tswicegood/133321 to your computer and use it in GitHub Desktop.
Save tswicegood/133321 to your computer and use it in GitHub Desktop.
def feature_kwargs(self, feat):
"""
Given an OGR Feature, this will return a dictionary of keyword arguments
for constructing the mapped model.
Note: This is a reimplementation of GeoDjango's LayerMapping.feature_kwargs
because they do all of the work inside the method.
"""
# The keyword arguments for model construction.
kwargs = {}
# Incrementing through each model field and OGR field in the
# dictionary mapping.
for field_name, ogr_name in self.mapping.items():
model_field = self.fields[field_name]
val = self.generate_feature_kwarg_val(feat, model_field, ogr_name)
# Setting the keyword arguments for the field name with the
# value obtained above.
kwargs[field_name] = val
return kwargs
def generate_feature_kwarg_val(self, feat, model_field, ogr_name):
if isinstance(model_field, GeometryField):
# Verify OGR geometry.
return self.verify_geom(feat.geom, model_field)
elif isinstance(model_field, models.base.ModelBase):
# The related _model_, not a field was passed in -- indicating
# another mapping for the related Model.
return self.verify_fk(feat, model_field, ogr_name)
else:
# Otherwise, verify OGR Field type.
return self.verify_ogr_field(feat[ogr_name], model_field)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment