python实现高斯投影正反算方式

yipeiwu_com5年前Python基础

使用Python实现了一下我们同事的C++高斯投影正反算,实际跑通,可用。

#!/ 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


  #将经纬度转换为弧度
  latitude2Rad = (math.pi / 180.0) * latitude

  beltNo = int((longitude + 1.5) / 3.0) #计算3度带投影度带号
  L = beltNo * 3 #计算中央经线
  l0 = longitude - L #经差
  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 #按3度带进行投影

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

  e2 = 2 * f - f * f #第一偏心率平方
  e1 = (1.0 - math.sqrt(1 - e2)) / (1.0 + math.sqrt(1 - e2))
  ee = e2 / (1 - e2) #第二偏心率平方

  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

  #计算经纬度(弧度单位的经纬度)
  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)

  #换换为deg
  longitude = longitude1 / iPI
  latitude = latitude1 / iPI

  return latitude, longitude

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

以上这篇python实现高斯投影正反算方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python时间戳使用和相互转换详解

本文实例为大家分享了Python时间戳使用和相互转换的具体代码,供大家参考,具体内容如下 1.将字符串的时间转换为时间戳 方法:    &nbs...

python中subprocess批量执行linux命令

可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 popen --废弃 commands --废弃,3....

python可视化实现代码

python可视化实现代码

python可视化 #导入两个库 import numpy as np import matplotlib.pyplot as plt #第一个参数就是x轴的初始值 #第二个参数是...

Python实现的统计文章单词次数功能示例

本文实例讲述了Python实现的统计文章单词次数功能。分享给大家供大家参考,具体如下: 题目是这样的:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文...

Python 自动刷博客浏览量实例代码

思路来源 今天很偶然的一个机会,听到别人在谈论现在的“刷量”行为,于是就激发了我的好奇心。然后看了下requests模块正好对我有用,就写了一个简单的测试用例。神奇的发现这一招竟然是管用...