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)
Following the OGC specification, MySQL implements spatial extensions as a subset of the SQL with Geometry Types environment. This term refers to an SQL environment that has been extended with a set of geometry types. A geometry-valued SQL column is implemented as a column that has a geometry type.
MySQL supports geometry types of Point , LineString , Polygon , MultiPoint , MultiLineString , MultiPolygon , and GeometryCollection .
Spatial query refers to the process of retrieving a data subset from a map layer by working directly with the map features. In a spatial database, data are stored in attribute tables and feature/spatial tables.
SPATIAL INDEX creates an R-tree index. For storage engines that support nonspatial indexing of spatial columns, the engine creates a B-tree index. A B-tree index on spatial values is useful for exact-value lookups, but not for range scans.
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.
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)
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With