geodjango: finding objects in radius

  • Last Update :
  • Techknowledgy :

Therefore you must change your query to something like this:

meters_to_degrees = CONVERT YOUR METERS TO DEGREES
res = models.PointsResult.objects.filter(
   from_location__dwithin = (center_point, meters_to_degrees)
)

You haven't specified the SRID on your center point, which means you're comparing two different SRIDs. You need to set the SRID on center_point:

center_point = 'SRID=4326;POINT(48.80033 2.49175)'

Suggestion : 2

I'm currently trying to get a list of points anycodings_django contained within a radius but I can't get it anycodings_django working. Here is the code of my view so far:,I do believe that you run into a similar anycodings_django problem to the one described here: anycodings_django GeoDjango & SpatiaLite - filtering anycodings_django nearby objects,Therefore you must change your query to anycodings_django something like this:,You haven't specified the SRID on your anycodings_django center point, which means you're anycodings_django comparing two different SRIDs. You need anycodings_django to set the SRID on center_point:

I'm currently trying to get a list of points anycodings_django contained within a radius but I can't get it anycodings_django working. Here is the code of my view so far:

from django.contrib.gis.geos
import Point
from django.contrib.gis.measure
import Distance

class AreaInfoViewSet(viewsets.ViewSet):
   queryset = models.AreaInfoRequest.objects.all()
serializer_class = serializers.AreaInfoRequestRequestSerializer

def list(self, request):
   center_point = 'POINT(48.80033 2.49175)'
radius = "50.0"

data = {
   "center_point": center_point,
   "radius": radius,
   "source_ip": utils.get_client_ip(request)
}
serializer = serializers.AreaInfoRequestRequestSerializer(data = data)
serializer.is_valid(raise_exception = True)
serializer.save()

# Contains an object with field "from_location" = "SRID=4326;POINT (48.80029 2.49157)"
objs = models.PointsResult.objects.all()

float_radius = serializer.data["radius"]
center_point = serializer.data["center_point"] # Point object

res = models.PointsResult.objects.filter(from_location__distance_lte = (
   center_point, Distance({
      "meter": float_radius
   })))
# Here the res doesn 't contain the unique object in the db even if it'
s within the radius

return Response(res)

Therefore you must change your query to anycodings_django something like this:

meters_to_degrees = CONVERT YOUR METERS TO DEGREES
res = models.PointsResult.objects.filter(
   from_location__dwithin = (center_point, meters_to_degrees)
)

You haven't specified the SRID on your anycodings_django center point, which means you're anycodings_django comparing two different SRIDs. You need anycodings_django to set the SRID on center_point:

center_point = 'SRID=4326;POINT(48.80033 2.49175)'

Suggestion : 3

Returns models where the distance to the geometry field from the lookup geometry are within the given distance from one another. Note that you can only provide Distance objects if the targeted geometries are in a projected system. For geographic geometries, you should use units of the geometry field (e.g. degrees for WGS84).,As a final note, on a DRF perspective, you are doing a model instance creation inside the list method of the ViewSet. That is absolutely wrong and should be avoided. You should override the create method if you like to interfere with the object creation process.,First things first I would suggest using dwithin instead of distance_lte for the filtering because it is optimized for that use (a very sort explanation here) From the dwithin docs we read:,Your inline comment says POINT (2.49157 48.80029), but your code uses POINT(48.80033 2.49175) - note that the latitude and longitude have swapped places. I don't know which of these you intend to use, but they refer to quite different locations.

Therefore you must change your query to something like this:

meters_to_degrees = CONVERT YOUR METERS TO DEGREES
res = models.PointsResult.objects.filter(
   from_location__dwithin = (center_point, meters_to_degrees)
)

You haven't specified the SRID on your center point, which means you're comparing two different SRIDs. You need to set the SRID on center_point:

center_point = 'SRID=4326;POINT(48.80033 2.49175)'

Suggestion : 4

Loosely-coupled to GeoDjango. For example, GEOSGeometry objects may be used outside of a Django project/application. In other words, no need to have DJANGO_SETTINGS_MODULE set or use a database, etc.,You find many TypeError or AttributeError exceptions filling your web server’s log files. This generally means that you are creating GEOS objects at the top level of some of your Python modules. Then, due to a race condition in the garbage collector, your module is garbage collected before the GEOS object. To prevent this, create GEOSGeometry objects inside the local scope of your functions/methods.,The properties in this section export the GEOSGeometry object into a different. This output may be in the form of a string, buffer, or even another object.,GEOSGeometry objects are ‘Pythonic’, in other words components may be accessed, modified, and iterated over using standard Python conventions. For example, you can iterate over the coordinates in a Point:

>>> from django.contrib.gis.geos
import GEOSGeometry
   >>>
   pnt = GEOSGeometry('POINT(5 23)') # WKT >>>
   pnt = GEOSGeometry('010100000000000000000014400000000000003740') # HEX >>>
   pnt = GEOSGeometry(buffer('\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x007@')) >>>
   pnt = GEOSGeometry('{ "type": "Point", "coordinates": [ 5.000000, 23.000000 ] }') # GeoJSON
>>> from django.contrib.gis.geos
import Point
   >>>
   pnt = Point(5, 23)
>>> from django.contrib.gis.geos
import GEOSGeometry, LineString, Point
   >>>
   print(GEOSGeometry('POINT (0 0)', srid = 4326))
SRID = 4326;
POINT(0 0) >>>
   print(LineString((0, 0), (1, 1), srid = 4326))
SRID = 4326;
LINESTRING(0 0, 1 1) >>>
   print(Point(0, 0, srid = 32140))
SRID = 32140;
POINT(0 0)
>>> from django.contrib.gis.geos
import fromfile
   >>>
   pnt = fromfile('/path/to/pnt.wkt') >>>
   pnt = fromfile(open('/path/to/pnt.wkt'))
>>> pnt = Point(5, 23) >>>
   [coord
      for coord in pnt
   ]
   [5.0, 23.0]
>>> pnt.coords(5.0, 23.0)