Hi All,
I have a column 'geocode' in a table which has attributes like 2329/4727 now I would like to convert these attributes in another column to 'lat' and 'long'. For example, 'Lat' column will have attribute '23.29' and 'long' column will have attribute '47.27'.
Is there a way through which this can be converted automatically by a query or so.
Looking fwd..
Thanks
YJ
_______________________________________________
postgis-users mailing list
postgi...@postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users
I'm a beginner, so take with a pinch of salt, but first I'd question
why you want/need to separate the latitude & longitude?
I would keep them together and store them as a point in a new column.
First create a column to store the Lat & Long in with something like
this:
SYNTAX: SELECT AddGeometryColumn(<table_name>, <column_name>,
<srid>, <type>, <dimension>)
EXAMPLE: SELECT AddGeometryColumn('parks', 'park_geom','4326', 'POINT', 2 );
Then you want to populate that column, with the existing data from
your geocode column. Using something like this:
UPDATE tablename
SET latlongcolumn = ST_GeomFromText('geocode', 4326)
I guess that there is a way to pull out the lat and long into seperate
columns, but I don't know how to do that I'm afraid.
Cheers
James
I would suggest that first you check your data has a consistent format, i.e. “zero” is represented as 0000, 12.3 degrees is represented as 1230 and 1.23 degrees is represented as 0123.
Otherwise you need to do a little preparation work…
HTH,
Michael
Instead of storing lat & long columns of numbers, create a point geometry with Postgis, you can still select the lat & long values as shown below. select addgeometrycolumn('','tablename','geom',4326,'POINT',2); To run this change 'tablename' to the name of the table with the geocode column. This will give you a Postgis geometry column to store lat/long points in. geocode is presumeably a string datatype, and there are a number of ways to parse this, here is one example. I'm assuming each value will always be 4 digits then "/" then 4 digits, as in your example. If not, then you'll need another approach. update tablename set geom=setsrid(makepoint(substr(geocode,6,4)::decimal(6,2)/100, substr(geocode,1,4)::decimal(6,2)/100), 4326); This takes characters 6-9 of the geocode value, converts to a number, divides by 100, then does the same to characters 1-4, which generates your lon & lat (in xy order) values as you describe them, then assigns a srid of 4326 to tell postgis the coordinates are indeed lat/long ones, and sets the value of each point geometry to these coordinates. If you want to see the resulting lat/long values now stored in the geom column, try: select geocode, astext(geom) from tablename; or select geocode, y(geom) as lat, x(geom) as lon from tablename; HTH, Brent Wood --- On Wed, 7/13/11, Yamini Singh <yamini...@live.com> wrote: |
-----Inline Attachment Follows----- |
Glad it helped. You can do that, but that is simply using Postgres columns, you do not require Postgis & geometry capabilities to do that. I recommend you avoid upper case characters in table & column names, otherwise you'll need to quote them. alter table <tablename> add column lat_dms int; update <tablename> set lat_dms=('-'||substr(geocode,1,4)||'00')::int; The "||" operator is a string concatenation operator, so the sql starts with '-', appends the specified substring from geocode, appends two more zeros, then converts the whole thing to an integer. If the columns you will be comparing them with are strings, not numbers, then create these two columns as the same datatype & don't do the "::int" conversion. Then do the same for lon - but remember to substr(geocode,6,4) instead (or combine the sqls to do both in a single statement). Note that my previous example creating a Postgis geometry assumed that 1234 was decimal degrees (12.34deg). So if the numbers reflect degrees & minutes (12deg 34min) then the result is incorrect. You should substring the deg & min separately, convert both to numeric, divide the minute value by 60 then add them to get the decimal degree value. Cheers, Brent |
--- On Wed, 7/13/11, Yamini Singh <yamini...@live.com> wrote: |
|
I will be on vacation with limited email from July 14 to August 7, 2011.
Bonjour,
Je serai en conge du 14 juillet jusqu'au 7 aout, 2011.
Hi Bret,
Thanks for your help. I am now able to update lat and long column as explained by you.
Sometimes I attributes like ‘0002N/5155’ in geocode column. Now in the lat_dms column the attribute should be ‘000200’ and ‘-515500’in long_dms column. But how do I get the ‘N’ in ‘lat’ part recognized in query so that it is not placed as ‘-000020’ but as positive coordinate. Also, the number of character will get changes in ‘lat’ it will now be 1 to 5 character and ‘7 to 10’ in long.
Is there a possibility of having one query that takes care of ‘geocode’ in 4 x 4 format as well as 5 x 4 ----format with fifth word as N?
+-----------------------------+
| Lat_dms | lat_dms |
-------------------------------
| -232900 | -472700 |
-------------------------------
| 000200 | -515500 |
+-----------------------------+
Thanks for your help.. actually I am novice to GIS and postgres world….
Thanks,
YJ
Glad it helped. You can do that, but that is simply using Postgres columns, you do not require Postgis & geometry capabilities to do that. I recommend you avoid upper case characters in table & column names, otherwise you'll need to quote them. alter table <tablename> add column lat_dms int; update <tablename> set lat_dms=('-'||substr(geocode,1,4)||'00')::int; The "||" operator is a string concatenation operator, so the sql starts with '-', appends the specified substring from geocode, appends two more zeros, then converts the whole thing to an integer. If the columns you will be comparing them with are strings, not numbers, then create these two columns as the same datatype & don't do the "::int" conversion. Then do the same for lon - but remember to substr(geocode,6,4) instead (or combine the sqls to do both in a single statement). Note that my previous example creating a Postgis geometry assumed that 1234 was decimal degrees (12.34deg). So if the numbers reflect degrees & minutes (12deg 34min) then the result is incorrect. You should substring the deg & min separately, convert both to numeric, divide the minute value by 60 then add them to get the decimal degree value. Cheers, Brent |
| Hi, Yes you can, you use the case statement. Here is an example: create table ttt (id serial primary key, geocode varchar(12), lat int, lon int); insert into ttt values (default, '1234/5678', null, null); insert into ttt values (default, '1234N/5678', null, null); update ttt set lat=(case when substr(geocode,5,1)='N' then (substr(geocode,1,4)||'00')::decimal(6,2) else ('-'||substr(geocode,1,4)||'00')::decimal(6,2) end); select * from ttt; id | geocode | lat | lon ----+------------+---------+----- 1 | 1234N/5678 | 123400 | 2 | 1234/5678 | -123400 | Note you will also need to use a similar case statement for lon, as the substr values will change due to the extra character offset, as below update ttt set lon=(case when substr(geocode,5,1)='N' then ('-'||substr(geocode,7,4)||'00')::int else ('-'||substr(geocode,6,4)||'00')::int end); select * from ttt; id | geocode | lat | lon ----+------------+---------+--------- 1 | 1234N/5678 | 123400 | -567800 2 | 1234/5678 | -123400 | -567800 HTH, Brent --- On Thu, 7/14/11, Yamini Singh <yamini...@live.com> wrote: |
| Not quite. You need to reproject the lat long coords to 29101 before writing them to your table. Your first SQL creating the column is fine, the second update needs changing. UPDATE tablename SET geom_col = st_transform(st_setsrid(makepoint(long_dd,lat_dd),4326),29101); this: creates a point from two numbers (makepoint) sets this to lat/long (setsrid to 4326) reprojects to 29101 (st_transform to 29101) updates each gem_col with this value Cheers, Brent |
--- On Sat, 7/16/11, Yamini Singh <yamini...@live.com> wrote: |
|
Date: Saturday, July 16, 2011, 5:39 AM |
|