Help getting my GeoDjango setup.

117 views
Skip to first unread message

JJ Zolper

unread,
Oct 16, 2012, 12:04:02 AM10/16/12
to django...@googlegroups.com
Hello everyone,

So I've installed GDAL, PostGIS, PROJ.4, and GEOS. I have Postgres set up too.

What I need help on is the logistics of getting the GeoDjango portion of my website up and running. I already have my website code I'm just trying to add in GeoDjango so I can handle geographic operations on the website.

So I've been reading here:


I see:

Create GeoDjango Project

Use the django-admin.py script like normal to create a geodjango project:

$ django-admin.py startproject geodjango

With the project initialized, now create a world Django application within the geodjango project:

$ cd geodjango
$ python manage.py startapp world

Configure settings.py

The geodjango project settings are stored in the geodjango/settings.py file. Edit the database connection settings appropriately:

DATABASES = {
    'default': {
         'ENGINE': 'django.contrib.gis.db.backends.postgis',
         'NAME': 'geodjango',
         'USER': 'geo',
     }
}

In addition, modify the INSTALLED_APPS setting to include django.contrib.admindjango.contrib.gis, and world (our newly created application):

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.gis',
    'world'
)


My question is do I need to start a new project if I already have one project already? I already have my project "madtrak" which is the project that contains my entire website. Do I need a separate project just for GeoDjango? I thought that the start project command was only done once and for the website. Or do I need to run "django-admin.py startproject geodjango" ?

If I find that I don't need to start a new project that would help a lot because then I would already have a settings.py file with my installed apps and an app that will have a model that connects up to the Geodjango database.

One more thing on this topic I see:

         'ENGINE': 'django.contrib.gis.db.backends.postgis',

Even if I don't need an entire separate project do I need to add this backend to my current django project? That way I can handle all the Geodjango necessities? 

Because currently I have:

        'ENGINE': 'django.db.backends.postgresql_psycopg2',

and I would guess that is not sufficient. Any advice on handling my current settings file and interfacing that with the GeoDjango database would be great!



Okay secondly I see this:

Create a Spatial Database

Note

MySQL and Oracle users can skip this section because spatial types are already built into the database.

First, a spatial database needs to be created for our project. If using PostgreSQL and PostGIS, then the following commands will create the database from a spatial database template:

$ createdb -T template_postgis geodjango

So it seems to me I need a new database because it will be a spatial database for GeoDjango? I would think my current database would not be sufficient because there is nothing special about it to hold the geometric data I will need to process with GeoDjango. So do I need to run this command and set up a new database and remove my old one? If I do this is the idea that I have those extra features that I need in my spatial database for GeoDjango and I then have the option on when to use that functionality within each django app I build?


Any advice regarding the integration of GeoDjango into a current django project environment would be really useful!

Thanks so much for your time.

JJ Zolper

smcoll

unread,
Oct 16, 2012, 10:57:58 AM10/16/12
to django...@googlegroups.com
You can add GIS support to your existing project, but as you suspected, your current db will not be sufficient.  Unless someone knows how to convert an existing database, i believe you'll need to set up a new database from the postgis template, and move all your data to it.  That process might look something like this:

From your current project, dump all your data to a json file:
$ python manage.py dumpdata --all --natural > all.json

The `natural` flag helps preserve some things like contenttypes and permissions.

Then switch to your new postgis template in your settings file (along with the other necessary items you already mentioned, like changing your backend to postgis).

Next:

$ python manage.py syncdb
$ python manage.py migrate # if using south
$ python manage.py loaddata all.json

You might run into an issue between syncdb and loaddata, because syncdb loads initial data into your tables for apps with a fixtures/initial_data.json file (auth, for example).  If you can use the dev (1.5) code, use the --no-initial-data flag.  Otherwise, you may need to run something like the following on your Postgres db before loaddata:

=# delete from auth_group_permissions; delete from auth_permission; delete from django_admin_log; delete from django_content_type;

Hope that helps.

JJ Zolper

unread,
Oct 17, 2012, 8:33:45 PM10/17/12
to django...@googlegroups.com
That's okay my website has not moved that far into the distance yet it's still pretty elementary so all the data in my database has been put there by me anyways. I can set up a new database. I see here:


that I should do:

$ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-2.0
# Creating the template spatial database.
$ createdb -E UTF8 template_postgis
$ createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
# Allows non-superusers the ability to create from this template
$ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
# Loading the PostGIS SQL routines
$ psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql
$ psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql
# Enabling users to alter spatial tables.
$ psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;"
$ psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;"
$ psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
to make a postgis spatial database template? Is that right?

Then it says to do:

hese commands may be placed in a shell script for later use; for convenience the following scripts are available:

PostGIS versionBash shell script
1.3create_template_postgis-1.3.sh
1.4create_template_postgis-1.4.sh
1.5create_template_postgis-1.5.sh
Debian/Ubuntucreate_template_postgis-debian.sh

Afterwards, you may create a spatial database by simply specifying template_postgis as the template to use (via the -T option):

$ createdb -T template_postgis <db name>
So I guess once I run this bash script from SSH I then create a new database from an SSH terminal? I'm on Webfaction.

** Okay so I'm trying to parse through what you posted because at this point in time I'm going to just trash my old database and not preserve anything.

Can you help me get GeoDjango set up from scratch? That's what I'm trying to do here. 

This is for moving my data only right:

From your current project, dump all your data to a json file:
$ python manage.py dumpdata --all --natural > all.json

The `natural` flag helps preserve some things like contenttypes and permissions.

Then switch to your new postgis template in your settings file (along with the other necessary items you already mentioned, like changing your backend to postgis).

Next:

$ python manage.py syncdb
$ python manage.py migrate # if using south
$ python manage.py loaddata all.json

You might run into an issue between syncdb and loaddata, because syncdb loads initial data into your tables for apps with a fixtures/initial_data.json file (auth, for example).  If you can use the dev (1.5) code, use the --no-initial-data flag.  Otherwise, you may need to run something like the following on your Postgres db before loaddata:

=# delete from auth_group_permissions; delete from auth_permission; delete from django_admin_log; delete from django_content_type;



If it is not only for that just let me know but that's what it seemed like. If I'm starting fresh I want a postgis database right? How do I then utilize GDAL, PROJ.4, and GEOS if my database gets set up as postgis?

Thanks for sticking it out through all the questions. I'm really not sure how to do this and what's the best way.

Thanks!

JJ

Shannon Collins

unread,
Oct 18, 2012, 4:06:31 PM10/18/12
to django...@googlegroups.com
Last i checked, WebFaction will set up a database for you with the PostGIS template if you submit a ticket to support.  i bet they could also point to you some documentation or a forum post about what you're trying to do as well.

JJ Zolper

unread,
Oct 18, 2012, 9:24:59 PM10/18/12
to django...@googlegroups.com
That's good advice! I forgot all about the tickets for a minute there on webfaction. I have just sent one.

I just want to do it correctly because basically this database is going to have to handle everything I do.

Thanks for helping to try and move my data. I'm just not that worried about moving the data.

JJ
Reply all
Reply to author
Forward
0 new messages