/
shp.rb
303 lines (251 loc) · 11.7 KB
/
shp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
require File.dirname(__FILE__) + '/dbf'
module GeoRuby
module Shp4r
#Enumerates all the types of SHP geometries. The MULTIPATCH one is the only one not currently supported by GeoRuby.
module ShpType
NULL_SHAPE = 0
POINT = 1
POLYLINE = 3
POLYGON = 5
MULTIPOINT = 8
POINTZ = 11
POLYLINEZ = 13
POLYGONZ = 15
MULTIPOINTZ = 18
POINTM = 21
POLYLINEM = 23
POLYGONM = 25
MULTIPOINTM = 28
MULTIPATCH = 31 #not supported here
end
#An interface to an ESRI shapefile (actually 3 files : shp, shx and dbf). Currently supports only the reading of geometries.
class ShpFile
attr_reader :shp_type, :record_count, :xmin, :ymin, :xmax, :ymax, :zmin, :zmax, :mmin, :mmax
include Enumerable
#Opens a SHP file. Both "abc.shp" and "abc" are accepted. The files "abc.shp", "abc.shx" and "abc.dbf" must be present
def initialize(file, access = "r")
#strip the shp out of the file if present
file = file.gsub(/.shp$/i,"")
#check existence of shp, dbf and shx files
unless File.exists?(file + ".shp") and File.exists?(file + ".dbf") and File.exists?(file + ".shx")
raise MalformedShpException.new("Missing one of shp, dbf or shx for: #{file}")
end
@dbf = Dbf::Reader.open(file + ".dbf")
@shx = File.open(file + ".shx","rb")
@shp = File.open(file + ".shp","rb")
read_index
end
#opens a SHP "file". If a block is given, the ShpFile object is yielded to it and is closed upon return. Else a call to <tt>open</tt> is equivalent to <tt>ShpFile.new(...)</tt>.
def self.open(file,access = "r")
shpfile = ShpFile.new(file,access)
if block_given?
yield shpfile
shpfile.close
else
shpfile
end
end
#Closes a shapefile
def close
@dbf.close
@shx.close
@shp.close
end
#return the description of data fields
def fields
@dbf.fields
end
#Tests if the file has no record
def empty?
record_count == 0
end
#Goes through each record
def each
(0...record_count).each do |i|
yield get_record(i)
end
end
alias :each_record :each
#Returns record +i+
def [](i)
get_record(i)
end
#Returns all the records
def records
Array.new(record_count) do |i|
get_record(i)
end
end
private
def read_index
file_length, @shp_type, @xmin, @ymin, @xmax, @ymax, @zmin, @zmax, @mmin,@mmax = @shx.read(100).unpack("x24Nx4VE8")
@record_count = (file_length - 50) / 4
unless @record_count == @dbf.record_count
raise MalformedShpException.new("Not the same number of records in SHP and DBF")
end
end
#TODO : refactor to minimize redundant code
def get_record(i)
return nil if record_count <= i or i < 0
dbf_record = @dbf.record(i)
@shx.seek(100 + 8 * i) #100 is the header length
offset,length = @shx.read(8).unpack("N2")
@shp.seek(offset * 2 + 8)
rec_shp_type = @shp.read(4).unpack("V")[0]
case(rec_shp_type)
when ShpType::POINT
x, y = @shp.read(16).unpack("E2")
geometry = GeoRuby::SimpleFeatures::Point.from_x_y(x,y)
when ShpType::POLYLINE #actually creates a multi_polyline
@shp.seek(32,IO::SEEK_CUR) #extent
num_parts, num_points = @shp.read(8).unpack("V2")
parts = @shp.read(num_parts * 4).unpack("V" + num_parts.to_s)
parts << num_points #indexes for LS of idx i go to parts of idx i to idx i +1
points = Array.new(num_points) do
x, y = @shp.read(16).unpack("E2")
GeoRuby::SimpleFeatures::Point.from_x_y(x,y)
end
line_strings = Array.new(num_parts) do |i|
GeoRuby::SimpleFeatures::LineString.from_points(points[(parts[i])...(parts[i+1])])
end
geometry = GeoRuby::SimpleFeatures::MultiLineString.from_line_strings(line_strings)
when ShpType::POLYGON
#TODO : TO CORRECT
#does not take into account the possibility that the outer loop could be after the inner loops in the SHP + more than one outer loop
#Still sends back a multi polygon (so the correction above won't change what gets sent back)
@shp.seek(32,IO::SEEK_CUR)
num_parts, num_points = @shp.read(8).unpack("V2")
parts = @shp.read(num_parts * 4).unpack("V" + num_parts.to_s)
parts << num_points #indexes for LS of idx i go to parts of idx i to idx i +1
points = Array.new(num_points) do
x, y = @shp.read(16).unpack("E2")
GeoRuby::SimpleFeatures::Point.from_x_y(x,y)
end
linear_rings = Array.new(num_parts) do |i|
GeoRuby::SimpleFeatures::LinearRing.from_points(points[(parts[i])...(parts[i+1])])
end
geometry = GeoRuby::SimpleFeatures::MultiPolygon.from_polygons([GeoRuby::SimpleFeatures::Polygon.from_linear_rings(linear_rings)])
when ShpType::MULTIPOINT
@shp.seek(32,IO::SEEK_CUR)
num_points = @shp.read(4).unpack("V")[0]
points = Array.new(num_points) do
x, y = @shp.read(16).unpack("E2")
GeoRuby::SimpleFeatures::Point.from_x_y(x,y)
end
geometry = GeoRuby::SimpleFeatures::MultiPoint.from_points(points)
when ShpType::POINTZ
x, y, z, m = @shp.read(24).unpack("E4")
geometry = GeoRuby::SimpleFeatures::Point.from_x_y_z_m(x,y,z,m)
when ShpType::POLYLINEZ
@shp.seek(32,IO::SEEK_CUR)
num_parts, num_points = @shp.read(8).unpack("V2")
parts = @shp.read(num_parts * 4).unpack("V" + num_parts.to_s)
parts << num_points #indexes for LS of idx i go to parts of idx i to idx i +1
xys = Array.new(num_points) { @shp.read(16).unpack("E2") }
@shp.seek(16,IO::SEEK_CUR)
zs = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
@shp.seek(16,IO::SEEK_CUR)
ms = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
points = Array.new(num_points) do |i|
Point.from_x_y_z_m(xys[i][0],xys[i][1],zs[i],ms[i])
end
line_strings = Array.new(num_parts) do |i|
GeoRuby::SimpleFeatures::LineString.from_points(points[(parts[i])...(parts[i+1])],DEFAULT_SRID,true,true)
end
geometry = GeoRuby::SimpleFeatures::MultiLineString.from_line_strings(line_strings,DEFAULT_SRID,true,true)
when ShpType::POLYGONZ
#TODO : CORRECT
@shp.seek(32,IO::SEEK_CUR)#extent
num_parts, num_points = @shp.read(8).unpack("V2")
parts = @shp.read(num_parts * 4).unpack("V" + num_parts.to_s)
parts << num_points #indexes for LS of idx i go to parts of idx i to idx i +1
xys = Array.new(num_points) { @shp.read(16).unpack("E2") }
@shp.seek(16,IO::SEEK_CUR)#extent
zs = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
@shp.seek(16,IO::SEEK_CUR)#extent
ms = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
points = Array.new(num_points) do |i|
Point.from_x_y_z_m(xys[i][0],xys[i][1],zs[i],ms[i])
end
linear_rings = Array.new(num_parts) do |i|
GeoRuby::SimpleFeatures::LinearRing.from_points(points[(parts[i])...(parts[i+1])],DEFAULT_SRID,true,true)
end
geometry = GeoRuby::SimpleFeatures::MultiPolygon.from_polygons([GeoRuby::SimpleFeatures::Polygon.from_linear_rings(linear_rings)],DEFAULT_SRID,true,true)
when ShpType::MULTIPOINTZ
@shp.seek(32,IO::SEEK_CUR)
num_points = @shp.read(4).unpack("V")[0]
xys = Array.new(num_points) { @shp.read(16).unpack("E2") }
@shp.seek(16,IO::SEEK_CUR)
zs = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
@shp.seek(16,IO::SEEK_CUR)
ms = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
points = Array.new(num_points) do |i|
Point.from_x_y_z_m(xys[i][0],xys[i][1],zs[i],ms[i])
end
geometry = GeoRuby::SimpleFeatures::MultiPoint.from_points(points,DEFAULT_SRID,true,true)
when ShpType::POINTM
x, y, m = @shp.read(24).unpack("E3")
geometry = GeoRuby::SimpleFeatures::Point.from_x_y_m(x,y,m)
when ShpType::POLYLINEM
@shp.seek(32,IO::SEEK_CUR)
num_parts, num_points = @shp.read(8).unpack("V2")
parts = @shp.read(num_parts * 4).unpack("V" + num_parts.to_s)
parts << num_points #indexes for LS of idx i go to parts of idx i to idx i +1
xys = Array.new(num_points) { @shp.read(16).unpack("E2") }
@shp.seek(16,IO::SEEK_CUR)
ms = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
points = Array.new(num_points) do |i|
Point.from_x_y_m(xys[i][0],xys[i][1],ms[i])
end
line_strings = Array.new(num_parts) do |i|
GeoRuby::SimpleFeatures::LineString.from_points(points[(parts[i])...(parts[i+1])],DEFAULT_SRID,false,true)
end
geometry = GeoRuby::SimpleFeatures::MultiLineString.from_line_strings(line_strings,DEFAULT_SRID,false,true)
when ShpType::POLYGONM
#TODO : CORRECT
@shp.seek(32,IO::SEEK_CUR)
num_parts, num_points = @shp.read(8).unpack("V2")
parts = @shp.read(num_parts * 4).unpack("V" + num_parts.to_s)
parts << num_points #indexes for LS of idx i go to parts of idx i to idx i +1
xys = Array.new(num_points) { @shp.read(16).unpack("E2") }
@shp.seek(16,IO::SEEK_CUR)
ms = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
points = Array.new(num_points) do |i|
Point.from_x_y_m(xys[i][0],xys[i][1],ms[i])
end
linear_rings = Array.new(num_parts) do |i|
GeoRuby::SimpleFeatures::LinearRing.from_points(points[(parts[i])...(parts[i+1])],DEFAULT_SRID,false,true)
end
geometry = GeoRuby::SimpleFeatures::MultiPolygon.from_polygons([GeoRuby::SimpleFeatures::Polygon.from_linear_rings(linear_rings)],DEFAULT_SRID,false,true)
when ShpType::MULTIPOINTM
@shp.seek(32,IO::SEEK_CUR)
num_points = @shp.read(4).unpack("V")[0]
xys = Array.new(num_points) { @shp.read(16).unpack("E2") }
@shp.seek(16,IO::SEEK_CUR)
ms = Array.new(num_points) {@shp.read(8).unpack("E")[0]}
points = Array.new(num_points) do |i|
Point.from_x_y_m(xys[i][0],xys[i][1],ms[i])
end
geometry = GeoRuby::SimpleFeatures::MultiPoint.from_points(points,DEFAULT_SRID,false,true)
else
geometry = nil
end
ShpRecord.new(geometry,dbf_record)
end
end
#A SHP record : contains both the geometry and the data fields (from the DBF)
class ShpRecord
attr_reader :geometry , :data
def initialize(geometry, data)
@geometry = geometry
@data = data
end
#Tests if the geometry is a NULL SHAPE
def has_null_shape?
@geometry.nil?
end
end
class MalformedShpException < StandardError
end
end
end