The correct data type for latitude and longitude? (in activerecord)

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

(I use activerecord / ruby ​​on rails, if that matters).

+70
types ruby-on-rails activerecord
Jul 28 '09 at 19:16
source share
6 answers

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

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

+20
Jul 28 '09 at 19:32
source share

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} 
+129
Aug 03 2018-11-11T00:
source share

If you don’t use a database with spatial support, Google Maps recommends using floats of size (10.6). This gives you 6 digits after the decimal number - if you want more precision, you can adjust accordingly.

+17
Jul 29 '09 at 22:03
source share

I would suggest using a float, although in reality this is not so much different. Floats are easier to do if you ever want it in the future.

+3
Jul 28 '09 at 19:27
source share

Typically, you want the Lat / Long to be stored in the largest float that you have. At some latitudes (for example, near the equator), very small changes in longitude can be equated to large differences in terms of surface distance.

I suppose if this is a field that you never want to do math, you can use a string. I would not have avoided this.

+2
Jul 28 '09 at 20:10
source share

Since they are fixed precision, you must convert and save as an integer to significantly improve performance.

(SEE http://www.postgresql.org/docs/9.1/static/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL )

-one
May 16 '16 at 22:28
source share



All Articles