views:

80

answers:

1

Hello all,

PostgreSQL / Django / newbie here.

I've got a bunch of JSON data with venues and lat/long information, and I'm looking to import it into my database. So far I've fenagled a script to format my JSON into SQL statements, so I can import them with a slew of INSERT statements.

But my 'location' field (a PointField) is giving me grief when I try to use ST_PointFromText('POINT(lon lat)') syntax. Specifically:

ERROR: new row for relation "finder_venue" violates check constraint "enforce_srid_location"

So how do I generate a proper SRID?

Or, given Django's ease, I can't help but feel like I'm committing programmatic heresy here. Can someone point me in a more intelligent direction?

Thanks,

Jon

+1  A: 

If you want to set the SRID in PostGIS, you can do this:

ST_SetSRID(ST_PointFromText('POINT(-122 37)'), 4326)

(4326 is the SRID here.)

For GeoDjango, your model will be defined like this:

from django.contrib.gis.db import models
class Thing(models.Model):
    # ...
    location = models.PointField(srid=4326)  
    objects = models.GeoManager()

If you're actually using 4326, you can leave it out, as that's the default. Then, you'll set the location of your model instances so:

from django.contrib.gis.geos import Point
some_thing.location = Point(-122, 37)
tcarobruce
Maybe that's part of my issue -- I don't know what a SRID even maps to, so I'm not sure which one I need to use. Can you elaborate?
bahoo
Sweet, your syntax worked like a charm. 4326 seems to be the ticket. For others who are lost like I was, this thread has good insights on what SRIDs are: http://stackoverflow.com/questions/373589/geometry-column-stgeomfromtext-and-srid-what-is-an-sridThanks!
bahoo