Pergunta

I am writing from two varChar formatted columns named 'lattitude' and 'longitude' to a Point formatted column named 'coordinate' using the statement below.

"UPDATE table_name SET coordinate = PointFromText(CONCAT('POINT(',table_name.longitude,' ',table_name.lattitude,')'))"

I attempting to do a spatial query on the coordinate column using the following statement.

SELECT id , coordinate FROM table_name WHERE MBRContains(GeomFromText('Polygon(-126.728566 49.226434, -123.652395 23.586457,-56.679738 23.908252,-53.076223 55.243002)'), coordinate)

According to this tool the polygon in my query covers the entire U.S. I know there are points in my table that fall in the U.S. but I am still not getting any results (note I'm getting a null result not an error). Any ideas as to what I'm doing wrong?

UPDATE. Below is an updated attempt at executing my query per the suggestion below.

SET @g1 = GeomFromText('Polygon((23.586457 -123.652395,23.908252 -56.679738,55.243002 -53.076223,55.243002 -53.076223,23.586457 -123.652395))');

SELECT id , coordinate FROM table_name WHERE MBRContains(@g1, coordinate)
Foi útil?

Solução

SELECT id , coordinate FROM table_name WHERE MBRContains(GeomFromText('Polygon((-126.728566 49.226434, -123.652395 23.586457,-56.679738 23.908252,-53.076223 55.243002, -126.728566 49.226434))'), coordinate)

This should work. The problem is that Polygon coordinates should be in double parenthesis. It has something to do about interior rings (polygons). Plus as David noticed You need to close polygon.

PS: I noticed You use longitude as X and latitude as Y when this should be other way around. X are horizontal and Y vertical.

Outras dicas

As you are dealing with coordinates you have the lat lng coodinates crossed.

I created a Google Map using the coordinates below(reversing the order of your coordinates in the Question.

new google.maps.LatLng(49.226434,-126.728566),
new google.maps.LatLng(23.586457,-123.652395),
new google.maps.LatLng(23.908252,-56.679738),
new google.maps.LatLng(55.243002,-53.076223)

Polygon

Google Maps does not require polygon to be closed.

According to documentation MBRs require the polygon to be closed.

mysql> SET @g1 = GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');

EDIT In answer to comment Have you the correct CHARSET ?

Typical spatial table

CREATE TABLE IF NOT EXISTS `table_name` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `coordinate` geometry NOT NULL,
   PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Ensure CHARSET=utf8;

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top