简体   繁体   中英

GeoDjango wanting coordinated in range [-180 -90, 180 90]?

Here is my model:

import datetime
from django.contrib.gis.db import models

class UserLocation(models.Model):
    user = models.ForeignKey('auth.User')
    coords = models.PointField()
    date_created = models.DateTimeField(default=datetime.datetime.now)
    objects = models.GeoManager()

    @classmethod
    def get_latest_location(cls, user):
        return cls.objects.filter(user=user).latest()

    class Meta:
        get_latest_by = 'date_created'

When I try to query it like so:

coords = Point(x=34.4208305, y=-119.6981901) # in california
UserLocation.objects.distance(coords)

I get this error:

DatabaseError: Coordinate values are out of range [-180 -90, 180 90] for GEOGRAPHY type
CONTEXT:  SQL function "st_distance_sphere" during inlining

Why is it wanting them in the range [-180 -90, 180 90]? That doesn't make any sense to me...

Considering a sphere (assuming the earth is round like a ball), using spherical coordinates (r, phi, tau), the given ranges are sufficient to identify every point on the sphere. For a detailed explanation of the coordinate system consult for instance Wikipedia at http://en.wikipedia.org/wiki/Spherical_coordinate_system .

If you have larger angles, you just fit them to the interval, eg

if (phi > 180):
    phi -= 360
elseif (phi < -180):
    phi += 360

and similarly for tau.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM