2

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

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

My question is this: can I store them in their original form ('TR3241', for example) or do I have to convert them to lat/lng first?

I don't know how to format them as WKT. This is what I get if I try inserting them as POINT(TR3241), which is obviously wrong:

INSERT INTO places (placeid, structidx, subidx, county, name, oscode) VALUES ('10', '1', '1', 'Kent', 'Dover', 'TR3241');
LINE 1: ...'1', 'Kent', 'D1', 'Eastry', 'Bewsbury', 'Dover', 'POINT(TR3...
                                                             ^
HINT:  "POINT(" <-- parse error at position 6 within geometry

So: is there a way that I can store points like 'TR3241' in GeoDjango, or do I have to convert them to standard lat/lng first?

I do have code to convert National Grid > lat/lng, but I was hoping GeoDjango would be able to store them in the original format, and do any conversion for me.

3 Answers 3

2

As your using OSGB36 SRID 27700 - which you have set.

but your using TR3241 [OS Map Tile Ref] this needs to be OS Coordinates therefore TR3241 = 632000,141000

http://www.nearby.org.uk/coord.cgi?p=TR3241&f=full

That will solve your parse error.

1
  • Wow, nearby.org.uk looks really useful. Thanks again.
    – AP257
    Aug 20, 2010 at 15:52
1

Grid reference systems have two components: the grid reference itself, and the coordinates that reference name represents in the gridded projection. You can safely store the latter in GeoDjango (really PostGIS), which understands any coordinates in a cartesian space, but cannot perform arbitrary transformations from gridded systems into cartesian coordinates. As Mapperz mentioned, you could store your point accurately as 632000,141000.

Perhaps the simplest solution is to convert your references into their coordinates, for example in this ruby implementation, see gridrefNumToLet(e, n, digits). This will allow you to retain the coordinates precisely, and convert to and from grid references, without any conversion to lat/long. From those coordinates you can reverse the process to generate the grid reference name as needed.

0

As much as I know, Geodjango can't auto-magically convert UK national grid to latlong.

It would be kinda weird if it could because WKT point has specific syntax POINT(x y), which actually depends on number of spatial dimensions.

So you need to convert your text data to latlong, and then save data in database. I would assume that this data already exist in more readable form, like in Shape file, but im not from UK, nor do i know which data is available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.