Trouble inserting polygon to mysql database - mysql

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

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.

Mysql - Convert Point Geometry in British National Grid (OSGB36) to Latitude/Longitude (wgs84)

I have a data dump (csv) of place names from https://www.ordnancesurvey.co.uk/business-and-government/help-and-support/products/os-open-names.html
I need to import this into mysql however the geometry co-ordinates use BNG (OSGB36). Does Mysql have any functions to convert these co-ordinates to wgs84 lat/long or is there any other sql method to achieve this?
another option is perhaps loading it into postgis - does postgis have any funtion to transform BNG to lat/long? Perhaps I could do that and then export the data once transformed and load it into mysql?
In postgis I could do something as follows (not tested)
select AddGeometryColumn('locations', 'the_geom', 27700, 'POINT', 2);
-- X and Y are the BNG co-ordinates
UPDATE locations SET the_geom = ST_GeomFromText('POINT(' || x || ' ' || y || ')', 27700 );
alter table locations add column lat real;
alter table locations add column long real;
update locations set long=st_x(st_transform(the_geom,4326)),
lat=st_y(st_transform(the_geom,4326));
Is it possible to do these type of function in mysql - basically what are the equivalent functions in mysql. I cant seem to figure the syntax out? The following doesnt work in mysql:
update locations set long=ST_X(ST_Transform(the_geom,4326)),
lat=ST_Y(ST_Transform(the_geom,4326));
I get error function ST_Transform does not exist. I'm using mysql 5.7
* UPDATE *
Sample data:
NAME1 LOCAL_TYPE GEOMETRY_X GEOMETRY_Y DISTRICT_BOROUGH REGION COUNTRY
Southport Town 333510 417225 Sefton North West England
The answer is, of all places, here
Answered by Hartmut Holzgraefe in this comment.
So far the SRID property is just a dummy in MySQL, it is stored as
part of a geometries meta data but all actual calculations ignore it
and calculations are done assuming Euclidean (planar) geometry.
So ST_Transform would not really do anything at this point anyway.
I think the same is still true for MariaDB, at least the knowledge [sic]
base page for the SRID() function still says so:
This WorkLog discusses the progress of implementing ST_Transform.
MySQL 8.0 does seem to have it implemented: https://dev.mysql.com/doc/refman/8.0/en/spatial-operator-functions.html#function_st-transform
So, the solution may require upgrading to MySQL 8.0.
The changelog for the very recent 8.0.13 says:
----- 2018-10-22 8.0.13 General Availability -- -- -----
MySQL now implements the
ST_Transform()
spatial function for use in converting geometry values from one
spatial reference system (SRS) to another. Currently, it supports
conversion between geographic SRSs. For details, see Spatial Operator
Functions.

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.

MySQL 5.7.19 Invalid GIS data provided to function st_geometryfromtext

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"

Retrieving the coordinates of the MySQL point type

I'm storing lat/long pairs in MySQL as a point using something like:
GeomFromText('POINT(32 -122)')
Given the point, how do i retrieve the individual X/Y coordinates?
Let's say you store GeomFromText('POINT(32 -122)') as a column called MY_POINT in a table called MY_TABLE.
Getting the X coordinate (will return 32 in this example):
SELECT ST_X(MY_POINT) as longitude FROM MY_TABLE;
Getting the Y coordinate (will return -122 in this example):
SELECT ST_Y(MY_POINT) as latitude FROM MY_TABLE;
Important: Prior to version 5.6, use X() and Y() instead of ST_X() and ST_Y().
Let's say you store GeomFromText('POINT(32 -122)') as a column called MY_POINT in a table called MY_TABLE.
There are several ways to get the point data.
First Check MySQL version
SELECT VERSION()
For MySQL 5.6 or older version using
SELECT X(MY_POINT) AS Latitude, Y(MY_POINT) AS Longitude FROM MY_TABLE
For MySQL 5.7 version using, although previous command also recognized.
SELECT ST_X(MY_POINT) AS Latitude, ST_Y(MY_POINT) AS Longitude FROM MY_TABLE
For MySQL 8.0 version using, although previous command also recognized. The main difference is this command for Point objects that have a geographic spatial reference system (SRS).
SELECT ST_Latitude(MY_POINT) AS Latitude, ST_Longitude(MY_POINT) AS Longitude FROM MY_TABLE
Hope this would helps.