Geodjango查询几何返回指针而不是几何

发布于 2025-01-18 11:26:27 字数 759 浏览 3 评论 0原文

我试图从这个模型中获取查询集中的单个 MultiPolygon 几何图形:

class local_administrative_unit(models.Model):
    lau_id = models.IntegerField(primary_key=True)
    lau_name = models.CharField(max_length=150)
    adm_level_2 = models.ForeignKey('administrative_level_2', on_delete=models.PROTECT)
    geom = models.MultiPolygonField(srid=4326)

在 Django shell 中尝试这种方式:

local_administrative_unit.objects.get(lau_id=1).geom

返回:

<MultiPolygon object at 0x7fb12af0ab10>

当我将其传递给 Centroid 函数时,它与我的不同寻找:

Centroid(Value(<MultiPolygon object at 0x7fb12af0ac90>))

您能告诉我如何获得实际的几何图形以便之后使用它 - 例如计算该多边形的质心吗?看起来我得到了一个指向我正在寻找的东西的指针,而不是实际的东西。

提前致谢。

I am trying to get a single MultiPolygon geometry in a Queryset from this model:

class local_administrative_unit(models.Model):
    lau_id = models.IntegerField(primary_key=True)
    lau_name = models.CharField(max_length=150)
    adm_level_2 = models.ForeignKey('administrative_level_2', on_delete=models.PROTECT)
    geom = models.MultiPolygonField(srid=4326)

trying it this way in the Django shell:

local_administrative_unit.objects.get(lau_id=1).geom

which returned:

<MultiPolygon object at 0x7fb12af0ab10>

when I pass this to the Centroid function, it does not what I was looking for:

Centroid(Value(<MultiPolygon object at 0x7fb12af0ac90>))

Can you please tell me how I get the actual geometry to use it afterwards - for example for calculating the Centroid of that polygon? Looks like I am getting a pointer to what I am looking for instead of the actual thing.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2025-01-25 11:26:27

同时,我找到了我错过的东西。基本上,所有这些都在。 Geodjango首先访问几何形状时,创建A 地理测定法对象,然后可以使用某些属性来做事。
在上面的情况下,这意味着:

foo = local_administrative_unit.objects.get(lau_id=1).geom
print(foo)
<MultiPolygon object at 0x7fb12af0ab10>

foo 将几何对象作为初始示例中的几何对象,要以通常可用的形式获取质心,我们可以使用一些属性:

bar = foo.centroid
bar.wkt

这将计算质心和返回然后在著名的文本中。

Meanwhile I found what i missed. It is basically all explained in the GeoDjango Tutorial which i appearently overlooked before. GeoDjango creates a GEOSGeometry object when the geometry is first accessed on which certain properties then can be used to do stuff.
In the above case this means:

foo = local_administrative_unit.objects.get(lau_id=1).geom
print(foo)
<MultiPolygon object at 0x7fb12af0ab10>

foo holds the geometry object as the one in the initial example, to get the centroid in a generally useable form we can just use some properties:

bar = foo.centroid
bar.wkt

This will calculate the centroid and return it then in well-known text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文