Skip to content

Commit

Permalink
- Pievienota Ruby programmēšanas valodas koordinātu pārveidošanas klase
Browse files Browse the repository at this point in the history
- Nelieli labojumi Python klases funkcionalitātē
  • Loading branch information
arvislacis committed Jan 25, 2016
1 parent 58ff04b commit 2c6e724
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Saraksts ar projektā pašlaik pieejamajām programmēšanas valodām - valodas,
| [JavaScript](https://github.com/arvislacis/lks92-wgs84/tree/master/javascript) | [Arvis Lācis](https://github.com/arvislacis) | 22.01.2016. |
| [PHP](https://github.com/arvislacis/lks92-wgs84/tree/master/php) | [Arvis Lācis](https://github.com/arvislacis) | 23.12.2015. |
| [Python2/Python3](https://github.com/arvislacis/lks92-wgs84/tree/master/python) | [Dāvis Mičulis](https://github.com/DavisMiculis) | 24.12.2015. |
| [Ruby](https://github.com/arvislacis/lks92-wgs84/tree/master/ruby) | [Arvis Lācis](https://github.com/arvislacis) | 25.01.2016. |
| [TypeScript](https://github.com/arvislacis/lks92-wgs84/tree/master/typescript) | [Arvis Lācis](https://github.com/arvislacis) | 22.01.2016. |
| [Vala](https://github.com/arvislacis/lks92-wgs84/tree/master/vala) | [Arvis Lācis](https://github.com/arvislacis) | 24.01.2016. |
| [Visual Basic](https://github.com/arvislacis/lks92-wgs84/tree/master/visual%20basic) | [Arvis Lācis](https://github.com/arvislacis) | 21.01.2016. |
Expand Down
54 changes: 27 additions & 27 deletions python/LKS92WGS84.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@

class LKS92WGS84:
# Koordinātu pārveidojumos izmantotās konstantes
__PI = math.pi # Skaitlis pi
__A_AXIS = 6378137 # Elipses modeļa lielā ass (a)
__B_AXIS = 6356752.31414 # Elipses modeļa mazā ass (b)
__CENTRAL_MERIDIAN = math.pi * 24 / 180 # Centrālais meridiāns
__OFFSET_X = 500000 # Koordinātu nobīde horizontālās (x) ass virzienā
__OFFSET_Y = -6000000 # Koordinātu nobīde vertikālās (y) ass virzienā
__SCALE = 0.9996 # Kartes mērogojuma faktors (reizinātājs)
__PI = math.pi # Skaitlis pi
__A_AXIS = 6378137 # Elipses modeļa lielā ass (a)
__B_AXIS = 6356752.31414 # Elipses modeļa mazā ass (b)
__CENTRAL_MERIDIAN = __PI * 24 / 180 # Centrālais meridiāns
__OFFSET_X = 500000 # Koordinātu nobīde horizontālās (x) ass virzienā
__OFFSET_Y = -6000000 # Koordinātu nobīde vertikālās (y) ass virzienā
__SCALE = 0.9996 # Kartes mērogojuma faktors (reizinātājs)

# Aprēķina loka garumu no ekvatora līdz dotā punkta ģeogrāfiskajam platumam
@staticmethod
def __getArcLengthOfMeridian(phi):
n = (LKS92WGS84.__A_AXIS - LKS92WGS84.__B_AXIS) / (LKS92WGS84.__A_AXIS + LKS92WGS84.__B_AXIS)
alpha = ((LKS92WGS84.__A_AXIS + LKS92WGS84.__B_AXIS) / 2) * (1 + (math.pow(n, 2) / 4) + (math.pow(n, 4) / 64))
beta = (-3 * n / 2) + (9 * math.pow(n, 3) / 16) + (-3 * math.pow(n, 5) / 32)
gamma = (15 * math.pow(n, 2) / 16) + (-15 * math.pow(n, 4) / 32)
delta = (-35 * math.pow(n, 3) / 48) + (105 * math.pow(n, 5) / 256)
epsilon = (315 * math.pow(n, 4) / 512)
alpha = ((LKS92WGS84.__A_AXIS + LKS92WGS84.__B_AXIS) / 2) * (1 + (n ** 2 / 4) + (n ** 4 / 64))
beta = (-3 * n / 2) + (9 * n ** 3 / 16) + (-3 * n ** 5 / 32)
gamma = (15 * n ** 2 / 16) + (-15 * n ** 4 / 32)
delta = (-35 * n ** 3 / 48) + (105 * n ** 5 / 256)
epsilon = (315 * n ** 4 / 512)

return alpha * (phi + (beta * math.sin(2 * phi)) + (gamma * math.sin(4 * phi)) + (delta * math.sin(6 * phi)) + (epsilon * math.sin(8 * phi)))

# Aprēķina ģeogrāfisko platumu centrālā meridiāna punktam
@staticmethod
def __getFootpointLatitude(y):
n = (LKS92WGS84.__A_AXIS - LKS92WGS84.__B_AXIS) / (LKS92WGS84.__A_AXIS + LKS92WGS84.__B_AXIS)
alpha = ((LKS92WGS84.__A_AXIS + LKS92WGS84.__B_AXIS) / 2) * (1 + (math.pow(n, 2) / 4) + (math.pow(n, 4) / 64))
alpha = ((LKS92WGS84.__A_AXIS + LKS92WGS84.__B_AXIS) / 2) * (1 + (n ** 2 / 4) + (n ** 4 / 64))
yd = y / alpha
beta = (3 * n / 2) + (-27 * math.pow(n, 3) / 32) + (269 * math.pow(n, 5) / 512)
gamma = (21 * math.pow(n, 2) / 16) + (-55 * math.pow(n, 4) / 32)
delta = (151 * math.pow(n, 3) / 96) + (-417 * math.pow(n, 5) / 128)
epsilon = (1097 * math.pow(n, 4) / 512)
beta = (3 * n / 2) + (-27 * n ** 3 / 32) + (269 * n ** 5 / 512)
gamma = (21 * n ** 2 / 16) + (-55 * n ** 4 / 32)
delta = (151 * n ** 3 / 96) + (-417 * n ** 5 / 128)
epsilon = (1097 * n ** 4 / 512)

return yd + (beta * math.sin(2 * yd)) + (gamma * math.sin(4 * yd)) + (delta * math.sin(6 * yd)) + (epsilon * math.sin(8 * yd))

Expand All @@ -44,9 +44,9 @@ def __getFootpointLatitude(y):
def __convertMapLatLngToXY(phi, lambda1, lambda0):
xy = [0, 0]

ep2 = (math.pow(LKS92WGS84.__A_AXIS, 2) - math.pow(LKS92WGS84.__B_AXIS, 2)) / math.pow(LKS92WGS84.__B_AXIS, 2)
nu2 = ep2 * math.pow(math.cos(phi), 2)
N = math.pow(LKS92WGS84.__A_AXIS, 2) / (LKS92WGS84.__B_AXIS * math.sqrt(1 + nu2))
ep2 = (LKS92WGS84.__A_AXIS ** 2 - LKS92WGS84.__B_AXIS ** 2) / LKS92WGS84.__B_AXIS ** 2
nu2 = ep2 * math.cos(phi) ** 2
N = LKS92WGS84.__A_AXIS ** 2 / (LKS92WGS84.__B_AXIS * math.sqrt(1 + nu2))
t = math.tan(phi)
t2 = t * t

Expand All @@ -59,10 +59,10 @@ def __convertMapLatLngToXY(phi, lambda1, lambda0):
l8coef = 1385 - 3111 * t2 + 543 * (t2 * t2) - (t2 * t2 * t2)

# x koordināta
xy[0] = N * math.cos(phi) * l + (N / 6 * math.pow(math.cos(phi), 3) * l3coef * math.pow(l, 3)) + (N / 120 * math.pow(math.cos(phi), 5) * l5coef * math.pow(l, 5)) + (N / 5040 * math.pow(math.cos(phi), 7) * l7coef * math.pow(l, 7))
xy[0] = N * math.cos(phi) * l + (N / 6 * math.cos(phi) ** 3 * l3coef * l ** 3) + (N / 120 * math.cos(phi) ** 5 * l5coef * l ** 5) + (N / 5040 * math.cos(phi) ** 7 * l7coef * l ** 7)

# y koordināta
xy[1] = LKS92WGS84.__getArcLengthOfMeridian(phi) + (t / 2 * N * math.pow(math.cos(phi), 2) * math.pow(l, 2)) + (t / 24 * N * math.pow(math.cos(phi), 4) * l4coef * math.pow(l, 4)) + (t / 720 * N * math.pow(math.cos(phi), 6) * l6coef * math.pow(l, 6)) + (t / 40320 * N * math.pow(math.cos(phi), 8) * l8coef * math.pow(l, 8))
xy[1] = LKS92WGS84.__getArcLengthOfMeridian(phi) + (t / 2 * N * math.cos(phi) ** 2 * l ** 2) + (t / 24 * N * math.cos(phi) ** 4 * l4coef * l ** 4) + (t / 720 * N * math.cos(phi) ** 6 * l6coef * l ** 6) + (t / 40320 * N * math.cos(phi) ** 8 * l8coef * l ** 8)

return xy

Expand All @@ -72,10 +72,10 @@ def __convertMapXYToLatLon(x, y, lambda0):
latLng = [0, 0]

phif = LKS92WGS84.__getFootpointLatitude(y)
ep2 = (math.pow(LKS92WGS84.__A_AXIS, 2) - math.pow(LKS92WGS84.__B_AXIS, 2)) / math.pow(LKS92WGS84.__B_AXIS, 2)
ep2 = (LKS92WGS84.__A_AXIS ** 2 - LKS92WGS84.__B_AXIS ** 2) / LKS92WGS84.__B_AXIS ** 2
cf = math.cos(phif)
nuf2 = ep2 * math.pow(cf, 2)
Nf = math.pow(LKS92WGS84.__A_AXIS, 2) / (LKS92WGS84.__B_AXIS * math.sqrt(1 + nuf2))
nuf2 = ep2 * cf ** 2
Nf = LKS92WGS84.__A_AXIS ** 2 / (LKS92WGS84.__B_AXIS * math.sqrt(1 + nuf2))
Nfpow = Nf

tf = math.tan(phif)
Expand Down Expand Up @@ -114,10 +114,10 @@ def __convertMapXYToLatLon(x, y, lambda0):
x8poly = 1385 + 3633 * tf2 + 4095 * tf4 + 1575 * (tf4 * tf2)

# Ģeogrāfiskais platums
latLng[0] = phif + x2frac * x2poly * (x * x) + x4frac * x4poly * math.pow(x, 4) + x6frac * x6poly * math.pow(x, 6) + x8frac * x8poly * math.pow(x, 8)
latLng[0] = phif + x2frac * x2poly * (x * x) + x4frac * x4poly * x ** 4 + x6frac * x6poly * x ** 6 + x8frac * x8poly * x ** 8

# Ģeogrāfiskais garums
latLng[1] = lambda0 + x1frac * x + x3frac * x3poly * math.pow(x, 3) + x5frac * x5poly * math.pow(x, 5) + x7frac * x7poly * math.pow(x, 7)
latLng[1] = lambda0 + x1frac * x + x3frac * x3poly * x ** 3 + x5frac * x5poly * x ** 5 + x7frac * x7poly * x ** 7

return latLng

Expand Down
147 changes: 147 additions & 0 deletions ruby/LKS92WGS84.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

class LKS92WGS84
# Koordinātu pārveidojumos izmantotās konstantes
CPI = Math::PI # Skaitlis pi
A_AXIS = 6378137 # Elipses modeļa lielā ass (a)
B_AXIS = 6356752.31414 # Elipses modeļa mazā ass (b)
CENTRAL_MERIDIAN = CPI * 24 / 180 # Centrālais meridiāns
OFFSET_X = 500000 # Koordinātu nobīde horizontālās (x) ass virzienā
OFFSET_Y = -6000000 # Koordinātu nobīde vertikālās (y) ass virzienā
SCALE = 0.9996 # Kartes mērogojuma faktors (reizinātājs)

def self.get_arc_length_of_meridian(phi)
n = (A_AXIS - B_AXIS) / (A_AXIS + B_AXIS)
alpha = ((A_AXIS + B_AXIS) / 2) * (1 + (n ** 2 / 4) + (n ** 4 / 64))
beta = (-3 * n / 2) + (9 * n ** 3 / 16) + (-3 * n ** 5 / 32)
gamma = (15 * n ** 2 / 16) + (-15 * n ** 4 / 32)
delta = (-35 * n ** 3 / 48) + (105 * n ** 5 / 256)
epsilon = (315 * n ** 4 / 512)

alpha * (phi + (beta * Math.sin(2 * phi)) + (gamma * Math.sin(4 * phi)) + (delta * Math.sin(6 * phi)) + (epsilon * Math.sin(8 * phi)))
end

def self.get_footpoint_latitude(y)
n = (A_AXIS - B_AXIS) / (A_AXIS + B_AXIS)
alpha = ((A_AXIS + B_AXIS) / 2) * (1 + (n ** 2 / 4) + (n ** 4 / 64))
yd = y / alpha
beta = (3 * n / 2) + (-27 * n ** 3 / 32) + (269 * n ** 5 / 512)
gamma = (21 * n ** 2 / 16) + (-55 * n ** 4 / 32)
delta = (151 * n ** 3 / 96) + (-417 * n ** 5 / 128)
epsilon = (1097 * n ** 4 / 512)

yd + (beta * Math.sin(2 * yd)) + (gamma * Math.sin(4 * yd)) + (delta * Math.sin(6 * yd)) + (epsilon * Math.sin(8 * yd))
end

def self.convert_map_lat_lng_to_xy(phi, lambda1, lambda0)
xy = [0, 0]

ep2 = (A_AXIS ** 2 - B_AXIS ** 2) / B_AXIS ** 2
nu2 = ep2 * Math.cos(phi) ** 2
n = A_AXIS ** 2 / (B_AXIS * Math.sqrt(1 + nu2))
t = Math.tan(phi)
t2 = t * t

l = lambda1 - lambda0
l3coef = 1 - t2 + nu2
l4coef = 5 - t2 + 9 * nu2 + 4 * (nu2 * nu2)
l5coef = 5 - 18 * t2 + (t2 * t2) + 14 * nu2 - 58 * t2 * nu2
l6coef = 61 - 58 * t2 + (t2 * t2) + 270 * nu2 - 330 * t2 * nu2
l7coef = 61 - 479 * t2 + 179 * (t2 * t2) - (t2 * t2 * t2)
l8coef = 1385 - 3111 * t2 + 543 * (t2 * t2) - (t2 * t2 * t2)

# x koordināta
xy[0] = n * Math.cos(phi) * l + (n / 6 * Math.cos(phi) ** 3 * l3coef * l ** 3) + (n / 120 * Math.cos(phi) ** 5 * l5coef * l ** 5) + (n / 5040 * Math.cos(phi) ** 7 * l7coef * l ** 7)

# y koordināta
xy[1] = get_arc_length_of_meridian(phi) + (t / 2 * n * Math.cos(phi) ** 2 * l ** 2) + (t / 24 * n * Math.cos(phi) ** 4 * l4coef * l ** 4) + (t / 720 * n * Math.cos(phi) ** 6 * l6coef * l ** 6) + (t / 40320 * n * Math.cos(phi) ** 8 * l8coef * l ** 8)

xy
end

def self.convert_map_xy_to_lat_lon(x, y, lambda0)
lat_lng = [0, 0]

phif = get_footpoint_latitude(y)
ep2 = (A_AXIS ** 2 - B_AXIS ** 2) / B_AXIS ** 2
cf = Math.cos(phif)
nuf2 = ep2 * cf ** 2
nf = A_AXIS ** 2 / (B_AXIS * Math.sqrt(1 + nuf2))
nfpow = nf

tf = Math.tan(phif)
tf2 = tf * tf
tf4 = tf2 * tf2

x1frac = 1 / (nfpow * cf)

nfpow *= nf # Nf^2
x2frac = tf / (2 * nfpow)

nfpow *= nf # Nf^3
x3frac = 1 / (6 * nfpow * cf)

nfpow *= nf # Nf^4
x4frac = tf / (24 * nfpow)

nfpow *= nf # Nf^5
x5frac = 1 / (120 * nfpow * cf)

nfpow *= nf # Nf^6
x6frac = tf / (720 * nfpow)

nfpow *= nf # Nf^7
x7frac = 1 / (5040 * nfpow * cf)

nfpow *= nf # Nf^8
x8frac = tf / (40320 * nfpow)

x2poly = -1 - nuf2
x3poly = -1 - 2 * tf2 - nuf2
x4poly = 5 + 3 * tf2 + 6 * nuf2 - 6 * tf2 * nuf2 - 3 * (nuf2 * nuf2) - 9 * tf2 * (nuf2 * nuf2)
x5poly = 5 + 28 * tf2 + 24 * tf4 + 6 * nuf2 + 8 * tf2 * nuf2
x6poly = -61 - 90 * tf2 - 45 * tf4 - 107 * nuf2 + 162 * tf2 * nuf2
x7poly = -61 - 662 * tf2 - 1320 * tf4 - 720 * (tf4 * tf2)
x8poly = 1385 + 3633 * tf2 + 4095 * tf4 + 1575 * (tf4 * tf2)

# Ģeogrāfiskais platums
lat_lng[0] = phif + x2frac * x2poly * (x * x) + x4frac * x4poly * x ** 4 + x6frac * x6poly * x ** 6 + x8frac * x8poly * x ** 8

# Ģeogrāfiskais garums
lat_lng[1] = lambda0 + x1frac * x + x3frac * x3poly * x ** 3 + x5frac * x5poly * x ** 5 + x7frac * x7poly * x ** 7

lat_lng
end

def self.convert_lat_lon_to_xy(coordinates)
lat = coordinates[0] * CPI / 180
lng = coordinates[1] * CPI / 180
xy = convert_map_lat_lng_to_xy(lat, lng, CENTRAL_MERIDIAN)

xy[0] = xy[0] * SCALE + OFFSET_X
xy[1] = xy[1] * SCALE + OFFSET_Y

if xy[1] < 0
xy[1] += 10000000
end

xy
end

def self.convert_xy_to_lat_lon(coordinates)
x = (coordinates[0] - OFFSET_X) / SCALE
y = (coordinates[1] - OFFSET_Y) / SCALE
lat_lng = convert_map_xy_to_lat_lon(x, y, CENTRAL_MERIDIAN)

lat_lng[0] = lat_lng[0] / CPI * 180
lat_lng[1] = lat_lng[1] / CPI * 180

lat_lng
end

private_class_method :get_arc_length_of_meridian
private_class_method :get_footpoint_latitude
private_class_method :convert_map_lat_lng_to_xy
private_class_method :convert_map_xy_to_lat_lon
end
37 changes: 37 additions & 0 deletions ruby/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

load 'LKS92WGS84.rb'

def test_case(coordinates)
converted = LKS92WGS84.convert_xy_to_lat_lon(LKS92WGS84.convert_lat_lon_to_xy(coordinates))

if converted[0].round(8) == coordinates[0].round(8) and converted[1].round(8) == coordinates[1].round(8)
result = 'izpildās'
else
result = 'neizpildās'
end

result
end

def test_case2(coordinates, lks_validate)
converted = LKS92WGS84.convert_lat_lon_to_xy(coordinates)

if converted[0].round(2) == lks_validate[0] and converted[1].round(2) == lks_validate[1]
result = 'izpildās'
else
result = 'neizpildās'
end

result
end

coordinates = [58.079501574948, 25.189986971284]
puts '"Baltās naktis" - Latvijas tālākais ziemeļu punkts [' + coordinates[0].to_s + ',' + coordinates[1].to_s + '] => ' + test_case(coordinates) + ' => ' + test_case2(coordinates, [570181.00, 438180.00])
coordinates = [56.172282784562, 28.095216442873]
puts '"Austras koks" - Latvijas tālākais austrumu punkts [' + coordinates[0].to_s + ',' + coordinates[1].to_s + '] => ' + test_case(coordinates) + ' => ' + test_case2(coordinates, [754190.00, 232806.00])
coordinates = [55.675228242509, 26.580528487143]
puts '"Saules puķe" - Latvijas tālākais dienvidu punkts [' + coordinates[0].to_s + ',' + coordinates[1].to_s + '] => ' + test_case(coordinates) + ' => ' + test_case2(coordinates, [662269.00, 172953.00])
coordinates = [56.377008455189, 20.979185882058]
puts '"Zaļais stars" - Latvijas galējais rietumu punkts [' + coordinates[0].to_s + ',' + coordinates[1].to_s + '] => ' + test_case(coordinates) + ' => ' + test_case2(coordinates, [313470.00, 252137.00])

0 comments on commit 2c6e724

Please sign in to comment.