Skip to content

Instantly share code, notes, and snippets.

@KMarkert
Last active February 21, 2017 18:45
Show Gist options
  • Save KMarkert/e217bf026221fa032bb2408e89832c54 to your computer and use it in GitHub Desktop.
Save KMarkert/e217bf026221fa032bb2408e89832c54 to your computer and use it in GitHub Desktop.
This Gist is focused on converting decimal degrees length to meters (or vise versa) for a specific point on the globe. It is based on an approximation and only should be used as a general measurement (errors around 10 m).
import numpy as np
def dd2meters(inPt,scale=0.1):
"""Function to convert decimal degrees to meters based on the approximation
given by: https://en.wikipedia.org/wiki/Geographic_coordinate_system
Args:
inPt (list or array): A Y,X point provided in geographic coordinates
in that order.
Keywords:
scale (int): Resolution of the raster value to covert into meters,
must be in decimal degrees.
Returns:
list: List of Y,X resolution values converted from meters to decimal degrees
"""
lat = inPt[0] #get latitude value
radLat = np.deg2rad(lat) #convert degree latitude to radians
a = 6378137 #radius of Earth in meters
ba = 0.99664719 #constant of b/a
ss = np.arctan(ba*np.tan(radLat)) #calculate the reduced latitude
#factor to convert meters to decimal degrees for X axis
xfct = (np.pi/180)*a*np.cos(ss)
#factor to convert meters to decimal degrees for Y axis
yfct = (111132.92-559.82*np.cos(2*radLat)+1.175*np.cos(4*radLat)-
0.0023*np.cos(6*radLat))
#get meter resolution
y_meters = scale * yfct
x_meters = scale * xfct
#return list of converted resolution values
return [y_meters,x_meters]
def main():
inPt = [75,0]
outScale = dd2meters(inPt)
print outScale
return
if __name__ == "__main__":
main()
import numpy as np
def meters2dd(inPt,scale=30):
"""Function to convert meters to decimal degrees based on the approximation
given by: https://en.wikipedia.org/wiki/Geographic_coordinate_system
Args:
inPt (list or array): A Y,X point provided in geographic coordinates
in that order.
Keywords:
scale (int): Resolution of the raster value to covert into decimal
degrees, must be in meters.
Returns:
list: List of Y,X resolution values converted from meters to decimal degrees
"""
lat = inPt[0] #get latitude value
radLat = np.deg2rad(lat) #convert degree latitude to radians
a = 6378137 #radius of Earth in meters
ba = 0.99664719 #constant of b/a
ss = np.arctan(ba*np.tan(radLat)) #calculate the reduced latitude
#factor to convert meters to decimal degrees for X axis
xfct = (np.pi/180)*a*np.cos(ss)
#factor to convert meters to decimal degrees for Y axis
yfct = (111132.92-559.82*np.cos(2*radLat)+1.175*np.cos(4*radLat)-
0.0023*np.cos(6*radLat))
#get decimal degree resolution
ydd = scale / yfct
xdd = scale / xfct
#return list of converted resolution values
return [ydd,xdd]
def main():
inPt = [75,0]
outScale = meters2dd(inPt)
print outScale
return
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment