determine if geography point falls on geography polygon line - sql-server

I understand that you cannot check if a geography point touches for example a geography polygon (see here) using something like this, which is possible in the geometry world:
WHERE A.Geo.STTouches(#s0) = 1
Is there a way to achieve this in the geography world? I guess a solution would be to transform the geography polygon + geography point into their geometry equivalents?
PS:
This should also work with complex polygons - such as this one with holes:
POLYGON ((99.999999999999986 5.3926387744713564E-14, 101.00000000000001 5.3926387744713564E-14, 100.99999999999987 1.0000000000000155, 100.00000000000013 1.0000000000000155, 99.999999999999986 5.3926387744713564E-14), (100.20000000000003 0.19999999999992918, 100.19999999999989 0.79999999999990257, 100.80000000000011 0.79999999999990234, 100.79999999999998 0.19999999999992912, 100.20000000000003 0.19999999999992918))

I'm assuming that my statement of your wanting to track whether or not the given point intersects with the boundary of your polygon. The below should do the trick:
SELECT #point.STIntersects(#polygon.RingN(1));
In essence, you're getting the outer boundary with RingN(1) (I'm making an assumption that this is a simple polygon, so the first ring should be the outer boundary) and then checking whether that intersects with the point you care about.
Edit: If you want to check if a given point lies on the boundary of any ring in the given polygon, something like this should do the trick:
select n.n as [IntersectedRingNumber],
#polygon.RingN(n) as [IntersectedRing]
from dbo.Numbers as n
where n <= #polygon.NumRings()
and #point.STIntersects(#polygon.RingN(n)) = 1;

Related

In Snowflake, can you create a circle polygon from a given centre point and radius?

I have a table containing geometrics points (called [Polygon_Centre]) and distances (called [Desired_Radius]). Anybody know which Snowflake function I could use to create polygon circles from this?
I'm trying to achieve a similar result to the Trade Area tool in Alteryx if anybody is familiar with that.
I was hoping for something like the following (which doesn't work):
ST_MAKEPOLYGON('Circle', [Polygon_Centre], [Desired_Radius])
Does anybody know how to create a circle polygon from a given centre point and radius in Snowflake?
Thanks everybody. I've found and used the following solution:
Calculate 120 points around the circle mathematically
Turn this into an ordered list with ST_COLLECT() (the order is critical and the start and end must match)
Use ST_MAKELINE() to turn these points into a line
Create the circle with ST_MAKEPOLYGON().

Postgis - How to find whether two circles based on two points overlap?

I can calculate the distance between two points using:
SELECT ST_Distance(
ST_GeomFromText('SRID=4326;POINT(54.5972850 -5.930119)')
, ST_GeomFromText('SRID=4326;POINT(54.516827 -5.958130)'),
false);
However, my goal is to create a rough circular zone (this can be square, hexagon, octagon .etc) around each point and then check if the zones overlap.
I am looking at ST_Overlaps as a possible solution but I am not sure how to convert these points into polygons to be compared. My ideal result would be something like:
SELECT ST_Overlaps(
ST_CreateCircularPolygon(geom1, 1000, 6)
ST_CreateCircularPolygon(geom2, 10000, 4)
);
Where:
ST_CreateCircularPolygon(geomerty, metreRadius, numberOfRadialPoints (e.g. 6 creates a hexagonal polygon))
Any guidance would be much appreciated!
You can use the quad_seg parameter of st_buffer to specify the number of segments per quarter of a circle. That is, the total number of segments in the output will be a factor of 4.
To produce a square:
select st_asText(st_buffer(st_geomFromText('Point(10 10)'), 1, 'quad_segs=1'));
st_astext
------------------------------------------------------
POLYGON((11 10,10 9,9 10,9.99999999999999 11,11 10))
(1 row)
Octagon:
select st_asText(st_buffer(st_geomFromText('Point(10 10)'), 1, 'quad_segs=2'));
st_astext
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
POLYGON((11 10,10.7071067811865 9.29289321881345,10 9,9.29289321881345 9.29289321881345,9 10,9.29289321881345 10.7071067811865,9.99999999999999 11,10.7071067811865 10.7071067811866,11 10))
(1 row)
Since you want to work in meters but have unprojected coordinates, you can cast your geometry to geography, apply a buffer in meters and cast back to geometry. Let's note that st_buffer in geography will internally cast to a geometry in UTM, do the buffer, then cast back to geography (a lot of casting, but it's handy!)
That being said, a square is not a circle and it sounds very very wrong to assume otherwise. The orientation of the square is not obvious: should a corner be at the north? or should a segment be facing norht? or should the square be rotated? by how much?
You will save yourself a lot of trouble by using a real circle. In this case, don't use st_buffer at all, nor st_distance but rather st_dwithin which can leverage spatial indexes

Buffer Polygon on Point in Polygon Query

I would like to buffer the warning polygon by two miles can anyone help me with
this so if ema personal are with in to miles of the warning the are listed, I've been trying to use ST Buffer (to expand the polygon coverage for the search) but cant seem to get it right? Is it in Meters (3218.69)? I'm using the latest opengeo suite.
SELECT DISTINCT ON (ema.name)
ST_X(ema.geom),ST_Y(ema.geom),ema."name", torpoly.expire
FROM ema INNER JOIN torpoly ON ST_Within(ema.geom, ST_BUFFER(torpoly.geom)
ORDER BY ema."name"
Your options are either:
Use an appropriate projected coordinate system for the region that uses linear units in metres or feet (UTM, State plane, etc.). All distance calculations on geometry types use a Cartesian coordinate system, which is quick and simple.
Use the geography type, which does distance calculations on objects with EPSG:4326 (lat/lon) with distance units in metres. If you don't want to change the data types, you can use a geom::geography cast, and maybe make an index on that cast.
And never do ST_Within(.., ST_Buffer()) for this type of analysis. It is slower and imperfect. Instead, use ST_DWithin, which finds all geometry/geography objects within a distance threshold of each other, which is just like a buffer. This function may use a spatial GiST index, if present.

SQL Server flat world map on geometry column

I need help regarding geometry and geography columns. Well not much about geography as that works fine. What im trying to achieve is to represent world map on geometry column instead of geography. Well not really represent itself but to be able to measure distance between two coordinates on flat map (as i know geography makes curves as earth itself is curved of course). Is there any way to do this?
I was reading a bit but nothing has make any difference to it if i do:
DECLARE #g geometry;
SET #g = geometry::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 0);
SELECT #g.STLength();
I get distance 0.016 while for geography its 1277... changing srid to 4326 doesn't change it also.. any help?
The geometry datatype is for Cartesian geometry. The line that you've defined is from point (x1, y1) = (-122.360, 47.656) to (x2, y2) = (-122.343, 47.656). The distance between these is sqrt( (x2-x1)^2 + (y2-y1)^2 ).
With the geography datatype, the points are given as degrees around the equator and degrees of inclination from the equator. The calculation of distance between two points so defined is given by the Haversine formula.
As you can see, the two are vastly different. But the real question is: what are you really trying to accomplish? If your points represent points on a globe, why are you using the geometry datatype at all?

How to create a polygon from a point?

In a GIS I am given a single point and shall create a regular polygon out of it, basically exactly what this function does.
Unfortunately, I have no idea on how to achieve this. I do not even have an idea what to look for on Google.
Any ideas?
PS: The system is based on a SQL Server, hence I am using T-SQL (if this is helpful / harmful in any way).
This sounds a bit like homework, so forgive me if I layout an algorithm in plain language rather than T-SQL
First, we're going to create a polygon with n sides with radius 1 around the origin (0,0) and then show how to get to the general case of a polygon with radius r, rotation theta and center (x,y).
There will be n vertices in this n-sided polygon. Each vertex will have coordinates (cos(k/n), sin(k/n)), with k ranging from 0 to n-1. From there, to get your polygon, make a line from vertex k to vertex k+1.
Now, how do we make it bigger? If the polygon has radius r instead of radius 1, the coordinates will be (r*cos(k/n), r*sin(k/n)). Rotating it by an angle theta? (r*cos((k/n)+theta), r*sin((k/n)+theta). Translating it to somewhere other than the origin? (x+r*cos((k/n)+theta), y+r*sin((k/n)+theta).

Resources