Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Storing Latitude and Longitude values in MySQL database

I want to store the values of latitude and longitude fetched from Google Maps GeoCoding API in a MySQL database. The values are in float format.

12.9274529

77.5905970

And when I want to store it in database (which is datatype float) it rounds up float and store it in following format:

12.9275

77.5906

Am I using the wrong datatype? If yes then what datatype should I be using to store latitude and longitude values?

Update :

here is the CREATE TABLE as requestted by Allin

CREATE TABLE `properties` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `description` text,
  `latitude` float DEFAULT NULL,
  `longitude` float DEFAULT NULL,
  `landmark` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `serial` (`serial`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
like image 726
Ibrahim Azhar Armar Avatar asked May 31 '11 13:05

Ibrahim Azhar Armar


People also ask

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 is the datatype of Latitude and Longitude in SQL?

The SQL Server geography data type stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.

Which of the following is the best data type to store Latitude and Longitude coordinates that are in decimal degrees?

Use DECIMAL(8,6) for latitude (90 to -90 degrees) and DECIMAL(9,6) for longitude (180 to -180 degrees). 6 decimal places is fine for most applications. Both should be "signed" to allow for negative values. DECIMAL type is intended for financial calculations where no floor/ceil is accepted.

How many bytes can you store Latitude and Longitude?

latitude : 4-Byte Floating Point Number Specifies the geographic latitude of the location. longitude : 4-Byte Floating Point Number Specifies the geographic longitude of the location.


2 Answers

You need to use decimal if you don't want the numbers to be approximated.

Fixed-Point (Exact-Value) Types

The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.

And now the "here you go" answer:

Use DECIMAL(10,7). Where 10 is the total number of digits in the number and 7 is the number of digits after the .. (This means that before the dot will be 3 digits.)

Adjust these numbers as needed. Also please take a look at the manual entry I linked earlier in the answer.

like image 72
Alin Purcaru Avatar answered Oct 06 '22 12:10

Alin Purcaru


MySQL has special types for GIS applications.

Use the point type and see:

http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html

For a general discussion see: http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html

Some guys made a special UDF for computing distances between points on a sphere (i.e. earth)
See: http://www.lenzg.net/archives/220-New-UDF-for-MySQL-5.1-provides-GIS-functions-distance_sphere-and-distance_spheroid.html

Here's a howto: http://howto-use-mysql-spatial-ext.blogspot.com/2007/11/using-circular-area-selection.html

like image 34
Johan Avatar answered Oct 06 '22 11:10

Johan