Python Django LayerMapping:如何在保存到数据库之前过滤形状文件
我有一个shape文件,我想使用django LayerMapping模块()将其导入django数据库,因为它将空间数据转换为GeoDjango模型。根据将形状文件导入db的常规方法如下:Python Django LayerMapping:如何在保存到数据库之前过滤形状文件,python,django,import,shapefile,geodjango,Python,Django,Import,Shapefile,Geodjango,我有一个shape文件,我想使用django LayerMapping模块()将其导入django数据库,因为它将空间数据转换为GeoDjango模型。根据将形状文件导入db的常规方法如下: lm = LayerMapping(table, path, mapping, transform=True , encoding='utf-8') # load shape-file lm.save(verbose=True) # save entire file to db lm = LayerMap
lm = LayerMapping(table, path, mapping, transform=True , encoding='utf-8') # load shape-file
lm.save(verbose=True) # save entire file to db
lm = LayerMapping(
table,
path,
mapping,
transform=True ,
unique=('field_name_1', 'field_name_2', ...),
encoding='utf-8'
)
但在我的例子中,要将形状文件数据导入的表不是空的。我只想将那些尚未出现的行(或形状文件lingo中的特征)添加到db中。但是,LayerMapping只提供了一种将整个形状文件保存到db的方法,而不是单个条目,在我的例子中,这将导致重复
因此,我的问题是:如何在保存图层映射对象之前过滤该对象的条目
到目前为止,我一直在考虑两种可能的解决方案:
所以问题还是一样的:在保存这个图层映射对象之前,我如何过滤它?值得尝试的一个选项是
unique
参数:
将其设置为给定模型的名称或名称元组,将创建仅对给定名称唯一的模型。每个特征的几何图形将添加到与唯一模型关联的集合中。强制事务模式为“自动提交”
如果检查现有的唯一的名称,我们可以看到它试图将给定的几何体附加到任何现有记录中:
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 , encoding='utf-8') # load shape-file
lm.save(verbose=True) # save entire file to db
lm = LayerMapping(
table,
path,
mapping,
transform=True ,
unique=('field_name_1', 'field_name_2', ...),
encoding='utf-8'
)
如果上述内容不符合您项目的需要,那么您提到的选项将很好地工作。值得尝试的选项是unique
参数:
将其设置为给定模型的名称或名称元组,将创建仅对给定名称唯一的模型。每个特征的几何图形将添加到与唯一模型关联的集合中。强制事务模式为“自动提交”
如果检查现有的唯一的名称,我们可以看到它试图将给定的几何体附加到任何现有记录中:
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 , encoding='utf-8') # load shape-file
lm.save(verbose=True) # save entire file to db
lm = LayerMapping(
table,
path,
mapping,
transform=True ,
unique=('field_name_1', 'field_name_2', ...),
encoding='utf-8'
)
如果上述内容不符合您项目的需要,那么您提到的选项就可以了