peewee_如何在peewee中使用postgis几何

peewee

It’s astonishing to me how many people have this problem, and nobody thought to write up a solution to make other people’s searches a little easier. So, here I am, to save you some time with your Peewee and PostGIS adventures.

令我惊讶的是,有多少人遇到了这个问题,没有人想到编写解决方案来使其他人的搜索更加轻松。 因此,我在这里为您节省了Peewee和PostGIS冒险的时间。

Let’s get straight to it!

让我们直接开始吧!

确保已安装PostGIS (Make sure PostGIS is installed)

This might seem trivial. But, when you don’t understand why your solution isn’t working even though you did every step only to realize PostGIS wasn’t installed, then you’d wish you checked it at first. It’s a small check that will save you some time down the road.

这似乎是微不足道的。 但是,即使您做了所有步骤只是为了意识到未安装PostGIS仍不理解您的解决方案为何不起作用,那么您希望首先检查一下它。 这是一张小支票,可以为您节省一些时间。

If you’re using a tool like PGAdmin with a local installation of PostgreSQL, go to the query tool and type in this command:

如果您正在本地安装PostgreSQL时使用PGAdmin之类的工具,请转到查询工具并输入以下命令:

CREATE EXTENSION postgis CASCADE;

This call will ensure that every other extension that PostGIS relies on will be installed as well. It will also let you know whether the extension is already there.

此调用将确保安装PostGIS依赖的所有其他扩展。 它还会告诉您扩展名是否已经存在。

The more likely scenario is that you’re using Docker intending to either integrate it into a Kubernetes cluster, or you just don’t want to clutter your system with a DB installation.

更有可能的情况是,您正在使用Docker,打算将其集成到Kubernetes集群中,或者只是不想通过DB安装使系统混乱。

In that case, you might want to pull from this docker registry. It seems like the cleanest one out there with updated versions of both PostgreSQL and PostGIS.

在这种情况下,您可能要从此docker Registry中拉取。 似乎是最干净的版本,其中包含PostgreSQL和PostGIS的更新版本。

用Peewee定义一个新字段 (Define a new Field with Peewee)

Peewee itself provides a class you can inherit from called Field. That will give you the ability to customize your own fields. Now, what we want to create is a field that correlates with an existing one in the database — the geometryfield introduced by PostGIS.

Peewee本身提供了一个可以从其继承的类Field 。 这将使您能够自定义自己的字段 。 现在,我们要创建的字段是与数据库中现有字段相关的字段-PostGIS引入的geometry字段。

The basic implementation of a new field in Peewee requires three things:

Peewee中新字段的基本实现需要三件事:

  1. The field name — this is represented in the name of the class you’re creating.

    字段名称-以您正在创建的类的名称表示。
  2. A function that defines the type of data when we insert into the DB.

    当我们插入数据库时​​,定义数据类型的函数。
  3. A function that defines the type of data when we pull from the DB.

    一个从数据库中提取数据时定义数据类型的函数。

Once you know how you want all three to work, you can do something like this:

一旦知道了您希望这三者如何工作,就可以执行以下操作:

class GeometryField(Field):
db_field = 'geometry'def db_value(self, value):
return fn.ST_GeomFromGeoJSON(value)def python_value(self, value):
return <use a python package like shapely to parse the data>

the functions db_value and python_value are provided by Peewee to manage the insertion and extraction of info from the DB. The db_valuefunction means the value goes through this transformation, in this case, a PostGIS geom before we save it into the DB. The python_value function means that after we extract the data from the DB, we’ll get it after it went through whatever we defined in that function.

Peewee提供了db_valuepython_value函数来管理从数据库中插入和提取信息。 db_value函数表示该值经过此转换(在本例中为PostGIS geom然后再将其保存到数据库中。 python_value函数意味着从数据库中提取数据之后,我们将通过该函数中定义的任何数据来获取它。

Then, the next step is to use this new field in a model:

然后,下一步是在模型中使用此新字段:

class ExampleModel(Model):
geometry_field = GeometryField()
geom_id = UUIDField(primary_key=True)

As a side-note: fn is provided by Peewee to access functions in the DB. In this case the functions ST_AsGeoJSON and ST_GeomFromGeoJSON are supplied by PostGIS. There are many more functions that come with PostGIS if you have anything else in mind.

附带说明:Peewee提供了fn来访问数据库中的功能。 在这种情况下,函数ST_AsGeoJSONST_GeomFromGeoJSON由PostGIS提供。 如果您还有其他想法,PostGIS会附带许多其他功能

结论 (Conclusion)

We brought together Peewee and PostGIS by defining a new field that connects the ORM and the extension by defining a db_field that tells Peewee what field to work with. Hopefully, this has been helpful in your own endeavors to use PostGIS with Peewee.

我们通过定义一个新的字段来将Peewee和PostGIS结合在一起,该字段通过将告诉Peewee使用哪个字段的db_field来定义连接ORM和扩展的字段。 希望这对您自己在Peewee中使用PostGIS的工作有所帮助。

Good luck and let me know if you have any questions! You can find me on Twitter as well.

祝您好运,如有任何疑问,请与我联系! 您也可以在Twitter上找到我。

翻译自: https://medium.com/a-geeks-blog/how-to-use-a-postgis-geometry-with-peewee-646217bedb38

peewee

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值