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?)
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) .
The SQL Server geography data type stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.
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).
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.
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}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With