I am trying to write a PostGIS nearest neighbor query where a set of coordinates are selected and the distance from closest polygon of flooding is determined. I then want to categorize the distance as either 'OUTSIDE, 'CLOSE' OR 'INSIDE':
WITH
point_loc (geom) AS (
SELECT ST_SetSRID(ST_MakePoint(531673.0, 180848.2),27700) ),
distances (gid, distance) AS (
SELECT
fl.gid,
ST_Distance(fl.geom, p.geom) AS distance
FROM
point_loc p, flooding fl
WHERE ST_DWithin(p.geom, fl.geom, 400)
SELECT
gid,
CASE WHEN distance > 300 THEN 'OUTSIDE'
WHEN distance > 50 AND <= 300 THEN 'CLOSE'
ELSE 'INSIDE'
END as flood_result
FROM distances;
I keep getting a syntax error on the final SELECT call. Can anyone see what I am doing wrong?
You're missing a closing parenthesis on your second CTE.
This line:
WHERE ST_DWithin(p.geom, fl.geom, 400)
Should be:
WHERE ST_DWithin(p.geom, fl.geom, 400) )
Related
I am using PostGIS, here is the SQL that I am using -
SELECT ST_Distance(
'SRID=4326;POINT(0 55)'::geography,
'SRID=4326;POLYGON((-180 45,-10 45,-10 -45,-180 -45,-180 45))'::geography
);
Visualizing these objects on a map suggests to me that the distance should be non-zero.
I use postgis and I want to calculate minimum bounding circle for my geometry.
My points are:
I use ST_MinimumBoundingCircle function, but it shows the ellipse instead of a circle (see ).
The following sample is to reproduce:
select ST_Centroid(collection), ST_MinimumBoundingCircle(collection) from (
select ST_COLLECT(point) as collection from (
select ST_SetSRID(ST_MakePoint(20.513371, 54.720205),4326) as point
UNION ALL
select ST_SetSRID(ST_MakePoint(20.493725, 54.717761),4326) as point
UNION ALL
select ST_SetSRID(ST_MakePoint(20.495189, 54.726808),4326) as point
UNION ALL
select ST_SetSRID(ST_MakePoint(20.501414, 54.716445),4326) as point
UNION ALL
select ST_SetSRID(ST_MakePoint(20.509221, 54.719836),4326) as point
) a
)b
I could not understand what I did wrong.
You're not doing anything wrong. You might see the buffer as an ellipse because the points you used to create it are pretty far from the equator (Kaliningrad). Keep in mind that you're projecting an ellipsoid into a 2D flat structure, therefore such distortions are just normal.
WITH j (geom) AS (
VALUES
('SRID=4326;POINT(20.513371 54.720205)'),
('SRID=4326;POINT(20.493725 54.717761)'),
('SRID=4326;POINT(20.495189 54.726808)'),
('SRID=4326;POINT(20.501414 54.716445)'),
('SRID=4326;POINT(20.509221 54.719836)')
)
SELECT ST_MinimumBoundingCircle(ST_Collect(geom::geometry))
FROM j;
But if you draw a similar buffer closer to the equator the distortion won't be so visible. See example bellow (north of Brazil):
WITH j (geom) AS (
VALUES
('SRID=4326;POINT(-56.30 1.55)'),
('SRID=4326;POINT(-56.63 1.14)'),
('SRID=4326;POINT(-55.95 0.70)'),
('SRID=4326;POINT(-55.57 1.38)')
)
SELECT ST_MinimumBoundingCircle(ST_Collect(geom::geometry))
FROM j;
Further reading: Buffers (Circle) in PostGIS
In order to avoid the distortion, transform to a coordinate system that represents lenghts more correctly before creating the buffer.
I facing out with Postgis.
I've a sql request :
SELECT st_x(ST_Transform(n.geom,4326)) AS x,
st_y(ST_Transform(n.geom,4326)) AS y,
fetchval(n.tags, 'name') as name,
n.id,
ST_Distance_sphere(ST_MakePoint(:longitude, :latitude), n.geom) as dist
FROM nodes n
WHERE ST_DWithin(n.geom, ST_SetSRID(ST_MakePoint(:longitude, :latitude),4326), 10)
ORDER BY ST_Distance(ST_SetSRID(ST_MakePoint(:longitude, :latitude),4326), n.geom)
Where :latitude and :longitude is my current location.
In this request I Want to return all nodes in 10 meters around me.
When I dump my results i've got some > 10 meters (the ST_Distance_sphere in SELECT return me nodes > 230 meters of me).
Any ideas of why ?
Thanks.
I finally find the solution,
ST_DWITHIN radius depends of SRID.
If I want it in meters I have to cast my points in meters :
(::geography)
SELECT st_x(ST_Transform(n.geom,4326)) AS x,
st_y(ST_Transform(n.geom,4326)) AS y,
fetchval(n.tags, 'name') as name,
n.id,
ST_Distance_sphere(ST_MakePoint(:longitude, :latitude), n.geom) as dist
FROM nodes n
WHERE ST_DWithin(n.geom::geography, ST_SetSRID(ST_MakePoint(:longitude, :latitude),4326)::geography, 280)
ORDER BY ST_Distance(ST_SetSRID(ST_MakePoint(:longitude, :latitude),4326), n.geom)
I have a point layer and a line layer. The points are road junctions, collected using DGPS, while the lines are the connecting roads, collected through Handheld GPS in tracking mode. As a result, the lines are not actually connected to the points.
Now I want to:
Remove all intermediate nodes from each line string, to make it a straight line.
Snap the start & end vertex of lines to the nearest points.
I am using PostGIS 2.0.This by far what I have done:
UPDATE line
SET geom = ST_Simplify(geom, 1000);
Q. Is there any other better way to accomplish it?(Since I am using an absurd tolerance)
UPDATE line
SET geom = ST_AddPoint(
(SELECT geom FROM line WHERE id = 1),
(SELECT p.geom FROM point AS p, line AS l
ORDER BY ST_Distance(p.geom,(SELECT ST_StartPoint(l.geom) FROM lt WHERE l.id=1)) LIMIT 1),
0)
WHERE id=1;
This will extend the line(with id=1) to the nearest point(point added at the beginning of the line).
Q. The above looks a bit complicated, is there any other efficient method/function available?
It seems to be reasonable to do both operations on the same query (not tested):
UPDATE line l
SET geom = ST_MakeLine(
(SELECT geom FROM point p ORDER BY ST_Distance(p.geom, ST_StartPoint(l.geom)) LIMIT 1),
(SELECT geom FROM point p ORDER BY ST_Distance(p.geom, ST_EndPoint(l.geom)) LIMIT 1)
);
If data set is small and you are running query only once, performance isn't issue - but you could add additional bbox comparision to speed things up:
SELECT geom FROM point p WHERE p.geom && ST_Expand(ST_StartPoint(l.geom), 100) ORDER BY ST_Distance(p.geom, ST_StartPoint(l.geom)) LIMIT 1
I have a table geofences which stores geometry of polygon.
I also have a point A which is inside the geometry. What I have to do is find the two closest points from point A that lie on the surface of the polygon geometry.
Function in PostGIS:
CREATE OR REPLACE FUNCTION accuracyCheck(Polygon geometry
,decimal lat
,decimal lon)
RETURNS VARCHAR AS
$BODY$
DECLARE height DECIMAL;
DECLARE accuracy VARCHAR(250);
BEGIN
CREATE TEMPORARY TABLE closePointStorage AS
SELECT ST_AsText(ST_ClosestPoint(geometry
,ST_GeomFromText('POINT(lat lon)',0)
)
) AS closestPoint
FROM (
SELECT ST_GeomFromText(geometry) as geometry
FROM gfe_geofences
WHERE is_active=true
) As tempName;
CREATE TEMPORARY TABLE areaStorage ON COMMIT DROP AS
SELECT ST_Area(ST_GeomFromText('Polygon((23.0808622876029 96.1304006624291
,28.0808622876029 99.1304006624291
,100 200
,23.0808622876029 96.1304006624291
))'
,0)
) AS area;
CREATE TEMPORARY TABLE distanceStorage ON COMMIT DROP AS
SELECT ST_Distance(
ST_GeomFromText('POINT(23.0808622876029 96.1304006624291)',-1)
,ST_GeomFromText('POINT(28.0808622876029 99.1304006624291)',-1)
) AS distance;
height = (SELECT area FROM areaStorage)
/(0.5*(SELECT distance FROM distanceStorage));
IF height < (SELECT radius_meters
FROM gfe_geofences Where is_active=true) THEN
accuracy = "FullConfirm";
RETURN accuracy;
ELSE
accuracy = "PartiallyConfirm";
RETURN accuracy;
END IF;
END;
$BODY$ LANGUAGE plpgsql;
I just want to find two points on boundary of polygon geometry. Just like I have found one from the query:
CREATE TEMPORARY TABLE closePointStorage AS
SELECT ST_AsText(ST_ClosestPoint(geometry
,ST_GeomFromText('POINT(lat lon)',0)
)
) AS closestPoint
FROM (
SELECT ST_GeomFromText(geometry) as geometry
FROM gfe_geofences
WHERE is_active=true
)
AS tempName;
Other then this point I have to find one more with distance greater then the point find above but smaller then the rest of points.
Use ST_DumpPoints() to dump the points of the polygon, then select from that order by ST_Distance to A limit 2. ?
So it is something like
SELECT * from ST_DumpPoints(poly) order by ST_Distance(A,geom) asc limit 2;
(assumes that this is an inner select where poly is the polygon, A is the point to compare to and geom is the geom column of one of the points in the poly being compared)
There generally is no second closest point on the boundary polygon, if you include the lines. Just like there is no real number second closest to zero.
Either you only wish to consider the points at the corners, like Markus suggests.
Or you have only one closest point.
1) Kind of a left-field idea, but to find the second-closest point to your destination, why not find the closest point to the point you already found?
2) Or, more germaine to your specific question,
find the set of points within some reasonable range of the point,
find the intersection of that set with the set of points lying on the polygon border (which I am guessing may be another PostGIS function; haven't used postG in a while so I'm not sure)
3) Farther into left field, dump some of your dataset into Mongo and use the $near function... http://docs.mongodb.org/manual/reference/operator/near/
I am assuming you want to find the edge of the polygon that passes the closest to the point in question
To obtain the distance 'd' of point 'C' from line [A,B]
First translate all points so A is at 0,0
B -= A //vector subtraction
C -= A
Then normalize B so it is of length 1.0
len = sqrt( B . B) //dotproduct of two vectors is the length squared
B /= len //scalar divide by length
Find length from A that is at right angles to C
dotp = B . C //dot product again
closestPointOnLine = B * dotp //scalar multiply
Now get the distance
diff = (C - ClosestPointOnLine)
d = sqrt(diff . diff)
Not sure how to do that in SQL. You will need to do the above for each edge on your polygon, and then find the smallest value 'd'
By the way the sign of the cross-product of B and C will now tell you whether the point is on the inside of the polygon or not