Question

In MySQL I have a a database with around 100 tables.

They all contain a column called ´shape´, this is a polygon type field.

It contains information in what I believe (st_srid returns 1, but it's wrong) is SRID 31287.

I would like to convert it to SRID 4326, how would I go about to do this?

UPDATE This is a very old question with very old answers, recently a new answer was submitted which is IMHO the approach to use if the versioning is correct. I am not marking the new answer as correct as it's unfair to the person who gave the correct answer at the time of the question. If you're on version 8 however, follow this answer to this question by UncertaintyP : https://stackoverflow.com/a/65314337/2973474

Was it helpful?

Solution 2

You have two problems here.

  1. Your internal representation is wrong, it's SRID 1, and it should be SRID 31287. Changing an internal representation is possible in every database: MySQL will get a mutator for it in version 8, ST_SRID, PostGIS has ST_SetSRID
  2. You need to actually convert from SRID 31287 to SRID 4326. This is possible ONLY in PostGIS. Neither MySQL nor Microsoft SQL can reproject an SRID.

To tackle the first problem in PostGIS, you'd use ST_SetSRID, and to tackle the second problem you'd use ST_Transform.

OTHER TIPS

An update of the conversion in MySQL for the lost souls that found their way to this question:

In version 8 you now have ST_Transform

mysql> SET @p = ST_GeomFromText('POINT(52.381389 13.064444)', 4326);
mysql> SELECT ST_AsText(@p);
+----------------------------+
| ST_AsText(@p)              |
+----------------------------+
| POINT(52.381389 13.064444) |
+----------------------------+
mysql> SET @p = ST_Transform(@p, 4230);
mysql> SELECT ST_AsText(@p);
+---------------------------------------------+
| ST_AsText(@p)                               |
+---------------------------------------------+
| POINT(52.38208611407426 13.065520672345304) |
+---------------------------------------------+

In MySQL it looks like this:

UPDATE `table name` SET `column name` = ST_GeomFromText(ST_AsText(`column name`), 4326);

Hope this helps

This worked for me

(For MySQL)

UPDATE Table SET SpatialColumn = ST_GeomFromText(ST_AsText(SpatialColumn), 4326);

I can only hope this will help you (either directly or as a pointer) as its an answer for SQL Server, but how about...

UPDATE Table SET SpatialColumn = Geometry::STGeomFromText(SpatialColumn.STAsText(), 4326);

Naturally, you can swap Geometry for Geography if you're using Geography columns. You'll need to replace Table with your table name and SpatialColumn with the name of your Spatial Column.

NOTE: This assumes your SRID of 31287 defines coordinates as decimal latitude / longitude values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top