Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import latitude/longitude data correctly into MySQL?

I've got a dataset of city names with their corresponding latitudes/longitudes that I've loaded into a MySQL table, like so:

city_id | city_name | latitude DECIMAL(9,6) | longitude DECIMAL(9,6)

Typical latitude/longitude coordinates might look like this: 54.284758 / 32.484736.

However, I'm only getting values with a scale of 2 to appear correctly in my table, in other words, the equivalent of DECIMAL(5,2). The data is uploaded from a text CSV that's been exported from OpenOffice Calc for UTF-8 purposes. I know OpenOffice has some problems with decimals but the full latitude/longitudes are certainly in the exported CSV. If I open the CSV with Notepad, the data is fine.

Can anyone see what I might be doing wrong?

Thanks.

UPDATE: Got it working, thanks for the all the input. I recreated everything from scratch, new schema file (I'm using an ORM), new CSV export, new table, new LOAD DATA INFILE, and it works with the correct decimal output. Beats me.

like image 947
Tom Avatar asked Jan 27 '10 15:01

Tom


People also ask

How do you insert latitude and longitude into a database?

php $lat=$_GET['lat']; $lng=$_GET['lng']; // write your insert query here to save $lat and $lng //get your mysql db connection ready $sql="insert into table_name (latitude, longitude) values('". $lat. "','".

What data type should be used for latitude and longitude?

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

How do you store longitude and latitude?

Longitude and latitude coordinates are stored with 15 decimal digits right of the decimal points.


2 Answers

This may be overkill for your needs, but when working geographic data you may want to consider using MySQL Spatial Extensions.

I'll add that the datatype you're currently using is enough to represent the data as it is in your CSV file. There is either something in your importer that is chopping it off at import time, or whatever you're using to view the data is truncating it.

like image 170
RC. Avatar answered Sep 19 '22 16:09

RC.


I would suggest using a string data type such as varchar for storing Lat, Long. I am working on an app that makes extensive use of these coordinates and I haven't ran into any problems storing it as a string. Storing it as a number you will run into precision issues.

Just something to consider.

like image 22
Lukasz Avatar answered Sep 18 '22 16:09

Lukasz