47

I created a spatial table with SRID:4326. Now I want to change total projection to SRID:32644 into a new table. The old table should remain unchanged.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Satya Chandra
  • 1,409
  • 4
  • 23
  • 47
  • I'm adding this as a comment instead of an answer because there should be a more elegant method. But you could copy the table, and then run: UPDATE SET the_geom=ST_Transform(the_geom,32644); (Assuming you have the complete entry in your spatial_ref_sys for 32644.)
    – L_Holcombe Nov 02 '12 at 08:05
  • i tried but igot this error update abc SET geom=ST_Transform(geom,32644); new row for relation "abc" violates check constraint "enforce_srid_geom" – Satya Chandra Nov 02 '12 at 08:31
  • 1
    drop that constraint. and its fixed – simpleuser001 Nov 06 '12 at 08:49

3 Answers3

84

If you're on PostGIS 2.0+, you can go:

ALTER TABLE mytable 
  ALTER COLUMN geom 
  TYPE Geometry(Point, 32644) 
  USING ST_Transform(geom, 32644);
Evan Carroll
  • 7,071
  • 2
  • 32
  • 58
Paul Ramsey
  • 19,865
  • 1
  • 47
  • 57
21
CREATE TABLE new_table AS 
  SELECT ST_Transform(the_geom,32644) AS the_geom 
  FROM original_table;

There should be an integer ID field in your spatial table in order to add it to QGIS.

underdark
  • 84,148
  • 21
  • 231
  • 413
Vladimir
  • 1,569
  • 7
  • 15
3

follow this way:

  1. CREATE TABLE 'new_table' AS SELECT * FROM 'old_table';
  2. ALTER TABLE new_table DROP CONSTRAINT enforce_srid_the_geom;
  3. ALTER TABLE new_table DROP CONSTRAINT enforce_geotype_the_geom;
  4. UPDATE new_table SET the_geom = ST_SetSRID(the_geom, new_srid);
  5. ALTER TABLE new_table ADD CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = (new_srid));
  6. ALTER TABLE new_table ADD CONSTRAINT enforce_geotype_geom CHECK ((geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL);
  7. That is it!

if you cannot create new table in first line pls try 2. and 3. first then create your table with number 1.

i hope it helps you...

urcm
  • 22,533
  • 4
  • 57
  • 109
  • 4
    This solution not reproject geometries. If geometries are stored using other SRID, data will be inconsistence after change constrains. You need use st_trasnform. – angelcervera Nov 12 '12 at 08:52