2011-08-27 2 views
4

내 모델 중 하나는 데이터베이스에 부동 소수점 숫자로 저장된 위도 및 경도 필드가 있습니다. 나는 그것들을 가장 효율적으로 사용할 수 있기 때문에 이런 식으로 유지하고 싶다.장고의 맞춤 위도/경도 양식 필드

사용자가 주식 관리 인터페이스에서 (+/-) DD MM SS.S (대부분의 GPS 장치가 최종 사용자에게 좌표를 표시하는 방법) 형식으로 편집 할 수 있기를 바랍니다. 너무 많은 오버 헤드가, 단순히 두 필드의 전체 프레임 워크를 필요로하지 않습니다 -

  1. 사용 GeoDjango가 :

    나는 이것을 구현하는 세 가지 방법을 생각했습니다.

  2. 사용자 정의 필드, somehow in this way을 정의하십시오. 많은 코딩과 같아서 Django 데이터베이스 인터페이스를 사용하여 쉽게 부동 소수점 표현에 액세스 할 수 있는지 여부가 확실하지 않습니다.
  3. MultiValueField 및 MultiWidget을 사용하십시오. 이것은 완전히 나쁜 해결책은 아니지만 매우 잘 문서화되지 않았으며 각도, 분 및 초에 대한 코딩 및 불필요한 위젯도 포함합니다.

그러나 이상적으로, 나는이 일을하고 싶습니다 :

  • 표준 TextInput 구성 형태의 위젯 및 표준 FloatField 모델 필드를 사용하는 것이 사용자 지정 형태 필드를 사용합니다.

to_python() 메서드가 텍스트 입력을 처리하고이를 float로 변환 할 수 있다고 확신합니다. 하지만 장고가 모델을 편집 할 때 float을 lat/lng로 변환하도록 어떻게 말합니까? 그리고 나는 어떻게 그것을 함께 붙일 수 있습니까?

답변

3

좌표 데이터를 포함하도록 모델에 필드를 추가 한 다음 모델의 save() 메소드에서이를 위도와 경도로 변환해야하는 이유는 무엇입니까? 그런 다음 관리자에서 값을 볼 수는 있지만 편집 할 수 없도록 lat/lon을 읽기 전용으로 만듭니다. 또는 전혀 표시하지 않기로 결정할 수도 있습니다. 예를 들어

:

class Location(models.Model): 

    latitude = ... 
    longitude = ... 

    lat_degrees = models.IntegerField() 
    lat_minutes = models.IntegerField() 
    lat_seconds = models.FloatField() 

    def save(self, *args, **kwargs): 
     # Do the maths here to calculate lat/lon 
     self.latitude = ... 
     self.longitude = ... 
     super(Location, self).save(*args, **kwargs) 

나는 나는 좌표에 전문가는 아니지만, 내가 추측하고있어, 당신은 또한 lon_degrees 필드를해야합니다 가정합니다. 나는 그것들을 본보기에서 버렸습니다. 관리자가 표시되도록 새 위젯을 만들거나 change_form.html을 오버라이드하여 세 줄을 동일한 줄에 표시 할 수도 있지만이 답변의 범위를 약간 벗어납니다.

+0

매우 간단하지만 구현하기 쉽고 간단합니다. 그것을 생각하지 않았습니다 - 감사합니다! –

0

나는 최근에이 요구 사항을 가지고 있었고 조금 옮겼다. 그러나 나는 나눌 것이라고 생각했다. (Django 2.0)

입력 된 좌표가 인 30 자 CharField를 만들었습니다.N 35º 44.265 W 41º 085.155(나는 그게 어디에 있든 ...) 그리고 필드 값을 저장하기 위해 모델에 맞게 정렬되어 있습니다.

import re 
from django.core.exceptions import ValidationError 

COORDINATES_REGEX = r'(?:[NS])\s*([0-9]{2})[\º\°]?\s+([0-9]{1,3}\.[0-9]{3})\s*(?:[EW])\s*([0-9]{2,3})[\º\°]?\s+([0-9]{2,3}\.[0-9]{3})' 

def decode_coords_string(str): 
    """ 
    Given a string, converts it to a decimal (lat, lng, 'OK', matched_string) tuple. 
    If invalid, returns "(None, None, <some reason>, None)." 

    Test for errors by checking that the coordinate is not 'None.' 

    'matched_string' returns the actual extent of the matched string regardless of where in the input-string it was, 
     for sanitizing the input when storing it in the database. (If the input string contains only blanks, we should 
     store an empty-string.) 
    The model will replace the field-value with this matched-string. 
    """ 
    # Dispose of empty input, returning an empty string(!) as the 'matched_string' in this case. 
    r = re.compile(r'^\s*$') 
    if r.match(str): 
     return (None, None, 'Empty string', '') 

    # Build the regex for coordinates. 
    r = re.compile(COORDINATES_REGEX, re.IGNORECASE) 

    # Try to match the string 
    p = r.match(str) 
    if p is None: 
     return (None, None, 'Syntax error', None) 

    # Get the pieces and expressly convert them to numeric types 
    (lat_degs, lat_mins, lng_degs, lng_mins) = p.groups() 

    lat_degs = int(lat_degs) 
    lat_mins = float(lat_mins) 
    lng_degs = int(lng_degs) 
    lng_mins = float(lng_mins) 

    # Throw out anything that simply does not make sense 
    if (lat_degs > 180) or (lng_degs > 180) or (lat_mins > 60.0) or (lng_mins > 60.0): 
     return (None, None, 'Degs/Mins value(s) out of bounds') 

    latitude = float(lat_degs) + (lat_mins/60.0) 
    longitude = (float(lng_degs) + (lng_mins/60.0)) * -1.0 

    return (latitude, longitude, 'OK', p.group()) 


def validate_coords(str): 
    """ 
    Simple validator for a coordinate string. 
    """ 
    (lat, lng, reason, str2) = decode_coords_string(str) 
    if lat is None: 
     raise ValidationError('Invalid coordinates: ' + reason) 

입력 CharField

는도 심볼은 여러 가지 방법으로 지정되거나 완전히 생략 될 수 있음을주의 validators=[validate_coords]도 지정한다. admin.py에서

def save(self, *args, **kwargs): 
""" 
Calculate and automatically populate the numeric lat/long figures. 
This routine assumes that the string is either empty or that it has been validated. 
An empty string – or, for that matter, an invalid one – will be (None, None). 
""" 

(lat, lng, reason, cleaned) = decode_coords_string(self.coordinate_str) 

self.coordinate_str = cleaned 
self.latitude  = lat 
self.longitude  = lng 
super().save(*args, **kwargs) 

는 I는 사용자 혼란을 방지하는 관점에서 latitudelongitude 필드 (플로트 필드이다 둘) 제외 :

하고 모델은 다음과 같은 간단한 방법을 포함한다. 숫자 필드는 자동으로 계산되지만 표시되지는 않습니다.