views:

42

answers:

1

I'm trying to insert some National Grid references into a Django PointField defined as follows:

oscode = models.PointField(srid=27700, null=True, blank=True)

However, I don't know how to format them correctly in WKT. This is what I get if I try simply using a basic National Grid reference, TR3241:

INSERT INTO places (placeid, structidx, subidx, county, name, oscode) VALUES ('10', '1', '1', 'Kent', 'Dover', 'TR3241');
psycopg2.InternalError: parse error - invalid geometry
LINE 1: ...'1', 'Kent', 'D1', 'Eastry', 'Bewsbury', 'Dover', 'TR3241', ...
                                                             ^
HINT:  You must specify a valid OGC WKT geometry type such as POINT, LINESTRING or POLYGON

And this is what I get if I (guessing wildly after reading up on WKT!) use POINT(TR3241):

psycopg2.InternalError: parse error - invalid geometry
LINE 1: ...'1', 'Kent', 'D1', 'Eastry', 'Bewsbury', 'Dover', 'POINT(TR3...
                                                             ^
HINT:  "POINT(" <-- parse error at position 6 within geometry

How do I format the grid ref correctly?

+1  A: 

You would just need something like this, assuming a point with lon1 lat1:

 insert into geography_table (name, geometry_field) values ('Chez Francois',\
 ST_GeomFromText('POINT(lon1 lat1)', 27700));

It looks like your geometry column is called oscode; to check use psql mydb and hit \d. You should see a list of all tables, the one(s) you are interested in will be listed as type geometry. There should be a line at the bottom something like "enforce_srid_oscode" CHECK (st_srid(oscode) = 27700).

The big picture bit that may be throwing you off is that these records are just regular database tables, with at least one column containing details about some geometry on Earth. The inserts on these geometric values must be geometric types, and the way you get from (relatively) plain English to these geometries is with a geometry constructor.

bvmou