Ruby-on-rails 纬度和经度的正确数据类型?(在活动记录中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1196174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct datatype for latitude and longitude? (in activerecord)
提问by Tom Lehman
Should I store latitude and longitude as strings or floats (or something else)?
我应该将纬度和经度存储为字符串还是浮点数(或其他东西)?
(I'm using activerecord / ruby on rails, if that matters).
(如果重要的话,我在 rails 上使用 activerecord / ruby)。
Update:
更新:
Mysql in development and postgresql in production (why does it matter?)
开发中的 Mysql 和生产中的 postgresql(为什么重要?)
采纳答案by Greg Campbell
If you need to do more complex geographical calculations, you can investigate PostGISfor Postgresql or MySQL Spatial Extensionsfor MySQL. Otherwise, a float (double precision might be a good idea) should work.
如果您需要进行更复杂的地理计算,您可以研究PostGISfor Postgresql 或MySQL Spatial Extensionsfor MySQL。否则,浮点数(双精度可能是个好主意)应该可以工作。
Edit:It looks like the GeoRubylibrary includes a Rails extension for working with the spatial/GIS extensions for both of the aforementioned databases.
编辑:看起来GeoRuby库包含一个 Rails 扩展,用于处理上述两个数据库的空间/GIS 扩展。
回答by Gokul
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}
回答by Chris B
If you're not using a spatially-enabled database, Google Maps recommendsusing floats of size (10,6). That gives you 6 digits after the decimal - if you want more precision, you can adjust accordingly.
如果您没有使用支持空间的数据库,Google Maps建议使用大小为 (10,6) 的浮点数。这为您提供了小数点后的 6 位数字 - 如果您想要更高的精度,可以相应地进行调整。
回答by Mike Trpcic
I would suggest using floats, although it doesn't really make that much of a difference. Floats are easier to do calculations on if you ever desire that in the future.
我建议使用浮动,尽管它并没有真正产生太大的不同。如果您将来需要,浮动更容易进行计算。
回答by T.E.D.
Generally you want Lat/Long stored in the largest float type you have. At some latitudes (eg: near the equator) very small changes in longitude can equate to large differences in terms of surface distance.
通常,您希望 Lat/Long 存储在您拥有的最大浮点类型中。在某些纬度(例如:赤道附近),经度的非常小的变化可能等同于表面距离的巨大差异。
I suppose if it is a field which you won't ever want to do any math on, you could use a string. I'd avoid that though.
我想如果它是一个你永远不想做任何数学运算的领域,你可以使用一个字符串。不过我会避免这种情况。
回答by Yarin
Since they're fixed precision, you should convert and store as integer for a significant performance improvement.
由于它们是固定精度,您应该转换并存储为整数以显着提高性能。
(SEE http://www.postgresql.org/docs/9.1/static/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL)
(见http://www.postgresql.org/docs/9.1/static/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL)

