rgeo 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
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
+ class ActiveRecordTable
45
+
46
+ @@class_counter = 0
47
+
48
+
49
+ def initialize(opts_={})
50
+ @cache = opts_[:cache] ? {} : nil
51
+ @ar_class = opts_[:ar_class]
52
+ unless @ar_class
53
+ ar_base_class_ = opts_[:ar_base_class] || ::ActiveRecord::Base
54
+ @ar_class = ::Class.new(ar_base_class_)
55
+ self.class.const_set("Klass#{@@class_counter}", @ar_class)
56
+ @@class_counter += 1
57
+ @ar_class.class_eval do
58
+ set_table_name(opts_[:table_name] || 'spatial_ref_sys')
59
+ end
60
+ end
61
+ connection_ = @ar_class.connection
62
+ if connection_.respond_to?(:srs_database_columns)
63
+ opts_ = connection_.srs_database_columns.merge(opts_)
64
+ end
65
+ @srid_column = opts_[:srid_column] || 'srid'
66
+ @auth_name_column = opts_[:auth_name_column]
67
+ @auth_srid_column = opts_[:auth_srid_column]
68
+ @name_column = opts_[:name_column]
69
+ @description_column = opts_[:description_column]
70
+ @srtext_column = opts_[:srtext_column]
71
+ @proj4text_column = opts_[:proj4text_column]
72
+ end
73
+
74
+
75
+ def get(ident_)
76
+ ident_ = ident_.to_i
77
+ return @cache[ident_] if @cache && @cache.include?(ident_)
78
+ obj_ = @ar_class.where(@srid_column => ident_).first
79
+ unless obj_
80
+ @cache[ident_] = nil if @cache
81
+ return nil
82
+ end
83
+ auth_name_ = @auth_name_column ? obj_[@auth_name_column] : nil
84
+ auth_srid_ = @auth_srid_column ? obj_[@auth_srid_column] : nil
85
+ name_ = @name_column ? obj_[@name_column] : nil
86
+ description_ = @description_column ? obj_[@description_column] : nil
87
+ coord_sys_ = proj4_ = nil
88
+ if @srtext_column
89
+ coord_sys_ = CS.create_from_wkt(obj_[@srtext_column]) rescue nil
90
+ end
91
+ if @proj4text_column && Proj4.supported?
92
+ proj4_ = Proj4.create(obj_[@proj4text_column].strip) rescue nil
93
+ end
94
+ result_ = Entry.new(ident_, :authority => auth_name_, :authority_code => auth_srid_, :name => name_, :description => description_, :coord_sys => coord_sys_, :proj4 => proj4_)
95
+ @cache[ident_] = result_ if @cache
96
+ result_
97
+ end
98
+
99
+
100
+ def clear_cache
101
+ @cache.clear if @cache
102
+ end
103
+
104
+
105
+ end
106
+
107
+
108
+ end
109
+
110
+ end
111
+
112
+ end
@@ -0,0 +1,112 @@
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
+ module Interface
55
+
56
+
57
+ def get(ident_)
58
+ nil
59
+ end
60
+
61
+
62
+ def clear_cache
63
+ nil
64
+ end
65
+
66
+
67
+ end
68
+
69
+
70
+ class Entry
71
+
72
+ def initialize(ident_, data_={})
73
+ @identifier = ident_
74
+ @authority = data_[:authority]
75
+ @authority_code = data_[:authority_code]
76
+ @name = data_[:name]
77
+ @description = data_[:description]
78
+ @coord_sys = data_[:coord_sys]
79
+ if @coord_sys.kind_of?(::String)
80
+ @coord_sys = CS.create_from_wkt(@coord_sys)
81
+ end
82
+ @proj4 = data_[:proj4]
83
+ if Proj4.supported?
84
+ if @proj4.kind_of?(::String) || @proj4.kind_of?(::Hash)
85
+ @proj4 = Proj4.create(@proj4)
86
+ end
87
+ else
88
+ @proj4 = nil
89
+ end
90
+ if @coord_sys
91
+ @name = @coord_sys.name unless @name
92
+ @authority = @coord_sys.authority unless @authority
93
+ @authority_code = @coord_sys.authority unless @authority_code
94
+ end
95
+ end
96
+
97
+ attr_reader :identifier
98
+ attr_reader :authority
99
+ attr_reader :authority_code
100
+ attr_reader :name
101
+ attr_reader :description
102
+ attr_reader :coord_sys
103
+ attr_reader :proj4
104
+
105
+ end
106
+
107
+
108
+ end
109
+
110
+ end
111
+
112
+ end
@@ -0,0 +1,143 @@
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
+ class Proj4Data
45
+
46
+
47
+ def initialize(path_, opts_={})
48
+ dir_ = nil
49
+ if opts_.include?(:dir)
50
+ dir_ = opts_[:dir]
51
+ else
52
+ ['/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_|
53
+ if ::File.directory?(d_) && ::File.readable?(d_)
54
+ dir_ = d_
55
+ break
56
+ end
57
+ end
58
+ end
59
+ @path = dir_ ? "#{dir_}/#{path_}" : path_
60
+ @cache = opts_[:cache] ? {} : nil
61
+ @authority = opts_[:authority]
62
+ @populate_state = @cache && opts_[:read_all] ? 1 : 0
63
+ end
64
+
65
+
66
+ def get(ident_)
67
+ ident_ = ident_.to_s
68
+ return @cache[ident_] if @cache && @cache.include?(ident_)
69
+ result_ = nil
70
+ if @populate_state == 0
71
+ data_ = _search_file(ident_)
72
+ unless data_
73
+ @cache[ident_] = nil if @cache
74
+ return nil
75
+ end
76
+ result_ = Entry.new(ident_, :authority => @authority, :authority_code => @authority ? ident_ : nil, :name => data_[1], :proj4 => data_[2])
77
+ @cache[ident_] = result_ if @cache
78
+ elsif @populate_state == 1
79
+ _search_file(nil) do |id_, name_, text_|
80
+ @cache[id_] = Entry.new(id_, :authority => @authority, :authority_code => @authority ? id_ : nil, :name => name_, :proj4 => text_)
81
+ result_ = @cache[id_] if id_ == ident_
82
+ end
83
+ @populate_state = 2
84
+ end
85
+ result_
86
+ end
87
+
88
+
89
+ def clear_cache
90
+ @cache.clear if @cache
91
+ @populate_state = 1 if @populate_state == 2
92
+ end
93
+
94
+
95
+ def _search_file(ident_) # :nodoc:
96
+ ::File.open(@path) do |file_|
97
+ cur_name_ = nil
98
+ cur_ident_ = nil
99
+ cur_text_ = nil
100
+ file_.each do |line_|
101
+ line_.strip!
102
+ if (comment_delim_ = line_.index('#'))
103
+ cur_name_ = line_[comment_delim_+1..-1].strip
104
+ line_ = line_[0..comment_delim_-1].strip
105
+ end
106
+ unless cur_ident_
107
+ if line_ =~ /^<(\w+)>(.*)/
108
+ cur_ident_ = $1
109
+ cur_text_ = []
110
+ line_ = $2.strip
111
+ end
112
+ end
113
+ if cur_ident_
114
+ if line_[-2..-1] == '<>'
115
+ cur_text_ << line_[0..-3].strip
116
+ cur_text_ = cur_text_.join(' ')
117
+ if block_given?
118
+ yield(ident_, cur_name_, cur_text_)
119
+ end
120
+ if cur_ident_ == ident_
121
+ return [ident_, cur_name_, cur_text_]
122
+ end
123
+ cur_ident_ = nil
124
+ cur_name_ = nil
125
+ cur_text_ = nil
126
+ else
127
+ cur_text_ << line_
128
+ end
129
+ end
130
+ end
131
+ end
132
+ nil
133
+ end
134
+
135
+
136
+ end
137
+
138
+
139
+ end
140
+
141
+ end
142
+
143
+ end
@@ -0,0 +1,88 @@
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
+ class SrOrg
48
+
49
+
50
+ def initialize(catalog_, opts_={})
51
+ @catalog = catalog_.to_s.downcase
52
+ @cache = opts_[:cache] ? {} : nil
53
+ end
54
+
55
+
56
+ attr_reader :catalog
57
+
58
+
59
+ def get(ident_)
60
+ ident_ = ident_.to_s
61
+ return @cache[ident_] if @cache && @cache.include?(ident_)
62
+ coord_sys_ = nil
63
+ proj4_ = nil
64
+ ::Net::HTTP.start('spatialreference.org') do |http_|
65
+ response_ = http_.request_get("/ref/#{@catalog}/#{ident_}/ogcwkt/")
66
+ coord_sys_ = response_.body if response_.kind_of?(::Net::HTTPSuccess)
67
+ response_ = http_.request_get("/ref/#{@catalog}/#{ident_}/proj4/")
68
+ proj4_ = response_.body if response_.kind_of?(::Net::HTTPSuccess)
69
+ end
70
+ result_ = Entry.new(ident_, :coord_sys => coord_sys_.strip, :proj4 => proj4_.strip)
71
+ @cache[ident_] = result_ if @cache
72
+ result_
73
+ end
74
+
75
+
76
+ def clear_cache
77
+ @cache.clear if @cache
78
+ end
79
+
80
+
81
+ end
82
+
83
+
84
+ end
85
+
86
+ end
87
+
88
+ end