PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/test/coord_sys/tc_proj4.rb

http://github.com/dazuma/rgeo
Ruby | 205 lines | 123 code | 49 blank | 33 comment | 3 complexity | 6e2fd83c164771dbdbd200d766157e25 MD5 | raw file
  1. # -----------------------------------------------------------------------------
  2. #
  3. # Tests for proj4 wrapper
  4. #
  5. # -----------------------------------------------------------------------------
  6. # Copyright 2010-2012 Daniel Azuma
  7. #
  8. # All rights reserved.
  9. #
  10. # Redistribution and use in source and binary forms, with or without
  11. # modification, are permitted provided that the following conditions are met:
  12. #
  13. # * Redistributions of source code must retain the above copyright notice,
  14. # this list of conditions and the following disclaimer.
  15. # * Redistributions in binary form must reproduce the above copyright notice,
  16. # this list of conditions and the following disclaimer in the documentation
  17. # and/or other materials provided with the distribution.
  18. # * Neither the name of the copyright holder, nor the names of any other
  19. # contributors to this software, may be used to endorse or promote products
  20. # derived from this software without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. # POSSIBILITY OF SUCH DAMAGE.
  33. # -----------------------------------------------------------------------------
  34. ;
  35. require 'test/unit'
  36. require 'rgeo'
  37. module RGeo
  38. module Tests # :nodoc:
  39. module CoordSys # :nodoc:
  40. class TestProj4 < ::Test::Unit::TestCase # :nodoc:
  41. def test_proj4_version
  42. assert_match(/^\d+\.\d+(\.\d+)?$/, ::RGeo::CoordSys::Proj4.version_string)
  43. end
  44. def test_create_wgs84
  45. proj_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  46. assert_equal(true, proj_.geographic?)
  47. assert_equal('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs', proj_.original_str)
  48. assert_equal(' +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0', proj_.canonical_str)
  49. end
  50. def test_get_wgs84_geographic
  51. proj_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  52. proj2_ = proj_.get_geographic
  53. assert_nil(proj2_.original_str)
  54. assert_equal(true, proj2_.geographic?)
  55. coords_ = ::RGeo::CoordSys::Proj4.transform_coords(proj_, proj2_, 1, 2, 0)
  56. assert_equal([1, 2, 0], coords_)
  57. end
  58. def test_identity_transform
  59. proj_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  60. assert_equal([1, 2, 0], ::RGeo::CoordSys::Proj4.transform_coords(proj_, proj_, 1, 2, 0))
  61. assert_equal([1, 2], ::RGeo::CoordSys::Proj4.transform_coords(proj_, proj_, 1, 2, nil))
  62. end
  63. def _project_merc(x_, y_)
  64. [x_ * 6378137.0, ::Math.log(::Math.tan(::Math::PI / 4.0 + y_ / 2.0)) * 6378137.0]
  65. end
  66. def _unproject_merc(x_, y_)
  67. [x_ / 6378137.0, (2.0 * ::Math.atan(::Math.exp(y_ / 6378137.0)) - ::Math::PI / 2.0)]
  68. end
  69. def _assert_close_enough(a_, b_)
  70. delta_ = ::Math.sqrt(a_*a_+b_*b_)*0.00000001
  71. delta_ = 0.000000000001 if delta_ < 0.000000000001
  72. assert_in_delta(a_, b_, delta_)
  73. end
  74. def _assert_xy_close(xy1_, xy2_)
  75. _assert_close_enough(xy1_[0], xy2_[0])
  76. _assert_close_enough(xy1_[1], xy2_[1])
  77. end
  78. def test_simple_mercator_transform
  79. geography_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs', :radians => true)
  80. projection_ = ::RGeo::CoordSys::Proj4.create('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs')
  81. _assert_xy_close(_project_merc(0, 0), ::RGeo::CoordSys::Proj4.transform_coords(geography_, projection_, 0, 0, nil))
  82. _assert_xy_close(_project_merc(0.01, 0.01), ::RGeo::CoordSys::Proj4.transform_coords(geography_, projection_, 0.01, 0.01, nil))
  83. _assert_xy_close(_project_merc(1, 1), ::RGeo::CoordSys::Proj4.transform_coords(geography_, projection_, 1, 1, nil))
  84. _assert_xy_close(_project_merc(-1, -1), ::RGeo::CoordSys::Proj4.transform_coords(geography_, projection_, -1, -1, nil))
  85. _assert_xy_close(_unproject_merc(0, 0), ::RGeo::CoordSys::Proj4.transform_coords(projection_, geography_, 0, 0, nil))
  86. _assert_xy_close(_unproject_merc(10000, 10000), ::RGeo::CoordSys::Proj4.transform_coords(projection_, geography_, 10000, 10000, nil))
  87. _assert_xy_close(_unproject_merc(-20000000, -20000000), ::RGeo::CoordSys::Proj4.transform_coords(projection_, geography_, -20000000, -20000000, nil))
  88. end
  89. def test_equivalence
  90. proj1_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  91. proj2_ = ::RGeo::CoordSys::Proj4.create(' +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  92. assert_equal(proj1_, proj2_)
  93. end
  94. def test_hashes_equal_for_equivalent_objects
  95. proj1_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  96. proj2_ = ::RGeo::CoordSys::Proj4.create(' +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  97. assert_equal(proj1_.hash, proj2_.hash)
  98. end
  99. def test_point_projection_cast
  100. geography_ = ::RGeo::Geos.factory(:proj4 => '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs', :srid =>4326)
  101. projection_ = ::RGeo::Geos.factory(:proj4 => '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs', :srid => 27700)
  102. proj_point_ = projection_.parse_wkt('POINT(473600.5000000000000000 186659.7999999999883585)')
  103. geo_point_ = ::RGeo::Feature.cast(proj_point_, :project => true, :factory => geography_)
  104. _assert_close_enough(-0.9393598527244420, geo_point_.x)
  105. _assert_close_enough(51.5740106527552697, geo_point_.y)
  106. end
  107. def test_point_transform_lowlevel
  108. geography_ = ::RGeo::Geos.factory(:proj4 => '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs', :srid =>4326)
  109. projection_ = ::RGeo::Geos.factory(:proj4 => '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs', :srid => 27700)
  110. proj_point_ = projection_.parse_wkt('POINT(473600.5000000000000000 186659.7999999999883585)')
  111. geo_point_ = ::RGeo::CoordSys::Proj4.transform(projection_.proj4, proj_point_, geography_.proj4, geography_)
  112. _assert_close_enough(-0.9393598527244420, geo_point_.x)
  113. _assert_close_enough(51.5740106527552697, geo_point_.y)
  114. end
  115. def test_geocentric
  116. obj1_ = ::RGeo::CoordSys::Proj4.create('+proj=geocent +ellps=WGS84')
  117. assert_equal(true, obj1_.geocentric?)
  118. end
  119. def test_get_geographic
  120. projection_ = ::RGeo::CoordSys::Proj4.create('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs')
  121. geographic_ = projection_.get_geographic
  122. expected_ = ::RGeo::CoordSys::Proj4.create('+proj=latlong +a=6378137 +b=6378137 +nadgrids=@null')
  123. assert_equal(expected_, geographic_)
  124. end
  125. def test_marshal_roundtrip
  126. obj1_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  127. dump_ = ::Marshal.dump(obj1_)
  128. obj2_ = ::Marshal.load(dump_)
  129. assert_equal(obj1_, obj2_)
  130. end
  131. def test_dup
  132. obj1_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  133. obj2_ = obj1_.dup
  134. assert_equal(obj1_, obj2_)
  135. end
  136. def test_dup_of_get_geographic
  137. obj1_ = ::RGeo::CoordSys::Proj4.create('+proj=latlong +datum=WGS84 +ellps=WGS84')
  138. obj2_ = obj1_.get_geographic
  139. obj3_ = obj2_.dup
  140. assert_equal(obj1_, obj3_)
  141. end
  142. if ::RGeo.yaml_supported?
  143. def test_yaml_roundtrip
  144. obj1_ = ::RGeo::CoordSys::Proj4.create('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
  145. dump_ = ::Psych.dump(obj1_)
  146. obj2_ = ::Psych.load(dump_)
  147. assert_equal(obj1_, obj2_)
  148. end
  149. end
  150. end
  151. end
  152. end
  153. end if ::RGeo::CoordSys::Proj4.supported?
  154. unless ::RGeo::CoordSys::Proj4.supported?
  155. puts "WARNING: Proj4 support not available. Related tests skipped."
  156. end