Extract the coordinates from the LineStringField

别来无恙 提交于 2021-01-29 04:48:39

问题


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)

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)

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 %}
    ]
}

But I see this:

{
            "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]
          ]
            }
            }

Instead of this:

{
      "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?


回答1:


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.

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]

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 %}
    ]
}

Doing your own serialization in this specific case has no benefit that I can see and I would suggest refactoring your process altogether to utilize the GeoJson Serializer:

from django.core.serializers import serialize

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

and in the template simply access the geometry context field:

var data_source = {{ geometry }}


来源:https://stackoverflow.com/questions/58766842/extract-the-coordinates-from-the-linestringfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!