ruanwz-sogoumap2pdf 0.2.1

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.
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,12 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ dir = File.dirname(__FILE__)
5
+ SOGOUMAP2PDF_ROOT = File.join(dir, 'sogoumap2pdf')
6
+ require File.join(SOGOUMAP2PDF_ROOT, "version")
7
+ require File.join(SOGOUMAP2PDF_ROOT, "sogou_map_image")
8
+ require File.join(SOGOUMAP2PDF_ROOT, "image_to_pdf")
9
+
10
+ module Sogoumap2pdf
11
+
12
+ end
@@ -0,0 +1,313 @@
1
+ require 'rubygems'
2
+ require 'pdf/writer'
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'RMagick'
6
+ include Magick
7
+
8
+ module Sogoumap2pdf
9
+ class ImageToPdf
10
+ def initialize(link_end,page_width,page_height)
11
+ #@page_width=600
12
+ @page_width=page_width.to_i
13
+ #@page_width=800
14
+ #@page_height=768
15
+ @page_height=page_height.to_i
16
+ #@page_height=480
17
+ @page_overlap=50
18
+ @text_height=15
19
+ @pdf=PDF::Writer.new(:paper=>[0,0,@page_width,@page_height])
20
+ @pdf.margins_pt(0,0,0,0)
21
+ # initialize icons
22
+ @icon_size=20
23
+ @link_end=link_end
24
+ end
25
+ def big_img_crop(x,y,w,h)
26
+ g=@g
27
+ nx0=x/256
28
+ ny0=y/256
29
+ nxn=(x+w)/256+1
30
+ nyn=(y+h)/256+1
31
+ delta_x=x-nx0*256
32
+ delta_y=y-ny0*256
33
+ # p "x #{x}"
34
+ # p "y #{y}"
35
+ # p "delta_x #{delta_x}"
36
+ # p "delta_y #{delta_y}"
37
+ ci=ImageList.new # image list to hold results
38
+ page=Rectangle.new( 0, 0, 0, 0)
39
+
40
+ for i in nx0..nxn
41
+ for j in ny0..nyn
42
+ #p "big_img_crop #{i},#{j}"
43
+ picture_file_name="~/.sogoumap/0/"+ [g.resource_type,g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+g.link_end
44
+ picture_file_name=File.expand_path(picture_file_name)
45
+ if File.exist?(picture_file_name) && File.size(picture_file_name)!= 0 then
46
+ tumb=ImageList.new.read(picture_file_name)[0] # image list to hold results
47
+ path_file_name="~/.sogoumap/0/"+ ["179",g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+".PNG"
48
+ path_file_name=File.expand_path(path_file_name)
49
+
50
+ if g.satellite_path && File.exist?(path_file_name) && File.size(path_file_name)!=0 then
51
+
52
+ path_tumb=ImageList.new.read(path_file_name)[0] # image list to hold results
53
+ tumb.composite!(path_tumb, 0, 0, OverCompositeOp)
54
+ end
55
+
56
+ #append tumb
57
+ ci << tumb
58
+ #update page
59
+ page.x=(i-nx0)*256
60
+ page.y=(nyn-j)*256
61
+ ci.page=page
62
+ else
63
+ end
64
+ end
65
+ end
66
+ if ci.length==0 then
67
+ resimg=ImageList.new.read("NULL:white") { self.size = "#{g.npx*@tumb_x}x#{g.npy*@tumb_y}"}
68
+ else
69
+ resimg=ci.mosaic
70
+ end
71
+ return resimg.crop(SouthWestGravity,delta_x,delta_y,w,h,true)
72
+ end
73
+
74
+ def perfect_fit(w,h,r,c)
75
+ @page_width=w
76
+ @page_height=h
77
+ @row=r
78
+ @column=c
79
+ @tumb_x=@page_width*256/(c*@page_width-(c-1)*@page_overlap)
80
+ @tumb_y=@page_height*256/(r*@page_height-(r-1)*@page_overlap)
81
+ @big_img_width,@big_img_height=c*@page_width-(c-1)*@page_overlap, r*@page_height-(r-1)*@page_overlap
82
+ return c*@page_width-(c-1)*@page_overlap, r*@page_height-(r-1)*@page_overlap
83
+ end
84
+ def create_pages(g)
85
+ # get the size of the image
86
+ size_x=@big_img_width
87
+ size_y=@big_img_height
88
+
89
+ # calculate the number of requred rows and columns
90
+ cols=(size_x-@page_overlap)/(@page_width-@page_overlap)
91
+ rows=(size_y-@page_overlap)/(@page_height-@page_overlap)
92
+ # create pdf
93
+ for i in 0...rows
94
+ for j in 0...cols
95
+ p "creating page #{i} #{j}"
96
+ p Time.now
97
+ @pdf.new_page
98
+ @pdf.add_destination("map part #{i},#{j}", "Fit")
99
+ pgimg=big_img_crop(j*(@page_width-@page_overlap),
100
+ (rows-1-i)*(@page_height-@page_overlap),@page_width,@page_height)
101
+
102
+ # pgimg=big_image.crop(j*(@page_width-@page_overlap),
103
+ # i*(@page_height-@page_overlap),@page_width,@page_height,true)
104
+ pgimg.write("itptmp#{@link_end}") { self.quality = 100 }
105
+ @pdf.add_image_from_file("itptmp#{@link_end}",0,0)
106
+ ss = PDF::Writer::StrokeStyle.new(4,:cap=>:round)
107
+ @pdf.stroke_style ss
108
+ if j>0 then
109
+ @pdf.add_internal_link("map part #{i},#{j-1}",0,@page_height/2,
110
+ @icon_size,@page_height/2+@icon_size)
111
+ @pdf.line(@icon_size,@page_height/2,0,@page_height/2+@icon_size/2).stroke
112
+ @pdf.line(0,@page_height/2+@icon_size/2,@icon_size,@page_height/2+@icon_size).stroke
113
+ end
114
+ if j<cols-1 then
115
+ @pdf.add_internal_link("map part #{i},#{j+1}",
116
+ @page_width-@icon_size,@page_height/2,
117
+ @page_width,@page_height/2+@icon_size)
118
+ @pdf.line(@page_width-@icon_size,@page_height/2,
119
+ @page_width,@page_height/2+@icon_size/2).stroke
120
+ @pdf.line(@page_width,@page_height/2+@icon_size/2,
121
+ @page_width-@icon_size,@page_height/2+@icon_size).stroke
122
+ end
123
+ if i>0 then
124
+ @pdf.add_internal_link("map part #{i-1},#{j}",@page_width/2,@page_height,
125
+ @page_width/2+@icon_size,@page_height-@icon_size)
126
+ @pdf.line(@page_width/2,@page_height-@icon_size,
127
+ @page_width/2+@icon_size/2,@page_height).stroke
128
+ @pdf.line(@page_width/2+@icon_size/2,@page_height,
129
+ @page_width/2+@icon_size,@page_height-@icon_size).stroke
130
+ end
131
+ if i<rows-1 then
132
+ @pdf.add_internal_link("map part #{i+1},#{j}",@page_width/2,0,
133
+ @page_width/2+@icon_size,@icon_size)
134
+ @pdf.line(@page_width/2,@icon_size,
135
+ @page_width/2+@icon_size/2,0).stroke
136
+ @pdf.line(@page_width/2+@icon_size/2,0,
137
+ @page_width/2+@icon_size,@icon_size).stroke
138
+ end
139
+ # @pdf.restore_state
140
+ end
141
+ end
142
+ end
143
+ def front_page(g)
144
+ size_x=@big_img_width
145
+ size_y=@big_img_height
146
+ # calculate the number of requred rows and columns
147
+ cols=(size_x-@page_overlap)/(@page_width-@page_overlap)
148
+ rows=(size_y-@page_overlap)/(@page_height-@page_overlap)
149
+
150
+ ci=ImageList.new # image list to hold results
151
+ page=Rectangle.new( 0, 0, 0, 0)
152
+ @g=g
153
+ for i in 0... g.npx
154
+ p "resizing #{i} j"
155
+ for j in 0...g.npy
156
+ picture_file_name="~/.sogoumap/0/"+ [g.resource_type,g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+g.link_end
157
+ picture_file_name=File.expand_path(picture_file_name)
158
+ if File.exist?(picture_file_name) && File.size(picture_file_name)!= 0 then
159
+ tumb=ImageList.new.read(picture_file_name)[0] # image list to hold results
160
+
161
+ path_file_name="~/.sogoumap/0/"+ ["179",g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+".PNG"
162
+ path_file_name=File.expand_path(path_file_name)
163
+ if g.satellite_path && File.exist?(path_file_name) && File.size(path_file_name)!=0 then
164
+ path_tumb=ImageList.new.read(path_file_name)[0] # image list to hold results
165
+ tumb.composite!(path_tumb, 0, 0, OverCompositeOp)
166
+ end
167
+ #resize each to tumb
168
+ tumb.resize!(@tumb_x,@tumb_y)
169
+ #append tumb
170
+ ci << tumb
171
+ #update page
172
+ page.x=i*@tumb_x
173
+ page.y=(g.npy-1-j)*@tumb_y
174
+ ci.page=page
175
+ else
176
+ end
177
+ end
178
+ end
179
+ #mosaic
180
+ if ci.length==0 then
181
+ p "@tumb_x,@tumb_y"
182
+ p @tumb_x
183
+ p @tumb_y
184
+ p "g.npx,g.npy"
185
+ p g.npx
186
+ p g.npy
187
+ p "#{g.npx*@tumb_x}x#{g.npy*@tumb_y}"
188
+ ## can't work
189
+ resimg=ImageList.new.read("null:white") { self.size = "#{g.npx.to_i*@tumb_x.to_i}x#{g.npy.to_i*@tumb_y.to_i}"}
190
+ else
191
+ resimg=ci.mosaic
192
+ end
193
+ #crop
194
+ #map2pdf.rb:339:in `crop!': bignum too big to convert into `long' (RangeError)
195
+ resx=@tumb_x*g.npx
196
+ resy=@tumb_y*g.npy
197
+
198
+ p "front_page size"
199
+ p resx
200
+ p resy
201
+ resx=@page_width if resx>@page_width
202
+ resy=@page_height if resy>@page_height
203
+ resimg.crop!(SouthWestGravity,resx,resy)
204
+ resimg.write("itptmp#{@link_end}"){ self.quality = 100 }
205
+ @pdf.add_image_from_file("itptmp#{@link_end}",0,0)
206
+ ss = PDF::Writer::StrokeStyle.new(2)
207
+ @pdf.stroke_style ss
208
+ for i in 1...cols
209
+ @pdf.line(i*@page_width/cols,0,
210
+ i*@page_width/cols,@page_height).stroke
211
+ end
212
+ for i in 1...rows
213
+ @pdf.line(0,i*@page_height/rows,
214
+ @page_width,i*@page_height/rows).stroke
215
+ end
216
+ for i in 0...rows
217
+ for j in 0...cols
218
+ p "adding #{i} #{j} link in front page"
219
+ p Time.now
220
+ @pdf.add_internal_link("map part #{i},#{j}",
221
+ j*@page_width/cols, @page_height-i*@page_height/rows,
222
+ (j+1)*@page_width/cols, @page_height-(i+1)*@page_height/rows)
223
+ end
224
+ end
225
+ end
226
+ def front_page_only(g)
227
+ size_x=@big_img_width
228
+ size_y=@big_img_height
229
+ # calculate the number of requred rows and columns
230
+ cols=(size_x-@page_overlap)/(@page_width-@page_overlap)
231
+ rows=(size_y-@page_overlap)/(@page_height-@page_overlap)
232
+
233
+ ci=ImageList.new # image list to hold results
234
+ page=Rectangle.new( 0, 0, 0, 0)
235
+ @g=g
236
+ for i in 0... g.npx
237
+ p "resizing #{i} j"
238
+ for j in 0...g.npy
239
+ picture_file_name="~/.sogoumap/0/"+ [g.resource_type,g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+g.link_end
240
+ picture_file_name=File.expand_path(picture_file_name)
241
+ if File.exist?(picture_file_name) && File.size(picture_file_name)!= 0 then
242
+ tumb=ImageList.new.read(picture_file_name)[0] # image list to hold results
243
+
244
+ path_file_name="~/.sogoumap/0/"+ ["179",g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+".PNG"
245
+ path_file_name=File.expand_path(path_file_name)
246
+ if g.satellite_path && File.exist?(path_file_name) && File.size(path_file_name)!=0 then
247
+ path_tumb=ImageList.new.read(path_file_name)[0] # image list to hold results
248
+ tumb.composite!(path_tumb, 0, 0, OverCompositeOp)
249
+ end
250
+ #resize each to tumb
251
+ tumb.resize!(@tumb_x,@tumb_y)
252
+ #append tumb
253
+ ci << tumb
254
+ #update page
255
+ page.x=i*@tumb_x
256
+ page.y=(g.npy-1-j)*@tumb_y
257
+ ci.page=page
258
+ else
259
+ end
260
+ end
261
+ end
262
+ #mosaic
263
+ if ci.length==0 then
264
+ p "@tumb_x,@tumb_y"
265
+ p @tumb_x
266
+ p @tumb_y
267
+ p "g.npx,g.npy"
268
+ p g.npx
269
+ p g.npy
270
+ p "#{g.npx*@tumb_x}x#{g.npy*@tumb_y}"
271
+ ## can't work
272
+ resimg=ImageList.new.read("null:white") { self.size = "#{g.npx.to_i*@tumb_x.to_i}x#{g.npy.to_i*@tumb_y.to_i}"}
273
+ else
274
+ resimg=ci.mosaic
275
+ end
276
+ #crop
277
+ #map2pdf.rb:339:in `crop!': bignum too big to convert into `long' (RangeError)
278
+ resx=@tumb_x*g.npx
279
+ resy=@tumb_y*g.npy
280
+ p "front_page size"
281
+ p resx
282
+ p resy
283
+ resx=@page_width if resx>@page_width
284
+ resy=@page_height if resy>@page_height
285
+ resimg.crop!(SouthWestGravity,resx,resy)
286
+ resimg.write("itptmp#{@link_end}"){ self.quality = 100 }
287
+ @pdf.add_image_from_file("itptmp#{@link_end}",0,0)
288
+ ss = PDF::Writer::StrokeStyle.new(2)
289
+ @pdf.stroke_style ss
290
+ for i in 1...cols
291
+ @pdf.line(i*@page_width/cols,0,
292
+ i*@page_width/cols,@page_height).stroke
293
+ end
294
+ for i in 1...rows
295
+ @pdf.line(0,i*@page_height/rows,
296
+ @page_width,i*@page_height/rows).stroke
297
+ end
298
+ # for i in 0...rows
299
+ # for j in 0...cols
300
+ # p "adding #{i} #{j} link in front page"
301
+ # p Time.now
302
+ # @pdf.add_internal_link("map part #{i},#{j}",
303
+ # j*@page_width/cols, @page_height-i*@page_height/rows,
304
+ # (j+1)*@page_width/cols, @page_height-(i+1)*@page_height/rows)
305
+ # end
306
+ # end
307
+ end
308
+
309
+ def save(save_file)
310
+ @pdf.save_as(save_file)
311
+ end
312
+ end
313
+ end
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/ruby
2
+ require 'rubygems'
3
+ require 'pdf/writer'
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'RMagick'
7
+ include Magick
8
+
9
+ # provides an access to sogou map
10
+ # emulates it as an image
11
+ #
12
+ # must be initialised by a link to an image from map.sogou.com
13
+ #
14
+ # command syntax:
15
+ # guangzhou: sogoumap2pdf http://hbpic3.go2map.com/seamless/0/180/792/504/105/100899_21033.JPG 1024x768 3x3 guangzhou.pdf sp
16
+ module Sogoumap2pdf
17
+ class SogouMapImage
18
+ attr_reader :link_end, :satellite_path, :start_link_x, :start_link_y, :picture_directory_name, :path_directory_name, :npx, :npy, :area_width, :area_height,:resource_type,:resource_level,:resource_extension
19
+ def initialize(sogou_link,option)
20
+ @link_beginning, @link_end = sogou_link.split(/\d+_\d+/)
21
+ @sogou_link=sogou_link
22
+ resource_head,resource_parameters= sogou_link.split(/\/0\//)
23
+ @resource_head = resource_head+"/0/"
24
+ @resource_type,@resource_level,@resource_x200,@resource_y200,@resource_x,@resource_y,@resource_extension=resource_parameters.split(/\/|_|\./)
25
+ @link_x=@resource_x.to_i
26
+ @link_y=@resource_y.to_i
27
+
28
+ @satellite_only=true if option=="s"
29
+ @satellite_path=true if option=="sp"
30
+ @map=true if option=="m"
31
+ @patch_size_x=256
32
+ @patch_size_y=256
33
+ #for sogou map
34
+ #@link_beginning="http://hbpic2.go2map.com/seamless/0/180/792/504/105/"
35
+ #@link_end=".JPG"
36
+ #@link_x=100960
37
+ #@link_y=21035
38
+ end
39
+ # get a patch of map from google maps
40
+ # the initial link is the patch 0,0
41
+ def get_or_read_picture(link_name,dir_name,file_name)
42
+ file_name=File.expand_path(file_name)
43
+ if File.exist? file_name then
44
+ #if File.size(file_name) !=0 then
45
+ # picture_block=File.read file_name
46
+ #else
47
+ # picture_block = File.read("empty.jpg") if link_name =~ /(JPG|GIF)$/
48
+ # picture_block = File.read("empty.png") if link_name =~ /PNG$/
49
+ #end
50
+ else
51
+ picture_data=Net::HTTP.get_response(URI.parse(link_name))
52
+ p picture_data.code
53
+ if picture_data.code == "200" then
54
+ picture_block = picture_data.body
55
+ unless File.exist? dir_name then
56
+ #Dir.mkdir dir_name
57
+ `mkdir -p #{dir_name}`
58
+ end
59
+ tmp_file=File.new(file_name,"w")
60
+ tmp_file.binmode
61
+ tmp_file.write(picture_data.body)
62
+ tmp_file.close
63
+ else
64
+ unless File.exist? dir_name then
65
+ #Dir.mkdir dir_name
66
+ `mkdir -p #{dir_name}`
67
+ end
68
+ `touch #{file_name}`
69
+ #temp_blob = ImageList.new.read("NULL:white") { self.size = "256x256"}
70
+ #picture_block = temp_blob.to_blob
71
+ #picture_block = File.read("empty.jpg") if link_name =~ /(JPG|GIF)$/
72
+ #picture_block = File.read("empty.png") if link_name =~ /PNG$/
73
+ end
74
+ end
75
+ return picture_block
76
+ end
77
+ def only_save_image(x,y)
78
+ http_get_picture = false
79
+ http_get_path = false
80
+ @resource_x200=((@link_x+x)/200).to_s
81
+ @resource_y200=((@link_y+y)/200).to_s
82
+ picture_link=@resource_head+[@resource_type,@resource_level,@resource_x200,@resource_y200].join("/")+"/"+(@link_x+x).to_s+"_"+(@link_y+y).to_s+"."+@resource_extension
83
+ p "getting #{x-@spx}/#{@npx-1}, #{y-@spy}/#{@npy-1} #{picture_link}"
84
+ picture_directory_name = "~/.sogoumap/0/"+ [@resource_type,@resource_level,@resource_x200,@resource_y200].join("/")
85
+ picture_file_name = picture_directory_name +"/"+ (@link_x+x).to_s+"_"+(@link_y+y).to_s+"."+@resource_extension
86
+ get_or_read_picture(picture_link,picture_directory_name,picture_file_name)
87
+ if @satellite_path then
88
+ path_link=@resource_head+["179",@resource_level,@resource_x200,@resource_y200].join("/")+"/"+(@link_x+x).to_s+"_"+(@link_y+y).to_s+"."+"PNG"
89
+ p "getting #{x-@spx}/#{@npx-1}, #{y-@spy}/#{@npy-1} #{path_link}"
90
+ path_data=Net::HTTP.get_response(URI.parse(path_link))
91
+ path_directory_name = "~/.sogoumap/0/"+ ["179",@resource_level,@resource_x200,@resource_y200].join("/")
92
+ path_file_name = path_directory_name + "/" +(@link_x+x).to_s+"_"+(@link_y+y).to_s+".PNG"
93
+ get_or_read_picture(path_link,path_directory_name, path_file_name)
94
+ end
95
+ end
96
+ # creates map of given size (in pixels), returns Image object
97
+ def fill_map(rx,ry)
98
+ @npx=npx=rx/@patch_size_x+1
99
+ @npy=npy=ry/@patch_size_y+1
100
+ @spacex=spacex=npx*@patch_size_x-rx
101
+ @spacey=spacey=npy*@patch_size_y-ry
102
+ # starting patch
103
+ @spx=spx=-(npx/2)
104
+ @spy=spy=-(npy/2)
105
+ @start_link_x=spx+@link_x
106
+ @start_link_y=spy+@link_y
107
+ @area_height=npy*@patch_size_y
108
+ @area_width=npx*@patch_size_x
109
+ # end patch
110
+ # epx=npx+spx-1
111
+ # epy=npy+spy-1
112
+ # for i in 0...npx
113
+ # for j in 0...npy
114
+ # self.only_save_image(i+spx,j+spy)
115
+ # end
116
+ # end
117
+ end
118
+ def fill_map2
119
+ for i in 0...@npx
120
+ for j in 0...@npy
121
+ self.only_save_image(i+@spx,j+@spy)
122
+ end
123
+ end
124
+ end
125
+
126
+ end
127
+ end