If I run this query
SELECT region_id FROM shape_region WHERE ST_Within(point(-117.10480, 32.72204),shape_region.shape)=1
on MySQL MariaDB version 10.1.13-MariaDB, there are no problems.
But on MySQL version 5.7.16-0ubuntu0.16.04.1 I get this error
Binary geometry function st_within given two geometries of different srids: 0 and 1, which should have been identical.
I do not understand the error, is there a comparable query I can use on this version of MySQL?
Answer found at this link
https://bugs.mysql.com/bug.php?id=79282
Compatible query that works on both MySQL and MariaDB:
SELECT region_id FROM shape_region WHERE ST_Contains( SHAPE, ST_GeomFromText( 'POINT(-122.392128 37.795653)', 1 ) )
As explained in the link
You can't compare a shape in one spatial reference system (SRID 1) with a point in another spatial reference system (SRID 0). The POINT() function[1] will always return a point in SRID 0, which is the unitless, Cartesian default spatial reference system.
In order to do the intended comparison, the point has to be in the same spatial reference system as the shape. E.g., use the SRID parameter of ST_GeomFromText()[2]:
Related
I am no MySQL expert so no doubt I have overlooked something simple.
I have a MySQL DB (V 5.7.26) and if I run the following query (as an example) on one of my tables I get a result of 39 which is what I expect:
SELECT count(*)
FROM `entities`
WHERE
ST_CONTAINS(
ST_GeomFromText('POLYGON((-0.4120 51.6009, -0.4120 51.3467, 0.1533 51.3467, 0.1533 51.6009, -0.4120 51.6009))', 4326),
gloc
) = 1;
I created the same DB in MySQL V 8.0.23 and did so by exporting table definitions and recreating them by running the queries in the new DB, so as far as I can tell tables definitions etc are identical.
I imported the same data into the new DB and I know the data is correct, for example if I pull a record by another criteria(selecting by record id for example) I can display the record on a map (leaflet) and it is in the correct location.
However if I run the same query as above in my new DB it returns a result of 0.
If I leave out the SRID of 4326 then I get an error (which I would expect) of
/* SQL Error (3033): Binary geometry function st_contains given two geometries of different srids: 0 and 4326, which should have been identical. */
/* Affected rows: 0 Found rows: 0 Warnings: 0 Duration for 0 of 1 query: 0.000 sec. */
So I don't think its an SRID issue.
gloc is defined as type 'geometry', NULL values are allowed though none exist. However if I disallow NULL values and then add a spatial index to gloc column the result is exactly the same.
I would check lat:lon order. MySql 8 follows lat:lon order for 4326 as defined by CRS. Assuming your data is in England, rather than Indian ocean, you should swap coordinate order. Also check other data for same issue.
See also
https://mysqlserverteam.com/axis-order-in-spatial-reference-systems/
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.
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))");
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.
Is it possible to change the srid of a column of geometry type? I just want to create a view of geometry type data from the raw latlon data and use it in the geoserver. However after using the pointfromtext function, the type of data I generate is geometry rather than point and geoserver would treat it as an feature typ of byte array which can not be used in the geoserver. However if I use the 'point' function directly in the mysql, I can get the exact type of point however the srid is not right.
So my question is can I set the srid for the geometry type of data?
This is one way of doing it in MySQL:
UPDATE table SET shape = ST_GeomFromText(ST_AsText(shape), SRID);
Where SRID should be the new SRID code (for example 4326 for WGS84). Keep in mind that this only changed the SRID code and not the actual coordinates stored in the shape.
Actually to do what you want in SQL Server 2008, I had to do the following (change all the data in EPGS:4326):
update TestGeom set geom = geometry::STGeomFromText(geom.STAsText(), 4326)
I don't know if in MySQL you can do the same kind of thing. Otherwise, you can rebuild your table with something similar to this:
update TestGeom
set geom = geometry::STGeomFromText('POINT ('+ REPLACE(CONVERT(nvarchar, TestGeom.Lon), ',','.')+' '+REPLACE(CONVERT(nvarchar, TestGeom.Lat), ',','.')+' )', 4326)
I hope it can help you.
I was able to do this in MySQL 5.7 using the following technique:
update location_polygons
set multipoly = ST_GeomFromGeoJSON(ST_AsGeoJSON(multipoly), 2, 0)
where SRID(multipoly) <> 0
Based on this documentation URL: https://dev.mysql.com/doc/refman/5.7/en/spatial-geojson-functions.html