简体   繁体   English

如何在GeoDjango中转换多边形?

[英]How to translate a polygon in GeoDjango?

I have a Place model which has a polygon field: 我有一个包含多边形字段的Place模型:

from django.contrib.gis.db import models

class Place(models.Model):
    area = models.PolygonField()

I want to translate the area field of an instance of this Place model. 我想翻译此Place模型的实例的area字段。

Here is how I am doing it right now: 这是我现在正在做的事情:

from django.contrib.gis.db.models.functions import Translate

place = Place.objects.get(pk=1)
place.area = Place.objects.filter(pk=place.pk).annotate(new_area=Translate('area', 0.001, 0.001)).first().new_area
place.save()

This seems very hacky. 这似乎很hacky。 I think that there should be a way of doing this in the following way: 我认为应该以以下方式实现此目的:

place = Place.objects.get(pk=1)
place.area = Translate(place.area, 0.001, 0.001)
place.save()

But this throws an exception. 但这引发了异常。

Should I be using some library? 我应该使用一些图书馆吗? Which one works well with GeoDjango and its models/fields? 哪一个适用于GeoDjango及其模型/字段?

What is the better way of doing it? 有什么更好的方法呢?

Note the "db" in django.contrib.gis.db.models.functions . 注意django.contrib.gis.db.models.functions的“ db”。 There aren't Python functions, these are classes (thus the initial capital in the names) that allow you to access the database system's spatial functions (usually provided by a spatial database extension like spatialite of SQLite or PostGIS for PostgreSQL). 没有Python函数,这些是类(因此,名称中的首字母大写)允许您访问数据库系统的空间功能(通常由空间数据库扩展(例如SQLite的空间数据库或PostgreSQL的PostGIS)提供)。 See the GeoDjango documentation 请参阅GeoDjango文档

The functions documented on this page allow users to access geographic database functions to be used in annotations , aggregations , or filters in Django. 此页面上记录的功能允许用户访问要在Django中的注释聚合过滤器中使用的地理数据库功能。

(Emphasis mine.) (强调我的。)

That is, these can only be used in queries , as the spatial calculation work of offloaded to the database. 也就是说,这些只能在查询中使用 ,因为空间计算工作被卸载到数据库中。

So what can you do instead of doing several queries? 那么,您可以做什么而不是做几个查询呢?

update query update查询

If you'd do this directly in SQL, you'd probably write an UPDATE query. 如果直接在SQL中执行此操作,则可能会编写UPDATE查询。 The Django ORM allows you to do the same from Django, with the update() method . Django ORM允许您使用update()方法从Django执行相同的操作。

It'd probably look something like this (untested): 它可能看起来像这样(未经测试):

Place.objects.filter(pk=1).update(area=Translate('area', 0.001, 0.001))

If you're using GeoDjango anyway, this is probably the preferred way. 如果仍然使用GeoDjango,则这可能是首选方式。

external (Python) library 外部(Python)库

As you suggested, you can use an external library instead of GeoDjango's Translate , eg shapely.affinity.translate from the shapely library . 如你所说,你可以使用一个外部库,而不是GeoDjango内置的的Translate ,如shapely.affinity.translateshapely However, I doubt it will accept GeoDjango's django.contrib.gis.geos.GEOSGeometry as input, so you might have to go through GeoJSON or WKT to convert back and forth. 但是,我怀疑它将接受GeoDjango的django.contrib.gis.geos.GEOSGeometry作为输入,因此您可能必须通过GeoJSON或WKT来回转换。

Use this approach if you need your spatial calculations to be independent of the underlying database system. 如果需要空间计算独立于基础数据库系统,请使用此方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM