Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
GEO
ENABLED
APPLICATIONS
WITH
MYSQL
5.6
Also
covered
new
MySQL
5.7
features
Alexander
Rubin
Principal
Consultant,
Percona
About
Me
hPp://www.mysqlperformanceblog.com/
author/alexanderrubin/
Agenda
US
Zip
codes/boundaries
www.census.gov
Point
of
Interests,
Roads,
Etc
www.openstreetmap.org
Free
GEO
Data
Sources
US
Zip
codes/boundaries
`p://`p2.census.gov/geo/Kger/TIGER2013/
ZCTA5/tl_2013_us_zcta510.zip
Point
of
Interests,
Roads,
Etc
hPp://download.geofabrik.de/north-america-
latest.osm.pbf
Free
GEO
Data
Sources
Formats
How
to
convert?
ConverFng
to
MySQL
OSM.PBF conversion
trac.osgeo.org/gdal/wiki/
DownloadingGdalBinaries
elgis.argeo.org
launchpad.net/~ubuntugis
GDAL
on
Linux
>
1.10
Current
RPMs
=
1.9
Ubuntu
has
unstable
release
for
v1.10
or
compile
from
source
hPp://www.gdal.org/ogr/drv_osm.html
ConverFng
to
MySQL
On
Ubuntu
$ apt-add-repository ppa:ubuntugis/ubuntugis-unstable
$ apt-get update && apt-get install gdal-bin
$ ogr2ogr --version
GDAL 1.10.1, released 2013/08/26
$ ogrinfo --formats|grep OSM
-> "OSM" (readonly)
hPp://www.mysqlperformanceblog.com/
2014/03/24/creaKng-geo-enabled-
applicaKons-mysql-5-6/
Geo-enabled
applicaFons
MyISAM
Supports
both
SPATIAL
types
and
indexes
InnoDB
Supports
SPATIAL
types
only,
not
indexes
added
SPATIAL
index
support
in
labs
version
of
MySQL
5.7
hPp://labs.mysql.com
MySQL
with
SpaFal
Extensions
`SHAPE` geometry
`SHAPE` geometry
3.2
3+
5.0
5.6
5.7
Distance
calculaKon
MySQL
4.1
5.5
Distance
on
the
Earth
Task:
Find
10
nearby
hotels
and
sort
by
distance
What
do
we
have:
1. Given
point
on
Earth:
LaKtude,
Longitude
Hotel Latitude Longitude
2. Hotels
table:
Name
R
=
earths
radius
lat
=
lat2
lat1;
long
=
long2
long1
a
=
sin(lat/2)
+
cos(lat1)
*
cos(lat2)
*
sin(long/2)
c
=
2*atan2(a,
(1a));
d
=
R*c
angles
need
to
be
in
radians
3956
*
2
*
ASIN
(
SQRT
(
POWER(SIN((orig.lat
-
dest.lat)*pi()/180
/
2),
2)
+
COS(orig.lat
*
pi()/180)
*
COS(dest.lat
*
pi()/180)
*
POWER(SIN((orig.lon
-
dest.lon)
*
pi()/180
/
2),
2)
)
)
as
distance
MySQL
Query:
Find
Nearby
Hotels
mysql> Explain
select_type: SIMPLE
table: dest
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1787219
Extra: Using filesort
1 row in set (0.00 sec)
How
to
speed
up
the
query
10
Miles
How
to
calculate
needed
coordinates
1
of
laKtude
~=
69
miles
1
of
longitude
~=
cos(laKtude)*69
SELECT destination.*,
<distance formula> as
distance
FROM users destination, users origin
WHERE origin.id=userid
and destination.longitude
between lon1 and lon2
and destination.latitude
between lat1 and lat2
order by distance limit 10
Geo-enabled
applicaFons
Distance
calculaKon
MySQL
5.6+
SpaFal
Data
types
in
MySQL
5.6
id: 1
select_type: SIMPLE
table: points
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 13660667
Extra: Using where; Using filesort
1 row in set (0.00 sec)
Geo-enabled
applicaFons
MySQL
5.6+
New
funcKons
with
st_
prex
ST_CONTAINS(g1,
g2)
exact
calculaKons
Uses
SPATIAL
index
What
is
the
dierence?
52
hPp://www.mysqlperformanceblog.com/2013/10/21/using-the-new-spaKal-funcKons-in-mysql-5-6-for-geo-enabled-applicaKons/
MySQLs
MBR
false
posiFves
53
QuesKons?
alexander.rubin@percona.com
hPp://www.mysqlperformanceblog.com/author/
alexanderrubin/