Positive and negative calculation of Gaussian projection in Python

Time:2020-9-23

Using Python to achieve our colleagues C + + Gauss projection forward and backward calculation, the actual run through, available.

#!/ usr/bin/python
# -*- coding:utf-8 -*-

import math


def LatLon2XY(latitude, longitude):
  a = 6378137.0
  # b = 6356752.3142
  # c = 6399593.6258
  # alpha = 1 / 298.257223563
  e2 = 0.0066943799013
  # epep = 0.00673949674227


  #Convert latitude and longitude to radian
  latitude2Rad = (math.pi / 180.0) * latitude

  Beltno = int ((longitude + 1.5) / 3.0) ා calculate the projection band number of degree 3
  L = beltno * 3 ා calculate the central meridian
  L0 = longitude - L ා meridian difference
  tsin = math.sin(latitude2Rad)
  tcos = math.cos(latitude2Rad)
  t = math.tan(latitude2Rad)
  m = (math.pi / 180.0) * l0 * tcos
  et2 = e2 * pow(tcos, 2)
  et3 = e2 * pow(tsin, 2)
  X = 111132.9558 * latitude - 16038.6496 * math.sin(2 * latitude2Rad) + 16.8607 * math.sin(
    4 * latitude2Rad) - 0.0220 * math.sin(6 * latitude2Rad)
  N = a / math.sqrt(1 - et3)

  x = X + N * t * (0.5 * pow(m, 2) + (5.0 - pow(t, 2) + 9.0 * et2 + 4 * pow(et2, 2)) * pow(m, 4) / 24.0 + (
  61.0 - 58.0 * pow(t, 2) + pow(t, 4)) * pow(m, 6) / 720.0)
  y = 500000 + N * (m + (1.0 - pow(t, 2) + et2) * pow(m, 3) / 6.0 + (
  5.0 - 18.0 * pow(t, 2) + pow(t, 4) + 14.0 * et2 - 58.0 * et2 * pow(t, 2)) * pow(m, 5) / 120.0)

  return x, y


def XY2LatLon(X, Y, L0):

  iPI = 0.0174532925199433
  a = 6378137.0
  f= 0.00335281006247
  Zonewide = 3 ා projected as a 3-degree band

  ProjNo = int(X / 1000000)
  L0 = L0 * iPI
  X0 = ProjNo * 1000000 + 500000
  Y0 = 0
  xval = X - X0
  yval = Y - Y0

  E2 = 2 * F - f * f # the square of the first eccentricity
  e1 = (1.0 - math.sqrt(1 - e2)) / (1.0 + math.sqrt(1 - e2))
  EE = E2 / (1 - E2) ා square of second eccentricity

  M = yval
  u = M / (a * (1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256))

  fai = u \
     + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * math.sin(2 * u) \
     + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * math.sin(4 * u) \
     + (151 * e1 * e1 * e1 / 96) * math.sin(6 * u)\
     + (1097 * e1 * e1 * e1 * e1 / 512) * math.sin(8 * u)
  C = ee * math.cos(fai) * math.cos(fai)
  T = math.tan(fai) * math.tan(fai)
  NN = a / math.sqrt(1.0 - e2 * math.sin(fai) * math.sin(fai))
  R = a * (1 - e2) / math.sqrt(
    (1 - e2 * math.sin(fai) * math.sin(fai)) * (1 - e2 * math.sin(fai) * math.sin(fai)) * (1 - e2 * math.sin(fai) * math.sin(fai)))
  D = xval / NN

  #Calculate longitude and latitude (longitude and latitude in radian units)
  longitude1 = L0 + (D - (1 + 2 * T + C) * D * D * D / 6 + (
  5 - 2 * C + 28 * T - 3 * C * C + 8 * ee + 24 * T * T) * D * D * D * D * D / 120) / math.cos(fai)
  latitude1 = fai - (NN * math.tan(fai) / R) * (
  D * D / 2 - (5 + 3 * T + 10 * C - 4 * C * C - 9 * ee) * D * D * D * D / 24 + (
  61 + 90 * T + 298 * C + 45 * T * T - 256 * ee - 3 * C * C) * D * D * D * D * D * D / 720)

  #Change to DEG
  longitude = longitude1 / iPI
  latitude = latitude1 / iPI

  return latitude, longitude

# 
# print LatLon2XY(40.07837722329, 116.23514827596)
# print XY2LatLon(434760.7611718801, 4438512.040474475, 117.0)

The way of forward and inverse calculation of Gaussian projection in Python is the whole content shared by Xiaobian. I hope to give you a reference and support developeppaer more.