This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esri_rest.rb
321 lines (271 loc) · 9.57 KB
/
esri_rest.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#########
# Esri REST style handler..
class ESRIRestTileHandler < RackWelder
require "json"
def initialize (cfg, log, http_cfg)
@logger = log
@cfg = cfg
@http_cfg = http_cfg
@url_root = http_cfg["base"]
#@tile_engine = Imlib2TileEngine.new(cfg, log)
@tile_engine = ExternalTileEngine.new(cfg, log) #Use the external tile engine..
@lt = self.class.to_s + ":"
@logger.loginfo(@lt+"Starting")
end
def process(request, response)
begin
##
# get start time, for tracking purposes..
start_tm = Time.now
params = request.params()
pp params.keys
# first check the style of request - if includes json, then probibly a getpoo request..
#obvoulsy better checking is in order..
if (params["f"] && params["f"]=="json")
##
# Do request..
# Dig up parms..
if (params["callback"])
give_X(response, 200, "text/plain", params["callback"] + "(" + get_poo().to_json + ");")
else
give_X(response, 200, "text/plain", get_poo().to_json)
end
return
end
##
# Now check to see if it is a "tile,export, or an index reqeust.." request..
case(request.env["PATH_INFO"].split("/MapServer/")[1])
when "export" then
give_301(response, get_export_url(params) )
return
when nil then
give_X(response, 200, "text/plain",
"A more complete page is forth comming, but meanwhile this service provides \""+
@cfg["esri_rest"]["description"] + "\"" )
return
end
##
# Now should be a tile request..
mn = "process:"
@logger.loginfo(@lt+mn + "hit -> #{request.env[@REMOTE_IP_TAG]} -> #{request.env["PATH_INFO"]}")
# Log access...
@logger.log_access(request)
##
#Remove prefix from url..
uri = request.env["PATH_INFO"]
uri = uri[@url_root.length,uri.length] if ( uri[0,@url_root.length] == @url_root)
uri = uri.split("/")
#url is like http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer/tile/0/0/1
# of the form junk/z/y/x.format
x = uri.last.split(".").first.to_i
y = uri[-2].to_i
z = uri[-3].to_i
y = (2**(z))-y-1 #flip to esri style numbering..
puts("URI -> #{request.env["PATH_INFO"]} -> #{x},#{y}, #{z}")
if (z < 0|| y < 0 || z < 0)
@logger.logerr("Bad uri '#{request.env["PATH_INFO"]} from #{request.env[@REMOTE_IP_TAG]}")
give404(response, "The uri, #{request.env["PATH_INFO"]}, is not good.\n")
return;
end
##
# Uri is good, so do something...
path = @tile_engine.get_tile(x,y,z)
# Wait for it to show up..
@tile_engine.check_and_wait(x,y,z)
##
# Do request..
size = send_file_full(path,request,response)
#Log xfer..
@logger.log_xfer(request,response,size, Time.now-start_tm)
rescue => excpt
###
# Ok, something very bad happend here... what to do..
send_file_full(@cfg["error"]["img"],request,response,@cfg["error"]["format"])
stuff = "Broken at #{Time.now.to_s}"
stuff += "--------------------------\n"
stuff += excpt.to_s + "\n"
stuff += "--------------------------\n"
stuff += excpt.backtrace.join("\n")
stuff += "--------------------------\n"
stuff += "request => " + YAML.dump(request)+ "\n-------\n"
Mailer.deliver_message(@cfg["mailer_config"], @cfg["mailer_config"]["to"], "shiv crash..", [stuff])
@logger.logerr("Crash in::#{@lt}" + stuff)
end
end
private
###
# Not sure what ersi calls the Mapserver/json requests, so I call it the "get_poo" request..
# Anyway, this generates that information as a hash
def get_poo()
hsh_template = {"spatialReference"=>{"wkid"=>666},
"mapName"=>"not known",
"serviceDescription"=>
"Not known.",
"singleFusedMapCache"=>true,
"layers"=>
[{"subLayerIds"=>nil,
"name"=>"Not known.",
"parentLayerId"=>-1,
"id"=>0,
"defaultVisibility"=>true}],
"copyrightText"=>"(c) someone",
"fullExtent"=>
{"spatialReference"=>{"wkid"=>10},
"ymax"=>0.0,
"xmax"=>0.0,
"ymin"=>-10.0,
"xmin"=>-3.0},
"units"=>"esriMeters",
"tileInfo"=>
{"lods"=> [],
"spatialReference"=>{"wkid"=>102006},
"format"=>"PNG",
"compressionQuality"=>0,
"origin"=>{"x"=>-0.0, "y"=>0.0},
"dpi"=>96,
"rows"=>512,
"cols"=>512},
"description"=>"Something should go here. \n",
"initialExtent"=>
{"spatialReference"=>{"wkid"=>10101},
"ymax"=>0.0,
"xmax"=>0.0,
"ymin"=>-10.0,
"xmin"=>-30.0},
"documentInfo"=>
{"Category"=>"shiv",
"Subject"=>"shiv",
"Keywords"=>"shiv",
"Author"=>"shiv",
"Title"=>"",
"Comments"=>"shiv auto generated rest responce"}}
#build resolutions
#basic idea => res_multiplyer = extents/pixels
extent_width = @cfg["base_extents"]["xmax"] - @cfg["base_extents"]["xmin"]
tile_width = @cfg["tiles"]["x_size"]
#Setup LODs..
@cfg["esri_rest"]["LOD"].each { |i| hsh_template["tileInfo"]["lods"] << i}
hsh_template["documentInfo"] = @cfg["esri_rest"]["documentInfo"] if ( @cfg["esri_rest"]["documentInfo"] )
hsh_template["copyrightText"] = @cfg["esri_rest"]["copyrightText"] if ( @cfg["esri_rest"]["copyrightText"] )
hsh_template["description"] = @cfg["esri_rest"]["description"] if ( @cfg["esri_rest"]["description"] )
#Set extents..
hsh_template["initialExtent"]["spatialReference"]={"wkid"=>(@cfg["esri_rest"]["projection"]).to_i}
hsh_template["initialExtent"]["xmin"] = @cfg["base_extents"]["xmin"]
hsh_template["initialExtent"]["ymin"] = @cfg["base_extents"]["ymin"]
hsh_template["initialExtent"]["xmax"] = @cfg["base_extents"]["xmax"]
hsh_template["initialExtent"]["ymax"] = @cfg["base_extents"]["ymax"]
hsh_template["fullExtent"] = hsh_template["initialExtent"].dup
hsh_template["serviceDescription"]=
hsh_template["spatialReference"] ={"wkid"=>(@cfg["esri_rest"]["projection"]).to_i}
hsh_template["tileInfo"]["spatialReference"]={"wkid"=>(@cfg["esri_rest"]["projection"]).to_i}
#Translate format types to esri like wonder wigets..
hsh_template["tileInfo"]["format"]= case @cfg["storage_format"]
when "png"
"PNG"
when "jpg"
"JPEG"
else "PNG"
end
#Translate compression values to something useful, though not sure what they are acutally used for... mystery magic i expect.
hsh_template["tileInfo"]["compressionQuality"]= case @cfg["storage_format"]
when "png"
0
when "jpg"
70
else 0
end
#fill out random other fields.. fun, fun,fun.
hsh_template["tileInfo"]["origin"]={"x"=>@cfg["base_extents"]["xmin"], "y"=> @cfg["base_extents"]["ymax"]}
hsh_template["tileInfo"]["rows"]=@cfg["tiles"]["x_size"]
hsh_template["tileInfo"]["cols"]=@cfg["tiles"]["y_size"]
hsh_template["serviceDescription"] = @cfg["esri_rest"]["description"]
hsh_template["mapName"] = @cfg["title"]
hsh_template["layers"][0]["name"] = @cfg["title"]
return hsh_template
end
def get_export_url(params)
width,height = params["size"].split(",")
bbox = params["bbox"]
pp width
pp height
pp bbox
url =sprintf(@cfg["esri_rest"]["export_url"], width, height, bbox)
pp url
return url
end
end
##
# Serves up kml..
class ESRI_Service_Fooler < RackWelder
#set stuff up, log=logger, cfg=shiv kml config.
def initialize ( log,cfg)
@logger = log
#the ip of the requesting host..
@REMOTE_IP_TAG="HTTP_X_FORWARDED_FOR"
#save the config..
@cfg = cfg
#save the root url.
@url_root = @cfg["root_url"]
end
# Do something..
def process(request, response)
begin
##
# get start time, for tracking purposes..
start_tm = Time.now
params = request.params()
give_X(response, 200, "text/plain", "Something is coming to this page soon.")
return
rescue => excpt
###
# Ok, something very bad happend here... what to do..
send_file_full(@cfg["error"]["img"],request,response,@cfg["error"]["format"])
stuff = "Broken at #{Time.now.to_s}"
stuff += "--------------------------\n"
stuff += excpt.to_s + "\n"
stuff += "--------------------------\n"
stuff += excpt.backtrace.join("\n")
stuff += "--------------------------\n"
stuff += "request => " + YAML.dump(request)+ "\n-------\n"
Mailer.deliver_message(@cfg["mailer_config"], @cfg["mailer_config"]["to"], "shiv crash..", [stuff])
@logger.logerr("Crash in::#{@lt}" + stuff)
end
end
end
# Serves up kml..
class ESRI_Service_Fooler_Info < RackWelder
#set stuff up, log=logger, cfg=shiv kml config.
def initialize ( log,cfg)
@logger = log
#the ip of the requesting host..
@REMOTE_IP_TAG="HTTP_X_FORWARDED_FOR"
#save the config..
@cfg = cfg
#save the root url.
@url_root = @cfg["root_url"]
end
# Do something..
def process(request, response)
begin
##
# get start time, for tracking purposes..
start_tm = Time.now
params = request.params()
give_X(response, 200, "text/plain", '{"currentVersion":10.04,"soapUrl":null,"secureSoapUrl":null,"authInfo":{"isTokenBasedSecurity":false}}')
return
rescue => excpt
###
# Ok, something very bad happend here... what to do..
send_file_full(@cfg["error"]["img"],request,response,@cfg["error"]["format"])
stuff = "Broken at #{Time.now.to_s}"
stuff += "--------------------------\n"
stuff += excpt.to_s + "\n"
stuff += "--------------------------\n"
stuff += excpt.backtrace.join("\n")
stuff += "--------------------------\n"
stuff += "request => " + YAML.dump(request)+ "\n-------\n"
Mailer.deliver_message(@cfg["mailer_config"], @cfg["mailer_config"]["to"], "shiv crash..", [stuff])
@logger.logerr("Crash in::#{@lt}" + stuff)
end
end
end