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

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?

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().

Geometry operations on latitude/longitude coordinates

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

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.

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.

How would I store OpenCV rotatedRect's in a database

I have a need to store data from an openCV rotatedRect into a database and also be able to search through them. In short, a rotated rectangle in openCV consists of:
a set of four points representing the four corners of the rectangle
the angle the rect is rotated at
the center point of the rect
the bounding box of the rotated rect
I'm not sure if this is more than one question, but I think they relate to each other nicely:
how would I represent this in PostgreSQL? One record will consist of (among other fields) several hundred rotated rect's in a sequential fashion. Does this look sensible?
// the rotated rect type
CREATE TYPE rotated_rect AS {
angle real,
points point ARRAY[4],
center point,
bounding_box box,
width real,
height real
};
// the item that will have several hundred rotated_rects
CREATE TABLE foo {
...,
rotated_rects rotated_rect[],
...
};
How would I then go about searching this data? I'd like to be able to compare one list of rotated rect's with those stored in the database for each record - quite possibly just the center-point of each rotated rect at this stage - and pick out the record/s which either match exactly, or are the closest in position. Would this require PostGIS for spatial data? Or would I have to read all the records into memory and search position manually?
EDIT: Added a stab at a type for postgresql.

Resources