Update table column by comparing geometry columns from other table - postgresql

I have 2 tables. 1. island 2. region
I want to update island table region column based on the closet region to it using st_distance function. For e.g. for g1, st_distance(g1, geom1), st_distance(g1, geom2)... st_distance(g1, geom4) and update the region column for g1 geometry with the closet distance.

That could work like this:
UPDATE island
SET region = (SELECT regions.geom <-> island.geom
FROM regions
ORDER BY regions.geom <-> island.geom
LIMIT 1);
This can use a GiST index on regions(geom), but it will still take a while if island is large.

Related

How to run st_difference for complete table?

I have 2 postGIS tables CITIES and WATERBODIES, I want to remove waterbody geometry from cities, I'm trying to use
CREATE TABLE usa_No_water_100 AS
SELECT ST_Difference(usa_100.geom, water_100.geom) AS geom
FROM usa_100, water_100
but this creates a table with 10000 entries, I'm looking for the same 100 rows which I have in cities shape but with waterbodies geometry subtracted.
Geometry layer is as follows
Water layer is as follows
I want to remove the cities shape which is under the water shape, so that geometry will be reduced
PS: I'm open to doing this via python as well, if you have any suggestions
You can to compare the geometries of usa_100 with water_100 table using ST_Difference as you suggested, but to avoid getting the product of both tables you have to put one in a subquery or CTE, e.g.
WITH j (geom) AS (
SELECT ST_Union(geom) FROM water_100
) SELECT ST_Difference(usa_100.geom,j.geom)
FROM usa_100,j
Note: keep in mind what the ST_Difference documentation says:
If A is completely contained in B then an empty geometry is returned.
So, if a polygon on usa_100 entirely lies on a the result of ST_Union of water_100, it will return an empty geometry. If they do not spatially overlap, you do not have to worry about it.

How to fill in gaps between polygons using postgis functions

I have a similar case as shown in the below question.
https://gis.stackexchange.com/questions/293695/filling-gaps-between-polygons-using-qgis
That solution uses qgis, but I want to use a postgis function on a table on the geometry column. The solution talks about convex hull but not sure how to use the convex_hull postgis function here. I want to fill the gap by moving/merging the gap to the neighboring polygon.
As long as you have a column to group them by (e.g., "postal_code"), you can do:
SELECT
st_convexhull(st_collect(geom_column))
FROM
my_geom_table
GROUP BY
grouping_column -- e.g., "postal_code"

How to filter points in postgres?

I have a table with points in the Postgres. The points are from the flight path. I need to filter out some points.
.
My question would be how can I select only points which are in line and then make a line from the selected points only if parallel lines are in no more then 20m distance in between lines. Turning points should be ignored.
What I have done so far is to select points that are in one line
WITH routes as (
SELECT
geom,
heading-lag(heading) over (order by time) AS direction
FROM mytable
)
SELECT direction, geom
FROM routes WHERE direction between -10 AND 10;
In my query, I calculated direction from heading, and selected points with a minor difference in the heading.
However, I don't know how to continue.
EDIT
Link to fiddler data table http://sqlfiddle.com/#!17/3262c/9/0
With my query from above, I can filter the points which are marked in red lines. How can I add those points in variables like line1=...
line2=... line3=... line4=... and line5=... ?
Thank you for any help.

Convert Points to Polygon using PostGIS from a table with multiple points for one attribute

I want to create a polygon table using PostGIS. Each row intest_table has points x and y. Table 'test_table' has the location information of points which is the column origin.
I tried this
SELECT ST_MakePolygon(ST_MakeLine (432099.197021 , 6736122.29126 , 432099.197021 , 6747306.948242 , 427835.719238 , 6747306.948242 , 427835.719238 , 6736122.29126, 23031));
FROM test_table
where origin = '126af84e-0a9b-407d-8036-1ffc316106dd'
XMAX,YMIN
XMAX,YMAX
XMIN,YMAX
XMIN,YMIN
No luck I was wondering if someone can explain it better to me
and is it possible to add a bounding box to the geometry? for all my attributes with points
You can make a line either from array of points or from WKT representation, and if you want to convert it to polygon the last point of the line should be the same as the first one (so that the polygon is closed). If I understand correctly you'd like to build bounding boxes for point clouds that share the same value of origin. This might be done like that:
with
bounds as (
select
origin
,min(x) as xmin
,min(y) as ymin
,max(x) as xmax
,max(y) as ymax
from test_table
group by 1
)
select
origin
,st_makepolygon(st_makeline(array[
st_makepoint(xmin,ymin)
,st_makepoint(xmax,ymin)
,st_makepoint(xmax,ymax)
,st_makepoint(xmin,ymax)
,st_makepoint(xmin,ymin)
]))
from bounds

Put points on the map, longitude/latitude not where they should be although SRID the same

Thank you for looking at this - I'm sure it's a simple question but this is all very new to me and after a lot of internet searching my points are still in the sea!
I have a set of UK postcode data that I have converted into latitude and longitude. I also have a UK counties shape file.
The code that I have used to convert the latitude/longitude to a geometry point column is as follows:
ALTER TABLE colic ADD COLUMN longlat geometry(POINT,27700);
update colic set longlat = st_setsrid(st_point( longitude, latitude), 27700);
And this produces a column that has values as follows (I've only included a couple!):
"0101000020346C000048BF7D1D3867E83FC05B2041F1334A40"
"0101000020346C0000F085C954C1A8F7BFA1F831E6AE954A40"
"0101000020346C000020D26F5F07CEF4BFE3361AC05B504A40"
"0101000020346C00007D3F355EBA49F73FB6847CD0B3614A40"
The .prj from my shape file is:
PROJCS["British_National_Grid", GEOGCS["GCS_OSGB_1936",
DATUM["D_OSGB_1936", SPHEROID["Airy_1830",6377563.396,299.3249646]],
PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",400000.0],
PARAMETER["False_Northing",-100000.0],
PARAMETER["Central_Meridian",-2.0],
PARAMETER["Scale_Factor",0.9996012717],
PARAMETER["Latitude_Of_Origin",49.0], UNIT["Meter",1.0]]
And I set its SRID to 27700 when I upload it using the PostGIS shapefile uploader.
In PG Admin, when I ask:
select st_srid(geom) from counties limit 1;
select st_srid(longlat) from colic limit 1;
I get 27700 for both.
But...when I try any spatial query I get no response, and when I load the layers into quantum, the point data are off the tip of Cornwall in the north Atlantic, and only represented as one dot on the scale of the counties map.If I zoom in on them, they are 'there', but just not in east anglia where they should be!
This is my first attempt at both SQL and mapping - I'm sure there is something really simple that I have missed.
You are mixing up spatial references (SRS). The SRS in the .prj file (aka SRID=27700) is projected eastings and northings, which have units of metres. This is not latitude and longitude! Furthermore, it is a deception to call a column longlat when it isn't for longitude/latitude coordinates.
If you try to insert latitude/longitude in a column with SRID=27700, the points will not behave as expected, e.g., they will be in the far bottom left corner of a map.
If you have lat/long data from WGS84 (SRID=4326), you can transform this to eastings and northings:
UPDATE colic SET
geom = ST_Transform(ST_SetSRID(ST_Point(longitude, latitude), 4326), 27700);