1

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

We have just completed a very large import of a dataset to Rec6 and have realised that a mistake was made in the initial field selection. The dataset in question was of tetrad records so all have a tetrad reference (e.g. ST86N), but also have a four-figure grid ref (which is not the centre of the tetrad so may be of some use or may not). For previous imports to this dataset we have used the tetrad reference as the spatial ref & put the four figure grid ref into the comment field, on this latest batch however the two fields were swapped so the four figure ref is now held as the spatial ref for the record, with the tetrad ref in the comment.

It seems that it should be straightforward to set up a query which will swap the contents of these two fields in the SAMPLE and SURVEY_EVENTS tables thus correcting the problem. I note however that both tables also have Lat and Long fields in addition to the spatial ref.

My questions are, what is the relevance of these lat & long fields? Does it matter if they are not exactly in agreement with the spatial_ref (although they should lie somewhere within the tetrad)? And if so can someone explain how I can update them to agree with the new spatial reference?

Thanks

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

2

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Hi Rob
The Lat and Long fields will be used whenever you do a calculation involving the record's spatial data, including things like bounding box or polygon queries or viewing the record in a different spatial reference system (which may not be of any use to you). So, ideally you would need to recalculate the lat longs when you swap the tetrad over, which makes the task quite a lot trickier.

Does anyone else have SQL Server scripts that might help convert the OSGB tetrads to lat long?

Best Wishes

John van Breda
Biodiverse IT

3

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Thanks John

I am more than happy to write the scripts myself if someone can tell me how the calculation is performed. I have tried looking for the information myself, but keep finding myself lead off on interesting but ultimately futile explorations of cyberspace leaving me with more questions than answers.

I assume that Recorder must incorporate routines which do the conversion in both directions, if you can advise me on the formulae I can generate the scrips.

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

4

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Mike Weideli might well have scripts that do this:

http://www.lfield.co.uk/

Charles Roper
Digital Development Manager | Field Studies Council
http://www.field-studies-council.org | https://twitter.com/charlesroper | https://twitter.com/fsc_digital

5

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Rob

Hi

OS have a spreadsheet (in the public domain) that addresses most grid reference conversion issues.

I can email you a copy if wanted, or upload to files?

Cheers

Paul Griffiths
Adit

6

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Thanks Paul, could you email me a copy please

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

7

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

I have some links in Delicious you may find useful, including the OS spreadsheet you're after (it's the 'OSNet' one):

http://delicious.com/charlesroper/osgb
http://delicious.com/charlesroper/gis.coordinates

Charles Roper
Digital Development Manager | Field Studies Council
http://www.field-studies-council.org | https://twitter.com/charlesroper | https://twitter.com/fsc_digital

8

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

OK I am bumping this one back to the top, because although the information I need is probably in the reponses above somewhere, I don't have time to do the study necessary to make it happen.

We have just inserted a large number of locations into our database, through the back door, because manually entering them with all the associated data would have taken days, rather than the couple of hours we had available.

Unfortunately I have been unable to fathom the spreadsheet that Charles supplied, simply because I don't have time to learn what it all means. I have little interest in the detailed complexities of co-ordinate transformations.

As a result, I now have a large number of locations in Recorder which, although they have valid spatial references, also have 0 in both the Lat and Long fields, which wil doubtless have unfortunate implications downstream soewhere.

All I need is a simple to use set of formulae which will take standard grid ref or easting/northing and will return Lat and Long values (without needing to know any other variables). These must exist within the UI because Recorder does it behind the scenes every time a new location is entered, but there does not appear to be a udf in the database which does this.

Can anyone help?

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

9

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

And before anyone mentions it, I realise that if I open each location in turn and put it into edit mode then save it will calculate the lat and long automatically, but this is not the solution I am after. This could be so easy and it isn't

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

10

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Rob

I do have a SQL  user defined functions which will calculate the Lat/Long and can make this availble.

Mike.

Mike Weideli

11

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Rob

The script below will install a UDF which you can use the caculate the lat/long. I think you will know how to install and use it. If not let me know.

Mike

USE [NBNData]
GO
/****** Object:  UserDefinedFunction [dbo].[LCGRTOLLGBNI]    Script Date: 09/09/2009 21:55:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[LCGRTOLLGBNI]
(@SpatialRef Varchar(20), @SpatialRefSystem varchar(4), @isLat bit,@C bit )
RETURNS float
--
-- DESCRIPTION
-- Function to return either the Latitude or the Longitude for a UK or Irish Spatial_reference
--  Produces  a lat/Long which is very close to that generated by Recorder 6
-- if not valid returns -9999999
--
-- PARAMETERS
-- NAME    DESCRIPTION
-- @SpatialRef - anything from the spatial_ref
--  @SpatialRefSystem   anything from the spatial_ref_system
--  @isLat bit if 1 then return latitude otherwise longitude
--  @C if 1 then centre of square is used. If 0 then SW corner
-- AUTHOR: Mike Weideli
-- CREATED: 08/9/2009
-- 

AS
BEGIN

--****************************************************************************************************
DECLARE @ReturnValue float
Declare @a float
Declare @b float 
Declare @N float
Declare @k float
Declare @EZero float
Declare @NZero float
Declare @Lattrueorigin float
Declare @Longtrueorigin float
declare @ESquared float
declare @LatTrueOriginRad float
declare @LongTrueOriginRad float
declare @Pi float
declare @iNorthings float
declare @iEastings float
declare @Meridian  float
declare @sink float
declare @tank float
declare @Cosk float
declare @v float
declare @rho float
declare @etaSquared float
declare @etrue float
declare @Jthree float
declare @jFour float
declare @jFive float
declare @latitude float
declare @longitude float
declare @JSix float
declare @JSEven float
declare @JEight float
declare @JNine float
Declare  @phiminus float
Declare  @phiPlus  float
declare  @NUtemp float
declare @REmember float
Declare @FiveFour float
Declare @Fifteeneight float
Declare @TwentyOneEight float
Declare @ThirtyFiveTwentyFour float
set @FiveFour = 1.25
set  @Fifteeneight = 1.875
set  @TwentyOneEight = 2.625
set  @ThirtyFiveTwentyFour = 1.45833333333333333

SET @ReturnValue = -999999
If @SpatialRefSystem = 'OSGB' or @SpatialRefSystem = 'OSNI'

BEGIN
set @iNorthings =  [dbo].[LCReturnNorthingsV2](@SpatialRef,@SpatialRefSystem,@C)
set @iEastings =  [dbo].[LCReturnEastingsV2](@SpatialRef,@SpatialRefSystem,@C)

if  @SpatialRefSystem = 'OSGB'     
BEGIN
set   @a = 6375020.481
set   @b = 6353722.49 
set   @Ezero = 400000
set   @Nzero = -100000

set  @Lattrueorigin = 49
set  @Longtrueorigin = -2
END
ELSE
    BEGIN
      set   @a =   6377563.396
   set   @b = 6356256.96 
   set   @Ezero = 200000
   set   @Nzero = 250000

   set  @Lattrueorigin = 53.5
   set  @Longtrueorigin = -8
    END
     
     
set @eSquared = (SQUARE(@a) - SQUARE(@b)) / SQUARE(@a)
     
Set @N = (@a - @b) / (@a + @b)

set @LatTrueOriginRad = RADIANS(@Lattrueorigin)
set @LongTrueOriginRad =  RADIANS(@Longtrueorigin)

set @k = ((@iNorthings - @Nzero) / @a) + @LatTrueOriginRad

set @phiminus = @k - @LatTrueOriginRad
set @phiPlus = @k + @LatTrueOriginRad 
set @NUTemp = ((1 + @N + ((@FiveFour) * (POWER(@N , 2))) + (@FiveFour) * (POWER(@N , 3)))
* @phiminus) - ((3 * @N + 3 * (POWER(@N , 2)) + (@TwentyOneEight)
* (POWER(@N , 3))) * Sin(@phiminus) * Cos(@phiPlus)) + (((@FifteenEight)
* (POWER(@N , 2)) + (@FifteenEight) * (POWER(@N, 3)))
* Sin(2 * @phiminus) * Cos(2 * @phiPlus)) - (((@ThirtyFiveTwentyfour) * (POWER(@N , 3)))
* Sin(3 * @phiminus) * Cos(3 * @phiPlus))

set @meridian = @B * @nutemp


While Abs(@iNorthings - @Nzero - @meridian) > 0.0001 
BEGIN 
      set @k = @k + ((@iNorthings - @Nzero - @meridian) / @a)
      set @phiminus = @k - @LatTrueOriginRad
      set @phiPlus = @k + @LatTrueOriginRad 
      set @NUTemp = ((1 + @N + ((@FiveFour) * (POWER(@N , 2))) + (@FiveFour) * (POWER(@N , 3))) * @phiminus) - ((3 * @N + 3 * (POWER(@N , 2)) + (@TwentyOneEight) * (POWER(@N , 3))) * Sin(@phiminus) * Cos(@phiPlus)) + (((@FifteenEight) * (POWER(@N , 2)) + (@FifteenEight) * (POWER(@N, 3))) * Sin(2 * @phiminus) * Cos(2 * @phiPlus)) - (((@ThirtyFiveTwentyfour) * (POWER(@N , 3))) * Sin(3 * @phiminus) * Cos(3 * @phiPlus))
      set @meridian = @B * @nutemp
END
set @sinK = SIN(@k)
set @tanK = TAN(@k)
set @cosK = COS(@k)

set @v = @a / Sqrt(1 - (@eSquared * (POWER(@sinK, 2) )))

set @rho = @v * (1 - @eSquared) / (1 - (@eSquared * (POWER(@sinK, 2))))

set   @etaSquared = @v / @rho - 1
set  @eTrue = @iEastings - @Ezero

set @JThree = @tanK / (2 * @rho * @v)
set @JFour = (@tanK / (24 * @rho * (POWER(@v , 3)))) * (5 + 3 * POWER(@tanK , 2) + @etaSquared - 9 * (POWER(@tanK,2)) * @etaSquared)
set @JFive = (@tanK / (720 * @rho * (POWER(@v , 5))) * (61 + 90 * (POWER(@tanK , 2)) + 45 * (POWER(@tanK , 4))))

set @Latitude = (@k - @JThree * (POWER(@eTrue , 2)) + (POWER(@eTrue , 4)) * @JFour - (POWER(@eTrue , 6)) * @JFive)




set  @JSix = 1.00000000000000 / (@cosK * @v)
set @JSeven = (1.000000000000 / (@cosK * 6 * (POWER(@v , 3)))) * ((@v / @rho) + 2 * (POWER(@tanK , 2)))
set  @JEight = (1.00000000000 / (@cosK * 120 * (POWER(@v, 5)))) * (5 + 28 * (POWER(@tanK , 2))  + 24 * (POWER(@tanK , 4)))
set @JNine = (1 / (@cosK * 5040 * (POWER(@v, 7)))) * (61 + 662 * (POWER(@tanK, 2)) + 1320 * (POWER(@tanK , 4)) + 720 * (POWER(@tanK,6)))


set @Longitude = @LongTrueOriginRad + (@eTrue * @JSix) - ((POWER(@eTrue,3)) * @JSeven) + ((POWER(@eTrue, 5)) * @JEight) - (POWER(@eTrue , 7) * @JNine)
 

         
if @isLat  = 1
    set @Returnvalue = DEGREES(@Latitude)
ELSE
    set @Returnvalue = DEGREES(@LOngitude) 


--****************************************************************************************************
end
RETURN @ReturnValue


   


END


GO

GRANT EXECUTE ON LCGRTOLLGBNI TO PUBLIC

GO

Mike Weideli

12

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Thanks Mike

I knew someone would have it

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

13

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Mike

I have just found another occasion to use this excellent function, but I have a small question. Can you tell me what is the default value in recorder for the variable @C? That is to say, when the UI calculates Lat & Long for a new sample, does it use the centre of the square or the southwest corner?

Thanks

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

14

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Recorder Lat/Longs are the SW corner.  So parameters should be 0.

Mike Weideli

15

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Thought so, thanks

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

16

Re: Lat/long fields in SAMPLE & SURVEY_EVENT

Just in case anyone else comes across this - I was just about to post saying something was wrong with Mikes UDF as it was given some very strange results, however my test grid reference had lower case letters - it works fine when using upper case!

Charlie Barnes
Information Officer
Greater Lincolnshire Nature Partnership