I have imported two shape-files using QGIS into pgAdmin 4 (PostgreSQL), I use the PostGIS extension to be able to access spatial commands.
I want to check all the POIS (points) that are included in the Gemeinden (multipolygon) table. To accomplish that I use the spatial command "ST_Contains" ([postgis.net documentation regarding the command])1. Contrary to the fact that the points are actually in the polygons, the returned query is empty (see QGIS and output screenshot). What could be the issue? Any help would be appreciated. Thank you in advance!
Output - pgAdmin
The command I use:
SELECT * FROM public."POIS" AS pois INNER JOIN public."Gemeinden" AS gem
ON (1 = 1)
WHERE ST_Contains(gem.geom, pois.geom) = true;
My tables:
POIS table content
[3
Gemeinden table content
[4
QGIS Screenshots:
Both shape-files together:
[5
Only the POIS shape-file:
[6
Only the Gemeinden shape-file:
[7
Update:
I have created a polygon table out of the multi-polygons using the command
CREATE TABLE polygon_table AS
SELECT id, public."Gemeinden".kg_nr, public."Gemeinden".kg, (ST_DUMP(geom)).geom::geometry(Polygon,4326) AS geom FROM public."Gemeinden";
Afterwards I've updated the SRID of both the POIS table and the newly created one using :
SELECT UpdateGeometrySRID('polygon_table','geom',4326);
and
SELECT UpdateGeometrySRID('POIS','geom',4326);
Sadly,
SELECT pois.* FROM public."POIS" AS pois JOIN public."polygon_table" AS
gem
ON ST_intersects(gem.geom, pois.geom);
still returns a empty query.
Any ideas? Thank you in advance!
First of all make sure the SRID of the both table must be same. If it is not same then spatial queries wont work.
Secondly convert the multipolygons to single polygons. Following link may help
PostGIS - convert multipolygon to single polygon
Finally the following query would be enough if you just want to get the POIS that intersects the polygons
SELECT pois.* FROM public."POIS" AS pois JOIN public."Gemeinden" AS gem
ON ST_intersects(gem.geom, pois.geom);
I have resolved the problem by going back to QGIS and saving both previously imported shape-files as the same SRID. Apparently, my code that converted the SRID in the database did not work.
Right click on the shape-file
Selecting the SRID
Note: I have done this for both shape-files, just to be sure.
Another factor that may have influenced the outcome was the selection of the "convert to single polygon" box while importing into the database from QGIS.
The box that I ticked for both shape-files while importing
PS: I use the German version of QGIS
Related
I have two tables with geometry data. One table has MULTIPOLYGON geometry in non-standard srid and another has POINT geometry in 4326. I have a spatial_ref_sys table with a correct description for my srid. Then I want to use st_contains for geometry from these tables. I use st_transform to convert geometry from one table to srid from another, but the function returns geometry under Africa (all geometry is on the territory of Europe). Also, I want to admit that I use proj description from QGIS, and in this program, all works correctly. I use PostGIS 3.2. Here is my code for st_contains:
select st_contains(st_transform(geom_multipolygon_in_4326, SRID_from_another_table), geom_point_in_non_standart_srid);
Here is my proj:
+proj=tmerc +lat_0=0 +lon_0=30 +k=1 +x_0=-10000 +y_0=-5540000 +ellps=krass +towgs84=23.92,-141.27,-80.9,0,0.35,0.82,-0.12000000004786 +units=m +no_defs
However, when I use PostGIS 2.2 all works correctly with an identical proj description. I don't understand what the problem is. Maybe someone can help me, because I only found a solution by creating a layer in qgis and then importing it into the database, but this is not a solution for me, because the point geometry is formed through a query to Google and I still need to transform it using st_transform.
I am very new in GIS. I am using an existing data set which is a collection of triplets along with its trajectory ID. I need to find similar trajectories. I am using postgres as database. I have imported the data set and I want to visualize the trajectories.How can I convert the existing data in linestring?
Do you just need a LineString for each trajectory ID (meaning that by 'finding similar trajectories')? If so, you want to use ST_MakeLine (spatial aggregate version) in conjunction with ST_MakePoint. For PostGIS 9.0 and higher you could do something like:
SELECT points.trackid,
ST_MakeLine(points.point_geom ORDER BY points.timedetails) AS line_geom
FROM (SELECT data.trackid,
data.timedetails,
ST_MakePoint(data.longitude, data.latitude) AS point_geom
FROM your_table AS data) AS points
GROUP BY points.trackid;
I am new to sql, and attempting to use it to speed up spatial analysis on a set of ~1.2 million trips from a csv that contains the lat and lon for pickup and dropoff points.
What I am trying to do in plain English is:
select all trips that start in the area of interest (loaded into my database as a shapefile) into one table
select all trips that end in the area of interest into another
-perform a spatial join between these points and a shapefile of census tracks (which contains neighborhood names)
count by neighborhood name to list the most frequent origins/destination of trips to/ from the area of interest.
The code I am working with is below (If its helpful, NTA or neighborhood tabulation area, is the neighborhood name which I want to display in my table at the end of this operation) :
--Select all trips that end in project area
SELECT *
INTO end_PA
FROM trips, projarea
WHERE ST_Intersects(trips.dropoff, projarea.geom);
--for trips that end in project area - index by NTA of pick up point
ALTER TABLE end_PA ADD COLUMN GID SERIAL;
CREATE TABLE points_ct_end AS
SELECT nyct2010.ntacode as ct_nta, end_PA.gid as point_id
from nyct2010, end_PA WHERE ST_Intersects(nyct2010.geom , end_PA.pickup);
--Count most common NTA
--return count for each NAT as a csv
copy(
select count(ct_nta) from points_ct_end
group by ct_nta
order by count desc)
to 'C://TaxiData//Analysis//Trips_Arriving_LM.csv' DELIMITER ',' CSV HEADER;
However, I am having problems from the very start - ST_Intersects does not return any points within the area of interest!
Troubleshooting solutions I have tried thus far:
My first thought is that the points weren't in the correct SRID. When I created the 'dropoff' point I set the SRID to 4326. I tried both using ST_SetSRID to change the projection of both data sets to 4326, and manually re projecting the shapefiles to 4326 in ArcMap - but neither worked.
I plotted a small sample of the points from the 'trips' data set in Arc Map to ensure they were correctly projected and overlapping with the ProjArea shapefile. They are.
I imported the multipoint shapefile this created into my geo database to test if that worked with ST_Intersects. Nope.
I tried using ST_Within. This threw the error message:
ERROR: function st_within(character varying, geometry) does not exist
....
HINT: No function matches the given name and argument types. You
might need to add explicit type casts.
I am using Big SQL and postgres
Thanks!!
My first thought is that the points weren't in the correct SRID. When I created the 'dropoff' point I set the SRID to 4326. I tried both using ST_SetSRID to change the projection of both data sets to 4326, and manually re projecting the shapefiles to 4326 in ArcMap - but neither worked.
ST_SetSRID doesn't change the projection (reproject). It just changes the internal representation. This can totally screw everything up if the previous SRID matched the input data. You likely wanted ST_Transform().
There isn't enough information here to trouble shoot this problem. However, we can answer this...
ERROR: function st_within(character varying, geometry) does not exist
This simply means the first argument is not a geometery. Of course, we can't do anything with that at all because we don't have your query that you tried with ST_Within().
Your syntax for ST_Intersects() looks to be right. But, there simply isn't enough information provided to help. Show some schema and sample data.
I started to use PostGIS recently and i have a table with a column longitudes and a column latitudes.
I want to know if it's possible (and how) to put these lon/lat columns informations into the new geographical column (with possibly ST_GeogFromText)
I already tried something like :
UPDATE my_table SET geo = ST_GeogFromText('SRID=4267;POINT(lon.my_table lat.my_table)');
Thanks for your reading and you help.
ST_GeogFromText takes a formatted text of EWKT, which is a lossy and inefficient method.
Try using ST_MakePoint instead:
UPDATE my_table SET geo = ST_SetSRID(ST_MakePoint(lon, lat), 4267)::geography;
In the title is what I need.
CREATE TABLE newTable1 AS SELECT t2.name,t2.the_geom2
FROM t1,t2
WHERE ST_Contains(ST_Expand(t2.the_geom2,0.05),t1.the_geom1)
and t1.gid=2;
CREATE TABLE newTable2 AS SELECT t1.the_geom,t1.label FROM t1 WHERE t1.gid=2;
First query result is all points within polygon and apart from it for 5min where this polygon has gid=2. But I also want to display this polygon. I tried to write in first query
... AS SELECT t2.name,t2.the_geom2,t1.the_geom1,t1.label ...but got only points without polygon...
This question is linked with already asked question "How to find all points away from some polygon?". But didn't get answere, so please...
And is ST_expand ok solution or it will be better to use ST_DWithin or ST_buffer ?
You can't combine two CREATE TABLE statements into one. Why are you creating tables if you are just querying data?
It sounds like what you are really trying to do is one query that will give you the points within the polygon and the polygon itself. Something like this?
SELECT
t1.the_geom AS polygon, t1.label AS polygon_label,
t2.the_geom2 AS point, t2.name AS point_name
FROM
t1, t2
WHERE
ST_Contains(ST_Expand(t2.the_geom2,0.05), t1.the_geom1)
AND t1.gid = 2;
If this is still not clear, post your complete table definitions and more details about what you are trying to do.