I am looking for a solution for finding wether a LAT,LNG Point is contained inside any Polygon in my MySQL table.
For some reason that extends past my SQL knowledge, my queries using ST_Intersects returning 'Function not found'. So i have stried in it's place:
SELECT `area_title` FROM `service_areas` WHERE MBRIntersects(`area_poly`, GEOMFROMTEXT('POINT(40.775032, -73.970778)'));
My table for testing is fairly simply and stores a
1. area_poly GEOMETRY stores POLYGON((-71.740723 41.360319,-71.685791 42.106374,-71.71875 42.407235,-71.905518 42.771211,-72.070312 43.036776,-72.432861 43.157109,-72.718506 43.397065,-73.190918 43.55651,-73.619385 43.580391,-74.32251 43.572432,-75.201416 43.277205,-75.717773 43.004647,-75.926514 42.795401,-76.135254 42.528796,-76.256103 42.138968,-76.289062 41.869561,-76.234131 41.442726,-76.190185 40.955011,-75.992432 40.472024,-75.849609 40.153687,-75.629883 39.783213,-75.311279 39.529467,-74.94873 39.368279,-74.520264 39.257778,-74.256592 39.478606,-71.5979 40.971604,-71.740723 41.360319))
And
2. area_title VARCHAR(20) stores service area 1
I am trying to pass a Point as POINT(40.775032, -73.970778) as shown above.
Expected result would be to return any area_title's that the point is within it's polygon; However, my query returns 0 Rows.
I know there are a ton of questions/answers on slack and the web, but i have not found a solution trying most of the methods. With my lack of SQL knowledge i'm not sure if i'm storing the Poly correctly or not using the correct function to find it.
Any help, even pointing in the right direction is appreciated.
A simple solution for anyone who is running into the same issue:
SELECT ea.name, ea.area_poly from service_areas as ea WHERE contains(ea.area_poly, GeomFromText(AsText(point(#lat, #lng)), 4326))
My polygon ea.area_poly is saved as Polygon((lat1 lng1,lat2 lng2, etc..)) as a geometry type in SQL.
Related
I have Query :
SELECT ST_PolygonFromText(CONCAT('Polygon((',DATA_GPS,'))'))
FROM TABLE_A
CROS JOIN TABLE_B
Show error :
Data truncation: Invalid GIS data provided to function st_geometryfromtext.
Error at line 8 because first and last gps not equal or not close polygon.
show like this :
Polygon((107.15778031165127 -6.304745648974358 107.15945401007279 -6.304639009497479 107.15949692542361 -6.307624906559413))
How to query to exclude not close polygon like that ?
Thanks anyway
I think the "prevention" needs to happen before handing DATA_GPS to the Spatial functions. However, this might work afterwards:
Build a MultiLineString, then check with IsClosed(). If necessary, add the original point at the end.
So I am new at ST_ functions in MySql and I think that I am missing something. I am trying to save a POLYGON in MySql, the problem is that when using the function ST_GEOMFROMTEXT and giving the coordinates of the POLYGON taken from Google Maps Javascript API it returns the error: Invalid GIS data provided to function st_geometryfromtext.
I've read a lot in Internet but everywhere it mostly says that it's a version problem, the thing here is the I have the most recent one right now (5.7.19)
These are the following queries I've tried
# WORKS
SELECT ST_GEOMFROMTEXT('POLYGON((13.517837674890684 76.453857421875,13.838079936422464 77.750244140625,14.517837674890684 79.453857421875,13.517837674890684 76.453857421875,13.517837674890684 76.453857421875))');
# ALL BELLOW RETURN ERROR
SELECT ST_GEOMFROMTEXT('POLYGON((19.4254572621497 -99.17182445526123, 19.42574056861496 -99.16570901870728, 19.421551629818985 -99.16558027267456, 19.421288552764135 -99.17210340499878))');
SELECT ST_GEOMFROMTEXT('POLYGON((-99.17182445526123 19.4254572621497, -99.16570901870728 19.42574056861496, -99.16558027267456 19.421551629818985, -99.17210340499878 19.421288552764135 ))');
SELECT ST_GEOMFROMTEXT('POLYGON((19.4249108840002 -99.17023658752441, 19.424951356518726 -99.16802644729614, 19.423393157277722 -99.16796207427979, 19.423393157277722 -99.17019367218018))')
Does anyone knows why these queries above are failing? Thank you a lot everyone
Please try these queries -
SELECT ST_GEOMFROMTEXT('POLYGON((19.4254572621497 -99.17182445526123, 19.42574056861496 -99.16570901870728, 19.421551629818985 -99.16558027267456, 19.421288552764135 -99.17210340499878, 19.4254572621497 -99.17182445526123))');
SELECT ST_GEOMFROMTEXT('POLYGON((-99.17182445526123 19.4254572621497, -99.16570901870728 19.42574056861496, -99.16558027267456 19.421551629818985, -99.17210340499878 19.421288552764135, -99.17182445526123 19.4254572621497 ))');
SELECT ST_GEOMFROMTEXT('POLYGON((19.4249108840002 -99.17023658752441, 19.424951356518726 -99.16802644729614, 19.423393157277722 -99.16796207427979, 19.423393157277722 -99.17019367218018, 19.4249108840002 -99.17023658752441))')
Basically, the polygon needs to be "closed"
I'm storing geo coordinates in a MySQL table using a spatial index. The problem is that queries where I want to know if a tuple/location is within a bounding box (closed polygon described by 5 coordinates; first an last coordinate are the same) return an empty results. Here's a minimum example that is not working for me as expected:
CREATE TABLE osTest(
loc Point NOT NULL
) ENGINE=MyISAM;
CREATE SPATIAL INDEX locIndex ON osTest(loc);
-- New York Central Park, The Great Lawn
INSERT INTO osTest (loc) VALUES (POINT(40.781343, -73.966598));
If I execute the following table scan query:
SELECT AsText(loc), X(loc), Y(loc) FROM osTest;
all seems to work fine -- the result looks like this:
+-----------------------------+-----------+------------+
| AsText(loc) | X(loc) | Y(loc) |
+-----------------------------+-----------+------------+
| POINT(40.781343 -73.966598) | 40.781343 | -73.966598 |
+-----------------------------+-----------+------------+
However, when I try to execute a query that is using the spatial index, the result is always empty. The polygon the in following polygon describes the whole Central Park:
SELECT AsText(loc), X(loc), Y(loc) FROM osTest WHERE WITHIN(loc, GeomFromText('POLYGON(40.800716 -73.958358, 40.796858 -73.949120, 40.764216 -73.973153, 40.768108 -73.981929, 40.800716 -73.958358)') );
I've also tried INTERSECTS, OVERLAPS -- same empty result set. What am I missing here?
I am not an expert on mysql GIS, but in postgis the points have to be Latitude,Longitude. My belief is that it is the same in mysql 5.6 GIS data types as well.
According to that theory; in your query, y and x should be switched
I found the issue. I had an error in my SQL query. It doesn't result in a syntax error, but obviously it doesn't work correctly. More specifically, I had to add additional brackets. I had to change:
GeomFromText('POLYGON(40.800716 -73.958358, ...)')
to
GeomFromText('POLYGON((40.800716 -73.958358, ...))')
I simply overlooked it when I searched for examples. And that I didn't get any syntax errors or something didn't help either.
I've been breaking my head for this already for a while. I need to check whether a lat/long position reside within a polygon through MySQL. According this post i should do it like this:
SELECT ST_Intersects(ST_GeometryFromText('POINT(-33.7507, 151.1445)'),
ST_GeometryFromText('POLYGON ((
-33.7508 151.1444,
-33.7505 151.1447,
-33.7507 151.1450,
-33.7509 151.1454,
-33.7514 151.1449,
-33.7508 151.1443,
-33.7508 151.1444))'))
However this is returning me null. Any ideas? Im using MySQL 5.6.13
Very cool. Didn't know mysql had this feature.
To construct a point, use no comma:
'POINT(-33.7507 151.1445)'
fiddle
I tried inserting
PolygonFromText("POLYGON((121.44842136764532 31.22119260287111,
121.45076025390631 31.221990825071376,
121.45402182006842 31.218366658611853,
121.45091045761114 31.217054584347302))")
as a value into a a field of both type Polygon and of type Geometry.
When I run
SELECT PolygonFromText("POLYGON((121.44842136764532 31.22119260287111,
121.45076025390631 31.221990825071376,
121.45402182006842 31.218366658611853,
121.45091045761114 31.217054584347302))")
it returns NULL
My Mysql Version is 5.1.41 - I find the MySql documentation very poor and not user friendly in these cases
I think a Polygon has to close so the last set of coordinates should be same as first one. This will return following
SELECT PolygonFromText("POLYGON((121.44842136764532 31.22119260287111,121.45076025390631 31.221990825071376,121.45402182006842 31.218366658611853,121.45091045761114 31.217054584347302,121.44842136764532 31.22119260287111))");