1

I am starting web app in Django, which must provide one simple task: get all records from DB which are close enough to other record.

For example: Iam in latlang (50, 10), and I need to get all records with latlang closer than 5km from me.

I found that geodjango thing called GeoDjango, but it contains a lot of other dependencies and libraries like GEOS, POSTGIS, and other stuff which i don't really need. I need only this one range functionality.

So should I use GeoDjango, or just write my own range calculation query?

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
ziomagic
  • 93
  • 1
  • 9
  • Just because you don't need them doesn't mean `GeoDjango` doesn't need them. They are called `dependencies` for a reason: `GeoDjango` is built with those building blocks. A library would save you much time in certain things, also they improve as time goes. Since it doesn't hurt you if you use it, why not :) ? – Shang Wang Sep 24 '15 at 14:06
  • It hurt me very much. I am using Windows in development, and Ubuntu in Prod... I had already problem with: Postgree, python posgre support in Django, and GEOS.. So I can't imagine how mad will I become, when it will not install softly on Ubuntu.. I am also just testing my concept (I am IT student, not a company) – ziomagic Sep 24 '15 at 14:17
  • I'm not sure where you got the idea that calculating linear distances from lat/long coordinates was a "simple calculation". It can get extremely complicated. – Mike T Sep 28 '15 at 21:46
  • I found this: http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/ and it really have what I need. – ziomagic Sep 29 '15 at 10:21

1 Answers1

2

Most definitely not write your own. As you get more familiar with geographic data you will realize that this particular calculation isn't at all simple see for example this question for a detailed discussion. However most of the solutions (answers) given in that question only produce approximate results. Partly due to the fact that the earth is not a perfect sphere.

On the other hand if you use Geospatial extensions for mysql (5.7 onwards) or postgresql you can make use of the ST_DWithin function.

ST_DWithin — Returns true if the geometries are within the specified distance of one another. For geometry units are in those of spatial reference and For geography units are in meters and measurement is defaulted to use_spheroid=true (measure around spheroid), for faster check, use_spheroid=false to measure along sphere.

ST_DWithin makes use of spatial indexes which home made solutions will be unable to. WHen GeoDjango is enabled, ST_DWithin becomes available as a filter to django querysets in the form of dwithin

Last but not least, if you write your own code, you will have to write a lot of code to test it too. Whereas dwithin is thoroughly tested.

Community
  • 1
  • 1
e4c5
  • 52,766
  • 11
  • 101
  • 134