Geometry column: STGeomFromText and SRID (what is an SRID?) - sql-server

I'm playing with the new geography column in SQL Server 2008 and the STGeomFromText function. Here is my code (works with AdventureWorks2008)
DECLARE #region geography;
set #region = geography::STGeomFromText('POLYGON((
-80.0 50.0, -90.0 50.0,
-90.0 25.0, -80.0 25.0,
-80.0 50.0))', 4326);
SELECT #region;
My question is about the 4326 in the code. It is supposed to be a spacial Reference ID. When I go to MSDN there isn't a lot on it. If I change the value to 56 I get an error telling me the value must be in the sys.spatial_reference_systems table.
You can look at that table by executing:
select * from sys.spatial_reference_systems
There is a well_known_text column in that table, but it doesn't tell me much. The value for 4326 is:
GEOGCS["WGS 84", DATUM["World Geodetic System 1984", ELLIPSOID["WGS 84", 6378137, 298.257223563]], PRIMEM["Greenwich", 0], UNIT["Degree", 0.0174532925199433]]
Can anyone explain this mystery to me? What is the SRID?

So I ended up talking with an ex-military guy yesterday who was a radar/mapping specialist.
Basically, he knew exactly what that number (4326) was, where it came from, and why it is there.
It is an industry standard for computing geography. The problem is that the earth is not a perfect sphere (it bulges in the middle), and SRID 4326 accounts for that.
As I stated, the table sys.spatial_reference_systems lists all of the code and what they are. But the short version is that you are really only going to use 4326 unless you have a very specific reason to use something different.

SRID = Spatial Reference IDentifier
coordinates must use the same SRID to be comparable. otherwise you'd end up comparing kilometeres and miles. or something similar.

There are a lot of systems to map the earth. For example you want to map some state in USA. You can set the most south-east point as 0,0 and map all other spatial coordinates according to this point. On the other hand you may want to map some spatial data that span all over the map. In any case you must choose some point as 0,0. In addition you must select some sort of measurement unit: miles/kilometers/degrees/some other magical unit that suits you better. Over the years a lot of such systems where developed. Each has its own zero point, its own coordinates, its own rules about if the earth is flat or not. SRID or SRS is the id of such system. Using this id you can map point expressed in one system to another system, although sometimes it involves some pretty complex math.
And about 4326 SRID. It also called "WGS 84"
(http://en.wikipedia.org/wiki/World_Geodetic_System) system. It's the most common system to represent point on spherical(not flat) earth. It uses degree,minute,second notation and its x and y coordinates are usually called latitude and longitude.
Most used non-spherical earth projection is called UTM. You can read about it here: http://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system
Anyway, as long you are not doing any spatial conversions from one system to other, you don't really care about the system that you data uses.

I have found this website: http://spatialreference.org/ref/epsg/4326/ quite helpful in understanding the SRID you intend to use. It provides a handy map, some bounding box information and other links.
For other SRIDs simply change the digits at the end of the URL to what you are after.

The distance returned depends on the "Spatial Reference Identifier (SRID)" you define for your geography types.
In the example below, the default SRID of 4336 is used, see the second argument of STGeomFromText. This means the distance returned is in meters, you find this via querying the catalog view spatial_reference_systems i.e. select srs.unit_of_measure from sys.spatial_reference_systems as srs where srs.spatial_reference_id = 4326
As an alternative to STGeomFromText, you can use parse which assumes a SRID of 4326 and you don't have to specify one explicitly.
When calculating the distance between two points, you must use the same SRID for both geography types else you get an error. Example:
DECLARE #address1 GEOGRAPHY
DECLARE #address2 GEOGRAPHY
DECLARE #distance float
SET #address1 = GEOGRAPHY::STGeomFromText ('point(53.046908 -2.991673)',4326)
SET #address2 = GEOGRAPHY::STGeomFromText ('point(51.500152 -0.126236)',4326)
SET #distance = #address1.STDistance(#address2)
SELECT #distance --this is the distance in meters

Related

geometry data type of snowflake

I am trying to use the geometry data type and was wondering what the unit of the spatial functions will be. The documentation says as below. Is there a way I can pass a unit to the function. If not, how do i find out the unit
The measurement functions (e.g. ST_LENGTH) use the same units as the
coordinate system
Thanks
I was surprised by the results, and the docs could be clearer.
With a geography, st_length() returns the distance in meters, as described in the docs. For example, for a line between SF and a point 10km to the east:
select st_length(
to_geography('LINESTRING(-122.4194 37.7749,-122.3094 37.7749 )')
);
-- 9,668.032993573
However, that's not what you get when you have a geometry between the same points:
select st_length(
to_geometry('LINESTRING(-122.4194 37.7749,-122.3094 37.7749 )')
);
-- 0.11
So where does the 0.11 come from? Vertica says:
"For GEOMETRY objects, the length is measured in Cartesian coordinate units. For GEOGRAPHY objects, the length is measured in meters."
Same on PostGIS:
For geometry types: returns the 2D Cartesian length of the geometry [...]
For geography types: [...] Units of length are in meters. [...]
https://postgis.net/docs/ST_Length.html
I'll notify our docs team so we make the necessary update on https://docs.snowflake.com/en/sql-reference/functions/st_length.html#returns.

Mix and match spatial-reference systems?

Assuming that I have a table of postal codes, with a Geography column that was populated with Global - WGS84 (SRID 4326), can I accurately compare them (using STDistance) with a Geography point that has been populated with North America – NAD83 (SRID 4269)?
In short, No.
SQL Server requires for spatial functions that all items' have the same SRID. This is because the SRID provides other information in the background used to calculate distances and such on an ellipsoidal model.
That said, you could have a second column which is calculated to have a common SRID and use that for distance calculations. It's as simple as:
Declare #commonSrid geography = geography::STGeomFromWKB(<existing column>.STAsBinary(), 4326);
In doing this, you must be sure that all SRIDs are based from latitude and longitude decimal coordinates, and not for example grid references. Also because you're not doing a proper conversion between them, you may find distances are not 100% accurate - but they will be very very close.

SRID meaning in postgis

I would like to find out what is the pragmatic meaning of SRID (spatial reference id) in postgis.
I really do not understand what it is for. Can anyone throw some light on the matter?
For instance I noticed that the postigs function ST_GeomFromText(text WKT, integer srid) accept such an (optional) param as second argument. Why would I need to pass it in the get postigs to turn the text representation into a binary one? What is the value it adds?
Thanks
Spatial reference ID refers to the spatial reference system being employed -- this is important when going from a a geographic view of the world to a projected view of the world, ie, what you see when you look at a 2 dimensional paper map.
Spatial reference systems contain a couple of elements.
Firstly, the geoid, is a model of the shape of the earth -- the earth is not a sphere (sh, don't tell Google), it is in fact an oblate spheroid. The geoid shape used for GPS is known as WGS84, which is a model that works faily well globally. National mapping agencies use other geoids, that might be a better fit to local geographies.
Secondly, the projection type. This is essentially the mathematical model used to go from a 3D to a 2D representation of the world. Types include Mercator, Transverse Mercator, (both cylindical), Azimuthal, Conic, etc. All of these have trade-offs between accurately measuring distance, area or direction -- you can't preserve all three.
So, essentially when you declare a SRID in Postgis you are saying use this geoid and this projection model. Under the hood, Postgis uses a library called Proj.4, and based on the SRID information, it can convert from one coordinate system to another.
So, for example, to convert from lat/lon, which is know as 4326 in SRID terms to 900913, which is spherical Mercator, as used by Google/Bing maps, and other web mapping frameworks, you could run something like:
select st_astext(st_transform(st_setsrid(st_makepoint(-.5,52),4326),900913));
This is an example of a query I use. It uses the Lambert azimuthal equal-area projection (ETRS89-LAEA, srid = 3035).
ST_GeomFromText('POINT(2843711.1098048678, 2279498.6551480694)', 3035);
If you don't pass the srid, postgis will not know which spatial reference system to use.

STIntersects in the wrong lat/lng

I'm facing a problem regarding the order of the lat/lng in the following query, it works if i put the coordinates in the wrong order (lng,lat). However geography::Point should take the lat then lng, this is very weird. I tried checking the lat/lng on google maps, and its in the correct position, the polygone in a clockwise order, everything looks fine, the query is as follows:
DECLARE #branch_id int = 0;
DECLARE #point geography;
SET #point = geography::Point(31.3353608924066, 30.0798141318826, 4326);
SELECT B.ID, B.DeliveryZone, A.LatLong
FROM [dbo].[Branches] AS B
INNER JOIN [dbo].Addresses AS A ON B.AddressID = A.ID
WHERE B.ServiceProviderID = 2 AND
B.Deleted = 0 AND
B.DeliveryZone.STBuffer(1000).STIntersects( #point ) > 0 ;
The SQL Server documentation for Point is inconsistent. It says:
Lat - Is a float expression representing the x-coordinate of the Point being generated.
Latitude is generally used as a y-coordinate, that is, how far north or south you are from the equator.
The same inconsistency is given for Long:
Long - Is a float expression representing the y-coordinate of the Point being generated.
Of course, longitude is usually intepreted as an x-coordinate, that is, how far east or west you are from the prime meridian.
Also, lat and lng aren't used by all SRIDs, just ones that define unprojected coordinate systems like 4326, so "Lat" and "Long" are poor choices for generic parameter names in the documentation.
Based on these issues in the docs, it's most likely that for EPSG 4326, the database expects coordinates in (x,y) order, so (lng, lat), not (lat, lng). This also is consistent with the way most other spatial systems define coordinates in EPSG 4326.

Spatial querying, converting metres to radians?

I have geographic data that was loaded into the geography datatype. For very specific purposes, I now need to have store this as a geometry. However, I need to perform a query like this.
DECLARE #radius INT -- e.g. 3000 metres
DECLARE #geo geometry -- my starting shape
SET #geo = #geo.STBuffer(#radius) -- this obviously doesnt work..
SELECT Geo FROM GeometryTable
WHERE Geo.STWithin (#geo) = 1
Meters are a measure of length while radians are a measure of an angle so i don't think you can.
Are you trying to calculate an arc length?
Take a look at the links below:
http://wiki.answers.com/Q/How_do_you_convert_radians_to_meters
http://www.regentsprep.org/Regents/math/algtrig/ATM1/arclengthlesson.htm
This trick might work, if someone can validate this technique or provide a better alternative then they'll get the accepted answer.
Basically, I am thinking I can use the STBuffer of a geography which will apply the correct radius in metres around my geographical shape and then I convert back to a geometry. As the shapes were loaded originally as geography all the points are the same. This shape-flip should give me a resulting geometry with a pretty accurate buffer around it.
DECLARE #radius INT -- e.g. 3000 metres
DECLARE #geo geometry -- my starting shape
SET #geo = GEOMETRY::STGeomFromWKB(GEOGRAPHY::STGeomFromWKB(#geo.STAsBinary(),
4326).STBuffer(#radius).STAsBinary(),4326)
SELECT Geo FROM GeometryTable
WHERE Geo.STWithin (#geo) = 1

Resources