Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct datatype for latitude and longitude? (in activerecord)

Should I store latitude and longitude as strings or floats (or something else)?

(I'm using activerecord / ruby on rails, if that matters).

Update:

Mysql in development and postgresql in production (why does it matter?)

like image 864
Tom Lehman Avatar asked Jul 28 '09 19:07

Tom Lehman


People also ask

Which datatype is used for latitude and longitude?

p. 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) .

What data type is coordinates?

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

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).

Is latitude an integer?

The latitude and longitude are displayed in decimal notation. The number of decimal places must be specified and between 1 and 6 inclusive. -dd dd'dd" - Degrees, minutes, and seconds. The latitude and longitude are displayed as integers in degrees, followed by minutes and seconds.


2 Answers

This is what I use:

add_column :table_name, :lat, :decimal, {:precision=>10, :scale=>6} add_column :table_name, :lng, :decimal, {:precision=>10, :scale=>6} 
like image 100
Gokul Avatar answered Oct 11 '22 19:10

Gokul


If you need to do more complex geographical calculations, you can investigate PostGIS for Postgresql or MySQL Spatial Extensions for MySQL. Otherwise, a float (double precision might be a good idea) should work.

Edit: It looks like the GeoRuby library includes a Rails extension for working with the spatial/GIS extensions for both of the aforementioned databases.

like image 37
Greg Campbell Avatar answered Oct 11 '22 18:10

Greg Campbell