我在我的MySQL數據庫中有一個名爲house
的表。將緯度/經度文本列移動到「點」類型列
在house
表格中,有幾個文本列,分別爲latitude
和longitude
。
我添加了一個名爲coords
新列,point
型 - http://dev.mysql.com/doc/refman/5.0/en/gis-class-point.html
我將如何移動latitude
和longitude
值到新coords
列?
我在我的MySQL數據庫中有一個名爲house
的表。將緯度/經度文本列移動到「點」類型列
在house
表格中,有幾個文本列,分別爲latitude
和longitude
。
我添加了一個名爲coords
新列,point
型 - http://dev.mysql.com/doc/refman/5.0/en/gis-class-point.html
我將如何移動latitude
和longitude
值到新coords
列?
假設你想要一個SPATIAL
指數在此列:
ALTER TABLE mytable ADD coords Point;
UPDATE mytable
SET coords = Point(lon, lat);
ALTER TABLE mytable MODIFY coords NOT NULL;
CREATE SPATIAL INDEX sx_mytable_coords ON mytable(coords);
如果不這樣做,你可以省略最後兩步。
更新:
在早期版本的MySQL
,你需要使用WKT
填充Point
列:
UPDATE mytable
SET coords = GeomFromText(CONCAT('POINT (', lon, ' ', lat, ')'))
簡明扼要:
UPDATE myTable SET coords = GeometryFromText(CONCAT('POINT(', lon, ' ', lat, ')'));
注意,從Quassnoi是回答因爲正確的輸入格式是POINT(XY)或in地球條款POINT(lon lat)。
注意您可以通過X()和Y()函數,如下面的例子表明幾點:
SELECT X(GeometryFromText(CONCAT('POINT(', 35, ' ', 60, ')'))) AS x, Y(GeometryFromText(CONCAT('POINT(', 35, ' ', 60, ')'))) AS y;
你是積極的,它是lon-lat ..在網絡上的其他一切都說相反 – 2012-12-03 09:33:59
@Jonathan它是POINT(x,y)。看到這個證明'SELECT X(POINT(3,2))AS x'你會回來3。 – 2012-12-03 17:01:13
MySQL版本5.5.8
我的緯度和經度float類型。要更新現有的行...
UPDATE table_name SET coord = POINT(longitude_field, latitude_field);
要考慮的事情,如果你正在收集數據,並需要單獨保存的緯度和經度,在各自的專欄中,我建議增加一個觸發器到表
CREATE DEFINER=`username`@`localhost` TRIGGER `table_name`.`create_point_geom`
BEFORE INSERT ON database_name.table_name FOR EACH ROW
BEGIN
SET NEW.coord = POINT(NEW.longitude, NEW.latitude);
END;
我收集地理標記的社交媒體數據,我使用這種方法添加幾何到我的表。
第二個命令似乎沒有將值移入座標。我得到這個錯誤:無法從您發送到GEOMETRY字段的數據中獲取幾何對象 – cannyboy 2011-04-22 14:25:46
@cannyboy:您正在使用哪個版本的MySQL? 'lat'和'lon'可以'NULL'嗎? – Quassnoi 2011-04-22 14:27:04
MySQL 5.0.45。緯度和經度文本列不爲空。 – cannyboy 2011-04-22 14:31:57