rgeo 0.1.19 → 0.1.20
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +11 -0
- data/README.rdoc +23 -7
- data/Version +1 -1
- data/ext/geos_c_impl/factory.c +1 -1
- data/{lib/rgeo/geography/simple_mercator.rb → ext/proj4_c_impl/extconf.rb} +35 -30
- data/ext/proj4_c_impl/main.c +256 -0
- data/lib/rgeo.rb +54 -42
- data/lib/rgeo/active_record/arel_modifications.rb +2 -2
- data/lib/rgeo/active_record/base_modifications.rb +1 -1
- data/lib/rgeo/active_record/mysql_common.rb +12 -12
- data/lib/rgeo/all.rb +5 -4
- data/lib/rgeo/cartesian.rb +3 -2
- data/lib/rgeo/cartesian/bounding_box.rb +9 -9
- data/lib/rgeo/cartesian/factory.rb +30 -13
- data/lib/rgeo/cartesian/feature_classes.rb +33 -33
- data/lib/rgeo/cartesian/interface.rb +2 -2
- data/lib/rgeo/{geography/all.rb → coord_sys.rb} +22 -4
- data/lib/rgeo/coord_sys/proj4.rb +275 -0
- data/lib/rgeo/{errors.rb → error.rb} +5 -6
- data/lib/rgeo/{features.rb → feature.rb} +22 -20
- data/lib/rgeo/{features → feature}/curve.rb +6 -6
- data/lib/rgeo/{features → feature}/factory.rb +19 -3
- data/lib/rgeo/{features → feature}/factory_generator.rb +1 -1
- data/lib/rgeo/{features → feature}/geometry.rb +34 -34
- data/lib/rgeo/{features → feature}/geometry_collection.rb +4 -4
- data/lib/rgeo/{features → feature}/line.rb +1 -1
- data/lib/rgeo/{features → feature}/line_string.rb +4 -4
- data/lib/rgeo/{features → feature}/linear_ring.rb +1 -1
- data/lib/rgeo/{features → feature}/multi_curve.rb +3 -3
- data/lib/rgeo/{features → feature}/multi_line_string.rb +1 -1
- data/lib/rgeo/{features → feature}/multi_point.rb +1 -1
- data/lib/rgeo/{features → feature}/multi_polygon.rb +1 -1
- data/lib/rgeo/{features → feature}/multi_surface.rb +4 -4
- data/lib/rgeo/{features → feature}/point.rb +5 -5
- data/lib/rgeo/{features → feature}/polygon.rb +5 -5
- data/lib/rgeo/{features → feature}/surface.rb +4 -4
- data/lib/rgeo/feature/types.rb +301 -0
- data/lib/rgeo/geo_json.rb +1 -1
- data/lib/rgeo/geo_json/coder.rb +13 -13
- data/lib/rgeo/geo_json/interface.rb +2 -2
- data/lib/rgeo/geography.rb +33 -6
- data/lib/rgeo/geography/factory.rb +82 -39
- data/lib/rgeo/geography/interface.rb +135 -89
- data/lib/rgeo/geography/proj4_projector.rb +98 -0
- data/lib/rgeo/geography/projected_feature_classes.rb +213 -0
- data/lib/rgeo/geography/projected_feature_methods.rb +228 -0
- data/lib/rgeo/geography/projected_window.rb +7 -7
- data/lib/rgeo/geography/simple_mercator_projector.rb +133 -0
- data/lib/rgeo/geography/spherical_feature_classes.rb +212 -0
- data/lib/rgeo/geography/{simple_spherical/feature_methods.rb → spherical_feature_methods.rb} +39 -43
- data/lib/rgeo/geography/{simple_spherical/calculations.rb → spherical_math.rb} +7 -7
- data/lib/rgeo/geos.rb +1 -1
- data/lib/rgeo/geos/factory.rb +37 -19
- data/lib/rgeo/geos/impl_additions.rb +10 -11
- data/lib/rgeo/geos/interface.rb +1 -1
- data/lib/rgeo/geos/zm_factory.rb +15 -15
- data/lib/rgeo/geos/zm_impl.rb +10 -10
- data/lib/rgeo/{impl_helpers.rb → impl_helper.rb} +8 -8
- data/lib/rgeo/{impl_helpers → impl_helper}/basic_geometry_collection_methods.rb +13 -13
- data/lib/rgeo/{impl_helpers → impl_helper}/basic_geometry_methods.rb +2 -2
- data/lib/rgeo/{impl_helpers → impl_helper}/basic_line_string_methods.rb +13 -13
- data/lib/rgeo/{impl_helpers → impl_helper}/basic_point_methods.rb +5 -5
- data/lib/rgeo/{impl_helpers → impl_helper}/basic_polygon_methods.rb +6 -6
- data/lib/rgeo/{impl_helpers → impl_helper}/math.rb +1 -1
- data/lib/rgeo/shapefile.rb +1 -1
- data/lib/rgeo/shapefile/reader.rb +3 -3
- data/lib/rgeo/wkrep.rb +1 -1
- data/lib/rgeo/wkrep/wkb_generator.rb +17 -17
- data/lib/rgeo/wkrep/wkb_parser.rb +12 -12
- data/lib/rgeo/wkrep/wkt_generator.rb +8 -8
- data/lib/rgeo/wkrep/wkt_parser.rb +10 -10
- data/{tests → test}/active_record/readme.txt +0 -0
- data/{tests → test}/active_record/tc_mysqlspatial.rb +2 -2
- data/{tests → test}/common/geometry_collection_tests.rb +22 -22
- data/{tests → test}/common/line_string_tests.rb +20 -20
- data/{tests → test}/common/multi_line_string_tests.rb +19 -19
- data/{tests → test}/common/multi_point_tests.rb +17 -17
- data/{tests → test}/common/multi_polygon_tests.rb +16 -16
- data/{tests → test}/common/point_tests.rb +9 -9
- data/{tests → test}/common/polygon_tests.rb +9 -9
- data/test/coord_sys/tc_proj4.rb +111 -0
- data/{tests → test}/geos/tc_factory.rb +1 -1
- data/{tests → test}/geos/tc_geometry_collection.rb +1 -1
- data/{tests → test}/geos/tc_line_string.rb +0 -0
- data/{tests → test}/geos/tc_misc.rb +1 -1
- data/{tests → test}/geos/tc_multi_line_string.rb +1 -1
- data/{tests → test}/geos/tc_multi_point.rb +1 -1
- data/{tests → test}/geos/tc_multi_polygon.rb +1 -1
- data/{tests → test}/geos/tc_point.rb +1 -1
- data/{tests → test}/geos/tc_polygon.rb +0 -0
- data/{tests → test}/geos/tc_zmfactory.rb +2 -2
- data/test/projected_geography/tc_geometry_collection.rb +62 -0
- data/test/projected_geography/tc_line_string.rb +62 -0
- data/test/projected_geography/tc_multi_line_string.rb +62 -0
- data/test/projected_geography/tc_multi_point.rb +62 -0
- data/test/projected_geography/tc_multi_polygon.rb +63 -0
- data/test/projected_geography/tc_point.rb +93 -0
- data/test/projected_geography/tc_polygon.rb +62 -0
- data/{tests → test}/shapefile/shapelib_testcases/readme.txt +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test.dbf +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test0.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test0.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test1.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test1.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test10.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test10.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test11.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test11.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test12.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test12.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test13.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test13.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test2.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test2.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test3.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test3.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test4.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test4.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test5.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test5.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test6.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test6.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test7.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test7.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test8.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test8.shx +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test9.shp +0 -0
- data/{tests → test}/shapefile/shapelib_testcases/test9.shx +0 -0
- data/{tests → test}/shapefile/tc_shapelib_tests.rb +17 -17
- data/{tests → test}/simple_cartesian/tc_calculations.rb +0 -0
- data/{tests → test}/simple_cartesian/tc_geometry_collection.rb +0 -0
- data/{tests → test}/simple_cartesian/tc_line_string.rb +0 -0
- data/{tests → test}/simple_cartesian/tc_multi_line_string.rb +0 -0
- data/{tests → test}/simple_cartesian/tc_multi_point.rb +0 -0
- data/{tests → test}/simple_cartesian/tc_multi_polygon.rb +0 -0
- data/{tests → test}/simple_cartesian/tc_point.rb +0 -0
- data/{tests → test}/simple_cartesian/tc_polygon.rb +0 -0
- data/{tests → test}/simple_mercator/tc_geometry_collection.rb +1 -1
- data/{tests → test}/simple_mercator/tc_line_string.rb +0 -0
- data/{tests → test}/simple_mercator/tc_multi_line_string.rb +1 -1
- data/{tests → test}/simple_mercator/tc_multi_point.rb +1 -1
- data/{tests → test}/simple_mercator/tc_multi_polygon.rb +1 -1
- data/{tests → test}/simple_mercator/tc_point.rb +2 -2
- data/{tests → test}/simple_mercator/tc_polygon.rb +0 -0
- data/{tests → test}/simple_mercator/tc_window.rb +1 -1
- data/test/spherical_geography/tc_calculations.rb +203 -0
- data/{tests/simple_spherical → test/spherical_geography}/tc_geometry_collection.rb +2 -2
- data/{tests/simple_spherical → test/spherical_geography}/tc_line_string.rb +2 -2
- data/{tests/simple_spherical → test/spherical_geography}/tc_multi_line_string.rb +2 -2
- data/{tests/simple_spherical → test/spherical_geography}/tc_multi_point.rb +2 -2
- data/{tests/simple_spherical → test/spherical_geography}/tc_multi_polygon.rb +3 -3
- data/{tests/simple_spherical → test/spherical_geography}/tc_point.rb +7 -7
- data/{tests/simple_spherical → test/spherical_geography}/tc_polygon.rb +2 -2
- data/{tests → test}/tc_cartesian_analysis.rb +0 -0
- data/{tests → test}/tc_geojson.rb +0 -0
- data/{tests → test}/tc_oneoff.rb +2 -1
- data/{tests → test}/wkrep/tc_wkb_generator.rb +0 -0
- data/{tests → test}/wkrep/tc_wkb_parser.rb +32 -32
- data/{tests → test}/wkrep/tc_wkt_generator.rb +0 -0
- data/{tests → test}/wkrep/tc_wkt_parser.rb +46 -46
- metadata +183 -164
- data/lib/rgeo/features/types.rb +0 -272
- data/lib/rgeo/geography/simple_mercator/feature_classes.rb +0 -279
- data/lib/rgeo/geography/simple_mercator/feature_methods.rb +0 -278
- data/lib/rgeo/geography/simple_mercator/projector.rb +0 -112
- data/lib/rgeo/geography/simple_spherical.rb +0 -68
- data/lib/rgeo/geography/simple_spherical/feature_classes.rb +0 -216
- data/tests/simple_spherical/tc_calculations.rb +0 -203
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
module RGeo
|
38
38
|
|
39
|
-
module
|
39
|
+
module Feature
|
40
40
|
|
41
41
|
|
42
42
|
# == SFS 1.1 Description
|
@@ -73,7 +73,7 @@ module RGeo
|
|
73
73
|
# Returns a floating-point scalar value.
|
74
74
|
|
75
75
|
def x
|
76
|
-
raise
|
76
|
+
raise Error::UnsupportedCapability, "Method Point#x not defined."
|
77
77
|
end
|
78
78
|
|
79
79
|
|
@@ -86,7 +86,7 @@ module RGeo
|
|
86
86
|
# Returns a floating-point scalar value.
|
87
87
|
|
88
88
|
def y
|
89
|
-
raise
|
89
|
+
raise Error::UnsupportedCapability, "Method Point#y not defined."
|
90
90
|
end
|
91
91
|
|
92
92
|
|
@@ -97,7 +97,7 @@ module RGeo
|
|
97
97
|
# not support the <tt>z_coordinate</tt> capability.
|
98
98
|
|
99
99
|
def z
|
100
|
-
raise
|
100
|
+
raise Error::UnsupportedCapability, "Method Point#z not defined."
|
101
101
|
end
|
102
102
|
|
103
103
|
|
@@ -108,7 +108,7 @@ module RGeo
|
|
108
108
|
# not support the <tt>m_coordinate</tt> capability.
|
109
109
|
|
110
110
|
def m
|
111
|
-
raise
|
111
|
+
raise Error::UnsupportedCapability, "Method Point#m not defined."
|
112
112
|
end
|
113
113
|
|
114
114
|
|
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
module RGeo
|
38
38
|
|
39
|
-
module
|
39
|
+
module Feature
|
40
40
|
|
41
41
|
|
42
42
|
# == SFS 1.1 Description
|
@@ -94,7 +94,7 @@ module RGeo
|
|
94
94
|
# Returns an object that supports the LinearRing interface.
|
95
95
|
|
96
96
|
def exterior_ring
|
97
|
-
raise
|
97
|
+
raise Error::UnsupportedCapability, "Method Polygon#exterior_ring not defined."
|
98
98
|
end
|
99
99
|
|
100
100
|
|
@@ -107,7 +107,7 @@ module RGeo
|
|
107
107
|
# Returns an integer.
|
108
108
|
|
109
109
|
def num_interior_rings
|
110
|
-
raise
|
110
|
+
raise Error::UnsupportedCapability, "Method Polygon#num_interior_rings not defined."
|
111
111
|
end
|
112
112
|
|
113
113
|
|
@@ -121,7 +121,7 @@ module RGeo
|
|
121
121
|
# if the given n is out of range.
|
122
122
|
|
123
123
|
def interior_ring_n(n_)
|
124
|
-
raise
|
124
|
+
raise Error::UnsupportedCapability, "Method Polygon#interior_ring_n not defined."
|
125
125
|
end
|
126
126
|
|
127
127
|
|
@@ -129,7 +129,7 @@ module RGeo
|
|
129
129
|
# that support the LinearRing interface.
|
130
130
|
|
131
131
|
def interior_rings
|
132
|
-
raise
|
132
|
+
raise Error::UnsupportedCapability, "Method Polygon#interior_rings not defined."
|
133
133
|
end
|
134
134
|
|
135
135
|
|
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
module RGeo
|
38
38
|
|
39
|
-
module
|
39
|
+
module Feature
|
40
40
|
|
41
41
|
|
42
42
|
# == SFS 1.1 Description
|
@@ -83,7 +83,7 @@ module RGeo
|
|
83
83
|
# Returns a floating-point scalar value.
|
84
84
|
|
85
85
|
def area
|
86
|
-
raise
|
86
|
+
raise Error::UnsupportedCapability, "Method Surface#area not defined."
|
87
87
|
end
|
88
88
|
|
89
89
|
|
@@ -97,7 +97,7 @@ module RGeo
|
|
97
97
|
# Returns an object that supports the Point interface.
|
98
98
|
|
99
99
|
def centroid
|
100
|
-
raise
|
100
|
+
raise Error::UnsupportedCapability, "Method Surface#centroid not defined."
|
101
101
|
end
|
102
102
|
|
103
103
|
|
@@ -110,7 +110,7 @@ module RGeo
|
|
110
110
|
# Returns an object that supports the Point interface.
|
111
111
|
|
112
112
|
def point_on_surface
|
113
|
-
raise
|
113
|
+
raise Error::UnsupportedCapability, "Method Surface#point_on_surface not defined."
|
114
114
|
end
|
115
115
|
|
116
116
|
|
@@ -0,0 +1,301 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Feature type management and casting
|
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 Feature
|
40
|
+
|
41
|
+
|
42
|
+
# These methods are available as module methods (not instance methods)
|
43
|
+
# of the various feature types.
|
44
|
+
# For example, you may determine whether a feature object is a
|
45
|
+
# point by calling:
|
46
|
+
#
|
47
|
+
# ::RGeo::Feature::Point.check_type(object)
|
48
|
+
#
|
49
|
+
# A corresponding === operator is provided so you can use the type
|
50
|
+
# modules in a case-when clause.
|
51
|
+
#
|
52
|
+
# You may also use the presence of this module to determine whether
|
53
|
+
# a particular object is a feature type:
|
54
|
+
#
|
55
|
+
# object.kind_of?(::RGeo::Feature::Type)
|
56
|
+
|
57
|
+
module Type
|
58
|
+
|
59
|
+
|
60
|
+
# All geometry implementations MUST include this submodule.
|
61
|
+
# This serves as a marker that may be used to test an object for
|
62
|
+
# feature-ness.
|
63
|
+
|
64
|
+
module Instance
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Returns true if the given object is this type or a subtype
|
69
|
+
# thereof, or if it is a feature object whose geometry_type is
|
70
|
+
# this type or a subtype thereof.
|
71
|
+
#
|
72
|
+
# Note that feature objects need not actually include this module.
|
73
|
+
|
74
|
+
def check_type(rhs_)
|
75
|
+
rhs_ = rhs_.geometry_type if rhs_.kind_of?(Instance)
|
76
|
+
rhs_.kind_of?(Type) && (rhs_ == self || rhs_.include?(self))
|
77
|
+
end
|
78
|
+
alias_method :===, :check_type
|
79
|
+
|
80
|
+
|
81
|
+
# Returns true if this type is the same type or a subtype of the
|
82
|
+
# given type.
|
83
|
+
|
84
|
+
def subtype_of?(type_)
|
85
|
+
self == type_ || self.include?(type_)
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
# Returns the OpenGIS type name of this type.
|
90
|
+
|
91
|
+
def type_name
|
92
|
+
self.name.sub('RGeo::Feature::', '')
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
class << self
|
100
|
+
|
101
|
+
|
102
|
+
# Cast the given object according to the given parameters, if
|
103
|
+
# possible, and return the resulting object. If the requested cast
|
104
|
+
# is not possible, nil is returned.
|
105
|
+
#
|
106
|
+
# Parameters may be provided as a hash, or as separate arguments.
|
107
|
+
# Hash keys are as follows:
|
108
|
+
#
|
109
|
+
# <tt>:factory</tt>::
|
110
|
+
# Set the factory to the given factory. If this argument is not
|
111
|
+
# given, the original object's factory is kept.
|
112
|
+
# <tt>:type</tt>::
|
113
|
+
# Cast to the given type, which must be a module in the
|
114
|
+
# RGeo::Feature namespace. If this argument is not given, the
|
115
|
+
# result keeps the same type as the original.
|
116
|
+
# <tt>:project</tt>::
|
117
|
+
# If this is set to true, and both the original and new factories
|
118
|
+
# support proj4 projections, then the cast will also cause the
|
119
|
+
# coordinates to be transformed between those two projections.
|
120
|
+
# If set to false, the coordinates are not modified. Default is
|
121
|
+
# false.
|
122
|
+
# <tt>:keep_subtype</tt>::
|
123
|
+
# Value must be a boolean indicating whether to keep the subtype
|
124
|
+
# of the original. If set to false, casting to a particular type
|
125
|
+
# always casts strictly to that type, even if the old type is a
|
126
|
+
# subtype of the new type. If set to true, the cast retains the
|
127
|
+
# subtype in that case. For example, casting a LinearRing to a
|
128
|
+
# LineString will normally yield a LineString, even though
|
129
|
+
# LinearRing is already a more specific subtype. If you set this
|
130
|
+
# value to true, the casted object will remain a LinearRing.
|
131
|
+
# Default is false.
|
132
|
+
# <tt>:force_new</tt>::
|
133
|
+
# Always return a newly-created object, even if neither the type
|
134
|
+
# nor factory is modified. Normally, if this is set to false, and
|
135
|
+
# a cast is not set to modify either the factory or type, the
|
136
|
+
# original object itself is returned. Setting this flag to true
|
137
|
+
# causes cast to return a clone in that case. Default is false.
|
138
|
+
#
|
139
|
+
# You may also pass the new factory, the new type, and the flags
|
140
|
+
# as separate arguments. In this case, the flag names must be
|
141
|
+
# passed as symbols, and their effect is the same as setting their
|
142
|
+
# values to true. You can even combine separate arguments and hash
|
143
|
+
# arguments. For example, the following three calls are equivalent:
|
144
|
+
#
|
145
|
+
# Feature.cast(geom, :type => Feature::Point, :project => true)
|
146
|
+
# Feature.cast(geom, Feature::Point, :project => true)
|
147
|
+
# Feature.cast(geom, Feature::Point, :project)
|
148
|
+
#
|
149
|
+
# RGeo provides a default casting algorithm. Individual feature
|
150
|
+
# implementation factories may override this and customize the
|
151
|
+
# casting behavior by defining the override_cast method. See
|
152
|
+
# ::RGeo::Feature::Factory#override_cast for more details.
|
153
|
+
|
154
|
+
def cast(obj_, *params_)
|
155
|
+
# Interpret params
|
156
|
+
factory_ = obj_.factory
|
157
|
+
type_ = obj_.geometry_type
|
158
|
+
opts_ = {}
|
159
|
+
params_.each do |param_|
|
160
|
+
case param_
|
161
|
+
when Factory::Instance
|
162
|
+
opts_[:factory] = param_
|
163
|
+
when Type
|
164
|
+
opts_[:type] = param_
|
165
|
+
when ::Symbol
|
166
|
+
opts_[param_] = true
|
167
|
+
when ::Hash
|
168
|
+
opts_.merge!(param_)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
force_new_ = opts_[:force_new]
|
172
|
+
keep_subtype_ = opts_[:keep_subtype]
|
173
|
+
project_ = opts_[:project]
|
174
|
+
nfactory_ = opts_.delete(:factory) || factory_
|
175
|
+
ntype_ = opts_.delete(:type) || type_
|
176
|
+
|
177
|
+
# Let the factory override
|
178
|
+
if nfactory_.respond_to?(:override_cast)
|
179
|
+
override_ = nfactory_.override_cast(obj_, ntype_, opts_)
|
180
|
+
return override_ unless override_ == false
|
181
|
+
end
|
182
|
+
|
183
|
+
# Default algorithm
|
184
|
+
ntype_ = type_ if keep_subtype_ && type_.include?(ntype_)
|
185
|
+
if ntype_ == type_
|
186
|
+
# Types are the same
|
187
|
+
if nfactory_ == factory_
|
188
|
+
force_new_ ? obj_.dup : obj_
|
189
|
+
else
|
190
|
+
if type_ == Point
|
191
|
+
proj_ = nproj_ = nil
|
192
|
+
if project_ && factory_.has_capability?(:proj4) && nfactory_.has_capability?(:proj4)
|
193
|
+
proj_ = factory_.proj4
|
194
|
+
nproj_ = nfactory_.proj4
|
195
|
+
end
|
196
|
+
hasz_ = factory_.has_capability?(:z_coordinate)
|
197
|
+
nhasz_ = nfactory_.has_capability?(:z_coordinate)
|
198
|
+
if proj_ && nproj_
|
199
|
+
coords_ = CoordSys::Proj4.transform_coords(proj_, nproj_, obj_.x, obj_.y, hasz_ ? obj_.z : nil)
|
200
|
+
coords_ << (hasz_ ? obj_.z : 0.0) if nhasz_ && coords_.size < 3
|
201
|
+
else
|
202
|
+
coords_ = [obj_.x, obj_.y]
|
203
|
+
coords_ << (hasz_ ? obj_.z : 0.0) if nhasz_
|
204
|
+
end
|
205
|
+
coords_ << (factory_.has_capability?(:m_coordinate) ? obj_.m : 0.0) if nfactory_.has_capability?(:m_coordinate)
|
206
|
+
nfactory_.point(*coords_)
|
207
|
+
elsif type_ == Line
|
208
|
+
nfactory_.line(cast(obj_.start_point, nfactory_, opts_), cast(obj_.end_point, nfactory_, opts_))
|
209
|
+
elsif type_ == LinearRing
|
210
|
+
nfactory_.linear_ring(obj_.points.map{ |p_| cast(p_, nfactory_, opts_) })
|
211
|
+
elsif type_ == LineString
|
212
|
+
nfactory_.line_string(obj_.points.map{ |p_| cast(p_, nfactory_, opts_) })
|
213
|
+
elsif type_ == Polygon
|
214
|
+
nfactory_.polygon(cast(obj_.exterior_ring, nfactory_, opts_),
|
215
|
+
obj_.interior_rings.map{ |r_| cast(r_, nfactory_, opts_) })
|
216
|
+
elsif type_ == MultiPoint
|
217
|
+
nfactory_.multi_point(obj_.map{ |g_| cast(g_, nfactory_, opts_) })
|
218
|
+
elsif type_ == MultiLineString
|
219
|
+
nfactory_.multi_line_string(obj_.map{ |g_| cast(g_, nfactory_, opts_) })
|
220
|
+
elsif type_ == MultiPolygon
|
221
|
+
nfactory_.multi_polygon(obj_.map{ |g_| cast(g_, nfactory_, opts_) })
|
222
|
+
elsif type_ == GeometryCollection
|
223
|
+
nfactory_.collection(obj_.map{ |g_| cast(g_, nfactory_, opts_) })
|
224
|
+
else
|
225
|
+
nil
|
226
|
+
end
|
227
|
+
end
|
228
|
+
else
|
229
|
+
# Types are different
|
230
|
+
if ntype_ == Point && (type_ == MultiPoint || type_ == GeometryCollection) ||
|
231
|
+
(ntype_ == Line || ntype_ == LineString || ntype_ == LinearRing) && (type_ == MultiLineString || type_ == GeometryCollection) ||
|
232
|
+
ntype_ == Polygon && (type_ == MultiPolygon || type_ == GeometryCollection)
|
233
|
+
then
|
234
|
+
if obj_.num_geometries == 1
|
235
|
+
cast(obj_.geometry_n(0), nfactory_, ntype_, opts_)
|
236
|
+
else
|
237
|
+
nil
|
238
|
+
end
|
239
|
+
elsif ntype_ == Point
|
240
|
+
nil
|
241
|
+
elsif ntype_ == Line
|
242
|
+
if type_ == LineString && obj_.num_points == 2
|
243
|
+
nfactory_.line(cast(obj_.point_n(0), nfactory_, opts_), cast(obj_.point_n(1), nfactory_, opts_))
|
244
|
+
else
|
245
|
+
nil
|
246
|
+
end
|
247
|
+
elsif ntype_ == LinearRing
|
248
|
+
if type_ == LineString
|
249
|
+
nfactory_.linear_ring(obj_.points.map{ |p_| cast(p_, nfactory_, opts_) })
|
250
|
+
else
|
251
|
+
nil
|
252
|
+
end
|
253
|
+
elsif ntype_ == LineString
|
254
|
+
if type_ == Line || type_ == LinearRing
|
255
|
+
nfactory_.line_string(obj_.points.map{ |p_| cast(p_, nfactory_, opts_) })
|
256
|
+
else
|
257
|
+
nil
|
258
|
+
end
|
259
|
+
elsif ntype_ == MultiPoint
|
260
|
+
if type_ == Point
|
261
|
+
nfactory_.multi_point([cast(obj_, nfactory_, opts_)])
|
262
|
+
elsif type_ == GeometryCollection
|
263
|
+
nfactory_.multi_point(obj_.map{ |g_| cast(p_, nfactory_, opts_) })
|
264
|
+
else
|
265
|
+
nil
|
266
|
+
end
|
267
|
+
elsif ntype_ == MultiLineString
|
268
|
+
if type_ == Line || type_ == LinearRing || type_ == LineString
|
269
|
+
nfactory_.multi_line_string([cast(obj_, nfactory_, opts_)])
|
270
|
+
elsif type_ == GeometryCollection
|
271
|
+
nfactory_.multi_line_string(obj_.map{ |g_| cast(p_, nfactory_, opts_) })
|
272
|
+
else
|
273
|
+
nil
|
274
|
+
end
|
275
|
+
elsif ntype_ == MultiPolygon
|
276
|
+
if type_ == Polygon
|
277
|
+
nfactory_.multi_polygon([cast(obj_, nfactory_, opts_)])
|
278
|
+
elsif type_ == GeometryCollection
|
279
|
+
nfactory_.multi_polygon(obj_.map{ |g_| cast(p_, nfactory_, opts_) })
|
280
|
+
else
|
281
|
+
nil
|
282
|
+
end
|
283
|
+
elsif ntype_ == GeometryCollection
|
284
|
+
if type_ == MultiPoint || type_ == MultiLineString || type_ == MultiPolygon
|
285
|
+
nfactory_.collection(obj_.map{ |g_| cast(p_, nfactory_, opts_) })
|
286
|
+
else
|
287
|
+
nfactory_.collection([cast(obj_, nfactory_, opts_)])
|
288
|
+
end
|
289
|
+
else
|
290
|
+
nil
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
|
296
|
+
end
|
297
|
+
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
data/lib/rgeo/geo_json.rb
CHANGED
data/lib/rgeo/geo_json/coder.rb
CHANGED
@@ -40,7 +40,7 @@ module RGeo
|
|
40
40
|
|
41
41
|
|
42
42
|
# This object encapsulates encoding and decoding settings (principally
|
43
|
-
# the RGeo::
|
43
|
+
# the RGeo::Feature::Factory and the RGeo::GeoJSON::EntityFactory to
|
44
44
|
# be used) so that you can encode and decode without specifying those
|
45
45
|
# settings every time.
|
46
46
|
|
@@ -95,7 +95,7 @@ module RGeo
|
|
95
95
|
if @@json_available
|
96
96
|
@json_parser = ::Proc.new{ |str_| ::JSON.parse(str_) }
|
97
97
|
else
|
98
|
-
raise
|
98
|
+
raise Error::RGeoError, "JSON library is not available. You may need to install the 'json' gem."
|
99
99
|
end
|
100
100
|
when :yajl
|
101
101
|
if @@yajl_available.nil?
|
@@ -109,7 +109,7 @@ module RGeo
|
|
109
109
|
if @@yajl_available
|
110
110
|
@json_parser = ::Proc.new{ |str_| ::Yajl::Parser.new.parse(str_) }
|
111
111
|
else
|
112
|
-
raise
|
112
|
+
raise Error::RGeoError, "Yajl library is not available. You may need to install the 'yajl' gem."
|
113
113
|
end
|
114
114
|
when :active_support
|
115
115
|
if @@activesupport_available.nil?
|
@@ -123,7 +123,7 @@ module RGeo
|
|
123
123
|
if @@activesupport_available
|
124
124
|
@json_parser = ::Proc.new{ |str_| ::ActiveSupport::JSON.decode(str_) }
|
125
125
|
else
|
126
|
-
raise
|
126
|
+
raise Error::RGeoError, "ActiveSupport::JSON library is not available. You may need to install the 'activesupport' gem."
|
127
127
|
end
|
128
128
|
when ::Proc, nil
|
129
129
|
# Leave as is
|
@@ -137,7 +137,7 @@ module RGeo
|
|
137
137
|
|
138
138
|
|
139
139
|
# Encode the given object as GeoJSON. The object may be one of the
|
140
|
-
# geometry objects specified in RGeo::
|
140
|
+
# geometry objects specified in RGeo::Feature, or an appropriate
|
141
141
|
# GeoJSON wrapper entity supported by this coder's entity factory.
|
142
142
|
#
|
143
143
|
# This method returns a JSON object (i.e. a hash). In order to
|
@@ -193,7 +193,7 @@ module RGeo
|
|
193
193
|
end
|
194
194
|
|
195
195
|
|
196
|
-
# Returns the RGeo::
|
196
|
+
# Returns the RGeo::Feature::Factory used to generate geometry objects.
|
197
197
|
|
198
198
|
def geo_factory
|
199
199
|
@geo_factory
|
@@ -237,37 +237,37 @@ module RGeo
|
|
237
237
|
end
|
238
238
|
end
|
239
239
|
case object_
|
240
|
-
when
|
240
|
+
when ::RGeo::Feature::Point
|
241
241
|
{
|
242
242
|
'type' => 'Point',
|
243
243
|
'coordinates' => point_encoder_.call(object_),
|
244
244
|
}
|
245
|
-
when
|
245
|
+
when ::RGeo::Feature::LineString
|
246
246
|
{
|
247
247
|
'type' => 'LineString',
|
248
248
|
'coordinates' => object_.points.map(&point_encoder_),
|
249
249
|
}
|
250
|
-
when
|
250
|
+
when ::RGeo::Feature::Polygon
|
251
251
|
{
|
252
252
|
'type' => 'Polygon',
|
253
253
|
'coordinates' => [object_.exterior_ring.points.map(&point_encoder_)] + object_.interior_rings.map{ |r_| r_.points.map(&point_encoder_) }
|
254
254
|
}
|
255
|
-
when
|
255
|
+
when ::RGeo::Feature::MultiPoint
|
256
256
|
{
|
257
257
|
'type' => 'MultiPoint',
|
258
258
|
'coordinates' => object_.map(&point_encoder_),
|
259
259
|
}
|
260
|
-
when
|
260
|
+
when ::RGeo::Feature::MultiLineString
|
261
261
|
{
|
262
262
|
'type' => 'MultiLineString',
|
263
263
|
'coordinates' => object_.map{ |ls_| ls_.points.map(&point_encoder_) },
|
264
264
|
}
|
265
|
-
when
|
265
|
+
when ::RGeo::Feature::MultiPolygon
|
266
266
|
{
|
267
267
|
'type' => 'MultiPolygon',
|
268
268
|
'coordinates' => object_.map{ |poly_| [poly_.exterior_ring.points.map(&point_encoder_)] + poly_.interior_rings.map{ |r_| r_.points.map(&point_encoder_) } },
|
269
269
|
}
|
270
|
-
when
|
270
|
+
when ::RGeo::Feature::GeometryCollection
|
271
271
|
{
|
272
272
|
'type' => 'GeometryCollection',
|
273
273
|
'geometries' => object_.map{ |geom_| _encode_geometry(geom_, point_encoder_) },
|