Thank you very much for your attention on this post. SA3 table has a bigger boundary. I believe my transaction table need to match anything falling into that boundary.
Every transnational LAT/LONG is not available in SA3 table. Is there a way to check anything falling into the lat/long boundary.
I guess, transaction file has LAT/LONG for every single point. What I am trying to find all those point into a bigger boundary SA3 area. The software like MapInfo does this. Wondering how is there a way to do the same in SQL server.
Do the difference and take the closest one as Area
Difference Latitude ...
Transaction table lat = 100
SA3 Table London lat = 110 so difference of lat 110 - 100 = 10
SA3 table NewYork lat = 80 so difference of lat 80 - 100 = 20
since Transaction table lat is closest to London ( diff lat 10 )
then its London Area
I'm not sure if I interpret your question correct. But it seems to me that you want to check if a point lies within an area (polygon) defined by a number of points.
The STDistance() function gives no indication if the point lies within an area, only how far it is from another point.
The easiest way would be if ABS provides polygons of the SA3 areas and not only a number of unordered points.
If they do, you can use the STIntersects() function as in this example:
Declare @point geometry
Declare @polygon geometry
SET @polygon = geometry::STGeomFromText('POLYGON ((-88.2 41.5, -88.2 41.6, -88.3 41.6, -88.3 41.5, -88.2 41.5))', 4326)
SET @point = geometry::STGeomFromText('POINT (-88.22 41.50000001)', 4326)
Select @point.STIntersects(@polygon) -- STIntersects() returns 1 if the point lies within the polygon
SET @point = geometry::STGeomFromText('POINT (-88.0 41.50000001)', 4326)
Select @point.STIntersects(@polygon) -- STIntersects() returns 0 if the point does not lie within the polygon
As Wim said, you need to first plot your polygon, then see if your other locations fall into that polygon.
using sql server
added the following long,lat for Brogo, New South Wales 2550, Australia
(10999999,'2019-04-01', 149.767249, -36.503551, '149.767249, -36.503551')
and it shows as intersecting. you would need to do some visual and data validations on your own though.
DECLARE @BuildString NVARCHAR(MAX), @closepolygon NVARCHAR(MAX), @PolygonFromPoints geography ;
select @BuildString= COALESCE(@BuildString + ',', '') + CAST(a.Longitude AS NVARCHAR(50)) + ' ' + CAST(a.Latitude AS NVARCHAR(50))
from [SA3] a
order by code
select top 1 @closepolygon = CAST(tak.Longitude AS NVARCHAR(50)) + ' ' + CAST(tak.Latitude AS NVARCHAR(50))
from [SA3] tak
order by code
SET @BuildString = ' POLYGON((' + @BuildString + ', ' + @closepolygon + '))'
SET @PolygonFromPoints = geography::STPolyFromText(@BuildString, 4326);
select @PolygonFromPoints
;with src
as
(
select geography::STGeomFromText('POINT(' + CAST([PLong] AS VARCHAR(20)) + ' ' + CAST(PLat AS VARCHAR(20)) + ')', 4326) as geoPoint, *
from Tab1
)
select case geoPoint.STIntersects(@PolygonFromPoints)
when 0 then 'Does not Intersect'
else 'Intersects' end as Status ,*
From src
This is exactly my issue is. Transaction file has all the individual point(?AT/LONG). I want to find the area(Polygon) of ABS SA3 table that has LAT/Long but not one-to-one. How actually I can match if my point in transaction table will fits the range of that bigger LAT/LONG boundary.
In plain term, if my point(LONG/LAT) in transaction table in the range of (ie Queanbeyan LONG/LAT 149.6468291 / -35.52366613). How could I check an individual point with a bigger LONG/LAT boundary.
SA3 table data.
I don't have enough experience in this field to help you any further. I would suggest to contact ABS. You won't be the first with questions and to my experience, such organisations are very willing to help.
The Australian Bureau of Statistics (ABS) has divided the country in different area's.
They also defined a hierarchy of area's. The lowest is Statistical Area 1 (SA1).
An SA4 area consists of a number of SA3's.
An SA3 area consists of a number of SA2's.
And an SA2 area consists of a number of SA1's.
It's similar to the hierarchy you described.with city, state, country, ..
He wants to check for a location (point), defined by a longitude and latitude, in what SA3 area it is located.
Converting a longitude - latitude location to a SQL Server GEOGRAPHY POINT is easy.
SET @point = geometry::STGeomFromText('POINT (-88.22 41.50000001)', 4326)
The challenge is to convert the open data from ABS to a SQL Server GEOGRAPHY POLYGON.
Once that is done, the rest is easy.
Select @point.STIntersects(@polygon) -- STIntersects() returns 1 if the point lies within the polygon, 0 otherwise
You get a polygon by stringing long lat pairs separated by a comma. The first pair should also show up as the last pair to close the polygon loop. Not sure what more you are looking for?
select @BuildString= COALESCE(@BuildString + ',', '') + CAST(a.Longitude AS NVARCHAR(50)) + ' ' + CAST(a.Latitude AS NVARCHAR(50))
from [SA3] a
order by code
select top 1 @closepolygon = CAST(tak.Longitude AS NVARCHAR(50)) + ' ' + CAST(tak.Latitude AS NVARCHAR(50))
from [SA3] tak
order by code
You get a polygon by stringing long lat pairs separated by a comma.
The earth is a globe. the order of the points that define the polygon matters. Suppose you want to create a polygon of the North Pole. Depending on the order of the points (West-ward or East-ward) the system will interpret the polygon as defining the North Pole - as intended - or the whole planet, except the North Pole.
Good Morning.
I appreciate your attention on this topic.
I like to project my response time performance (let’s say) by City/State/Country based on Lat/long in transaction table.
More specifically, I want to make a group Green/Yellow/Red based on response time within 7/10/15 minutes by City/State/Country.
I have lat/long in all level data. But that is not one-to-one match.
How could I match my individual lat/long point of transaction table against lat/long in respective city/state/country lat/long.
Then I could say City a,b,c are green, d,x are Yellow… Again State I,P are Red.. based on my transaction table lat/long.