Pointfield的纬度/经度小部件?

时间:2013-06-10 10:33:34

标签: django django-forms geodjango

PointField是否有一个小部件作为单独的纬度/经度输入?就像DateTimeField的SplitDateTimeWidget一样。

6 个答案:

答案 0 :(得分:8)

这是我的工作自定义字段和小部件:

class LatLongWidget(forms.MultiWidget):
    """
    A Widget that splits Point input into two latitude/longitude boxes.
    """

    def __init__(self, attrs=None, date_format=None, time_format=None):
        widgets = (forms.TextInput(attrs=attrs),
                   forms.TextInput(attrs=attrs))
        super(LatLongWidget, self).__init__(widgets, attrs)

    def decompress(self, value):
        if value:
            return tuple(reversed(value.coords))
        return (None, None)

class LatLongField(forms.MultiValueField):

    widget = LatLongWidget
    srid = 4326

    default_error_messages = {
        'invalid_latitude' : _('Enter a valid latitude.'),
        'invalid_longitude' : _('Enter a valid longitude.'),
    }

    def __init__(self, *args, **kwargs):
        fields = (forms.FloatField(min_value=-90, max_value=90), 
                  forms.FloatField(min_value=-180, max_value=180))
        super(LatLongField, self).__init__(fields, *args, **kwargs)

    def compress(self, data_list):
        if data_list:
            # Raise a validation error if latitude or longitude is empty
            # (possible if LatLongField has required=False).
            if data_list[0] in validators.EMPTY_VALUES:
                raise forms.ValidationError(self.error_messages['invalid_latitude'])
            if data_list[1] in validators.EMPTY_VALUES:
                raise forms.ValidationError(self.error_messages['invalid_longitude'])
            # SRID=4326;POINT(1.12345789 1.123456789)
            srid_str = 'SRID=%d'%self.srid
            point_str = 'POINT(%f %f)'%tuple(reversed(data_list))
            return ';'.join([srid_str, point_str])
        return None

答案 1 :(得分:4)

@Ramast以更加pythonic的方式处理同样的问题。我已将更改合并到我的代码中,总是可以改进。感觉很棒。


这就是我管理-at last的方式 - 为了保持格子和经度的单独字段而不必将它们保存在数据库中,因为这些值已经保存在PointField中。

这个想法是:

  • 如果我们要插入新条目,纬度和经度字段将用于设置PointField
  • 如果我们打开现有的PointField条目,它将用于在相关的FormFields中提供纬度和经度值。

models.py

from django.contrib.gis.db import models as geomodels


class Entry(geomodels.Model):
    point = geomodels.PointField(
        srid=4326,
        blank=True,
        )

admin.py

from myapp.forms import EntryForm
from django.contrib import admin


class EntryAdmin(admin.ModelAdmin):
    form = EntryForm


admin.site.register(Entry, EntryAdmin)

forms.py

from django import forms
from myapp.models import Entry
from django.contrib.gis.geos import Point


class MarketEntryForm(forms.ModelForm):

    latitude = forms.FloatField(
        min_value=-90,
        max_value=90,
        required=True,
    )
    longitude = forms.FloatField(
        min_value=-180,
        max_value=180,
        required=True,
    )

    class Meta(object):
        model = MarketEntry
        exclude = []
        widgets = {'point': forms.HiddenInput()}

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        coordinates = self.initial.get('point', None)
        if isinstance(coordinates, Point):
            self.initial['longitude'], self.initial['latitude'] = coordinates.tuple

    def clean(self):
        data = super().clean()
        latitude = data.get('latitude')
        longitude = data.get('longitude')
        point = data.get('point')
        if latitude and longitude and not point:
            data['point'] = Point(longitude, latitude)
        return data

答案 2 :(得分:3)

这是基于@ rara_tiru的解决方案,但有一些改进

class GisForm(forms.ModelForm):
    """
    This form can be used by any model that has "coordinates" field.
    It will show a better looking map than the default one
    """
    lat = forms.FloatField(required=False)
    lng = forms.FloatField(required=False)
    coordinates = PointField(widget=CustomPointWidget(), required=False, srid=4326)

    def __init__(self, *args, **kwargs):
        super(GisForm, self).__init__(*args, **kwargs)
        coordinates = self.initial.get("coordinates", None)
        if isinstance(coordinates, Point):
            self.initial["lng"], self.initial["lat"] = coordinates.tuple

    def clean(self):
        data = super(GisForm, self).clean()
        if "lat" in self.changed_data or "lng" in self.changed_data:
            lat, lng = data.pop("lat", None), data.pop("lng", None)
            data["coordinates"] = Point(lng, lat, srid=4326)

        if not (data.get("coordinates") or data.get("lat")):
            raise forms.ValidationError({"coordinates": "Coordinates is required"})
        return data

这基本上允许用户在地图上设置位置或手动输入lat / lng值。保存后,地图将反映特定的纬度/经度值

答案 3 :(得分:3)

答案非常适合构建您自己的解决方案,但如果您想要一个看起来很好且易于使用的简单解决方案,请尝试: http://django-map-widgets.readthedocs.io/en/latest/index.html

你可以:

  • 输入坐标
  • 直接在地图上选择
  • 从Google地方查找商品。

Screen Shot from Django Map Widgets

答案 4 :(得分:0)

为此创建自定义小部件,您可以受到SelectDateWidget here的启发。

答案 5 :(得分:0)

我自己也在寻找这个问题的答案。作为初学者,我对许多不同的解决方案感到非常困惑。但我能够使用以下代码实现它。

  1. 填充 PointField 只需要输入纬度和经度文本,不需要输入地图,表单中也没有地图
  2. PointField 可以有空值
  3. 验证 Lat Long 值的组合是否设置正确,即两者都不应该为空或都应该为空。

模型类

class DataSet(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20, blank=False, null=False, unique=True)
    latlong = geo_models.PointField(null=True,blank=True)

ModelForm 类

class DataSetForm(ModelForm):
lat = forms.FloatField(
    min_value=-90,
    max_value=90,
    required=False,
)
lng = forms.FloatField(
    min_value=-180,
    max_value=180,
    required=False,
)

class Meta:
    model = DataSet
    fields = ["name"]
    labels = {"name": "Name", "lat": "Latitude", "lng": "Longitude"}

def clean(self):
    super().clean()
    data = self.cleaned_data
    if (data['lat'] is None and data['lng'] is not None) or ( 
      data['lat'] is not None and data['lng'] is None):
       raise forms.ValidationError(
          _('Both Lat Long Values must be set'), code='invalid')

查看课程

class AddDataSet(CreateView):
    model = DataSet
    form_class = DataSetForm
    template_name = "create.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update({"title": "Create Dataset"})
        return context

    def form_valid(self, form):
        lat = form.cleaned_data['lat']
        lng = form.cleaned_data['lng']
        response = super(CreateView, self).form_valid(form)
        if (lat is not None and lng is not None):
          self.object.latlong = Point(lat, lng)
        self.object.save()
        return response

我不确定这种方法是否存在缺陷。但据我测试,它有效。