Django LayerMapping:如何在保存到数据库之前过滤形状文件
Django LayerMapping: How to filter shape-file before saving to database
我有一个形状文件,我想使用 Django-LayerMapping 模块导入到 django 数据库中(Link to module), because it transforms the spatial data into GeoDjango models. The vanilla way to import a shape-file to the db according to the tutorial 如下:
lm = LayerMapping(table, path, mapping, transform=True , encoding='utf-8') # load shape-file
lm.save(verbose=True) # save entire file to db
但在我的例子中,我要导入形状文件数据的 table 不是空的。我只想将那些尚不存在的行(或形状文件术语中的特征)添加到数据库中。但是,LayerMapping 只提供了一种将整个形状文件保存到数据库的方法,而不是单个条目,这在我的情况下会导致重复。
因此,我的问题是:如何在保存之前过滤图层映射对象的条目?
到目前为止,我想到了两种可能的解决方案:
过滤图层映射对象中的条目并使用提供的 .save() 方法保存整个对象。但是我不知道如何从图层映射对象中删除单个条目。
遍历层映射对象中的所有条目并检查每个条目是否已存在于数据库中,如果不存在则仅保存。但是,我没有找到将单个条目保存到数据库的图层映射方法。可以只读取属性并自己创建对象,但那样我就无法访问坐标转换,这是使用图层映射模块的最初原因。
所以问题还是一样:如何在保存之前过滤这个图层映射对象?
LayerMapping
值得尝试的一个选项是 unique
论点:
Setting this to the name, or a tuple of names, from the given model, will create models unique only to the given name(s). Geometries from each feature will be added into the collection associated with the unique model. Forces the transaction mode to be 'autocommit'
.
在现有 unique
名称的情况下检查 code executed 我们可以看到它试图将给定的几何附加到任何现有记录:
if self.unique:
# If we want unique models on a particular field, handle the
# geometry appropriately.
try:
# Getting the keyword arguments and retrieving
# the unique model.
u_kwargs = self.unique_kwargs(kwargs)
m = self.model.objects.using(self.using).get(**u_kwargs)
is_update = True
# Getting the geometry (in OGR form), creating
# one from the kwargs WKT, adding in additional
# geometries, and update the attribute with the
# just-updated geometry WKT.
geom_value = getattr(m, self.geom_field)
if geom_value is None:
geom = OGRGeometry(kwargs[self.geom_field])
else:
geom = geom_value.ogr
new = OGRGeometry(kwargs[self.geom_field])
for g in new:
geom.add(g)
setattr(m, self.geom_field, geom.wkt)
except ObjectDoesNotExist:
# No unique model exists yet, create.
m = self.model(**kwargs)
如果这符合您的功能需求,那么您可以尝试如下独特的选项:
lm = LayerMapping(
table,
path,
mapping,
transform=True ,
unique=('field_name_1', 'field_name_2', ...),
encoding='utf-8'
)
如果以上不符合您项目的需要,那么您提到的选项也可以。
我有一个形状文件,我想使用 Django-LayerMapping 模块导入到 django 数据库中(Link to module), because it transforms the spatial data into GeoDjango models. The vanilla way to import a shape-file to the db according to the tutorial 如下:
lm = LayerMapping(table, path, mapping, transform=True , encoding='utf-8') # load shape-file
lm.save(verbose=True) # save entire file to db
但在我的例子中,我要导入形状文件数据的 table 不是空的。我只想将那些尚不存在的行(或形状文件术语中的特征)添加到数据库中。但是,LayerMapping 只提供了一种将整个形状文件保存到数据库的方法,而不是单个条目,这在我的情况下会导致重复。
因此,我的问题是:如何在保存之前过滤图层映射对象的条目?
到目前为止,我想到了两种可能的解决方案:
过滤图层映射对象中的条目并使用提供的 .save() 方法保存整个对象。但是我不知道如何从图层映射对象中删除单个条目。
遍历层映射对象中的所有条目并检查每个条目是否已存在于数据库中,如果不存在则仅保存。但是,我没有找到将单个条目保存到数据库的图层映射方法。可以只读取属性并自己创建对象,但那样我就无法访问坐标转换,这是使用图层映射模块的最初原因。
所以问题还是一样:如何在保存之前过滤这个图层映射对象?
LayerMapping
值得尝试的一个选项是 unique
论点:
Setting this to the name, or a tuple of names, from the given model, will create models unique only to the given name(s). Geometries from each feature will be added into the collection associated with the unique model. Forces the transaction mode to be
'autocommit'
.
在现有 unique
名称的情况下检查 code executed 我们可以看到它试图将给定的几何附加到任何现有记录:
if self.unique:
# If we want unique models on a particular field, handle the
# geometry appropriately.
try:
# Getting the keyword arguments and retrieving
# the unique model.
u_kwargs = self.unique_kwargs(kwargs)
m = self.model.objects.using(self.using).get(**u_kwargs)
is_update = True
# Getting the geometry (in OGR form), creating
# one from the kwargs WKT, adding in additional
# geometries, and update the attribute with the
# just-updated geometry WKT.
geom_value = getattr(m, self.geom_field)
if geom_value is None:
geom = OGRGeometry(kwargs[self.geom_field])
else:
geom = geom_value.ogr
new = OGRGeometry(kwargs[self.geom_field])
for g in new:
geom.add(g)
setattr(m, self.geom_field, geom.wkt)
except ObjectDoesNotExist:
# No unique model exists yet, create.
m = self.model(**kwargs)
如果这符合您的功能需求,那么您可以尝试如下独特的选项:
lm = LayerMapping(
table,
path,
mapping,
transform=True ,
unique=('field_name_1', 'field_name_2', ...),
encoding='utf-8'
)
如果以上不符合您项目的需要,那么您提到的选项也可以。