Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ideal data type to use when storing latitude / longitude in a MySQL database?

Bearing in mind that I'll be performing calculations on lat / long pairs, what datatype is best suited for use with a MySQL database?

like image 961
Codebeef Avatar asked Oct 01 '08 19:10

Codebeef


People also ask

What is the datatype for latitude and longitude in mysql?

precision you should use DECIMAL . Latitudes range from -90 to +90 (degrees), so DECIMAL(10,8) is ok for that, but longitudes range from -180 to +180 (degrees) so you need DECIMAL(11,8) . The first number is the total number of digits stored, and the second is the number after the decimal point.

How do you store latitude and longitude in a database?

Storing Latitude & Longitude data as Floats or Decimal This is one of the most fundamental ways of storing geocoordinate data. Latitude & longitude values can be represented & stored in a SQL database using decimal points (Decimal degrees) rather than degrees (or Degrees Minutes Seconds).

What type of data is latitude and longitude?

Location data are information about the geographic positions of devices (such as smartphones or tablets) or structures (such as buildings, attractions). The geographic positions of location data are called coordinates, and they are commonly expressed in Latitude and Longitude format.

What is the datatype for longitude in SQL Server?

Longitude has the range [-180;180] so it should be stored in DECIMAL(9,6) .


2 Answers

Use MySQL's spatial extensions with GIS.

like image 90
Kirk Strauser Avatar answered Oct 13 '22 23:10

Kirk Strauser


Basically it depends on the precision you need for your locations. Using DOUBLE you'll have a 3.5nm precision. DECIMAL(8,6)/(9,6) goes down to 16cm. FLOAT is 1.7m...

This very interesting table has a more complete list: http://mysql.rjweb.org/doc.php/latlng :

Datatype               Bytes            Resolution  Deg*100 (SMALLINT)     4      1570 m    1.0 mi  Cities DECIMAL(4,2)/(5,2)     5      1570 m    1.0 mi  Cities SMALLINT scaled        4       682 m    0.4 mi  Cities Deg*10000 (MEDIUMINT)  6        16 m     52 ft  Houses/Businesses DECIMAL(6,4)/(7,4)     7        16 m     52 ft  Houses/Businesses MEDIUMINT scaled       6       2.7 m    8.8 ft FLOAT                  8       1.7 m    5.6 ft DECIMAL(8,6)/(9,6)     9        16cm    1/2 ft  Friends in a mall Deg*10000000 (INT)     8        16mm    5/8 in  Marbles DOUBLE                16       3.5nm     ...    Fleas on a dog 

Hope this helps.

like image 29
Simon Avatar answered Oct 13 '22 23:10

Simon