SET QUOTED_IDENTIFIER OFF 

GO

SET ANSI_NULLS OFF 

GO



CREATE FUNCTION fHtmCover (@Region VARCHAR(8000))

---------------------------------------------------------------------

--/H  Return table of HTM range pairs covering the region

-------------------------------------------------------------

--/T The region has the syntax

--/T <pre>

--/T coverSpec	 =>     polySpec | rectSpec | circleSpec | regionSpec | hullSpec

--/T circleSpec  =>     CIRCLE J2000   [Ln] ra dec radArcMin  

--/T            |       CIRCLE  CARTESIAN [ln] x y z  radArcMin

--/T coverSpec	 =>     polySpec | rectSpec | circleSpec | regionSpec | hullSpec

--/T circleSpec  =>      CIRCLE J2000     [Li] ra dec rad  

--/T  	        |       CIRCLE CARTESIAN [Li] x y z  rad 

--/T rectSpec    =>     RECT J2000     {ra dec}4

--/T            |       RECT CARTESIAN {x y z }4

--/T polySpec    =>     POLY [J2000]   {ra dec}

--/T            |       POLY CARTESIAN {x y z}3+

--/T hullSpec    =>     CHULL J2000 {ra dec}3+

--/T                           Cartesian not implemented currently

--/T  

--/T convexSpec	 =>     CONVEX { x y z D}+

--/T regionSpec	 =>     REGION {convexSpec}+  

--/T </pre><br> 

--/T <p>returned table:  

--/T <li> HTMIDstart bigint not null primary key, -- Start of htm range (20 deep HTMs)

--/T <li> HTMIDend   bigint not null,             -- end of htm range   

--/T <br> Sample call to find ht m triangles covering area withn 5 arcminutes of xyz -.0996,-.1,0

--/T <br><samp>

--/T <br>select * from fHtmCover('CIRCLE CARTESIAN -.996 -.1 0 5')  

--/T </samp>  

--/T <br>see also fHtmCoverError 

---------------------------------------------------------------------

RETURNS @Triangles TABLE (

		HTMIDstart BIGINT NOT NULL PRIMARY KEY,  

		HTMIDend   BIGINT NOT NULL)

AS

BEGIN

   DECLARE @Vector 	VARBINARY(8000)	-- the outpvector from xp_HTM 

   DECLARE @Elements 	INT		-- size of output vector (in bigints)

   DECLARE @Cell     	INT		-- index to output vector cell

   DECLARE @StartHTM 	BIGINT		-- value of first htm cell

   DECLARE @EndHTM 	BIGINT		-- value of second htm cell

   DECLARE @Retcode 	INT		-- retcode from xp_HTM.

   DECLARE @ErrorMsg	VARCHAR(1000)   -- error message from HTM code

   -- get a vector of up to 1,000 triangles covering the desired area (input params missing at present).



   EXECUTE  @retcode = master.dbo.xp_HTM2_Cover @Region, @Vector OUTPUT, @ErrorMsg OUTPUT



   SET @Elements = DATALENGTH(@Vector)/8		-- each element is 8 bytes

   IF ((@Elements % 2) != 0) SET @Elements = 0  -- error if an odd number of elements

   SET @Cell = 0				--

   WHILE (@Cell < @Elements)			-- loop over array

	BEGIN					-- extracting pairs of cells

	SET @StartHTM = CAST (substring(@Vector, (8 * @Cell)+1, 8) AS BIGINT)

	SET @EndHTM = CAST (substring(@Vector, (8 * (@Cell+1))+1, 8) AS BIGINT)

   	INSERT @Triangles VALUES(@StartHTM, @EndHTM+1)	-- insert pair in answer table

 	SET @Cell = @Cell + 2			-- go to next pair

	END					-- end of loop to get 

   RETURN					--

END						-- end of fHtmCover



GO

SET QUOTED_IDENTIFIER OFF 

GO

SET ANSI_NULLS ON 

GO



