Skip to content

Instantly share code, notes, and snippets.

@dodecaphonic
Created November 6, 2012 17:55
Show Gist options
  • Save dodecaphonic/4026343 to your computer and use it in GitHub Desktop.
Save dodecaphonic/4026343 to your computer and use it in GitHub Desktop.
RGeo, calc zone
def self.calculate_offset(point, normal, distance)
utm = utm_factory.point(point.x, point.y).projection
lon, lat = RGeo::CoordSys::Proj4.transform_coords(proj4_utm, proj4_geo,
utm.x + distance * normal.x,
utm.y + distance * normal.y)
new_pos = factory.point(lon, lat)
(point - Vertex.new(x: new_pos.x, y: new_pos.y)).length
end
def self.utm_factory
@utm_factory ||= RGeo::Geographic.projected_factory(projection_proj4: WGS84_UTM_PROJ4)
end
WGS84_UTM_PROJ4 = '+proj=utm +zone=23 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
# ---- snip ----
long_temp = (longitude + 180) - ((longitude + 180) / 360).to_i * 360 - 180
zone_number = ((long_temp + 180) / 6).to_i + 1
zone_number = 32 if latitude >= 56.0 && latitude < 64.0 &&
long_temp > 3.0 && long_temp < 12
# Special zones for Svalbard
if latitude >= 72.0 && latitude < 84.0
if long_temp >= 0.0 && long_temp < 9.0
zone_number = 31
elsif long_temp >= 9.0 && long_temp < 21.0
zone_number = 33
elsif long_temp >= 21.0 && long_temp < 33.0
zone_number = 35
elsif long_temp >= 33.0 && long_temp < 42.0
zone_number = 37
end
end
# --------- sniiiiiiiiiiiip ----------
UTM_ZONE_LETTERS = ('C'..'X').to_a - ['I', 'O']
def utm_letter_designator(latitude)
base, limit = -80, 84
current = base
zone_letter = UTM_ZONE_LETTERS.find do |letter|
rstart, rend = current, current + 8
current = rend
range = if letter == UTM_ZONE_LETTERS.last
(rstart..84)
else
(rstart...rend)
end
range === latitude
end
unless zone_letter
raise OutsideUTMBoundsException, 'Outside UTM zone bounds'
else
zone_letter
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment