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

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

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.

Google Data Studio Geo Map stays blank while using Lat,Long as Dimension

I want to use Google Data Studio to visualize the location of NGO´s in Europe. Some of these share the same HQ Location in the Data e.g. Italy Rome. Idea is to transfer these into Lat,Long coordinates and slightly manipulate the values to show them with their name in the GMaps bubble diagram. The original data is unfit, because the digram cannot display multiple tooltips/names for the same location.
I have been following this guide https://michaelhoweely.com/2020/05/04/how-to-build-a-custom-google-map-in-data-studio-using-google-sheets-and-geocode/ to do the Geo Coding of the locations.
Problem is that the Geo Chart goes blank if I choose Lat,Long as the location dimension. Even the original data (not modified by me) will cause the same issue.
GDataStudio_Lat_Long_Vis_Issue
Any idea of what I am doing wrong here or having a better idea of how to do the visualization?
You also have to make sure you change the type to GEO, as it sometimes defaults to ABC or numeric.
It may help to concatenate the latitude and longitude values to follow the WKT format, as shown below.
CONCAT(<longitude_column>, ", ", <latitude_column>)
In the current state, the latitude and longitude are reversed, so the error is probably caused by each value exceeding the default range for latitude and longitude.

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

Convert st_astext result to single text columns

I need some help with postgis.
I have made a point geometry that have converted to text through st_astext.
I currently have these three columns:
object_id(uuid) komnr(int) st_astext_result(text)
2bcc1b26-0a7c-472a-9e74-9cadfefc2d44 320 POINT(687989.85 6135746.91)
I would like two columns each containing the east and north coordinate in the same syntax as it is now. Do you have any suggestions to how i may do this?
Preferred outcome would be:
east north
687989.85 6135746.91

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.