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,138 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Common methods for Point features
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 ImplHelper # :nodoc:
40
+
41
+
42
+ module BasicPointMethods # :nodoc:
43
+
44
+
45
+ def initialize(factory_, x_, y_, *extra_)
46
+ _set_factory(factory_)
47
+ @x = x_.to_f
48
+ @y = y_.to_f
49
+ @z = factory_.property(:has_z_coordinate) ? extra_.shift.to_f : nil
50
+ @m = factory_.property(:has_m_coordinate) ? extra_.shift.to_f : nil
51
+ if extra_.size > 0
52
+ raise ::ArgumentError, "Too many arguments for point initializer"
53
+ end
54
+ _validate_geometry
55
+ end
56
+
57
+
58
+ def x
59
+ @x
60
+ end
61
+
62
+
63
+ def y
64
+ @y
65
+ end
66
+
67
+
68
+ def z
69
+ @z
70
+ end
71
+
72
+
73
+ def m
74
+ @m
75
+ end
76
+
77
+
78
+ def eql?(rhs_)
79
+ rhs_.is_a?(self.class) && rhs_.factory.eql?(@factory) && @x == rhs_.x && @y == rhs_.y && @z == rhs_.z && @m == rhs_.m
80
+ end
81
+
82
+
83
+ def dimension
84
+ 0
85
+ end
86
+
87
+
88
+ def geometry_type
89
+ Feature::Point
90
+ end
91
+
92
+
93
+ def is_empty?
94
+ false
95
+ end
96
+
97
+
98
+ def is_simple?
99
+ true
100
+ end
101
+
102
+
103
+ def envelope
104
+ self
105
+ end
106
+
107
+
108
+ def boundary
109
+ factory.collection([])
110
+ end
111
+
112
+
113
+ def convex_hull
114
+ self
115
+ end
116
+
117
+
118
+ def equals?(rhs_)
119
+ return false unless rhs_.is_a?(self.class) && rhs_.factory == self.factory
120
+ case rhs_
121
+ when Feature::Point
122
+ rhs_.x == @x && rhs_.y == @y
123
+ when Feature::LineString
124
+ rhs_.num_points > 0 && rhs_.points.all?{ |elem_| equals?(elem_) }
125
+ when Feature::GeometryCollection
126
+ rhs_.num_geometries > 0 && rhs_.all?{ |elem_| equals?(elem_) }
127
+ else
128
+ false
129
+ end
130
+ end
131
+
132
+
133
+ end
134
+
135
+
136
+ end
137
+
138
+ end
@@ -0,0 +1,121 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Common methods for Polygon features
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 ImplHelper # :nodoc:
40
+
41
+
42
+ module BasicPolygonMethods # :nodoc:
43
+
44
+
45
+ def initialize(factory_, exterior_ring_, interior_rings_)
46
+ _set_factory(factory_)
47
+ @exterior_ring = Feature.cast(exterior_ring_, factory_, Feature::LinearRing)
48
+ unless @exterior_ring
49
+ raise Error::InvalidGeometry, "Failed to cast exterior ring #{exterior_ring_}"
50
+ end
51
+ @interior_rings = (interior_rings_ || []).map do |elem_|
52
+ elem_ = Feature.cast(elem_, factory_, Feature::LinearRing)
53
+ unless elem_
54
+ raise Error::InvalidGeometry, "Could not cast interior ring #{elem_}"
55
+ end
56
+ elem_
57
+ end
58
+ _validate_geometry
59
+ end
60
+
61
+
62
+ def eql?(rhs_)
63
+ if rhs_.is_a?(self.class) && rhs_.factory.eql?(@factory) && @exterior_ring.eql?(rhs_.exterior_ring) && @interior_rings.size == rhs_.num_interior_rings
64
+ rhs_.interior_rings.each_with_index{ |r_, i_| return false unless @interior_rings[i_].eql?(r_) }
65
+ else
66
+ false
67
+ end
68
+ end
69
+
70
+
71
+ def exterior_ring
72
+ @exterior_ring
73
+ end
74
+
75
+
76
+ def num_interior_rings
77
+ @interior_rings.size
78
+ end
79
+
80
+
81
+ def interior_ring_n(n_)
82
+ @interior_rings[n_]
83
+ end
84
+
85
+
86
+ def interior_rings
87
+ @interior_rings.dup
88
+ end
89
+
90
+
91
+ def dimension
92
+ 2
93
+ end
94
+
95
+
96
+ def geometry_type
97
+ Feature::Polygon
98
+ end
99
+
100
+
101
+ def is_empty?
102
+ @exterior_ring.is_empty?
103
+ end
104
+
105
+
106
+ def boundary
107
+ array_ = []
108
+ unless @exterior_ring.is_empty?
109
+ array_ << @exterior_ring
110
+ end
111
+ array_.concat(@interior_rings)
112
+ factory.multi_line_string(array_)
113
+ end
114
+
115
+
116
+ end
117
+
118
+
119
+ end
120
+
121
+ end
@@ -0,0 +1,50 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Math constants and tools
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 ImplHelper # :nodoc:
40
+
41
+ module Math # :nodoc:
42
+
43
+ RADIANS_PER_DEGREE = ::Math::PI/180.0
44
+ DEGREES_PER_RADIAN = 180.0/::Math::PI
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,52 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # RGeo version
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
+ begin
38
+ require 'versionomy'
39
+ rescue ::LoadError
40
+ end
41
+
42
+
43
+ module RGeo
44
+
45
+ # Current version of RGeo as a frozen string
46
+ VERSION_STRING = ::File.read(::File.dirname(__FILE__)+'/../../Version').strip.freeze
47
+
48
+ # Current version of RGeo as a Versionomy object, if the Versionomy gem
49
+ # is available.
50
+ VERSION = defined?(::Versionomy) ? ::Versionomy.parse(VERSION_STRING) : VERSION_STRING
51
+
52
+ end
@@ -0,0 +1,72 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Well-known representation for RGeo
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
+
40
+ # This module contains implementations of the OpenGIS well-known
41
+ # representations: the WKT (well-known text representation) and the
42
+ # WKB (well-known binary representation), as defined in the Simple
43
+ # Features Specification, version 1.1. Facilities are provided to
44
+ # serialize any geometry into one of these formats, and to parse a
45
+ # serialized string back into a geometry. Support is also provided for
46
+ # the common extensions to these formats-- notably, the EWKT and EWKB
47
+ # formats used by PostGIS.
48
+ #
49
+ # To serialize a geometry into WKT (well-known text) format, use
50
+ # the WKRep::WKTGenerator class.
51
+ #
52
+ # To serialize a geometry into WKB (well-known binary) format, use
53
+ # the WKRep::WKBGenerator class.
54
+ #
55
+ # To parse a string in WKT (well-known text) format back into a
56
+ # geometry object, use the WKRep::WKTParser class.
57
+ #
58
+ # To parse a byte string in WKB (well-known binary) format back into a
59
+ # geometry object, use the WKRep::WKBParser class.
60
+
61
+ module WKRep
62
+ end
63
+
64
+
65
+ end
66
+
67
+
68
+ # Implementation files
69
+ require 'rgeo/wkrep/wkt_parser'
70
+ require 'rgeo/wkrep/wkt_generator'
71
+ require 'rgeo/wkrep/wkb_parser'
72
+ require 'rgeo/wkrep/wkb_generator'
@@ -0,0 +1,267 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Well-known binary generator for RGeo
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 WKRep
40
+
41
+
42
+ # This class provides the functionality of serializing a geometry as
43
+ # WKB (well-known binary) format. You may also customize the
44
+ # serializer to generate PostGIS EWKB extensions to the output, or to
45
+ # follow the Simple Features Specification 1.2 extensions for Z and M
46
+ # coordinates.
47
+ #
48
+ # To use this class, create an instance with the desired settings and
49
+ # customizations, and call the generate method.
50
+ #
51
+ # === Configuration options
52
+ #
53
+ # The following options are recognized. These can be passed to the
54
+ # constructor, or set on the object afterwards.
55
+ #
56
+ # [<tt>:type_format</tt>]
57
+ # The format for type codes. Possible values are <tt>:wkb11</tt>,
58
+ # indicating SFS 1.1 WKB (i.e. no Z or M values); <tt>:ewkb</tt>,
59
+ # indicating the PostGIS EWKB extensions (i.e. Z and M presence
60
+ # flagged by the two high bits of the type code, and support for
61
+ # embedded SRID); or <tt>:wkb12</tt> (indicating SFS 1.2 WKB
62
+ # (i.e. Z and M presence flagged by adding 1000 and/or 2000 to
63
+ # the type code.) Default is <tt>:wkb11</tt>.
64
+ # [<tt>:emit_ewkb_srid</tt>]
65
+ # If true, embed the SRID in the toplevel geometry. Available only
66
+ # if <tt>:type_format</tt> is <tt>:ewkb</tt>. Default is false.
67
+ # [<tt>:hex_format</tt>]
68
+ # If true, output a hex string instead of a byte string.
69
+ # Default is false.
70
+ # [<tt>:little_endian</tt>]
71
+ # If true, output little endian (NDR) byte order. If false, output
72
+ # big endian (XDR), or network byte order. Default is false.
73
+
74
+ class WKBGenerator
75
+
76
+
77
+ # :stopdoc:
78
+ TYPE_CODES = {
79
+ Feature::Point => 1,
80
+ Feature::LineString => 2,
81
+ Feature::LinearRing => 2,
82
+ Feature::Line => 2,
83
+ Feature::Polygon => 3,
84
+ Feature::MultiPoint => 4,
85
+ Feature::MultiLineString => 5,
86
+ Feature::MultiPolygon => 6,
87
+ Feature::GeometryCollection => 7,
88
+ }
89
+ # :startdoc:
90
+
91
+
92
+ # Create and configure a WKB generator. See the WKBGenerator
93
+ # documentation for the options that can be passed.
94
+
95
+ def initialize(opts_={})
96
+ @type_format = opts_[:type_format] || :wkb11
97
+ @emit_ewkb_srid = opts_[:emit_ewkb_srid] ? true : false if @type_format == :ewkb
98
+ @hex_format = opts_[:hex_format] ? true : false
99
+ @little_endian = opts_[:little_endian] ? true : false
100
+ end
101
+
102
+
103
+ # Returns the format for type codes. See WKBGenerator for details.
104
+ def type_format
105
+ @type_format
106
+ end
107
+
108
+ # Sets the format for type codes. See WKBGenerator for details.
109
+ def type_format=(value_)
110
+ @type_format = value_
111
+ end
112
+
113
+ # Returns whether SRID is embedded. See WKBGenerator for details.
114
+ def emit_ewkb_srid?
115
+ @emit_ewkb_srid
116
+ end
117
+
118
+ # Sets whether SRID is embedded. Available only when the type_format
119
+ # is <tt>:ewkb</tt>. See WKBGenerator for details.
120
+ def emit_ewkb_srid=(value_)
121
+ @emit_ewkb_srid = @type_format == :ewkb && value_
122
+ end
123
+
124
+ # Returns whether output is converted to hex.
125
+ # See WKBGenerator for details.
126
+ def hex_format?
127
+ @hex_format
128
+ end
129
+
130
+ # Sets whether output is converted to hex.
131
+ # See WKBGenerator for details.
132
+ def hex_format=(value_)
133
+ @hex_format = value_ ? true : false
134
+ end
135
+
136
+ # Returns whether output is little-endian (NDR).
137
+ # See WKBGenerator for details.
138
+ def little_endian?
139
+ @little_endian
140
+ end
141
+
142
+ # Sets whether output is little-endian (NDR).
143
+ # See WKBGenerator for details.
144
+ def little_endian=(value_)
145
+ @little_endian = value_ ? true : false
146
+ end
147
+
148
+
149
+ # Generate and return the WKB format for the given geometry object,
150
+ # according to the current settings.
151
+
152
+ def generate(obj_)
153
+ factory_ = obj_.factory
154
+ if @type_format == :ewkb || @type_format == :wkb12
155
+ @cur_has_z = factory_.property(:has_z_coordinate)
156
+ @cur_has_m = factory_.property(:has_m_coordinate)
157
+ else
158
+ @cur_has_z = nil
159
+ @cur_has_m = nil
160
+ end
161
+ @cur_dims = 2 + (@cur_has_z ? 1 : 0) + (@cur_has_m ? 1 : 0)
162
+ _start_emitter
163
+ _generate_feature(obj_, true)
164
+ _finish_emitter
165
+ end
166
+
167
+
168
+ def _generate_feature(obj_, toplevel_=false) # :nodoc:
169
+ _emit_byte(@little_endian ? 1 : 0)
170
+ type_ = obj_.geometry_type
171
+ type_code_ = TYPE_CODES[type_]
172
+ unless type_code_
173
+ raise Error::ParseError, "Unrecognized Geometry Type: #{type_}"
174
+ end
175
+ emit_srid_ = false
176
+ if @type_format == :ewkb
177
+ type_code_ |= 0x80000000 if @cur_has_z
178
+ type_code_ |= 0x40000000 if @cur_has_m
179
+ if @emit_ewkb_srid && toplevel_
180
+ type_code_ |= 0x20000000
181
+ emit_srid_ = true
182
+ end
183
+ elsif @type_format == :wkb12
184
+ type_code_ += 1000 if @cur_has_z
185
+ type_code_ += 2000 if @cur_has_m
186
+ end
187
+ _emit_integer(type_code_)
188
+ _emit_integer(obj_.srid) if emit_srid_
189
+ if type_ == Feature::Point
190
+ _emit_doubles(_point_coords(obj_))
191
+ elsif type_.subtype_of?(Feature::LineString)
192
+ _emit_line_string_coords(obj_)
193
+ elsif type_ == Feature::Polygon
194
+ exterior_ring_ = obj_.exterior_ring
195
+ if exterior_ring_.is_empty?
196
+ _emit_integer(0)
197
+ else
198
+ _emit_integer(1 + obj_.num_interior_rings)
199
+ _emit_line_string_coords(exterior_ring_)
200
+ obj_.interior_rings.each{ |r_| _emit_line_string_coords(r_) }
201
+ end
202
+ elsif type_ == Feature::GeometryCollection
203
+ _emit_integer(obj_.num_geometries)
204
+ obj_.each{ |g_| _generate_feature(g_) }
205
+ elsif type_ == Feature::MultiPoint
206
+ _emit_integer(obj_.num_geometries)
207
+ obj_.each{ |g_| _generate_feature(g_) }
208
+ elsif type_ == Feature::MultiLineString
209
+ _emit_integer(obj_.num_geometries)
210
+ obj_.each{ |g_| _generate_feature(g_) }
211
+ elsif type_ == Feature::MultiPolygon
212
+ _emit_integer(obj_.num_geometries)
213
+ obj_.each{ |g_| _generate_feature(g_) }
214
+ end
215
+ end
216
+
217
+
218
+ def _point_coords(obj_, array_=[]) # :nodoc:
219
+ array_ << obj_.x
220
+ array_ << obj_.y
221
+ array_ << obj_.z if @cur_has_z
222
+ array_ << obj_.m if @cur_has_m
223
+ array_
224
+ end
225
+
226
+
227
+ def _emit_line_string_coords(obj_) # :nodoc:
228
+ array_ = []
229
+ obj_.points.each{ |p_| _point_coords(p_, array_) }
230
+ _emit_integer(obj_.num_points)
231
+ _emit_doubles(array_)
232
+ end
233
+
234
+
235
+ def _start_emitter # :nodoc:
236
+ @cur_array = []
237
+ end
238
+
239
+
240
+ def _emit_byte(value_) # :nodoc:
241
+ @cur_array << [value_].pack("C")
242
+ end
243
+
244
+
245
+ def _emit_integer(value_) # :nodoc:
246
+ @cur_array << [value_].pack(@little_endian ? 'V' : 'N')
247
+ end
248
+
249
+
250
+ def _emit_doubles(array_) # :nodoc:
251
+ @cur_array << array_.pack(@little_endian ? 'E*' : 'G*')
252
+ end
253
+
254
+
255
+ def _finish_emitter # :nodoc:
256
+ str_ = @cur_array.join
257
+ @cur_array = nil
258
+ @hex_format ? str_.unpack("H*")[0] : str_
259
+ end
260
+
261
+
262
+ end
263
+
264
+
265
+ end
266
+
267
+ end