dynamic creation of model with geodjango ?

79 views
Skip to first unread message

guillaume

unread,
Aug 13, 2008, 3:39:03 AM8/13/08
to Django users
Hi,

I'm pretty glad to see that GeoDjango has joined the main trunk. As it
is a great tool for managing geographic data, I am wondering if it is
possible to create a new model dynamically. Imagine you want to add a
new geographic data source to your app, how can it be done ?

Regards,

Guillaume

springmeyer

unread,
Aug 18, 2008, 1:22:44 PM8/18/08
to Django users
Guillame,

Absolutely. GeoDjango can be used to read new or existing geographic
datasources from dozens of formats. GeoDjango provides a ctypes
interface to the foundational GDAL/OGR library, which is the open
source industry standard for format translation. Look for the utility
called `orginspect`.

While the GeoDjango documentation is still being worked on, the
Docstrings are excellent. To see how ogrinspect works you'll need to
do nothing but (a sample project and shapefile can be downloaded from
http://code.google.com/p/geodjango-basic-apps/wiki/GeographicAdminQuickStart):

>>> from django.contrib.gis.utils import ogrinspect
>>> help(ogrinspect)


Help on function ogrinspect in module
django.contrib.gis.utils.ogrinspect:

ogrinspect(*args, **kwargs)
Given a data source (either a string or a DataSource object) and a
string
model name this function will generate a GeoDjango model.

Usage:

>>> from django.contrib.gis.utils import ogrinspect
>>> ogrinspect('/path/to/shapefile.shp','NewModel')

...will print model definition to stout

or put this in a python script and use to redirect the output to a
new
model like:

$ python generate_model.py > myapp/models.py

# generate_model.py
from django.contrib.gis.utils import ogrinspect
shp_file = 'data/mapping_hacks/world_borders.shp'
model_name = 'WorldBorders'

print ogrinspect(shp_file, model_name, multi_geom=True, srid=4326,
geom_name='shapes', blank=True)
:

Cheers,

Dane


On Aug 13, 12:39 am, guillaume <guillaume.su...@neogeo-online.net>
wrote:

guillaume

unread,
Aug 19, 2008, 9:18:54 AM8/19/08
to Django users
Hi Dane,

Thanks for these informations. Does it mean that a model dynamically
created will now appear in admin section ?

Regards,

Guillaume

On 18 août, 19:22, springmeyer <dane.springme...@gmail.com> wrote:
> Guillame,
>
> Absolutely. GeoDjango can be used to read new or existing geographic
> datasources from dozens of formats. GeoDjango provides a ctypes
> interface to the foundational GDAL/OGR library, which is the open
> source industry standard for format translation. Look for the utility
> called `orginspect`.
>
> While the GeoDjango documentation is still being worked on, the
> Docstrings are excellent.  To see how ogrinspect works you'll need to
> do nothing but (a sample project and shapefile can be downloaded fromhttp://code.google.com/p/geodjango-basic-apps/wiki/GeographicAdminQui...

Malcolm Tredinnick

unread,
Aug 19, 2008, 9:24:16 AM8/19/08
to django...@googlegroups.com

On Tue, 2008-08-19 at 06:18 -0700, guillaume wrote:
> Hi Dane,
>
> Thanks for these informations. Does it mean that a model dynamically
> created will now appear in admin section ?

The previous poster described a way to inspect an existing data source
and print out the model definition to stdout. You will then need to
paste that output into a models file (and possibly edit it, if anything
needs tweaking, which is why any inspectdb-like operations prints out
the result). The model isn't "dynamically created" at all.

It will appear in the admin if you put it into a models file *and* write
an admin class for it, as per normal.

If you really want to dynamically create models, you'll need to poke
around the internals of Django a lot, since that isn't documented
(oustide of docstrings and the code) and isn't public API, so is subject
to change without notice in the future. If you have to ask on this list
how to do that, quite honestly, you probably aren't ready to take that
step, because it does require confidence at reading the source and
understanding it. You'll want to start with
django.db.models.base.ModelBase.__new__ to see how model classes are
created, for example. Reading that will give you an idea of whether
you're going to able to understand the code or not.

And if you do understand that and feel comfortable playing with it --
please feel free to do so. I'm not saying "don't do that." Rather, I'm
cautioning that this is deep internals stuff and if you're looking for
step-by-step instructions or support, you're going to have to adjust
your expectations.

Regards,
Malcolm


guillaume

unread,
Aug 19, 2008, 9:46:11 AM8/19/08
to Django users
Yes, actually I didn't hope anything else.
I'll try to catch some geodjango guys up in Cape Town FOSS4G to have a
chat about this. It can be something I can find funding for, as it is
extremely critical in deploying Spatial data infrastructures.

Regards,

Guillaume

On 19 août, 15:24, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Justin Bronn

unread,
Aug 19, 2008, 10:57:31 AM8/19/08
to Django users
> Yes, actually I didn't hope anything else.
> I'll try to catch some geodjango guys up in Cape Town FOSS4G to have a
> chat about this. It can be something I can find funding for, as it is
> extremely critical in deploying Spatial data infrastructures.
>

Guillaume,

Howdy -- nice hearing from you again. Unfortunately, I won't be in
Cape Town, but Travis Pinney and Josh Livni will be there representing
GeoDjango.

I've successfully created dynamic models in private, production code
using GeoDjango, but not quite to the full extent I think you want.
If I understand, you wish to go from spatial data source (e.g.,
shapefile) -> admin automatically, with the intermediate step of
creating spatial database tables and importing. I believe this is
possible, but not trivial.

Regards,
-Justin

guillaume

unread,
Aug 25, 2008, 12:03:59 PM8/25/08
to Django users
Hi Justin,

Glad to hear you too actually ! Your analysis is completely right !
I've already build an app which allows to feed a postgis database with
Django. But geodata is completely disconnected from the app itself. So
yes, the idea would be to automatically build a geodjango model from
that table, and publish it on the admin site to let people manage the
data from there. And I definitely understand it is not trivial at
all !

Regards,

Guillaume

On 19 août, 16:57, Justin Bronn <jbr...@gmail.com> wrote:
> > Yes, actually I didn't hope anything else.
> > I'll try to catch somegeodjangoguys up in Cape Town FOSS4G to have a
> > chat about this. It can be something I can find funding for, as it is
> > extremely critical in deploying Spatial data infrastructures.
>
> Guillaume,
>
> Howdy -- nice hearing from you again.  Unfortunately, I won't be in
> Cape Town, but Travis Pinney and Josh Livni will be there representingGeoDjango.
>
> I've successfully created dynamic models in private, production code
> usingGeoDjango, but not quite to the full extent I think you want.
Reply all
Reply to author
Forward
0 new messages