1

Topic: Error in Spatial caculations

Recorder 6 does not calculate spatial co-ordinates correctly using the inbuilt SQL functions.

The problem I was trying to solve was to display all the Locations that have the same specified parent Location in QGIS. Within SQL Server 2014 Management Studio I created a table with the fields of
CREATE TABLE [dbo].[Location_Geometry](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Location_Key] [char](16) NOT NULL,
    [GEOMTYPE] [nchar](10) NULL,
    [GEOM] [geometry] NULL,
PRIMARY KEY CLUSTERED (....

I then populated the table using the command
INSERT INTO [dbo].[Location_Geometry]
           ([Location_Key],
           [GEOMTYPE],
           [GEOM])
     SELECT LOC.[LOCATION_KEY], 'point',
     [dbo].[LCSpatial_OSGB_Geom](LOC.[SPATIAL_REF], LOC.[SPATIAL_REF_SYSTEM],1)
     FROM [NBNData].[dbo].[LOCATION] AS LOC

This uses the function as documented in this post http://forums.nbn.org.uk/viewtopic.php?pid=17111#p17111
Which in turn calls functions supplied by Recorder 6

The tables
LOCATION
LOCATION_NAME
Location_Geometry - (the table created above)

Where joined and the results plotted after suitable filtering. The filter selected the correct number of rows. However, not all the results were plotted. On further investigation it was found that four sites close together were being plotted as one site at the location of Chiddingfold Forest West, but being named as Chiddingfold Forest East. The other three sites not being plotted.
Attached is an extract of the four sites showing their spatial references.

If it helps, I have SQL code that correctly calculates the geometry for any OSGB grid reference which works for PostgreSQL, so will probably need some modification to work correctly in MS SQL Server.

Post's attachments

Same_Point.csv 445 b, 8 downloads since 2016-12-05 

You don't have the permssions to download the attachments of this post.
Harry Clarke
Surrey County Butterfly Recorder

2

Re: Error in Spatial caculations

Correction, the points that were not visible actually have a zero or very small Northings, so has disappeared off the map.

Harry Clarke
Surrey County Butterfly Recorder

3

Re: Error in Spatial caculations

Not sure why this should be and will investigate. The calculation of Eastings and Northings seems to be correct so it must be in the GEOM calculation which is difficult for me to check without GIS.   What is have been able to check is that in SQL the values in the geometry tables are not the same for any two of the example and that I can convert them back to the correct Easting and Northings from within SQL Server. Also using StDistance in SQL SQL Server the distances between the points seem to be calculated correctly.   Help in understanding why Harry is having problems would be appreciated  from someone with a better understanding of GIS than I have.

Mike Weideli

4

Re: Error in Spatial caculations

The problem is caused by an illegal grid reference, which is two characters longer than expected, with the characters carriage return and line-feed appended. Consequently this results in incorrect Eastings and Northings being calculated.

Looking at the central Spatial Reference for the Location in Recorder 6, and trying to edit the field would indicate that the field is correct, and only n characters long, and not n+2 characters long. The carriage-return and line-feed are not displayed, and the cursor is next to the last digit.

Unsure how it is possible to enter an illegal grid reference (I guess a copy and paste). But Recorder 6 should be validating the entry and only accepting a valid grid reference, and stripping out and/or rejecting anything that is not an alphanumeric character.

I guess the problem could reside in other tables containing spatial references, so how can these tables be cleaned up?

Harry Clarke
Surrey County Butterfly Recorder

5

Re: Error in Spatial caculations

You can import a grif ref which is longer than that allowed, so probably  you can copy and paste one.  This is a bug which  is scheduled for fixing is 6.26.  If you change the grid ref in R6 to something slightly different and save the item, then change it back to the correct value this should  force R6 to save the change.  I don't think this problem is picked up by the DataCorrection1 Bath Update, which deals with spaces in grid refs, but if what I suggest above causes problems then the Batch Update could be changed to make the correction.

Mike Weideli

6

Re: Error in Spatial caculations

Yes it would be useful if the DataCorrection1 Batch update could be updated, as it only eliminates the spaces. I used the following SQL query in PostgreSQL which updated the spatial_ref field correctly.

UPDATE recorder6.location
SET spatial_ref = substring(spatial_ref, '\w*\d*\w*')

Which means zero or more alpha characters (\w*), zero or more digits (\d*), zero or more alpha characters (\w*)

Not sure what the equivalent is for SQL Server.

Harry Clarke
Surrey County Butterfly Recorder