28

Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude
Page 2: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Spatial Data in MySQL 8.0

Norvald H. RyengSoftware Engineer

Page 3: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

3Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle's products remains at the sole discretion of Oracle.

Page 4: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

4Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

AgendaGIS basics

Demo

Preparing for the upgrade to 8.0

1

2

3

4

5

6

7

Page 5: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

Copyright © 2014 Oracle and/or its affiliates. All rights reserved.

GIS Basics

Page 6: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

6Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

“GIS is a form of digital mapping technology. Kind of like Google Earth, but better.”— Arnold Schwarzenegger, Governor of California

Page 7: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

8Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Data Types● Geometry

– Point

– Linestring

– Polygon

– Geometry collection● Multipoint

● Multilinestring

● Multipolygon

Non-instantiable, but can be used as column type.

Page 8: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

12Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Spatial Reference Systems

SRID 0 Projected SRS Geographic SRS

Cartesian SRS

5.7 8.0

Page 9: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

13Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Spatial Reference Systems● Each SRS has a unique spatial reference system ID (SRID)

– Numeric identifier

– No formal standard/catalog of SRIDs

– De facto standard: EPSG Dataset● 4326 = WGS 84 (“GPS coordinates”)

● 3857 = WGS 84 / World Mercator (“Web Mercator”)

● A property of each geometry value

● Mixing geometries in different SRIDs in one computation doesn't make sense and will raise an error (also in 5.7)

Page 10: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

14Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Data TypesCREATE TABLE t ( g GEOMETRY, pt POINT NOT NULL, ls LINESTRING SRID 0, py POLYGON SRID 4326 NOT NULL);

INSERT INTO t (pt, py) VALUES ( POINT(0, 0), ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 0))', 4326));

Page 11: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

15Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Import Functions● Linestring(Point(0, 0), Point(1, 0), Point(2, 2))

● ST_GeomFromText('POINT(0 0)', 4326)

● ST_PolyFromText('POLYGON((0 0, 10 0, 10 10, 0 0))', 4326, 'axis-order=long-lat')

● ST_PointFromGeohash('gc7x3q7t60', 4326)

● ST_GeomFromGeoJSON('{"type":"Point","coordinates":[102.0, 0.0]}')

Page 12: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

16Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Export Functions● ST_AsText(@geometry)

● ST_AsBinary(@geometry, 'axis-order=lat-long')

● ST_AsGeoJSON(@geometry)

● ST_Geohash(@point, 10)

Page 13: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

17Copyright © 2014 Oracle and/or its affiliates. All rights reserved.

Functions● Import

– ST_GeomCollFromTxt/ST_GeomCollFromText, ST_GeomCollFromWKB, ST_GeomFromGeoJSON, ST_GeomFromText, ST_GeomFromWKB, ST_LineFromText, ST_LineFromWKB, ST_MLineFromText, ST_MLineFromWKB, ST_MPointFromText, ST_MPointFromWKB, ST_MPolyFromText, ST_MPolyFromWKB, ST_PointFromGeohash, ST_PolyFromText, ST_PolyFromWKB

● Export

– ST_AsBinary, ST_AsGeoJSON, ST_AsText, ST_Geohash

Page 14: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

18Copyright © 2014 Oracle and/or its affiliates. All rights reserved.

Functions● Comparison

– ST_Contains, ST_Crosses, ST_Disjoint, ST_Equals, ST_Intersects, ST_Overlaps, ST_Touches, ST_Within

– MBRContains, MBRCoveredBy, MBRCovers, MBRDisjoint, MBREquals, MBRIntersects, MBROverlaps, MBRTouches, MBRWithin

● Produce new geometries

– ST_Buffer, ST_Centroid, ST_ConvexHull, ST_Envelope, ST_MakeEnvelope, ST_Simplify, ST_Difference, ST_Intersection, ST_SymDifference, ST_Union

Page 15: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

19Copyright © 2014 Oracle and/or its affiliates. All rights reserved.

Set Operations

Page 16: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

20Copyright © 2014 Oracle and/or its affiliates. All rights reserved.

Functions● Measures

– ST_Area, ST_Distance, ST_Distance_Sphere, ST_Length

● Extract properties

– ST_Dimension, ST_EndPoint, ST_ExteriorRing, ST_GeometryN, ST_GeometryType, ST_InteriorRingN, ST_IsClosed, ST_IsEmpty, ST_IsSimple, ST_IsValid, ST_PointN, ST_SRID, ST_StartPoint, ST_X, ST_Y

● Helper functions

– ST_LatFromGeohash, ST_LongFromGeohash, ST_Validate, ST_SwapXY

Page 17: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

21Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Spatial Indexes● Column must be NOT NULL to create index

● Column must have SRID restriction, or index is not used

– New requirement in 8.0

– Geographic indexes only available in InnoDB

● Used automatically by the optimizer based on cost model

● Triggered by the use of spatial relation functions

– ST_Within, MBRWithin, ST_Contains, etc.

Page 18: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

22Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

“Geography is just physics slowed down, with a couple of trees stuck in it.”— Terry Pratchett in The Last Continent

Page 19: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

Copyright © 2014 Oracle and/or its affiliates. All rights reserved.

Demo

Page 20: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

24Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

LAMPFrontend

● OpenLayers 4.3.2

Backend

● Ubuntu 16.04 LTS

● Apache 2.4.18

● PHP 7.0 using mysqli

– Thin wrapper to run queries and return result to web app

● MySQL 8.0

– InnoDB with spatial index

– Produce GeoJSON output

Page 21: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

What can I do now to prepare for what's coming?

Page 22: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

27Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Use the Right SRID● MySQL 5.7 and older assumed everything was Cartesian

– Ignored SRID

● If you know what you're doing, use the correct SRID

– Beware of geographic computations in 8.0

● If unsure, use SRID 0

– No surprises when upgrading to 8.0

– Makes no claim about the SRS except that it's flat

Page 23: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

28Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Axis Order● MySQL 5.7 doesn't know about latitudes and longitudes

– Everything is Cartesian X and Y

– But a few functions expect X=longitude and Y=latitude

● MySQL 8.0 expects import/export to be in the SRS axis order

– Manual override is possible

● MySQL 8.0 uses SRSs from the EPSG Dataset

– All EPSG defined SRSs use latitude-longitude axis ordering

– MySQL 8.0 stores data as X=longitude and Y=latitude

Page 24: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

29Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

One SRID in Each Column● Mixed SRID indexes are not usable

– Will never be considered by the 8.0 optimizer

– You will be allowed to create it (to ensure dump-restore compatibility)

● Mixed SRID in non-indexed columns is OK

– May even be what you want

– Restricts the computations you can do (no mixing of SRIDs)

Page 25: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

30Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

✓✓Prepare Now● Think through your use of SRIDs

– Use SRID 0 if you're unsure

● Use longitude-latitude ordering in 5.7

– But remember that import and export functions follow SRS defined axis order in 8.0

● Use one SRID in each column

– Be ready to rebuild indexes in 8.0

Page 26: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

Copyright © 2014 Oracle and/or its affiliates. All rights reserved.

Feature descriptions and design detailsdirectly from the source.

http://mysqlserverteam.com/

Page 27: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude

33Copyright © 2017 Oracle and/or its affiliates. All rights reserved.

Page 28: Spatial Data in MySQL 8 - Percona · MySQL 5.7 doesn't know about latitudes and longitudes – Everything is Cartesian X and Y – But a few functions expect X=longitude and Y=latitude