Is GeoDjango Too Much For This?

311 views
Skip to first unread message

DF

unread,
Feb 23, 2012, 8:13:35 PM2/23/12
to Django users
I'm currently building my first Django-based app, which is proceeding
fairly well, thus far. One of the goals is to allow users to post
information (text, pics, video) and for the app to be able to
automatically detect the location where they posted this information.
That data would then ideally be able to be filtered later, such as
viewing the posts that were made within a specific radius.

I've been reading a bit about GeoDjango and it sounds intriguing. A
colleague, though, suggested everything that can be done using
GeoDjango is equally efficient utilizing the Google Maps API with
JavaScript or JQuery to obtain the proper coordinates.

Essentially, I'm looking to see what benefits GeoDjango would give
this project over using just the Google Maps API. If I've already
started a project in basic Django, is incorporating GeoDjango
problematic? I'm still attempting to master the basics of Django and
venturing into GeoDjango may be too much. Or not.

Any insight appreciated.

kenneth gonsalves

unread,
Feb 24, 2012, 12:54:18 AM2/24/12
to django...@googlegroups.com
On Thu, 2012-02-23 at 17:13 -0800, DF wrote:
> I've been reading a bit about GeoDjango and it sounds intriguing. A
> colleague, though, suggested everything that can be done using
> GeoDjango is equally efficient utilizing the Google Maps API with
> JavaScript or JQuery to obtain the proper coordinates.

if your activity is commercial, google will make you pay. Geodjango with
OSM is free - and OSM is more accurate than google and the raw data is
available. Also look at openlayers - that may fulfill your needs without
geodjango.
--
regards
Kenneth Gonsalves

Alex Mandel

unread,
Feb 24, 2012, 7:10:39 PM2/24/12
to django...@googlegroups.com

The benefits beyond that include things like being able to query your
data for points within a bounding box, or create a buffer around a point
to select nearby points, or test if you're points intersect with other
features of interest.

Thanks,
Alex

Carlos Leite

unread,
Feb 25, 2012, 8:53:57 AM2/25/12
to django...@googlegroups.com
My english is not that great, but I'll try anyway.

> I'm currently building my first Django-based app, which is proceeding
> fairly well, thus far. One of the goals is to allow users to post
> information (text, pics, video) and for the app to be able to
> automatically detect the location where they posted this information.
> That data would then ideally be able to be filtered later, such as
> viewing the posts that were made within a specific radius.

Points or a pair od coordinates are simple to perssit, you do not
necessarily have to persist it using geo formats , so you do not need
a orm or database that understand GEO DATA.

But, what you going to do with this informatios later ?
just keep it in a database and show in a map [1]? or you will need
spatial queries [2]?

Case 1 - you just need a frontend to get coordnates in your braowser
window and pass to your app throught JS.

Case 2 - you may use a spaytial database to do the job just like any
other query ( GeoDjango can do that for you)

> I've been reading a bit about GeoDjango and it sounds intriguing. A
> colleague, though, suggested everything that can be done using
> GeoDjango is equally efficient utilizing the Google Maps API with
> JavaScript or JQuery to obtain the proper coordinates.

> Essentially, I'm looking to see what benefits GeoDjango would give
> this project over using just the Google Maps API. If I've already
> started a project in basic Django, is incorporating GeoDjango
> problematic?  I'm still attempting to master the basics of Django and
> venturing into GeoDjango may be too much. Or not.

GeoDjanfgo among other thing gives you the ORM and Geo functions, like
projections import export, and geo classes
for |Geometrie like Point !

You can use GeoDjango TOGETHER with Google, Open Layers AND both with Jquery
You may use the maps from Google, but the Frontend interface from OpenLayers.


Google gives you = Maps + interface
OpenLayers = Just the interface (really nice by the way)

But some times.... its easier and fast just use Google maps API.

... but doenst mather ... google or openlayers, if you need SPATIAL
QUERIES in Django its = GeoDjango. (or rediscovery the wheel)

May be this project can help you http://pythonpeople.znc.com.br/ its
on github https://github.com/znc-sistemas/python-people

The code is not beautiful, I made this app in a cople days, and then
poeple start to sign in...

I use to use Open Layers, and this app was the firts one where the
frontend was entirely build with GoogleMaps API.... but geodjango
inside!

hope this help

Ethan Jucovy

unread,
Feb 25, 2012, 5:03:55 PM2/25/12
to django...@googlegroups.com
Others have answered your other questions well, so just addressing this one:

On Thu, Feb 23, 2012 at 8:13 PM, DF <donfer...@gmail.com> wrote:
Essentially, I'm looking to see what benefits GeoDjango would give
this project over using just the Google Maps API. If I've already
started a project in basic Django, is incorporating GeoDjango
problematic?  I'm still attempting to master the basics of Django and
venturing into GeoDjango may be too much. Or not.

I've added GeoDjango to existing "base Django" projects before, including one where the codebase was already storing lat/lon data in FloatFields and I had to migrate.  Short answer: it's really easy.  You just add django.contrib.gis to INSTALLED_APPS and add a few more well documented settings -- just like adding any other app into your project.

There are just two points where you need to pay close attention to the docs or else you'll get confusing errors:

* You'll need to change the ENGINE setting on your DATABASE(s) settings -- just replace the default Django ENGINEs with the appropriate one from this list: https://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#spatial-backends

* You'll want to add a line ``objects = GeoManager`` on each model that you will want to perform geospatial queries against.  This includes models with no direct geospatial data if they have relations to GIS models, that is if you want to be able to query them against geospatial properties on their relations.

The only other caution is setting up your geospatial database.  If you're using MySQL no extra work is needed, because it's all built in, but you'll get very few features -- MySQL has limited GIS support.  If you're using Postgres or SQLite you'll be able to use all the Geodjango features, but you'll need to set up your database for it in advance.  If you already have a Postgres or SQLite database with data you want to keep it will be a little extra work to migrate it over to a geospatial database.

Hope this helps,
Ethan

Ethan Jucovy

unread,
Feb 25, 2012, 5:07:30 PM2/25/12
to django...@googlegroups.com
Whoops, quick correction: 

On Sat, Feb 25, 2012 at 5:03 PM, Ethan Jucovy <ethan....@gmail.com> wrote:
* You'll want to add a line ``objects = GeoManager`` on each model that you will want to perform geospatial queries against.

That should be ``objects = GeoManager()`` (imported from django.contrib.gis.db.models).


-Ethan 

DF

unread,
Feb 25, 2012, 7:50:59 PM2/25/12
to Django users

Thanks for all the feedback. It is tremendously insightful.

Here are my qualms:

I'm a Django rookie, with basic Python knowledge. I'm using Django to
work on my master's project, which is a specific web application that
utilizes some form of geolocation, the latter of which is a mystery.

I have a good skeleton thus far for basic functionality. It's taken a
month, but I'm happy with what's been accomplished considering my
knowledge (also using django-registration and django-profiles, which
are terrific apps).

GeoDjango though, to be honest, sounds frightening and extremely
advanced. I've been reviewing the installation requirements and, at
least according to how these were written, is intense and ripe for
many errors. Part of the qualms I'm having is I've been developing on
the internal sqlite3 database. According to most of the documentation,
Postgres with GIS is recommended. This is concerning.

Essentially, I want to be able to have the application immediately
detect a users longitude and latitude and have all the content they
contribute be identified with those coordinates. I'd like a map
illustrating their location to appear with each content post. Finally,
I want to provide users the ability to search for nearby posts or
within a certain boundary (from the documentation I've read, this is
what GeoDjango apparently excels at).

Perhaps this is isn't a monumental task and I'm letting my mind get
the best of me, as it often does. But it sounds quite intimidating.

I appreciate any feedback, realistic expectations and advice. And I
also appreciate the tolerance of experienced and skilled programmers
at addressing annoying novice questions like mine. I've learned that
asking questions is one of the keys to learning.

Many thanks.

DF


On Feb 25, 5:07 pm, Ethan Jucovy <ethan.juc...@gmail.com> wrote:
> Whoops, quick correction:
>
> On Sat, Feb 25, 2012 at 5:03 PM, Ethan Jucovy <ethan.juc...@gmail.com>wrote:
>
> > * You'll want to add a line ``objects = GeoManager`` on each model that
> > you will want to perform geospatial queries against.
>
> That should be ``objects = GeoManager()`` (imported from
> django.contrib.gis.db.models).
>
> And, I meant to link to the docs on that:https://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/#geom...
>
> -Ethan

Javier Guerra Giraldez

unread,
Feb 25, 2012, 10:27:45 PM2/25/12
to django...@googlegroups.com
On Sat, Feb 25, 2012 at 7:50 PM, DF <donfer...@gmail.com> wrote:
> Finally,
> I want to provide users the ability to search for nearby posts or
> within a certain boundary (from the documentation I've read, this is
> what GeoDjango apparently excels at).

that's the point. it seems easy, until you write some SQL to express
the 'nearby' criteria... and find that you're doing a very expensive
full-table scan.

a GIS database handles spatial indexes, so queries like "order by
distance to this point, retrieve the 20 closest" or "all points inside
this polygon" are done quickly and efficiently no matter the number of
points in the database.

Postgres and SQLite handle these type or queries; some MySQL versions
can deal a few of them. better go with Postgres, as it's far more
tested (and used by core developers)


--
Javier

Ethan Jucovy

unread,
Feb 27, 2012, 11:57:58 AM2/27/12
to django...@googlegroups.com
On Sat, Feb 25, 2012 at 7:50 PM, DF <donfer...@gmail.com> wrote:
GeoDjango though, to be honest, sounds frightening and extremely
advanced. I've been reviewing the installation requirements and, at
least according to how these were written, is intense and ripe for
many errors.

What is your operating system?  On Ubuntu I've found all the installation to be very easy.

FWIW, when I was starting out with GeoDjango I found these tutorials to be useful -- they're written for specific OSes/distributions/database backends but I still found them easier to follow than the official documentation: [1,2,3]
 
Part of the qualms I'm having is I've been developing on
the internal sqlite3 database. According to most of the documentation,
Postgres with GIS is recommended. This is concerning.

I haven't used the spatial extensions to SQLite (SpatiaLite) but I believe SpatiaLite is fully supported by GeoDjango and has all the features you will need.  (As long as the features you need don't include multiple users writing to the database at the same time!  For production you will probably want Postgres instead of Sqlite, for reasons that have nothing to do with GIS.)
 
Essentially, I want to be able to have the application immediately
detect a users longitude and latitude and have all the content they
contribute be identified with those coordinates.

Depending what you mean by "immediately detect", you'll want to look into "geocoding" and/or "GeoIP" and/or the W3C/HTML5 "geolocation API".

Geocoding is the process of translating a text address into a lat/long, e.g. when an end-user types in his address in order to center a map there.  GeoDjango doesn't provide any direct geocoding features, but it's easy to implement in your GeoDjango-based application.  You'll most likely want to use a web service for this -- Google, Yahoo, and many other services (including some that are free software) exist.  Pay attention to the terms of use when picking a service: some (*coughGooglecough*) are actually very restrictive if you read the fine print.  Once you've picked your service, libraries like [4] or [5] will do most of the rest of the work for you.

GeoIP lets you infer a user's location based on his IP address when he visits your application.  GeoDjango does provide GeoIP features: [6]
 
The W3C Geolocation API is an HTML5 standard that lets the client browser provide the user's location directly in Javascript, if the client device knows it and the user gives permission.  Since this is a client-side feature, GeoDjango doesn't provide any support for it directly; you'd just write your HTML and Javascript to use it: [7,8]

I'd like a map illustrating their location to appear with each content post.

You'll want a web mapping frontend for this.  GeoDjango doesn't provide a web mapping frontend itself, but (like geocoding) it is easy to implement one in your application once you have lat/longs.  OpenLayers (which is free software) and Google Maps are the established tools here, and now there are a number of newer ones too -- which I've never tried out.  (Once again though pay attention to the terms of use.)
 
Finally, I want to provide users the ability to search for nearby posts or
within a certain boundary (from the documentation I've read, this is
what GeoDjango apparently excels at).

That's exactly right.  Note that in certain sufficiently constrained circumstances you could accomplish this without spatial queries in a database.  If you only need your users to see nearby posts, and the total number of posts is small enough, you could load all posts on a map and do the "search" via client-side geocoding APIs and re-centering your map.  This obviously doesn't scale well, and if you need these sorts of features in a "non-superficial" way then saving geolocated data and building queries through GeoDjango's ORM APIs [9,10] is exactly what you'll want.

Hope this helps!
Ethan





DF

unread,
Feb 27, 2012, 11:42:31 PM2/27/12
to Django users, ethan....@gmail.com
Ethan:

What a generous answer. Thank you so much. Here are my responses with
a few other questions:

> What is your operating system?  On Ubuntu I've found all the installation
> to be very easy.

I'm on a Mac. The Django GeoDjango installation instructions are
somewhat obtuse and there aren't alternative sites I've found with
more straightforward instructions. It's lame that these are confusing
but, well, they are. I will no doubt review the sites you recommended.

As a newcomer who suddenly feels he may be biting off more than he can
chew, one of my worries it that if I utilize the sqlite3 database for
development with Spatialite, when I finally deploy this to, say, a
Postgres database, it won't transition correctly. This is most likely
due to my naiveté about databases, but I'm just concerned once it's
deployed, it won't work because I screwed something up by using
sqlite3 and I'm now trying to use another type of database.

From your textbook descriptions (awesome) it seems that GeoIP is what
I'm looking for. Essentially, a logged-in user's coordinates would be
taken from their IP address (not sure if GeoDjango has a way to do
this without front-end JS work (more terror)) and be able to identify
their location on, let's say, a Google Map. When the user posts
content, it would be identified and recorded in the database the same
way. So if they're in a new location, the application can detect where
they are through the IP address and record those coordinates in the
database. This content could then be sorted via location (e.g., find
posts within a 10 mile radius), perhaps through a search or maybe
through a map with markers.

The problem with using HTML5 is, while it seems easy (there's pre-
formatted code out there that makes it a cinch to identify IP
location), it really provides no simple way to store or search for
content that's identified via GeoIP.

So, from reading your finely detailed response, it seems a valid and
straightforward way to proceed would be:

- GeoDjango using the GeoIP features
- OpenLayers (have heard about it; need to research more)
- Google Maps or another mapping service, being wary of limits

From what I can discern, this is not an extremely advanced use of
GeoDjango or geolocation in general. It may actually be simple. But
I'm just now getting over my fears of Django proper so GeoDjango seems
like jumping from the kiddie pool into deep-dea diving. Another person
who responded focused his very kind and generous response on having to
be adept at trig to make GeoDjango work. I've avoided trig since I was
14. What have I gotten into? :)

Your response, again, was awesome. Any more guidance is not only
appreciated, but welcomed.

What a great and kind resource this forum is.






On Feb 27, 11:57 am, Ethan Jucovy <ethan.juc...@gmail.com> wrote:

Ethan Jucovy

unread,
Mar 2, 2012, 9:11:46 AM3/2/12
to Django users, DF
On Mon, Feb 27, 2012 at 11:42 PM, DF <donfer...@gmail.com> wrote:
I'm on a Mac. The Django GeoDjango installation instructions are
somewhat obtuse and there aren't alternative sites I've found with
more straightforward instructions. It's lame that these are confusing
but, well, they are. I will no doubt review the sites you recommended.

Oh .. yeah, I've never tried installation on a Mac.  Googling around a bit turns up http://lincolnloop.com/blog/2010/apr/30/installing-geodjango-dependencies-homebrew/ (as well as a lot of confusing stufff that scared me off) -- maybe that would Just Work?
 
As a newcomer who suddenly feels he may be biting off more than he can
chew, one of my worries it that if I utilize the sqlite3 database for
development with Spatialite, when I finally deploy this to, say, a
Postgres database, it won't transition correctly. This is most likely
due to my naiveté about databases, but I'm just concerned once it's
deployed, it won't work because I screwed something up by using
sqlite3 and I'm now trying to use another type of database.

That is a reasonable concern.  For what it's worth, you're *likely* to not have any problems switching from sqlite to postgres midstream, as long as you stick to Django's ORM and querying methods; of course if you drop down into raw SQL problems will be more likely.  I usually start out local development on sqlite and switch to postgres sometime later.  But of course you're right that the most conservative approach is to pick one database and start with it.
 
From your textbook descriptions (awesome) it seems that GeoIP is what
I'm looking for. Essentially, a logged-in user's coordinates would be
taken from their IP address (not sure if GeoDjango has a way to do
this without front-end JS work (more terror))

Yes, using GeoIP doesn't require any front-end work.  Your backend just gets an IP address with every request, and you can ask django.contrib.gis to locate that IP.  Something like this: https://gist.github.com/1958529 -- which you would then store in your database through a GeoDjango PointField.
 
The problem with using HTML5 is, while it seems easy (there's pre-
formatted code out there that makes it a cinch to identify IP
location), it really provides no simple way to store or search for
content that's identified via GeoIP.

Literally speaking this is true, but you would use it in conjunction with backend code.  HTML5 Geolocation is just concerned with reliably identifying the user's location on the client side.  You could then write a bit of Javascript attached to your submission forms that inserts that location data into the form submission.  Then on the server side, you have a lat/lng you can store with GeoDjango's PointField for future querying.

HTML5 Geolocation is most useful (and I believe has the most implementations) in the mobile world. If your users will be uploading content to your application from smartphones then this is a good feature to use.

It's best to think about (client-side) HTML5 Geolocation and (server-side) GeoIP as complementary features for graceful degredation.  If data from HTML5 Geolocation is available, use it; if not, you can fall back on GeoIP.  HTML5 Geolocation data, if present, is more likely to be both accurate and precise; geotagging content based on the user's IP address is a coarser-grained solution and can also return incorrect data in some circumstances -- depending on the details of the user's internet connection, the originating IP address could be somewhere else entirely.

- GeoDjango using the GeoIP features
- OpenLayers (have heard about it; need to research more)
- Google Maps or another mapping service, being wary of limits

Yup, that sounds right, with the caveat above that you may want to layer on HTML5 Geolocation as a progressive enhancement.

Hope this helps,
Ethan

Josh Cartmell

unread,
Mar 2, 2012, 12:37:16 PM3/2/12
to django...@googlegroups.com, DF
I have two quick comments, check out geopy:
http://code.google.com/p/geopy/
and this website
http://www.movable-type.co.uk/scripts/latlong-db.html

Geopy can give you lat/lng for a given address and that other website has formulas for using lat/lng to calculate a bounding box/circle.  It may be a little simpler to do things that way vs. using GeoDjango although possibly not as efficient.
Reply all
Reply to author
Forward
0 new messages