Postgres pgrouting2 Dijkstra shortest path returns edges that don't exist - postgresql

I am struggling with a very strange problem for a few days now that I can't find a solution.
I am using postgresql 9.3 with postgis 2 and pgrouting 2 extentions. I have imported OSM data for a city and created topology network successfully with pgr_createTopology() function.
I can successfully find shortest path with Dijkstra algorithm by executing for example (ignore the simplified cost function)
SELECT * from pgr_dijkstra(
'SELECT id, source, target, st_length(way) as cost FROM planet_osm_roads',
5744, 5900, false, false
)
and getting the following result (seq,id1,id2,cost)
0;5744;178191032;428.359590042932
1;5749;177327184;61.7533237967002
2;5821;177327456;544.454553269731
3;5833;177338744;51.1559809959342
4;5871;177338880;71.0702814120015
5;5900;-1;0
The problem is that the returning id2 values, which corresponds to the id of the edges, are not present in the planet_osm_roads table. Actually those values cannot be found in any column of planet_osm_roads or planet_osm_roads_vertices_pgr tables. Am I missing something? Maybe someone had faced the same problem before.
Thank you all in advance

What kind of values to you have for edge ids?
pgRouting only supports 32 bit integer values, if your ids are larger then they will get silently truncated. This is a known problem.

Related

ETL Mapping LOINC vocabulary on OMOP Common Data Model

I am working on the lab test values mapping (MEASUREMENT table of the OMOP CDM).
My local mapping table (handmade) has my measurement name (in French) and the associated LOINC code.
The LOINC vocabulary has been loaded from Athena (OHDSI community tool) https://athena.ohdsi.org/search-terms/
I load my local concepts into the CONCEPT table, then use an SQL query to associate the equivalent LOINC concept_id (from concept_code mapping/LOINC source codes).
I realise that the link is not made on the LOINC concept_code.
Indeed, when I filter the CONCEPT table on a LOINC concept_code (ex 34714-6) I find no result.
select *
from omop.concept
where concept_code in ('34714-6');
When I filter on the corresponding concept_id (3032080) I find the result with the desired concept_code.
select *
from omop.concept
where concept_id in ('3032080');
I have tested concept_code like '34714__' which returns the expected line.
This is not due to the encoding because when I copy/paste the resulting concept_code (filtering on concept_id = ‘3032080’) into my query concept_code in ('34714-6') I get the same problem.
However other LOINC codes work:
select *
from omop.concept
where concept_code in ('14646-4');
When I check what symbol exacty is being used :
select ASCII(substr(concept_code,1,1))
,ASCII(substr(concept_code,2,1))
,ASCII(substr(concept_code,3,1))
,ASCII(substr(concept_code,4,1))
,ASCII(substr(concept_code,5,1))
,ASCII(substr(concept_code,6,1))
,ASCII(substr(concept_code,7,1))
from omop.concept
where concept_id = 3032080 ;
I also checked/removed the whitespaces.
The same process works on drugs (concept_code from ATC).
Can you tell me where this error comes from?
Thank you for your help.
please check that you're using the latest sql-client and JDBC driver versions

POSTGIS "ST_Contains" returns a empty query

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

Problems with spatial join

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.

Can I have more than 250 columns in the result of a PostgreSQL query?

Note that PostgreSQL website mentions that it has a limit on number of columns between 250-1600 columns depending on column types.
Scenario:
Say I have data in 17 tables each table having around 100 columns. All are joinable through primary keys. Would it be okay if I select all these columns in a single select statement? The query would be pretty complex but can be programmatically generated. The reason for doing this is to get denormalised data to populate a web page. Please do not ask why though :)
Quite obviously if I do create table table1 as (<the complex select statement>), I will be hitting the limit mentioned in the website. But do simple queries also face the same restriction?
I could probably find this out by doing the exercise myself. In the next few days I probably will. However, if someone has an idea about this and the problems I might face by doing a single query, please share the knowledge.
I can't find definitive documentation to back this up, but I have
received the following error using JDBC on Postgresql 9.1 before.
org.postgresql.util.PSQLException: ERROR: target lists can have at most 1664 entries
As I say though, I can't find the documentation for that so it may
vary by release.
I've found the confirmation. The maximum is 1664.
This is one of the metrics that is available for confirmation in the INFORMATION_SCHEMA.SQL_SIZING table.
SELECT * FROM INFORMATION_SCHEMA.SQL_SIZING
WHERE SIZING_NAME = 'MAXIMUM COLUMNS IN SELECT';

PostGIS Conversion Issues

I am having an issue using PostGIS (1.5.4) data. It may be that I'm just not familiar enough with this technology to see the obvious (I'm a regular expert with nearly 4 hours of experience), but I am running into an error that I have been unable to solve with Google.
I have a table which includes Polygon data (and yes, I checked; the column type is geometry, not polygon- the Postgres native type). The problem arises when I am trying to run a query on the table to find which shape contains a particular problem.
I am using the following query:
SELECT *
FROM geo_shape
WHERE ST_Contains(geoshp_polygon, POINT(-97.4388046000, 38.1112251000));
The error I receive is 'ERROR: function st_contains(geometry, point) does not exist'. I tried using a CAST() function, but got 'ERROR: cannot cast type geometry to polygon'. I'm guessing the issue has to do with the way the data is stored- PGAdmin shows it as hex data. I tried using ST_GeomFromHEXEWKB() just on a hunch, but received 'ERROR: function st_geomfromhexewkb(geometry) does not exist'.
I'm fairly confused as to what the issue is here, so any ideas at all would be much appreciated.
st_contains needs a geom,geom as arguments...
Give this a try...
SELECT * FROM geo_shape
WHERE ST_Contains(geoshp_polygon,
GeomFromText('POINT(-97.4388046000 38.1112251000)'));
Editted to correct , issue in the point data. ST_geomfromtext will work, kinda curious what the difference is there
You cannot mix PostgreSQL's geometric data types with PostGIS's geometry type, which is why you see that error. I suggest using one of PostGIS's geometry contstructors to help out:
SELECT *
FROM geo_shape
WHERE ST_Contains(geoshp_polygon,
ST_SetSRID(ST_MakePoint(-97.4388046000, 38.1112251000),4326);
Or a really quick text way is to piece together the well-known text:
SELECT 'SRID=4326;POINT(-97.4388046000 38.1112251000)'::geometry AS geom;
(this will output the WKB for the geometry type).