Trouver la distance entre deux points dans MYSQL. (En utilisant le type de données Point)

StackOverflow https://stackoverflow.com/questions/2270181

Question

Supposons que j'ai une table de la colonne 2 comme suit:

| user_id      | int(11) | NO   | UNI | NULL    |                |
| utm          | point   | NO   | MUL | NULL    |                |

Comme vous pouvez le voir, il est très simple. utm est un point type de données. Insérer comme ceci:

INSERT INTO mytable(user_id, utm) VALUES(1, PointFromWKB(point(50, 50)));

Alors, je crée un index spatial.

ALTER TABLE mytable ...add spatial index on(utm) or something. (forgot)

Bon, tout est bon. Maintenant, je veux select * où distance <99999. Mais cela ne fonctionne pas!

//This is supposed to select all where the distance is less than 99999999.
set @mypoint = PointFromWKB(point(20,20))
select * from mytable where GLength(LineString(utm, @mypoint)) < 9999999;
Empty set (0.00 sec)
select * from mytable where GLength(LineStringFromWKB(LineString(utm, @mypoint))) < 9999;
Empty set (0.00 sec)

Par ailleurs, j'ai essayé de INSERT INTO sans PointFromWKB ... et ça n'a pas marché ... c'est pourquoi quelqu'un a suggéré que PointFromWKB pour moi.

Était-ce utile?

La solution

Résolu. Voilà ce que je l'ai fait:

where GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(@mypoint)))) < 9999999999999;

Autres conseils

Vous pouvez aussi le faire de cette façon. Je ne sais pas si elle est plus rapide ou non.

select * from mytable where glength(geomfromtext(concat('linestring(', x(utm), ' ', y(utm), ',20 20', ')'))) < 99999999

Pour autant que je sache, u dois essayer cette façon -

select * from mytable
   where 
    (
        GLength(
          LineStringFromWKB(
            LineString(
              geoPoint, 
              GeomFromText('POINT(51.5177 -0.0968)')
            )
          )
        )
      ) < 99999999

Plus cette réponse .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top