Kindly help meI am using OSM data to write a PostgreSQL PL/Python function doingrun pgr_dijkstra() for weighted-path calculation. But after importing OSM data using osm2pgsql I populated some additional columns into my ways table. So now it has gid serial, the_geom geometry, source integer, target integer, source_height double precision, target_height double precision plus some other columns. The source_height and target_height columns I have populated using the exact job asDEM of the following Python coderegion. My problem
Simply running the pgr_dijkstra() on ways will give seq, id1 (node), id2 (edge), cost columns. What I want is mainlyto also determine the source_height or target_height for a particular edge if the node is equal to the source or target. I was trying to write a procedural function using PL/Python syntax. Thanks(although I don't know which procedural language is better) but couldn't complete.
import psycopg2 import osgeo.ogr connection = psycopg2.connect("host='localhost' dbname='my_db' user='postgres' password='pass123'") cursor1 = connection.cursor() cursor1.execute("SELECT id FROM table1") for i in cursor1: if i[0]>=100: print '+100' elif i[0]<100: print '-100' connection.commit() Let me rephrase my questionFollowing is the attempt to create a PL/Python function. The problem is I am almost done. Just wannadon't know some codes indicated byhow to append all the comments:rows coming from the for loop into a variable to return it as a table.
CREATE OR REPLACE FUNCTION test1(source int, target int, tablename text) RETURNS setof varchar # I am not sure if this return type is correct AS $$ import psycopg2 import osgeo.ogr import math statement1 = ("select count(*) from pgr_dijkstra(' SELECT gid AS id, source::integer, target::integer, length_dem::double precision AS cost FROM kn_ways_new_split_saveas'%s', 350%s, 329%s, false, false) a LEFT JOIN kn_ways_new_split_saveas%s b ON (a.id2 = b.gid)" % (tablename, source, target, tablename)) statement2 = ("select seq, id1 AS node, id2 AS edge, cost, b.the_geom, b.source, b.target, b.slope_st_pt, b.slope_end_pt from pgr_dijkstra(' SELECT gid AS id, source::integer, target::integer, length_dem::double precision AS cost FROM kn_ways_new_split_saveas'%s', 350%s, 329%s, false, false) a LEFT JOIN kn_ways_new_split_saveas%s b ON (a.id2 = b.gid)" % (tablename, source, target, tablename)) count1 = plpy.execute(statement1) run2 = plpy.execute(statement2) len1 = count1[0]['count'] row1 = []; for i in range(len1): if run2[i]['node']==run2[i]['target']run2[i]['node']==run2[i]['source']: # Here I wantCode to append my row1 variable with corresponding row's node, edge, the_geom etc. and slope_st_pt as slope to row1 elif run2[i]['node']==run2[i]['source']run2[i]['node']==run2[i]['target']: # Here I wantCode to append my row1 variable with corresponding row's node, edge, the_geom etc. returnand row1;slope_end_pt #as Hereslope to row1 return therow1; final# tableReturn row1 table just created byusing abovethe for loop above $$ LANGUAGE 'plpythonu' VOLATILE;