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

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

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

separate each points element on different polygon layers on qgis

I have a table called all_the_points which contains several points in rows for every element in a column, now all the elements are in the column called origin,
now each element can have several points, is possible for me to separate all points by their element on different layers on PostGIS in one view, I tried using the group by but still all my points and polygon are on one layer when I drag to my qgis.
Thank you
with
bounds as (
select
origin
,min(x) as xmin
,min(y) as ymin
,max(x) as xmax
,max(y) as ymax
from all_the_points
group by WHERE ((all_the_points.origin)::text = '126af84e-0a9b-407d-8036-1ffc316106dd'::text);
)
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
different layers on PostGIS in one view
No, this is not possible.
There are several options:
have one layer, and apply a different symbology for each category in QGIS
have one layer and filter data for a single category in QGIS. Eventually add the same layer a 2nd (and more) time and filter another category
have a distinct view in Postgres for each layer

How to find all points within polygon in postgis?

I have locations stored in location_table (point_location geometry), now i draw a polygon on google map and pass that polygon (geometry) to backend, I want to find all the locations that are within that polygon.
SELECT POINT_LOCATION
FROM LOCATIONS_TABLW
WHERE ST_Contains(GeomFromEWKT(?), POINT_LOCATION);
This is giving me random results when I pass the polygon from google maps to backend. Its not giving me all points that are exactly within the polygon. It gives me points that are even outside the polygon.
What is the correct way to find all points within polygon in postgis with accuracy (including border cases also)
Update :
we tried with st_intersects() it did not work as well.
UPDATE
Please find below queries
SRID=4326;POLYGON((-103.30549637500008 20.852735681153252,-103.08103481249998 20.612974162085475,-101.6261045 20.537532106266806,-99.83567868749998 20.395877027062447,-99.80306537500002 22.0572706994358,-99.64994812500004 28.918636198451633,-121.1212769375 8.69559423007209,-103.30549637500008 20.852735681153252))
SRID=4326;POINT(-103.496956 20.722446)
SRID=4326;POINT(-103.4955 20.723544)
select ST_Intersects(GeomFromEWKT('SRID=4326;POINT(-103.496956 20.722446)'), GeomFromEWKT('SRID=4326;POLYGON((-103.30549637500008 20.852735681153252,-10
3.08103481249998 20.612974162085475,-101.6261045 20.537532106266806,-99.83567868749998 20.395877027062447,-99.80306537500002 22.0572706994358,-99.64994812500004 28.918
636198451633,-121.1212769375 8.69559423007209,-103.30549637500008 20.852735681153252))'));
This Should Return False, but it's returning true.
You can use
SELECT POINT_LOCATION
FROM LOCATIONS_TABLE
WHERE ST_Contains(ST_GEOMFROMTEXT('POLYGON((P1.X P1.Y, P2.X P2.Y, ...))'), LOCATIONS_TABLE.POINT_LOCATION);
Note: Polygon must be closed (that means the last coordinate == first coordinate). Second parameter POINT_LOCATION must be the geometry column in your point table.
UPDATE:
I have tried to replay your steps in my pg database. I created 2 tables, LOCATIONS_TABLE (id, geom) and POLYGON (id, geom). After that i filled the LOCATIONS_TABLE with the 2 points
SRID=4326;POINT(-103.4955 20.723544)
SRID=4326;POINT(-103.496956 20.722446)
After that i inserted the polygon in the POLYGON table
SRID=4326;POLYGON((-103.305496375 20.8527356811533,-103.0810348125 20.6129741620855,-101.6261045 20.5375321062668,-99.8356786875 20.3958770270624,-99.803065375 22.0572706994358,-99.649948125 28.9186361984516,-121.1212769375 8.69559423007209,-103.305496375 (...)
I visualized the situation in qgis, see picture below:
As you can see, the 2 points are inside the polygon. So i manually created a point outside the polygon. After that, you can use the following sql query, to see if the points are inside the polygon:
SELECT ST_Contains(polygon.geom, point.geom)
FROM public."LOCATIONS_TABLE" point, public."POLYGON" polygon
It returns t for the 2 points inside and false for the third point.

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);