moduleNSWTopomoduleArcGISRasterincludeRaster,RasterRender,LogCREATE=%w[url]defget_raster(temp_dir)raise"no resolution specified for #{@name}"unlessNumeric===@mm_per_pxtxt_path=temp_dir/"mosaic.txt"vrt_path=temp_dir/"mosaic.vrt"service=ArcGIS::Service.new@urllocal_bbox=@map.cutline.bboxtarget_bbox=local_bbox.reproject_toservice.projectiontarget_resolution=@mm_per_px*Math::sqrt(target_bbox.first.area/local_bbox.first.area)raise"not a tiled map or image server: #{@url}"unlesstile_info=service["tileInfo"]lods=tile_info["lods"]origin=tile_info["origin"].values_at"x","y"tile_sizes=tile_info.values_at"cols","rows"lods.sort_by!do|lod|-lod["resolution"]endlod=lods.finddo|lod|lod["resolution"]<target_resolutionend||lods.lasttile_level,tile_resolution=lod.values_at"level","resolution"target_bbox.bounds.zip(origin,tile_sizes).mapdo|(min,max),origin,tile_size|[(min-origin)/tile_resolution/tile_size,(max-origin)/tile_resolution/tile_size]end.mapdo|min,max|(min.floor..max.ceil).each_cons(2).to_aend.inject(&:product).inject(GeoJSON::Collection.new(projection: service.projection))do|tiles,(cols,rows)|[cols,rows].zip(tile_sizes).mapdo|indices,tile_size|indices.map{|index|index*tile_size*tile_resolution}end.transpose.mapdo|corner|corner.zip(origin).map(&:sum)end.transpose.thendo|bounds|ring=bounds.inject(&:product).values_at(0,2,3,1,0)ullr=bounds.inject(&:product).values_at(1,2).flattenrow,col=rows[1].abs,cols[0]tiles.add_polygon[ring],ullr: ullr,row: row,col: colendend.clip(target_bbox.first).thendo|tiles|tiles.map.with_indexdo|feature,index|row,col,ullr=feature.values_at("row","col","ullr")rel_path="tile/#{tile_level}/#{row}/#{col}"jpg_path=temp_dir/"#{row}.#{col}"# could be pngtif_path=temp_dir/"#{row}.#{col}.tif"gdal_args=["-a_srs",service.projection,"-a_ullr",*ullr,"-of","GTiff",jpg_path,tif_path]log_update"%s: retrieving tile %i of %i"%[@name,index+1,tiles.length]service.get(rel_path,blankTile: true)do|response|jpg_path.binwriteresponse.bodyendOS.gdal_translate*gdal_argstif_pathendend.tapdo|tif_paths|log_update"%s: mosaicing %s tiles"%[@name,tif_paths.length]iftif_paths.length>1txt_path.writetif_paths.join(?\n)endOS.gdalbuildvrt"-input_file_list",txt_path,vrt_pathreturnvrt_pathendendend