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
Related
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.
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]:
I'm attempting to get my head around geospatial indexes in mysql and I'm having trouble getting simple queries to work and I think it's down to my lack of understanding. I'm basically trying to get a number of locations within a 10 mile radius of a lat/lon I pass in.
The table structure is very simple
Points:
name (varchar)
location (Point)
at present there's only one row
name - "point1" location:POINT(31.5 42.2)
Now I'm trying the following sql that I've lifted from here https://www.percona.com/blog/2013/10/21/using-the-new-mysql-spatial-functions-5-6-for-geo-enabled-applications/
set #lat= 31.5;
set #lon = 42.2;
set #dist = 10;
set #rlon1 = #lon-#dist/abs(cos(radians(#lat))*69);
set #rlon2 = #lon+#dist/abs(cos(radians(#lat))*69);
set #rlat1 = #lat-(#dist/69);
set #rlat2 = #lat+(#dist/69);
select astext(location), name from Points
where st_contains(location, envelope(linestring(point(#rlon1, #rlat1), point(#rlon2, #rlat2))))
order by st_distance(point(#lon, #lat), location) limit 10;
but it returns an empty result set. Anyone got any ideas or pointers?
thanks
I managed to get this working
I was assuming a 'Point' object was lat,lon but it's not as far as I can tell it's lon,lat. So in theory the problem was I had stored my initial 'Point' coordinate values the wrong way round in the table even though I thought the standard way was lat/lon
In the SQL Server 2012 there is methods to validate geography '.IsValidDetailed()' and to change orientation '.ReorientObject (geography)'.
I am working with SQL server 2008 and facing problems of polygon orientation.
Question is -
is there any solution to validate geography polygon or change the orientation to valid geography?
is it possible to copy that IsValidDetailed method from sqlserver 2012 to 2008?
if I have to do it manually, then what is the correct orientation of polygon for valid geography datatype?
We can validate geometry datatype in SQL server 2008, is it ok to convert geography polygon in to geometry polygon and validated it?
Please assist. Thanks in advance
This is working for me on SQL Server 2008. After loading the shape as a geometry, use MakeValid() to correct it, then reload into a geography.
declare #gt nvarchar(max)
declare #gm geometry
declare #gmvalid geometry
set #gmvalid = #gm.MakeValid()
set #gt = #gmvalid.STAsText()
--select #gt
if LEFT(#gt,7 ) = 'POLYGON'
begin
set #gg = geography::STPolyFromText(#gt, 4326)
end
else
begin
set #gg = geography::STMPolyFromText(#gt, 4326)
end
I found solution, SQL Server Saptial Tools
http://sqlspatialtools.codeplex.com/
Followings are the methods solved my problem.
IsValidGeographyFromText(string inputWKT, int srid)
Check if an input WKT can represent a valid geography. This function requires that
the WTK coordinate values are longitude/latitude values, in that order and that a valid
geography SRID value is supplied. This function will not throw an exception even in
edge conditions (i.e. longitude/latitude coordinates are reversed to latitude/longitude).
SqlGeography MakeValidGeographyFromText(string inputWKT, int srid)
Convert an input WKT to a valid geography instance.
This function requires that the WKT coordinate values are longitude/latitude values,
in that order and that a valid geography SRID value is supplied.
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))");