ziggy
ziggy

Reputation: 1538

Python adding decimals to longitude values

I have thousands of NAD83 latitude longitude values where the longitude values start with a negative like -747559718

this particular coordinate the decimal needs to be at after 4 to be properly used in the mapping application I am using. it should be -74.7559718

There are times where the longitude value will be -123092886 where the decimal should be after the 3. it should be -123.092886

I am reading these coordinates through a JSON file, for some reason they come with no decimals. I am looking for a pure python solution to insert the decimal at the correct place.

The numbers will be 9 or 10 digits long

I know i can do something like once i have my longitude value in a variable

 s = longitude
 s[:3] + '.' + s[3:]

Upvotes: 3

Views: 957

Answers (1)

ziggy
ziggy

Reputation: 1538

I have figured out my problem: all my lat long coordinates are in the contemporaneous US and I know, from being a geospatial analyst that any longitude value in the NAD83 projection that is in the contemporaneous US and starts with the 1 digit, it will have a decimal place after the 3rd digit. and the rest of the longitude values in the US either start with a 7,8 or 9 and those values always have a decimal after the second digit. I am unsure if this solution will work on the rest of the world but it gets it done here

here is my solution with z being the variable containing the longitude value:

if str(z)[1:2] == '1':
    longitude = str(z)[0:4]+'.'+str(z)[4::]
else:
    longitude = str(z)[0:3]+'.'+str(z)[3::]

UPDATE

better answer:

longitude = z * 0.0000001

this also works for the latitude values

Upvotes: 1

Related Questions