gpt4 book ai didi

mysql - 使用 GeomFromText ('POINT(1 1)' 转换纬度/经度对)并插入另一列

转载 作者:可可西里 更新时间:2023-11-01 06:32:46 25 4
gpt4 key购买 nike

在我之前的问题中 Search for range Latitude/Longitude coordinates我的解决方案是创建下表。

mysql> select * from spatial_table where MBRContains(GeomFromText('LINESTRING(9 9, 11 11)'), my_spots);
+------+---------------------------------+
| id | my_spots | my_polygons |
+------+-------------+-------------------+
| 1 | $@ $@ $@ $@ |
+------+-------------+-------------------+

现在我需要将下表中现有的经纬度对转换并移动到 spatial_table。我将如何构建我的查询来实现这一目标?我目前正在使用下面的查询来插入。

mysql> insert into spatial_table values (1, GeomFromText('POINT(1 1)'), GeomFromText('POLYGON((1 1, 2 2, 0 2, 1 1))'));
Query OK, 1 row affected (0.00 sec)

mysql> insert into spatial_table values (1, GeomFromText('POINT(10 10)'), GeomFromText('POLYGON((10 10, 20 20, 0 20, 10 10))') );
Query OK, 1 row affected (0.00 sec)

现有表:

+-------------+---------+--------+-----------+----- ------+-------------+--------------+
| location_id | country | region | city | latitude | longitude | name |
+=============|=========|========|===========|============|=============|==============|
| 316625 | US | CA | Santa Cruz| 37.044799 | -122.102096 | Rio Theatre |
+-------------+---------+--------+-----------+------------+-------------+--------------+

最佳答案

这是成功的秘诀:)我的原始表:

mysql> describe gls;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| location_id | int(255) | NO | PRI | 0 | |
| country | varchar(255) | NO | | | |
| region | varchar(255) | NO | | | |
| city | varchar(255) | NO | | | |
| latitude | float(13,10) | NO | | | |
| longitude | float(13,10) | NO | | | |
+-------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

第 1 步:添加新的 POINT 列

mysql> alter table gls add my_point point;
Query OK, 247748 rows affected (4.77 sec)
Records: 247748 Duplicates: 0 Warnings: 0

第 2 步:使用 lat/lng 字段中的值更新 my_point。

UPDATE gls SET my_point = PointFromText(CONCAT('POINT(',gls.longitude,' ',gls.latitude,')'));

第三步:检查

mysql> select aswkt(my_point) from gls where city='Santa Cruz';
+--------------------------------------+
| aswkt(my_point) |
+--------------------------------------+
| POINT(-122.1020965576 37.0447998047) |
| POINT(-66.25 -12.2833003998) |
| POINT(-2.3499999046 42.6666984558) |
+--------------------------------------+

关于mysql - 使用 GeomFromText ('POINT(1 1)' 转换纬度/经度对)并插入另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7115106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com