纬度和经度的正确数据类型?(在activerecord)

时间:2022-06-19 00:27:28

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

我应该将纬度和经度存储为字符串还是浮点数(或其他东西)?

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

(如果重要的话,我正在使用activerecord / ruby on rails)。

6 个解决方案

#1


17  

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.

如果您需要进行更复杂的地理计算,您可以为MySQL的Postgresql或MySQL空间扩展调查PostGIS。否则,浮点数(双精度可能是个好主意)应该可以工作。

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

编辑:看起来GeoRuby库包含一个Rails扩展,用于处理上述两个数据库的空间/GIS扩展。

#2


121  

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}

#3


17  

If you're not using a spatially-enabled database, Google Maps recommends using floats of size (10,6). That gives you 6 digits after the decimal - if you want more precision, you can adjust accordingly.

如果不使用支持空间的数据库,谷歌映射建议使用大小为10,6的浮点数。小数点后有6位数字——如果你想要更精确,你可以相应地调整。

#4


3  

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.

我建议使用浮点数,尽管它并没有带来多大的差别。如果将来您希望使用浮点数,那么就更容易进行计算。

#5


2  

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.

我想如果这是一个你永远都不想计算的场,你可以用一个字符串。我会避免这种情况。

#6


-1  

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)

#1


17  

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.

如果您需要进行更复杂的地理计算,您可以为MySQL的Postgresql或MySQL空间扩展调查PostGIS。否则,浮点数(双精度可能是个好主意)应该可以工作。

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

编辑:看起来GeoRuby库包含一个Rails扩展,用于处理上述两个数据库的空间/GIS扩展。

#2


121  

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}

#3


17  

If you're not using a spatially-enabled database, Google Maps recommends using floats of size (10,6). That gives you 6 digits after the decimal - if you want more precision, you can adjust accordingly.

如果不使用支持空间的数据库,谷歌映射建议使用大小为10,6的浮点数。小数点后有6位数字——如果你想要更精确,你可以相应地调整。

#4


3  

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.

我建议使用浮点数,尽管它并没有带来多大的差别。如果将来您希望使用浮点数,那么就更容易进行计算。

#5


2  

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.

我想如果这是一个你永远都不想计算的场,你可以用一个字符串。我会避免这种情况。

#6


-1  

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)