Convert set or array of points to polygon in PostgreSQL - postgresql

I have a function that performs a transform on points.
I've figured out how to convert a polygon into a set of points and transform each point with a custom function I wrote. But I can't figure out how to now convert the points back into a polygon.
Here's my code that returns an array of transformed points. How do I get a polygon from this?
SELECT array_agg(point_geotransform(point((dp).geom))) As p
FROM (SELECT ST_DumpPoints(polygon(circle('((0,0),10)'))::geometry) AS dp
) t;
Ps. If there is a more efficient way of extracting the points, I'd love to know.

This works (excluded the transform so others can see it works):
SELECT polygon(circle('((0,0),10)')) p
UNION ALL
SELECT ST_MakePolygon(ST_MakeLine((point((dp).geom))::geometry))::polygon p
FROM (SELECT ST_DumpPoints(polygon(circle('((0,0),10)'))::geometry) AS dp
) t;
Geometry must be converted to geometry and polygon to polygon, so to speak.

Related

How to get coordinates from the PostgreSQL point object

I have a point column in my PostgreSQL table. Is there a way to extract point's X/Y coordinates separately? I operate with native PostgreSQL data, not PostGIS.
The documentation tells you:
It is possible to access the two component numbers of a point as though the point were an array with indexes 0 and 1. For example, if t.p is a point column then SELECT p[0] FROM t retrieves the X coordinate and UPDATE t SET p[1] = ... changes the Y coordinate.

postgis How do I find results within a given bounding box that are close to the center?

I'm learning postgis and saw that there is ST_MakeEnvelope to which I can pass my bounding box coordinates:
ST_MakeEnvelope(${params.swLng},${params.swLat},${params.neLng},${params.neLat}, 4326)
However, is there a way to get the results that are closest to the center of the bounding box? Is it possible to combine the bounding box with a radius query?
You can order by distance between your geometries and the centroid of the envelope
SELECT *
FROM myTable
WHERE st_intersects(geom, ST_makeEnvelope(...))
ORDER BY geom <-> st_centroid(ST_makeEnvelope(...))

Getting a list of pixels coordinates from a circular or oddly shape blob - Matlab

I am new to image processing and I am trying to obtain a list of pixel coordinates that are found within a circular/oval/oddly shape blob.
The only way that I can think of doing it is using a bounding box but unfortunately the bounding box does go over the area.
Anyone has a better idea?
Thanks
Just use find to obtain the pixel coordinates. Assuming your image is binary and stored in im, do:
[r,c] = find(im);
r and c will be the rows and columns of every pixel that is white. This assumes that the object is fully closed - one caveat I'd like to mention. If there are holes in the interior of the object, consider using imfill to fill in these holes, then combine it with find:
bw = imfill(im, 'holes');
[r,c] = find(bw);
If you have more than one object, use regionprops and specify the PixelList attribute:
s = regionprops(im, 'PixelList');
This will return a N element structure where each structure contains a PixelList field that contains the (x,y) coordinates of each unique object. In your case, this will be a M x 2 matrix where the first column are the x or column coordinates and the second column are the y or row coordinates.
To access an object's pixel coordinate list, simply do:
coords = s(idx).PixelList;
idx is the object you want to access.

Use $within with a buffered MondoDB Linestring

I need to evaluate the proximity of a Point to a LineString using MongoDB.
Because the $near operator can only compare a Point to another Point, I need to generate a polygon out of the LineString, so I can use the $within operator. The distance between the LineString and the edges of the polygon should represent the radius I want to search in, such as represented in red below:
What might be a useful algorithm in order to accomplish this?
I think much easier would be to write your own function
To find (perpendicular) distance between point and line and then creating thickness of poly-line by polygon means.
Where:
P0,P1 are line endpoints
P is point
d is distance between them
Line is defined as: p(t)=P0+(P1-P0)*t where t=<0.0,1.0>
So the function should do this:
create perpendicular line
q(t)=P+DQ*u where u=(-inf,+inf)
DQ is perpendicular vector to (P1-P0)
In 2D you can obtain it easily like this (x,y) -> (y,-x). In higher dimensions use cross product with some non coplanar vectors.
compute line vs. line intersection
there are tons of stuff about this so google or solve the equation yourself here you can extract mine implementation.
now after successful intersection
just compute d as distance between P and intersection point. Do not forget that parameter t must be in range. If not (or if no intersection) then return min(|P-P0|,|P-P1|)
[hints]
t and u parameters can be obtained directly from intersection test so if the perpendicular vector to (P1-P0) is normalized to size = 1 then the abs(u) parameter of intersection point is the distance
[notes]
I am not familiar with mongodb so if you have no means to use own tests inside then this answer is of coarse obsolete.
Unfortunately, MongoDB provides very basic geospatial query, so you should create the buffer by your own. You can read how to do it here: Computing a polygon that surrounds a multi-point line
If you have longitude/latitude coordinates like WGS84 you must adjust this code; Read here how to calculate distance between point on a sphere https://en.wikipedia.org/wiki/Haversine_formula

creating a polygon from points around a point in postgis

I have a postgresql db using postgis 2.0 and a table of thousands of points, I would like create a polygon of the furthest points originating around a particular central location.
I haven't got a clue how this would be done, any ideas anyone??
Filter and aggregate the points, and return the convex hull of the points.
So to select the points in mytable that are within a distance of 10 from id=123, and return the enclosing polygon:
SELECT ST_ConvexHull(ST_Collect(A.geom))
FROM mytable A, mytable B
WHERE B.id=123 AND ST_DWithin(A.geom, B.geom, 10)