3

I have Django 1.7 with PostgreSQL 9.3 behind.

I want to store a model with geometrical field circle and be able to get true or false if a given point is within the stored geometrical circle. I found that question and that library and also this option, so I got lost...

What is the right and simpler way to do it, is it better to save a point and a radius and then calculate it (so I need to install the django-postgres-geometry?), or should I update Django and PostgreSQL?

Community
  • 1
  • 1

1 Answers1

1

You will need the PostGIS extension installed into PostgreSQL to achieve this. This is what Django and Postgres use for spatial calculations.

To check if a point is in a circle you can do the following in the Django shell:

   from django.contrib.gis.geos import Point
   point = Point(45.3, 34.2, srid=4326) # lon, lat

   # check if point in polygon
   mypoly.contains(point) # this returns True/False
djq
  • 14,810
  • 45
  • 122
  • 157
  • Can I achieve it with my versions of Django and PostgreSQL or should I upgrade? –  May 16 '16 at 18:31
  • 1
    @Rada your versions of Django and Postgres are fine - you just need a third library installed into the Postgres database called `PostGIS` http://postgis.net/ This is what Django/Postgres uses for spatial calculations – djq May 16 '16 at 18:33
  • @djq- but also I need some library for Django to use circleField in my model? How I suppose to create the field and save it in postgresql? –  May 17 '16 at 08:13
  • I guess I will use https://docs.djangoproject.com/en/1.7/ref/contrib/gis/tutorial/ :) –  May 17 '16 at 12:24