从LineStringField提取坐标 [英] Extract the coordinates from the LineStringField

查看:88
本文介绍了从LineStringField提取坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自GeoDjango的简单模型,用于线向量:

I've this simple model from GeoDjango for a line vector:

    from django.contrib.gis.db import models
    class LineBuffer(models.Model):
        geom = models.LineStringField()

        def __int__(self):
            return self.pk

        @property
        def coordinates(self):
            return str(self.geom.x) + ', ' + str(self.geom.y)

我需要使用Turf.js创建一个缓冲区;结果将使用MapBox重新显示.

I need to create a buffer using Turf.js; the results will be redered using MapBox.

使用此视图,我可以创建地图:

With this view I create my map:

def line_mapbox_turf_buffer(request):
    geometry = LineBuffer.objects.all()
    context = {
        'geometry': geometry,
    }
    template = 'buffer/reading/line_mapbox_turf_buffer.html'
    return render(request, template, context)

我尝试生成GeoJSON

I try to generate the GeoJSON

var data_source = {
    "type": "FeatureCollection",
    "features": [{% for d in geometry %}
        {
        "type": "Feature",
        "properties": {
            "pk": "{{ d.pk }}"
        },
        "geometry": {
            "type": "LineString",
            "coordinates": [{{ d.coordinates }}]
        }
        {% if forloop.last %}} {% else %}}, {% endif %}{% endfor %}
    ]
}

但是我看到了:

{
            "type": "Feature",
            "properties": {
                "pk": "1"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
          [14.364295, 14.3662612, 14.3681209, 14.3702697, 14.3730481, 14.3742791, 14.3763224], 
          [40.8086793, 40.8101317, 40.8118721, 40.8139257, 40.8165981, 40.8177693, 40.8206666]
          ]
            }
            }

代替此:

{
      "type": "Feature",
      "properties": {
        "pk": "1"
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [14.364295,40.8086793],
          [14.3662612,40.8101317],
          [14.3681209,40.8118721],
          [14.3702697,40.8139257],
          [14.3730481,40.8165981],
          [14.3742791,40.8177693],
          [14.3763224,40.8206666]
        ]
      }
    }

我认为我的问题是属性 坐标.如何正确提取线向量的坐标?

I think that my problem is the property coordinates. How I can extract correctly the coordinates of a line vector?

推荐答案

在错误中做一个简单的验尸,我们可以清楚地看到 coordinates 属性的响应需要重构为满足您的需求.

Well doing a simple post mortem in the error, we can plainly see that the response of the coordinates property needs to be refactored to fit your needs.

我建议使用 geom.coords 属性访问和操作 LineString 的坐标.
Django的 LineString 坐标看起来像这样:((x_0,y_0),(x_1,y_1),...)所以很清楚我们需要做什么:

I would suggest using the geom.coords property to access and manipulate the LineString's coordinates.
Django's LineString coords will look like this: ((x_0, y_0), (x_1, y_1), ...) so it is quite clear what we need to do:

@property
def coordinates(self):
    return [list(coord_pair) for coords_pair in self.geom.coords]

,并且在模板中,当我们使用 coordinates 属性时,我们需要省略 list 强制转换:

and from the template, we need to omit the list cast when we use the coordinates property:

var data_source = {
    "type": "FeatureCollection",
    "features": [{% for d in geometry %}
        {
        "type": "Feature",
        "properties": {
            "pk": "{{ d.pk }}"
        },
        "geometry": {
            "type": "LineString",
            "coordinates": {{ d.coordinates }}
        }
        {% if forloop.last %}} {% else %}}, {% endif %}{% endfor %}
    ]
}


在这种情况下进行自己的序列化没有任何好处,我建议完全重构您的流程以利用,并且在模板中只需访问 geometry 上下文字段:

and in the template simply access the geometry context field:

var data_source = {{ geometry }}

这篇关于从LineStringField提取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆