MySQL 5.7.19 Invalid GIS data provided to function st_geometryfromtext - mysql

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"

Related

Mysql finding intersecting lat lng point in polygon

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.

How to Prevent error Mysql ST_PolygonFromText because first and last Coordinate not equals

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.

Multiple functions in a $where clause

I am constructing a url for a CMS data query. I get the following to work just fine:
https://data.cms.gov/resource/din4-7td8.json?$$app_token=REDACTED&$where=(starts_with(nppes_provider_zip,'63703') OR starts_with(nppes_provider_zip,'63701')) AND (hcpcs_code='31623' OR hcpcs_code='31622')
When I try to substitute calling multiple hcpcs_code values, I get a query.compiler.malformed error. The following generates the error:
https://data.cms.gov/resource/din4-7td8.json?$$app_token=REDACTED&$where=(starts_with(nppes_provider_zip,'63703') OR starts_with(nppes_provider_zip,'63701') AND hcpcs_code in('31622','31623'))
Is it possible that I am using the in(...) function incorrectly?
I think I may have caught you on IRC after you asked this question, but I'll answer here too.
It looks like you're using an old version of that API endpoint which doesn't support the IN(...) function. If you migrate to the new version of that dataset API you'll be able to issue your query:
http://dev.socrata.com/foundry/#/data.cms.gov/5fnr-qp4c
https://data.cms.gov/resource/5fnr-qp4c.json?$where=(starts_with(nppes_provider_zip,'63703') OR starts_with(nppes_provider_zip,'63701') AND hcpcs_code in('31622','31623'))

How to close an open polygon in mysql GIS spacial extensions

I have a mysql table containing polygons some of which are open and I need to close them. Presumably by appending the "StartPoint" to the end of the "Linestring" and rewriting as a polygon. Any ideas?
Sure I can grab out and test a polygon to see if its open or closed with
SELECT ExteriorRing(SHAPE) into #testshape from tablename where OGR_FID=1;
SELECT (StartPoint(#testshape)) INTO #SP;
SELECT (EndPoint(#testshape)) INTO #EP;
SELECT #EP=#SP as myIsRing;
ok fine but when it's open I need to append the Startpoint to the end of the line string in order to close the polygon. Is there a way to append that "closure" point and create a closed polygon?
Might be worth noting that I tried (as per the man page)
SELECT UNION(#testshape,#SP) INTO #newshape;
And got the standard syntax error ERROR 1064 (42000) as though my version didnt accept the UNION function.
Sorry everyone, Threw a couple more hours at the problem and got the following
select polygon(geomfromtext(astext(concat(geomfromtext(astext(#testshape)),(geomfromtext(astext(#SP))))))) INTO #newpoly;
Not exactly clean but it worked and can be tested with:
SELECT (StartPoint(EXTERIORRING(#newpoly))) INTO #SP2;
SELECT (EndPoint(EXTERIORRING(#newpoly))) INTO #EP2;
SELECT #EP2=#SP2 as myIsRing;

Trouble inserting polygon to mysql database

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))");