Geometry operations on latitude/longitude coordinates - sql-server

My question is probably a duplicate, but all the answers I have seen so far do not satisfy me or still leaves me in doubt.
I have a web application that uses Google Maps API to draw and save shapes (circles and polygons) in a SQL Server DB with the geometry data type (where I save lat/long coordinates) and an SRID = 4326.
My objective is to later on, determine if a point is contained in the area of those circles/polygons thanks to SQL function geometry::ST_Intersects().
I have been told so far that my method wouldn't work because I am using geometry instead of geography. But to my surprise... after checking with a few tests, it works perfectly well with geometry and I am not able to understand why or how?
Could somebody explain to me why the geometry type works well with operations on lat/long whereas geography would be more suited?

I post as an answer because as comment is too long
geometry works well to the extent that your intersections are approximable to flat intersections.
the difference between geometry and geography consists in the fact that the former works by hypothesizing to work on plane surfaces the second on spherical surfaces. in the case in which the polygons in question are related to small areas in the order of a few thousand meters geometry works very well. the difference between measured distance by imagining that the points lie on a plane or that the points lie on the earth's sphere is so small as to be negligible. Unlike the question if the points are a few hundred kilometers in this case the distance measured in the plane or on the sphere is very different and proportionally is also the result of the intersection between these areas

Related

Any way to get an accurate ST_MinimumBoundingCircle calculation on a geography?

ST_MinimumBoundingCircle accepts the geometry type, i.e. planar geometry. Is there any way to get the minimum bounding circle of a geography collection using PostGIS? That is, the minimum bounding circle of a collection of features on the sphere. Simply converting a geography to a geometry and the resulting bounding circle back to geography does not create an accurate result. This guy (via https://observablehq.com/#fil/spherical-smallest-circle-problem) seems to have it worked out in JavaScript by
Going forward (to the stereographic plane), we compute the circumcenter of three projected points on each circle. Going backward, we use d3-geo-voronoi to retrieve the enclosing circle’s center.
Is there any built-in way to do this on PostGIS or achieve the same result using equivalent functions?

Difference between geodist() and dist() for Geo-Spacial Search

What is the Difference between Geodist(sfield,x,y) and dist(2,x,y,a,b) in Apache Solr for Geo-Spacial Searches ??
dist(2,x,y,0,0) :- calculates the Euclidean distance between (0,0) and (x,y) for each document. Return the Distance between two Vectors (points) in an n-dimensional space.
I was earlier using geodist() distance function for Geo-Spatial searches on my website but its response time was large. so have done a POC(proof of concept) for different distance functions and found that dist(2,x,y,0,0) distance function is relatively taking half of the time. But I want to know the reason behind this and the algorithms which both functions are using to calculate the distance.
I have to make a difference matrix for the same to convey it further.
The main difference is that geodist() is intended to work with spatial field types.
Most spatial implementation are based on Lucene's Points API, which is a BKD Index. This field type is strictly limited to coordinates in lat/lon decimal degrees. Behind the scenes, latitude and longitude are indexed as separate numbers. Four main field types are available for spatial search :
LatLonPointSpatialField
LatLonType (now deprecated) and its non-geodetic twin PointType
SpatialRecursivePrefixTreeFieldType (RPT for short), including RptWithGeometrySpatialField, a derivative
BBoxField (for areas, 4 instances of another field type referred to by numberType)
In geodist (sfield, x, y), sfield is a spatial field type that represents two points (lat,lon), so the direct equivalent using dist() would be to implement dist (2, sfieldX, sfieldY, x, y) with sfieldX and sfieldY being respectively the (lat,lon) coordinates of sfield.
Using dist (power, a, b, ...) you can't query a spatial field type. In order to perform the same spatial search, you would have to specify every point's dimension separately. It would require 2 indexed fields (or values per field at least) for 2 dimensions, 3 for 3d, and so on. That makes a huge difference because you would have to index every coordinates of each point separately.
Besides, you can also use geodist() as is with the BBoxField field type that indexes a single rectangle per document field and supports searching via a bounding box. To do the same with dist() you would have to compute the center point of the box to input each one of its coordinates as a function argument, so it would be too much hassle to yield the same result if you want to use an area as parameter.
Lastly, LatLonPointSpatialField for example does distance calculations based on Haversine formula (Great Circle), BBoxField does it a little faster because the rectangular shape is faster to compute. It's true that dist() may be even faster but remember that requires more field to be indexed, a lot of preprocess at query time to be able to yield the same calculated distance, and, as mentioned by Mats, it wouldn't take the earth' curvature into account.
An euclidean distance doesn't account for the curvature of the earth. If you're only sorting by the distance, the behavior can be OK - but only if your hits are within a small geographical area (the value of a unit compared to meters greatly change when you're getting closer to the poles).
There's an extensive and good answer that explains the difference between a Euclidean distance and a proper geographical distance (usually calculated using haversine) available at the GIS Stack Exchange.
Although at small scales any smooth surface looks like a plane, the accuracy of the Pythagorean formula depends on the coordinates used. When those coordinates are latitude and longitude on a sphere (or ellipsoid), we can expect that
Distances along lines of longitude will be reasonably accurate.
Distances along the Equator will be reasonably accurate.
All other distances will be erroneous, in rough proportion to the differences in latitude and longitude.

Real time linear interpolation about 3-D servo motor

As we know in ordinary linear interpolation, the final destination is fixed. I want to use a camera to catch the moving objects and the coordinate can be the final destination. Anybody could help me finish this algorithm in C code?
Assuming that you're trying to track a moving object with a gimballed camera, the problem is the mismatch between the linear, constant speed assumption and the motion of the camera. Even if your object is moving at a constant speed, the camera will have to rotate at a non-constant speed to keep track of the object. For example, the camera will have to rotate quickly when the object is near the camera, but will rotate very slowly when the object is far away.
1) Figure out the Cartesian (XYZ) coordinates of the starting and ending points.
2) Compute a sequence of linear interpolations between the start and end point in Cartesian space. This is a sequence of points in Cartesian space that estimate the object's trajectory.
3) Convert the sequence of Cartesian points from the Cartesian coordinate system to the Spherical coordinate system.
4) The spherical coordinates Theta and Phi are the angles that your camera must move through in time.
All of the computations described above are simple and closed-form. You shouldn't need to apply any "real-time" programming techniques aside basic concepts like no dynamic allocation and no interpreted or garbage collected languages. If reliability is very important then you will want to employ a suitable real-time OS. Linux has a good real-time patch that provides pretty good soft-real-time performance.

how to map wgs84 coordinates to sql server b-tree index (w/o using built in spatial indexes)

I would like to be able to map geographic points from WGS84, I believe, formatted for ms sql server, to the set of cells it would touch if the same coordinate pair were to be tessallated into an sql server spatial index with 4 hiearchical grids of 256 cells each.
The sql server 2008 spatial index does not ideally suit our needs because it doesn't support the use of both geographic and non-geographic data within the same index.
If we knew how to perform the translation ourselves it would seem like that would allow us to bypass the above limitation.
this page shows visually how a geography point can be mapped to planar cells which can be individually encoded as a sequence of from 1 and up to 4 positive integers. For an example of numbered cells please refer to the section "Deepest-Cell Rule" within the linked page.
I'm basically looking for pseduo-code on how to do just that.
Any help will be appreciated and if you know the algorithmic details that would be very much appreciated.
Thank you in advance.
A simple solution is to interleave x-and y co-ordinate, like in a morton curve a.k.a z curve. You can verify it by calcuting the upper bound using the most significant bit. Bing maps uses a morton curve:http://msdn.microsoft.com/en-us/library/bb259689.aspx but a hilbert curve is much more complicated but also better in most cases. In hilbert curve it's using a gray code for the co-ordinate. Here is some code example:Mapping N-dimensional value to a point on Hilbert curve.

Maps: Does calculating distance between 2 points factor in altitude?

Does Postgres' Spatial plugin, or any Spatial package for that manner, factor in the altitude when calculating the distance between 2 points?
I know the Spatial packages factor in the approximate curvature of the earth but if one location is at the top of a mountain and the other location is close to the sea - it seems like the calculated difference between those two points would greatly vary if the difference in altitude was not factored into account.
Also keep in mind that if I have 2 points are at the same ocean altitude but a mountain exists between the 2 points - the distance package should account for this.
Those factors are not being counted at all. Why? The software only knows about the two features (the two points you are getting the distance, the sphere/spheroid and a datum/projection factor).
For that to happen you need to probably use a developed linestring, in which you will connect your point with n vertices, each of them being Z aware.
Imagine this (loose WKT): LINESTRING((0,1,2),(0,2,3),(0,3,4),(0,10,15),(0,11,-1)).
Asking the software to calculate the distance between each vertex and summing it up, will consider the variations of terrain. But without something like that, it is impossible to map for irregularities in terrain.
All GIS softwares cannot tell, by themselves, what are those irregularities in terrain, and therefore, not take them in account.
You can create such linestrings (automatically) with softwares like ArcGIS (and others), using a line (between two points), and a surface file, such as the ones provided freely by NASA (SRTM project). These files come in a raster format, and each pixel has a X Y and Z value, in meters. Traversing the line you want, coupled with that terrain profile, you can achieve the calculation you want to achieve. If you need to have super extra precise calculations, you need a precise surface, and precise Z values in each vertex of this profile line.
That cleared up?
If the distance formula you're using does not take the altitude of the two points as parameters (in addition to the Latitudes and Longitudes of the two points), then it does not factor in altitude to the distance calculation. In any event, altitude difference does not have a very significant effect on calculated distance.
As usual with GPS, the difference in distance calculations that altitude would make is probably smaller than the error in most commercial GPS devices anyway, so in most applications altitude can be safely dispensed with (altitude measurements themselves are pretty inaccurate with commercial GPS devices, although survey data on altitudes is quite accurate).
PostgreSQL does not factor in altitude when calculating distances. It is all done in a planar surface.
Most of database spatial packages will not take this into account, altought, if your point is 3d, i.e., has a Z coordinate that might happend.
I don´t have PostgreSQL in this machine, but try this.
SELECT ST_DISTANCE(ST_POINT(0,0,10),ST_POINT(0,0,0));
It´s fairly easy to know if it is taking into account your Z value, since the return should be > 0; If that turns out to be true, just create Z aware features, and you will be successfull.
What SQL SERVER 2008, for example, takes into account when calculating distances, is the position of a Geography feature in a sphere. Geometry features in SQL SERVER will always use planar calculations.
EDIT: checked this in PostGIS manual
For Z aware points you must use the ST_MakePoint function. It takes up to 4 arguments (X Y Z and M). St_POINT only takes two (X Y)
http://postgis.refractions.net/documentation/manual-1.4/ST_Distance.html
ST_DISTANCE = 2D calculations
ST_DISTANCE_SPHERE documentation (takes in account a fixed sphere for calculations - aka not planar)
http://postgis.refractions.net/documentation/manual-1.4/ST_Distance_Sphere.html
ST_DISTANCE_SPHEROID documentation (takes into account a choosen spheroid for your calculations)
http://postgis.refractions.net/documentation/manual-1.4/ST_Distance_Spheroid.html
ST_POINT documentation
http://postgis.refractions.net/documentation/manual-1.4/ST_Point.html

Resources