schleyfox-rgeo 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. data/History.rdoc +199 -0
  2. data/README.rdoc +172 -0
  3. data/Spatial_Programming_With_RGeo.rdoc +440 -0
  4. data/Version +1 -0
  5. data/ext/geos_c_impl/extconf.rb +84 -0
  6. data/ext/geos_c_impl/factory.c +468 -0
  7. data/ext/geos_c_impl/factory.h +224 -0
  8. data/ext/geos_c_impl/geometry.c +705 -0
  9. data/ext/geos_c_impl/geometry.h +55 -0
  10. data/ext/geos_c_impl/geometry_collection.c +482 -0
  11. data/ext/geos_c_impl/geometry_collection.h +69 -0
  12. data/ext/geos_c_impl/line_string.c +509 -0
  13. data/ext/geos_c_impl/line_string.h +64 -0
  14. data/ext/geos_c_impl/main.c +70 -0
  15. data/ext/geos_c_impl/point.c +193 -0
  16. data/ext/geos_c_impl/point.h +62 -0
  17. data/ext/geos_c_impl/polygon.c +265 -0
  18. data/ext/geos_c_impl/polygon.h +66 -0
  19. data/ext/geos_c_impl/preface.h +50 -0
  20. data/ext/proj4_c_impl/extconf.rb +88 -0
  21. data/ext/proj4_c_impl/main.c +271 -0
  22. data/lib/rgeo.rb +124 -0
  23. data/lib/rgeo/cartesian.rb +60 -0
  24. data/lib/rgeo/cartesian/analysis.rb +118 -0
  25. data/lib/rgeo/cartesian/bounding_box.rb +337 -0
  26. data/lib/rgeo/cartesian/calculations.rb +161 -0
  27. data/lib/rgeo/cartesian/factory.rb +209 -0
  28. data/lib/rgeo/cartesian/feature_classes.rb +173 -0
  29. data/lib/rgeo/cartesian/feature_methods.rb +106 -0
  30. data/lib/rgeo/cartesian/interface.rb +150 -0
  31. data/lib/rgeo/coord_sys.rb +79 -0
  32. data/lib/rgeo/coord_sys/cs/entities.rb +1524 -0
  33. data/lib/rgeo/coord_sys/cs/factories.rb +208 -0
  34. data/lib/rgeo/coord_sys/cs/wkt_parser.rb +308 -0
  35. data/lib/rgeo/coord_sys/proj4.rb +312 -0
  36. data/lib/rgeo/coord_sys/srs_database/active_record_table.rb +194 -0
  37. data/lib/rgeo/coord_sys/srs_database/interface.rb +165 -0
  38. data/lib/rgeo/coord_sys/srs_database/proj4_data.rb +188 -0
  39. data/lib/rgeo/coord_sys/srs_database/sr_org.rb +108 -0
  40. data/lib/rgeo/coord_sys/srs_database/url_reader.rb +108 -0
  41. data/lib/rgeo/error.rb +63 -0
  42. data/lib/rgeo/feature.rb +88 -0
  43. data/lib/rgeo/feature/curve.rb +156 -0
  44. data/lib/rgeo/feature/factory.rb +332 -0
  45. data/lib/rgeo/feature/factory_generator.rb +138 -0
  46. data/lib/rgeo/feature/geometry.rb +614 -0
  47. data/lib/rgeo/feature/geometry_collection.rb +129 -0
  48. data/lib/rgeo/feature/line.rb +66 -0
  49. data/lib/rgeo/feature/line_string.rb +102 -0
  50. data/lib/rgeo/feature/linear_ring.rb +66 -0
  51. data/lib/rgeo/feature/multi_curve.rb +113 -0
  52. data/lib/rgeo/feature/multi_line_string.rb +66 -0
  53. data/lib/rgeo/feature/multi_point.rb +73 -0
  54. data/lib/rgeo/feature/multi_polygon.rb +97 -0
  55. data/lib/rgeo/feature/multi_surface.rb +116 -0
  56. data/lib/rgeo/feature/point.rb +120 -0
  57. data/lib/rgeo/feature/polygon.rb +141 -0
  58. data/lib/rgeo/feature/surface.rb +122 -0
  59. data/lib/rgeo/feature/types.rb +305 -0
  60. data/lib/rgeo/geographic.rb +75 -0
  61. data/lib/rgeo/geographic/factory.rb +287 -0
  62. data/lib/rgeo/geographic/interface.rb +410 -0
  63. data/lib/rgeo/geographic/proj4_projector.rb +98 -0
  64. data/lib/rgeo/geographic/projected_feature_classes.rb +213 -0
  65. data/lib/rgeo/geographic/projected_feature_methods.rb +228 -0
  66. data/lib/rgeo/geographic/projected_window.rb +467 -0
  67. data/lib/rgeo/geographic/simple_mercator_projector.rb +157 -0
  68. data/lib/rgeo/geographic/spherical_feature_classes.rb +212 -0
  69. data/lib/rgeo/geographic/spherical_feature_methods.rb +97 -0
  70. data/lib/rgeo/geographic/spherical_math.rb +206 -0
  71. data/lib/rgeo/geos.rb +72 -0
  72. data/lib/rgeo/geos/factory.rb +301 -0
  73. data/lib/rgeo/geos/impl_additions.rb +76 -0
  74. data/lib/rgeo/geos/interface.rb +139 -0
  75. data/lib/rgeo/geos/zm_factory.rb +275 -0
  76. data/lib/rgeo/geos/zm_impl.rb +432 -0
  77. data/lib/rgeo/impl_helper.rb +53 -0
  78. data/lib/rgeo/impl_helper/basic_geometry_collection_methods.rb +235 -0
  79. data/lib/rgeo/impl_helper/basic_geometry_methods.rb +85 -0
  80. data/lib/rgeo/impl_helper/basic_line_string_methods.rb +197 -0
  81. data/lib/rgeo/impl_helper/basic_point_methods.rb +138 -0
  82. data/lib/rgeo/impl_helper/basic_polygon_methods.rb +121 -0
  83. data/lib/rgeo/impl_helper/math.rb +50 -0
  84. data/lib/rgeo/version.rb +52 -0
  85. data/lib/rgeo/wkrep.rb +72 -0
  86. data/lib/rgeo/wkrep/wkb_generator.rb +267 -0
  87. data/lib/rgeo/wkrep/wkb_parser.rb +315 -0
  88. data/lib/rgeo/wkrep/wkt_generator.rb +275 -0
  89. data/lib/rgeo/wkrep/wkt_parser.rb +496 -0
  90. data/test/common/geometry_collection_tests.rb +238 -0
  91. data/test/common/line_string_tests.rb +324 -0
  92. data/test/common/multi_line_string_tests.rb +209 -0
  93. data/test/common/multi_point_tests.rb +201 -0
  94. data/test/common/multi_polygon_tests.rb +208 -0
  95. data/test/common/point_tests.rb +331 -0
  96. data/test/common/polygon_tests.rb +232 -0
  97. data/test/coord_sys/tc_active_record_table.rb +102 -0
  98. data/test/coord_sys/tc_ogc_cs.rb +356 -0
  99. data/test/coord_sys/tc_proj4.rb +138 -0
  100. data/test/coord_sys/tc_proj4_srs_data.rb +76 -0
  101. data/test/coord_sys/tc_sr_org.rb +70 -0
  102. data/test/coord_sys/tc_url_reader.rb +82 -0
  103. data/test/geos/tc_factory.rb +91 -0
  104. data/test/geos/tc_geometry_collection.rb +62 -0
  105. data/test/geos/tc_line_string.rb +62 -0
  106. data/test/geos/tc_misc.rb +72 -0
  107. data/test/geos/tc_multi_line_string.rb +62 -0
  108. data/test/geos/tc_multi_point.rb +62 -0
  109. data/test/geos/tc_multi_polygon.rb +63 -0
  110. data/test/geos/tc_point.rb +86 -0
  111. data/test/geos/tc_polygon.rb +86 -0
  112. data/test/geos/tc_zmfactory.rb +85 -0
  113. data/test/projected_geographic/tc_geometry_collection.rb +62 -0
  114. data/test/projected_geographic/tc_line_string.rb +62 -0
  115. data/test/projected_geographic/tc_multi_line_string.rb +62 -0
  116. data/test/projected_geographic/tc_multi_point.rb +62 -0
  117. data/test/projected_geographic/tc_multi_polygon.rb +63 -0
  118. data/test/projected_geographic/tc_point.rb +93 -0
  119. data/test/projected_geographic/tc_polygon.rb +62 -0
  120. data/test/simple_cartesian/tc_calculations.rb +145 -0
  121. data/test/simple_cartesian/tc_geometry_collection.rb +69 -0
  122. data/test/simple_cartesian/tc_line_string.rb +70 -0
  123. data/test/simple_cartesian/tc_multi_line_string.rb +67 -0
  124. data/test/simple_cartesian/tc_multi_point.rb +67 -0
  125. data/test/simple_cartesian/tc_multi_polygon.rb +70 -0
  126. data/test/simple_cartesian/tc_point.rb +91 -0
  127. data/test/simple_cartesian/tc_polygon.rb +67 -0
  128. data/test/simple_mercator/tc_geometry_collection.rb +62 -0
  129. data/test/simple_mercator/tc_line_string.rb +62 -0
  130. data/test/simple_mercator/tc_multi_line_string.rb +62 -0
  131. data/test/simple_mercator/tc_multi_point.rb +62 -0
  132. data/test/simple_mercator/tc_multi_polygon.rb +63 -0
  133. data/test/simple_mercator/tc_point.rb +93 -0
  134. data/test/simple_mercator/tc_polygon.rb +62 -0
  135. data/test/simple_mercator/tc_window.rb +219 -0
  136. data/test/spherical_geographic/tc_calculations.rb +203 -0
  137. data/test/spherical_geographic/tc_geometry_collection.rb +70 -0
  138. data/test/spherical_geographic/tc_line_string.rb +70 -0
  139. data/test/spherical_geographic/tc_multi_line_string.rb +67 -0
  140. data/test/spherical_geographic/tc_multi_point.rb +67 -0
  141. data/test/spherical_geographic/tc_multi_polygon.rb +70 -0
  142. data/test/spherical_geographic/tc_point.rb +100 -0
  143. data/test/spherical_geographic/tc_polygon.rb +67 -0
  144. data/test/tc_cartesian_analysis.rb +107 -0
  145. data/test/tc_oneoff.rb +63 -0
  146. data/test/wkrep/tc_wkb_generator.rb +249 -0
  147. data/test/wkrep/tc_wkb_parser.rb +353 -0
  148. data/test/wkrep/tc_wkt_generator.rb +362 -0
  149. data/test/wkrep/tc_wkt_parser.rb +480 -0
  150. metadata +267 -0
@@ -0,0 +1,165 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # SRS database interface
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 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
+
36
+
37
+ module RGeo
38
+
39
+ module CoordSys
40
+
41
+
42
+ # This module contains tools for accessing spatial reference
43
+ # databases. These are databases (either local or remote) from which
44
+ # you can look up coordinate system specifications, typically in
45
+ # either OGC or Proj4 format. For example, you can access the
46
+ # <tt>spatial_ref_sys</tt> table provided with an OGC-compliant RDBMS
47
+ # such as PostGIS. You can also read the database files provided with
48
+ # the proj4 library, or access online databases such as the
49
+ # spatialreference.org site.
50
+
51
+ module SRSDatabase
52
+
53
+
54
+ # Interface specification for spatial reference system databases.
55
+ # This module exists primarily for the sake of documentation.
56
+ # Database implementations need not actually include this module,
57
+ # but at least need to duck-type its methods.
58
+
59
+ module Interface
60
+
61
+
62
+ # Retrieve an Entry given an identifier. The identifier is usually
63
+ # a numeric spatial reference ID (SRID), but could be a string
64
+ # value for certain database types.
65
+
66
+ def get(ident_)
67
+ nil
68
+ end
69
+
70
+
71
+ # Clears any cache utilized by this database.
72
+
73
+ def clear_cache
74
+ nil
75
+ end
76
+
77
+
78
+ end
79
+
80
+
81
+ # An entry in a spatial reference system database.
82
+ # Every entry has an identifier, but all the other attributes are
83
+ # optional and may or may not be present depending on the database.
84
+
85
+ class Entry
86
+
87
+
88
+ # Create an entry.
89
+ # You must provide an identifier, which may be numeric or a
90
+ # string. The data hash should contain any other attributes,
91
+ # keyed by symbol.
92
+ #
93
+ # Some attribute inputs have special behaviors:
94
+ #
95
+ # [<tt>:coord_sys</tt>]
96
+ # You can pass a CS coordinate system object, or a string in
97
+ # WKT format.
98
+ # [<tt>:proj4</tt>]
99
+ # You can pass a Proj4 object, or a proj4-format string.
100
+ # [<tt>:name</tt>]
101
+ # If the name is not provided directly, it is taken from the
102
+ # coord_sys.
103
+ # [<tt>:authority</tt>]
104
+ # If the authority name is not provided directly, it is taken
105
+ # from the coord_sys.
106
+ # [<tt>:authority_code</tt>]
107
+ # If the authority code is not provided directly, it is taken
108
+ # from the coord_sys.
109
+
110
+ def initialize(ident_, data_={})
111
+ @identifier = ident_
112
+ @authority = data_[:authority]
113
+ @authority_code = data_[:authority_code]
114
+ @name = data_[:name]
115
+ @description = data_[:description]
116
+ @coord_sys = data_[:coord_sys]
117
+ if @coord_sys.kind_of?(::String)
118
+ @coord_sys = CS.create_from_wkt(@coord_sys)
119
+ end
120
+ @proj4 = data_[:proj4]
121
+ if Proj4.supported?
122
+ if @proj4.kind_of?(::String) || @proj4.kind_of?(::Hash)
123
+ @proj4 = Proj4.create(@proj4)
124
+ end
125
+ else
126
+ @proj4 = nil
127
+ end
128
+ if @coord_sys
129
+ @name = @coord_sys.name unless @name
130
+ @authority = @coord_sys.authority unless @authority
131
+ @authority_code = @coord_sys.authority unless @authority_code
132
+ end
133
+ end
134
+
135
+
136
+ # The database key or identifier.
137
+ attr_reader :identifier
138
+
139
+ # The authority name, if present. Example: "epsg".
140
+ attr_reader :authority
141
+
142
+ # The authority code, e.g. an EPSG code.
143
+ attr_reader :authority_code
144
+
145
+ # A human-readable name for this coordinate system.
146
+ attr_reader :name
147
+
148
+ # A human-readable description for this coordinate system.
149
+ attr_reader :description
150
+
151
+ # The CS::CoordinateSystem object.
152
+ attr_reader :coord_sys
153
+
154
+ # The Proj4 object.
155
+ attr_reader :proj4
156
+
157
+
158
+ end
159
+
160
+
161
+ end
162
+
163
+ end
164
+
165
+ end
@@ -0,0 +1,188 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # SRS database interface
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 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
+
36
+
37
+ module RGeo
38
+
39
+ module CoordSys
40
+
41
+ module SRSDatabase
42
+
43
+
44
+ # A spatial reference database implementation backed by coordinate
45
+ # system files installed as part of the proj4 library. For a given
46
+ # Proj4Data object, you specify a single file (e.g. the epsg data
47
+ # file), and you can retrieve records by ID number.
48
+
49
+ class Proj4Data
50
+
51
+
52
+ # Connect to one of the proj4 data files. You should provide the
53
+ # file name, optionally the installation directory if it is not
54
+ # in a typical location, and several additional options.
55
+ #
56
+ # These options are recognized:
57
+ #
58
+ # [<tt>:dir</tt>]
59
+ # The path for the share/proj directory that contains the
60
+ # requested data file. By default, the Proj4Data class will
61
+ # try a number of directories for you, including
62
+ # /usr/local/share/proj, /opt/local/share/proj, /usr/share/proj,
63
+ # and a few other variants. However, if you have proj4 installed
64
+ # elsewhere, you can provide an explicit directory using this
65
+ # option. You may also pass nil as the value, in which case all
66
+ # the normal lookup paths will be disabled, and you will have to
67
+ # provide the full path as the file name.
68
+ # [<tt>:cache</tt>]
69
+ # If set to true, this class caches previously looked up entries
70
+ # so subsequent lookups do not have to reread the file. If set
71
+ # to <tt>:read_all</tt>, then ALL values in the file are read in
72
+ # and cached the first time a lookup is done. If set to
73
+ # <tt>:preload</tt>, then ALL values in the file are read in
74
+ # immediately when the database is created. Default is false,
75
+ # indicating that the file will be reread on every lookup.
76
+ # [<tt>:authority</tt>]
77
+ # If set, its value is taken as the authority name for all
78
+ # entries. The authority code will be set to the identifier. If
79
+ # not set, then the authority fields of entries will be blank.
80
+
81
+ def initialize(filename_, opts_={})
82
+ dir_ = nil
83
+ if opts_.include?(:dir)
84
+ dir_ = opts_[:dir]
85
+ else
86
+ ['/usr/local/share/proj', '/usr/local/proj/share/proj', '/usr/local/proj4/share/proj', '/opt/local/share/proj', '/opt/proj/share/proj', '/opt/proj4/share/proj', '/opt/share/proj', '/usr/share/proj'].each do |d_|
87
+ if ::File.directory?(d_) && ::File.readable?(d_)
88
+ dir_ = d_
89
+ break
90
+ end
91
+ end
92
+ end
93
+ @path = dir_ ? "#{dir_}/#{filename_}" : filename_
94
+ @authority = opts_[:authority]
95
+ if opts_[:cache]
96
+ @cache = {}
97
+ case opts_[:cache]
98
+ when :read_all
99
+ @populate_state = 1
100
+ when :preload
101
+ _search_file(nil)
102
+ @populate_state = 2
103
+ else
104
+ @populate_state = 0
105
+ end
106
+ else
107
+ @cache = nil
108
+ @populate_state = 0
109
+ end
110
+ end
111
+
112
+
113
+ # Retrieve the Entry for the given ID number.
114
+
115
+ def get(ident_)
116
+ ident_ = ident_.to_s
117
+ return @cache[ident_] if @cache && @cache.include?(ident_)
118
+ result_ = nil
119
+ if @populate_state == 0
120
+ data_ = _search_file(ident_)
121
+ result_ = Entry.new(ident_, :authority => @authority, :authority_code => @authority ? ident_ : nil, :name => data_[1], :proj4 => data_[2]) if data_
122
+ @cache[ident_] = result_ if @cache
123
+ elsif @populate_state == 1
124
+ _search_file(nil)
125
+ result_ = @cache[ident_]
126
+ @populate_state = 2
127
+ end
128
+ result_
129
+ end
130
+
131
+
132
+ # Clear the cache if one exists.
133
+
134
+ def clear_cache
135
+ @cache.clear if @cache
136
+ @populate_state = 1 if @populate_state == 2
137
+ end
138
+
139
+
140
+ def _search_file(ident_) # :nodoc:
141
+ ::File.open(@path) do |file_|
142
+ cur_name_ = nil
143
+ cur_ident_ = nil
144
+ cur_text_ = nil
145
+ file_.each do |line_|
146
+ line_.strip!
147
+ if (comment_delim_ = line_.index('#'))
148
+ cur_name_ = line_[comment_delim_+1..-1].strip
149
+ line_ = line_[0..comment_delim_-1].strip
150
+ end
151
+ unless cur_ident_
152
+ if line_ =~ /^<(\w+)>(.*)/
153
+ cur_ident_ = $1
154
+ cur_text_ = []
155
+ line_ = $2.strip
156
+ end
157
+ end
158
+ if cur_ident_
159
+ if line_[-2..-1] == '<>'
160
+ cur_text_ << line_[0..-3].strip
161
+ cur_text_ = cur_text_.join(' ')
162
+ if ident_.nil?
163
+ @cache[ident_] = Entry.new(ident_, :authority => @authority, :authority_code => @authority ? id_ : nil, :name => cur_name_, :proj4 => cur_text_)
164
+ end
165
+ if cur_ident_ == ident_
166
+ return [ident_, cur_name_, cur_text_]
167
+ end
168
+ cur_ident_ = nil
169
+ cur_name_ = nil
170
+ cur_text_ = nil
171
+ else
172
+ cur_text_ << line_
173
+ end
174
+ end
175
+ end
176
+ end
177
+ nil
178
+ end
179
+
180
+
181
+ end
182
+
183
+
184
+ end
185
+
186
+ end
187
+
188
+ end
@@ -0,0 +1,108 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # SRS database interface
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 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
+
36
+
37
+ require 'net/http'
38
+
39
+
40
+ module RGeo
41
+
42
+ module CoordSys
43
+
44
+ module SRSDatabase
45
+
46
+
47
+ # A spatial reference database implementation that fetches data
48
+ # from the spatialreference.org website.
49
+
50
+ class SrOrg
51
+
52
+
53
+ # Create a database backed by the given catalog of the
54
+ # spatialreference.org website. Catalogs currently supported by
55
+ # spatialreference.org are "epsg", "esri", "iau2000" and "sr-org".
56
+ #
57
+ # Options:
58
+ #
59
+ # [<tt>:cache</tt>]
60
+ # If set to true, lookup results are cached so if the same URL
61
+ # is requested again, the result is served from cache rather
62
+ # than issuing another HTTP request. Default is false.
63
+
64
+ def initialize(catalog_, opts_={})
65
+ @catalog = catalog_.to_s.downcase
66
+ @cache = opts_[:cache] ? {} : nil
67
+ end
68
+
69
+
70
+ # The spatialreference.org catalog used by this database.
71
+ attr_reader :catalog
72
+
73
+
74
+ # Retrieve the Entry from a spatialreference.org catalog given an
75
+ # integer ID.
76
+
77
+ def get(ident_)
78
+ ident_ = ident_.to_s
79
+ return @cache[ident_] if @cache && @cache.include?(ident_)
80
+ coord_sys_ = nil
81
+ proj4_ = nil
82
+ ::Net::HTTP.start('spatialreference.org') do |http_|
83
+ response_ = http_.request_get("/ref/#{@catalog}/#{ident_}/ogcwkt/")
84
+ coord_sys_ = response_.body if response_.kind_of?(::Net::HTTPSuccess)
85
+ response_ = http_.request_get("/ref/#{@catalog}/#{ident_}/proj4/")
86
+ proj4_ = response_.body if response_.kind_of?(::Net::HTTPSuccess)
87
+ end
88
+ result_ = Entry.new(ident_, :coord_sys => coord_sys_.strip, :proj4 => proj4_.strip)
89
+ @cache[ident_] = result_ if @cache
90
+ result_
91
+ end
92
+
93
+
94
+ # Clear the cache if one exists.
95
+
96
+ def clear_cache
97
+ @cache.clear if @cache
98
+ end
99
+
100
+
101
+ end
102
+
103
+
104
+ end
105
+
106
+ end
107
+
108
+ end