简体   繁体   English

Django LayerMapping:如何在保存到数据库之前过滤形状文件

[英]Django LayerMapping: How to filter shape-file before saving to database

I have a shape-file which I want to import to a django database using the Django-LayerMapping module ( Link to module ), because it transforms the spatial data into GeoDjango models.我有一个形状文件,我想使用 Django-LayerMapping 模块( 链接到模块)将其导入 django 数据库,因为它将空间数据转换为 GeoDjango 模型。 The vanilla way to import a shape-file to the db according to the tutorial is as follows:根据教程将形状文件导入数据库的香草方法如下:

lm = LayerMapping(table, path, mapping, transform=True , encoding='utf-8') # load shape-file
lm.save(verbose=True) # save entire file to db

But in my case, the table to which I want to import the shape-file data is not empty.但在我的情况下,我想导入形状文件数据的表不是空的。 I only want to add those rows (or features in shape-file lingo) to the db which are not already present.我只想将那些尚不存在的行(或形状文件术语中的功能)添加到数据库中。 However, the LayerMapping only provides a method to save an entire shape-file to the db, not single entries, which would result in duplicates in my case.但是,LayerMapping 只提供了一种将整个形状文件保存到数据库的方法,而不是单个条目,在我的情况下这会导致重复。

Thus, my question is: how can I filter the entries of a layer mapping-object before saving it?因此,我的问题是:如何在保存图层映射对象之前过滤它的条目?

Until now, I thought about two possible solutions:到现在为止,我想到了两种可能的解决方案:

  1. Filter the entries in the layer mapping object and saving the entire object with the .save()-method that is provided.过滤图层映射对象中的条目并使用提供的 .save() 方法保存整个对象。 But I don't know how to delete single entries from a layer mapping object.但我不知道如何从图层映射对象中删除单个条目。

  2. Iterate through all entries in the layer mapping object and check for each if it is already present in the database and only save it if it is not present.遍历图层映射对象中的所有条目,并检查每个条目是否已存在于数据库中,如果不存在则仅保存它。 However, I didn't find a layer-mapping-method to save single entries to the db.但是,我没有找到将单个条目保存到数据库的图层映射方法。 It would be possible to just read the attributes and create the objects myself but then I wouldn't have access to the coordinate-transformation which was the initial reason to use layer mapping-module.可以只读取属性并自己创建对象,但是我将无法访问坐标转换,这是使用图层映射模块的最初原因。

So the question remains the same: How can I filter this layer-mapping object before saving it?所以问题保持不变:如何在保存此图层映射对象之前对其进行过滤?

An option worth trying with LayerMapping is the unique argument that:一个值得尝试使用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' .强制事务模式为'autocommit'

Checking the code executed in case of an existing unique name we can see that it tries to append the given geometry to any existing records:检查在存在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)

If this is what fits your needs as functionality, then you can try the unique option as follows:如果这符合您的功能需求,那么您可以尝试以下独特的选项:

lm = LayerMapping(
    table, 
    path, 
    mapping, 
    transform=True , 
    unique=('field_name_1', 'field_name_2', ...), 
    encoding='utf-8'
)

If the above does not fit the needs of your project, then the options you mention will work fine.如果以上不适合您项目的需要,那么您提到的选项将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何从文本文件创建点形状文件 - Python: how to create a point shape-file from a text file Django 如何在保存到数据库之前连接表单数据 - Django how to concatenate form data before saving to the database 如何在 python Django 中保存到数据库之前进行逻辑操作 - How to do a logical operation before saving to database in python Django Django:如何在发布请求后将数据保存到数据库之前如何检查数据是否正确? - Django: How to check if data is correct before saving it to a database on a post request? 保存到数据库之前在Django中添加到用户窗体 - Adding to userform in django before saving in database Django - 在最终保存之前编辑上传的文件 - Django - edit uploaded file Before final saving Django:使用LayerMapping更新现有模型? - Django: Use LayerMapping to update an existing model? 如何在将字段保存到 Django 之前覆盖字段? - How to override field before saving it in Django? 在将实例保存到数据库之前/之后是否触发了Django post_save? - Is Django post_save triggered before/after saving instance to database? 将密码保存到django的数据库表之前的最佳散列方法 - Best hashing method before saving password into database table for django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM