Me sale este error:
3674 The spatial index on column 'position' will not be used by the
query optimizer since the column does not have an SRID attribute.
Consider adding an SRID attribute to the column.
Así que después de leer este artículo: https://mysqlserverteam.com/geographic-indexes-in-innodb/ , he decidido añadir SRID a la columna, también traté de leer la documentación, pero todavía no entiendo de qué se trata. Así que hago lo que dice el texto y añadir así:
DROP DATABASE IF EXISTS `gis`;
CREATE DATABASE IF NOT EXISTS `gis`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `gis`;
DROP TABLE IF EXISTS user;
CREATE TABLE IF NOT EXISTS user (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(48) NOT NULL,
gender ENUM('male', 'female') NOT NULL,
age TINYINT UNSIGNED NOT NULL,
position POINT NOT NULL SRID 4326
);
ALTER TABLE user ADD SPATIAL INDEX(position);
Y luego intento agregar algunas filas:
INSERT INTO user (firstname, gender, age, position) VALUES ('Alexander', 'male', 34, POINT(63.429909, 10.393035));
INSERT INTO user (firstname, gender, age, position) VALUES ('Dina', 'female', 21, POINT(63.426300, 10.392481));
INSERT INTO user (firstname, gender, age, position) VALUES ('Martin', 'male', 32, POINT(63.422304, 10.432027));
INSERT INTO user (firstname, gender, age, position) VALUES ('Tina', 'female', 19, POINT(63.430603, 10.373038));
INSERT INTO user (firstname, gender, age, position) VALUES ('Kristin', 'female', 20, POINT(63.434858, 10.411359));
INSERT INTO user (firstname, gender, age, position) VALUES ('Mette', 'female', 33, POINT(63.420422, 10.403811));
INSERT INTO user (firstname, gender, age, position) VALUES ('Andres', 'male', 34, POINT(63.419488, 10.395722));
INSERT INTO user (firstname, gender, age, position) VALUES ('Sandra', 'female', 25, POINT(63.432053, 10.408738));
INSERT INTO user (firstname, gender, age, position) VALUES ('Kine', 'female', 29, POINT(63.432302, 10.412643));
INSERT INTO user (firstname, gender, age, position) VALUES ('Henrik', 'male', 25, POINT(63.421055, 10.443288));
Pero me da error:
Error Code: 3643. The SRID of the geometry does not match the SRID of the column 'position'.
The SRID of the geometry is 0, but the SRID of the column is 4326.
Consider changing the SRID of the geometry or the SRID property of the column.
Si quito el SRID entonces el índice no funciona cuando hago una consulta como esta con explique:
SET @distance = 3.5;
SET @my_place_lng = 63.431592;
SET @my_place_lat = 10.396210;
EXPLAIN SELECT
id,
firstname,
gender,
age,
ST_Distance_Sphere(Point(@my_place_lng, @my_place_lat), position) AS distance_from_me
FROM user
WHERE
ST_Contains(ST_MakeEnvelope(
Point((@my_place_lng+(@distance/111)), (@my_place_lat+(@distance/111))),
Point((@my_place_lng-(@distance/111)), (@my_place_lat-(@distance/111)))
), position)
ORDER BY distance_from_me ASC;
¿Que puedo hacer? Ni siquiera sé dónde hay que leer para entender más, no he visto ningún ejemplo que entiendo.
especificar el valor SRID para la geometría, utilizando el ST_SRID
decir la función
envuelva la definición punto
... , 34, POINT(63.429909, 10.393035 ));
Me gusta esto:
... , 34, ST_SRID( POINT(63.429909, 10.393035 ,4326) ));
^^^^^^^^ ^^^^^^
Tenga en cuenta que también tendrá el valor SRID para las geometrías que se fijará en el ejemplo de la consulta.
manual de MySQL Referencia dice que en lugar de que la creación de la geometría en SRID 0 y luego echándola a SRID 4326, la alternativa preferida es la de crear la geometría en SRID 4326 directamente, por ejemplo,
... , 34, ST_PointFromText('POINT(63.429909, 10.393035)',4326) ));
^^^^^^^^^^^^^^^^^^ ^^^^^^^
Si no queremos hacer eso, entonces otra alternativa (menos deseable) sería la de establecer el valor SRID de la columna en el valor predeterminado 0 en lugar de 4326. Cuando no fijamos el valor SRID, MySQL utiliza el valor por defecto valor de 0. Pero entonces estaríamos trabajando en el plano liso sin unidades del sistema de coordenadas. Eso no es bueno si queremos que la geometría de punto a ser manejado como grados tierra GPS latitud / longitud coordenadas.
Referencia:
https://dev.mysql.com/doc/refman/8.0/en/gis-general-property-functions.html#function_st-srid