Geographic Features in MySQL
Tibor Korocz Percona
Geographic Features in MySQL Tibor Korocz Percona What do I try - - PowerPoint PPT Presentation
Geographic Features in MySQL Tibor Korocz Percona What do I try to answer today? - Can MySQL 8 help us with the common usecases? - Distance Calculation. - What is near by me? - Does MySQL 8 give us better options/solutions for these
Tibor Korocz Percona
2
3
4
5
6
OpenStreetMap, Bing, ArcGIS, ESRI
INDEX(g));
Click to add text
8
9
10
11
12
ST_GeomFromText(wkt[, srid]) - Constructs a geometry value of any type using its WKT representation and SRID.If the geometry argument is NULL or not a syntactically well-formed geometry, or if the SRID argument is NULL, the return value is NULL. WKT - The Well-Known Text (WKT) representation of geometry values is designed for exchanging geometry data in ASCII form. Example: POINT(10 15) Be careful, this is not the same like POINT(10,5). This is geometry type in MySQL.
13
create DEFINER = CURRENT_USER function haversine_distance_sp (lat1 double, lon1 double, lat2 double, lon2 double) returns double begin …. set phi1 = radians(lat1); set phi2 = radians(lat2); set d_phi = radians(lat2-lat1); set d_lambda = radians(lon2-lon1); set a = sin(d_phi/2) * sin(d_phi/2) + cos(phi1) * cos(phi2) * sin(d_lambda/2) * sin(d_lambda/2); set c = 2 * atan2(sqrt(a), sqrt(1-a)); set d = R * c; return d; End; Full function can be found here.
14
15
16
17
18
19
20
CREATE TABLE `test_distance` ( `id` int(10) NOT NULL AUTO_INCREMENT, `city` varchar(50) DEFAULT NULL, `latitude` double(15,5) DEFAULT NULL, `longitude` double(15,5) DEFAULT NULL, `pt` point NOT NULL /*!80003 SRID 4326 */, PRIMARY KEY (`id`), SPATIAL KEY `idx_spatial_pt` (`pt`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
21
insert into test_distance (city,latitude,longitude,pt) values ('london','51.509865','-0.118092',point(51.509865,-0.118092)); ERROR 3643 (HY000): The SRID of the geometry does not match the SRID of the column 'pt'. The SRID of the geometry is 0, but the SRID of the column is 4326. Consider changing the SRID
We have to define the correct SRID. insert into test_distance (city,latitude,longitude,pt) values ('london','51.509865','-0.118092',ST_GeomFromText('point(51.509865 -0.118092)',4326)); Query OK, 1 row affected (0.09 sec)
22
23
24
What I could found in MySQL manual: “A tree data structure used for spatial indexing of multi-dimensional data such as geographical coordinates, rectangles or polygons.” What IBM’s manual says: “The R-tree access method organizes data in a tree-shaped structure called an R-tree
contains the bounded object or objects. Bounding boxes can enclose data objects or other bounding boxes.”
25
26
calculation.
Click to add text
28
29
CREATE TABLE `us` ( `id` int(10) NOT NULL AUTO_INCREMENT, `zipcode` char(5) DEFAULT NULL, `city` varchar(50) DEFAULT NULL, …. `latitude` double(15,5) DEFAULT NULL, `longitude` double(15,5) DEFAULT NULL, `some_field` tinyint(4) DEFAULT NULL, `pt` point NOT NULL /*!80003 SRID 4326 */, `pt2` point NOT NULL, PRIMARY KEY (`id`), SPATIAL KEY `idx_spatial_pt2` (`pt2`), SPATIAL KEY `idx_spatial_pt` (`pt`) ) ENGINE=InnoDB AUTO_INCREMENT=40976 DEFAULT CHARSET=latin1
30
But we have to be careful with SRIDS.
31
32
33
34
set @radius = 2; set @a= (select -74.0059413); set @b= (select 40.7127837); set @a1= (select (@a+(@radius/111))); set @a2= (select (@a-(@radius/111))); set @b1= (select (@b+(@radius/111))); set @b2= (select (@b-(@radius/111))); SELECT id,city,zipcode,latitude,longitude,ST_Distance_Sphere(ST_SRID(Point(@a, @b),4326), pt) AS distance_in_meters FROM us WHERE ST_Contains(ST_SRID(ST_MakeEnvelope( (point(@a1, @b1)), (point(@a2, @b2))),4326), pt);
35
36
37
38
39
40
42
Their customer can draw any kind of shape
which car crossed that shape.
44
45
46
SET @shape = ST_GEOMFROMTEXT( 'POLYGON( (-74.0812570352 40.6940266249, -73.8395578164 40.6940266249, -73.8395578164 40.8386006946, -74.0812570352 40.8386006946, 74.0912570352 40.9386006946 …..
)' ); SELECT id,car FROM table_car_gps WHERE ST_CONTAINS(ST_SRID(@shape,4326), gps_points);
47
ST_ExteriorRing(poly) Returns the exterior ring of the Polygon value poly as a LineString. ST_InteriorRingN(poly, N) Returns the N-th interior ring for the Polygon value poly as a LineString. Rings are numbered beginning with 1.
51
52