Determine distance from list of Latitude and Longitude
Currently i have a table looking something like this
_ID DeviceID Timestamp Latitude Longitude
4 13 1.1.2011 10:00:13 30.234 24.953
5 13 1.1.2011 10:00:17 30.235 24.953
6 13 1.1.2011 10:00:20 30.235 24.954
7 14 1.1.2011 10:00:21 54.532 13.256
8 13 1.1.2011 10:00:22 30.235 24.955
9 13 1.1.2011 10:00:24 30.234 24.954
- _ID
- Primary Key
- int
- DeviceID
- int
- not null
- Timestamp
- datetime
- not null
- Latitude
- real
- not null
- Longitude
- real
- not null
After a little searching (also here on SO) i'll find a lot of solutions about calculating the distance between two points. But i'd like to calculate the distance out of a list of points.
So i search a little more and i found the STLength method which seems to should do what i want. The problem is that i need to construct a geometry out of the list of points i have and that the help page about constructing geometry instances doesn't tell me how to do exactly this. (At least it doesn't tell it in a way that i could understand it.)
Can anyone tell me how to create a geometry out of a list of lat/lng?
Update
Ok, there is one thing i missed in my question till now:
I have a lot of points for one distance to calculate (normally somewhere between 1,000 and 20,000 but one or two times up to 40,000 points).
Also maybe i'm simply taking the complete false approach to get the distance out of it. So let me know if you have any other idea to get the distance out of the data.
Also don't care about ordering, adding or deletion of points. W开发者_运维百科hen the query runs the data is stable in these terms.
To answers Chris comment: Yes, the connection is from 4 - 5 - 6 - 8 - 9 (determined by the timestamp). Within the table are the values of different devices (like 7) but those can easily be sort out with a where clause. The distance i'd like to get is the length of the line you would get if you connect the points from the above list.
Here's a script that creates a basic table with the data you outlined, makes it into a valid LINESTRING string, then calculates the length on it:
declare @yourTable table (latitude decimal(10,7), longitude decimal(10,7))
insert into @yourTable select 30.234, 24.953
insert into @yourTable select 30.235, 24.953
insert into @yourTable select 30.235, 24.954
insert into @yourTable select 54.532, 13.256
insert into @yourTable select 30.235, 24.955
insert into @yourTable select 30.234, 24.954
DECLARE @LINE VARCHAR(MAX) = 'LINESTRING (';
SELECT @LINE = @LINE+ convert(varchar(20),latitude) + ' ' + convert(varchar(20),longitude) + ','
FROM @yourTable
select @LINE = LEFT(@LINE,LEN(@LINE)-1)+')'
DECLARE @g geography;
SET @g = geography::STGeomFromText(@LINE, 4326);
SELECT @g.STLength();
精彩评论