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;
Related
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
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;
I have import OpenstreetMap data into Postgres with gis extension with tool
osm2pgsql (-s option)
of course, I have the following tables
planet_osm_point
planet_osm_ways
....
Within planet_osm_ways I have a column called way, type geometry(LineString, 4326), content like following
"0102000020E6100000070000005E70BCF1A49F2540D3D226987B134840896764EB749F25403B5DCC858013484040D1860D609F2540C426327381134840CE50DCF1269F2540EF552B137E1348405AAB2CC02D9E2540F978324976134840D66F26A60B9D2540CE8877256E1348403CA81F2FFF9C2540BC1D86FB6D134840"
What is that ? How could I get all points along this way ?
Thanks a lot
That's hex-encoded extended well-known binary (EWKB) of a LINESTRING.
There are several methods to get the points along the way. To get individual coordinates as points, use ST_DumpPoints. Or to simply output the geometry in other human-readable formats (WKT, EWKT, GeoJSON, GML, etc.), see the relevant manual section.
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.