import math


def get_lat_len_to_metre_factors_at(lat):
  """Return the factors required to convert the length
  of a degree of latitude and longitude at the given
  latitude `lat` to meters from
  [understanding terms in length of degree formula](https://gis.stackexchange.com/questions/75528/understanding-terms-in-length-of-degree-formula/75535#75535)
  """
  m1 = 111132.92
  m2 = -559.82
  m3 = 1.175
  m4 = -0.0023
  p1 = 111412.84
  p2 = -93.5
  p3 = 0.118

  # Calculate the length of a degree of latitude and longitude in meters
  
  latlen = m1 \
    + (m2 * math.cos(2 * lat)) \
    + (m3 * math.cos(4 * lat)) \
    + (m4 * math.cos(6 * lat))
  
  longlen = (p1 * math.cos(lat)) \
    + (p2 * math.cos(3 * lat)) \
    + (p3 * math.cos(5 * lat))

  # calculate correction for 14 cm error
  #print (latlen)
  #print ('deg lat per metre', 1/latlen)
  #print ('deg lat per cm', 0.01/latlen)
  #print ('deg lat for 12 cm', 0.12/latlen)
  #print (47.61227235282722 + 0.14/latlen)
  #exit(1)

  return (latlen,longlen)