Quantcast
Channel: Question and Answer » pgrouting
Viewing all 47 articles
Browse latest View live

How do I create a new vertex in the middle of an edge in postGIS?

$
0
0

I have a postGIS database populated with OSM data with the osm2po import tool. I have a number of fixed points that I want to route between using pgrouting. In order to do this accurately, I want to create new nodes (vertices) at nearest points on nearest edges to the actual coordinates to be used as sources and targets for routing. I also want to replace the original edge with the two edge fragments split from the original and have one of their endpoints be the new node and the other endpoint be the original endpoints of the original edge. Since the points are fixed, creating a fixed number of these new nodes permanently to the database should not be an issue.

What would be the best way to achieve this? It looks like the ST_NewEdgesSplit function [1] might be able to do this but it requires a parameter “varchar atopology”, which I assume is a topology schema name. I’m not sure I have one, since I’ve constucted the databse with osm2po.


how to Find nearest line for the given point from a collection of lines?

$
0
0

I have table with lines(linestring).For a point wich i give it , I want to find out nearest line in point.
I almost developed a query, but it is return the same line every point i give.

the query

   SELECT "NAME_LABEL", ST_Distance(ST_Transform(ST_GeomFromText('POINT(40.587840 22.951521)',4326),2100),geom) AS distance FROM geom ORDER BY distance ASC LIMIT 1;

PgRouting function wrapper

$
0
0

I’m trying to adapt the pgr_fromAtoB function to pgr_fromAtoMult replacing pgr_dijkstra function to pgr_drivingdistance.Here is the code:

CREATE OR REPLACE FUNCTION pgr_fromAtoMult(IN tbl character varying, IN x double precision, IN y double precision, OUT seq integer, OUT gid integer, OUT name text, OUT heading double precision, OUT cost double precision, OUT geom geometry)

  RETURNS SETOF record AS
$BODY$
DECLARE
    sql     text;
    rec     record;
    source  integer;
    point   integer;
    dist    double precision;

BEGIN
        -- Find nearest node
        EXECUTE 'SELECT id::integer FROM layer_vertices_pgr
                        ORDER BY the_geom <-> ST_GeometryFromText(''POINT('
                        || x || ' ' || y || ')'',4326) LIMIT 1' INTO rec;
        source := rec.id;

        -- Changing pgr_dijkstra to pgr_drivingdistance query 
        seq := 0;
        sql := 'SELECT id, geom_way, osm_name, cost, source, target,
                                ST_Reverse(geom_way) AS flip_geom FROM 

' ||
                        'pgr_drivingdistance(''SELECT id as id, source::int, dist::double precision, '
                                        || 'time_4::float AS cost FROM '
                                        || quote_ident(tbl) || ''', '
                                        || source || ', ' || dist
                                        || ' , false, false), '
                                || quote_ident(tbl) || ' WHERE id2 = id ORDER BY seq';

        -- Remember start point
    point := source;

            -- Flip geometry (if required)
            IF ( point != rec.source ) THEN
                    rec.geom_way := rec.flip_geom;
                    point := rec.source;
            ELSE
                    point := rec.target;
            END IF;

            -- Calculate heading (simplified)
            EXECUTE 'SELECT degrees( ST_Azimuth(
                            ST_StartPoint(''' || rec.geom_way::text || '''),
                            ST_EndPoint(''' || rec.geom_way::text || ''') ) )'
                    INTO heading;

            -- Return record
            seq     := seq + 1;
            gid     := rec.id;
            name    := rec.osm_name;
            cost    := rec.cost;
            geom    := rec.geom_way;
            RETURN NEXT;

    RETURN;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE STRICT
  COST 100
  ROWS 1000;
ALTER FUNCTION pgr_fromAtoMult(character varying, double precision, double precision)
  OWNER TO postgres;

And here is the Error:

ERROR:  el registro «rec» no tiene un campo «source»
CONTEXT:  sentencia SQL: «SELECT ( point != rec.source )»
función PL/pgSQL pgr_fromAtoMult(character varying,double precision,double precision) en la línea 32 en IF
********** Error **********

ERROR: el registro «rec» no tiene un campo «source»
SQL state: 42703
Context: sentencia SQL: «SELECT ( point != rec.source )»

question about Pgrouting dijkstra algorithm

$
0
0

I own two tables one with streets and one points, i use the table with the roads to calculate routes with dijkstra algorithm. But I want when someone add one point in the table Point and this point is part of road from streets table ,then how i make the dijkstra algorithm not to take into consideration this road wich contain this point, when it calculate a route.


If i want to goa from A –> B is give me the read way the dikjstra . i want if i put a point like the point of image the dijkstra change the way and give me the orange way. Can i make this?

enter image description here

pgr_drivingdistance wrapper

$
0
0

Pgr_drivingdistance function works as :

SELECT seq, id1 AS node, cost
    FROM pgr_drivingDistance(
            'SELECT id, source, target, cost FROM edge_table',
            id, distance, false, false
    );

How could I get this to work substituting x, y for id?

SELECT seq, id1 AS node, cost
    FROM pgr_drivingDistance(
            'SELECT id, source, target, cost FROM edge_table',
            x,y, distance, false, false
    );

Checking the pgrouting functions in Postgis I have found SQL code to change but not for pgr_drivingdistance.

Syntax error drivingdistance

$
0
0

I am new in sql and i am trying to get that works a subquery with pgr_drivingdistance and pgr_pointsaspolygon but i get the next error:

subqueries in FROM must to have an alias,for example, FROM (SELECT …) [AS] foo.But in the code below the alias is di

Here is the code:

SELECT 1 as result,* FROM  pgr_pointsaspolygon('SELECT id, ST_X(the_geom) AS x, ST_Y(the_geom) AS y FROM ( 
     SELECT id,seq, id1 AS node, id2 AS edge, cost, the_geom FROM pgr_drivingdistance(
       ''''SELECT id, source, target, time_4 as cost FROM jaen'''',
          1, 15, false, false
      )as di
      JOIN jaen_vertices_pgr pt
      ON di.id1 = pt.id)');

Indoor routing using Leaflet and pgrouting

$
0
0

I am going to create a project with leaflet and pgrouting for indoor mapping. now I want to search for possibilities of doing this,I want to chose right library for this .Does any one has any experiences about indoor routing?
I want to use pgrouting as my routing core (I don’t think there is better approach for routing indoor) and to show maps I want to use leaflet ,Here I have some quetions about leaflet..Is there any example how to use pgrouting and leaflet together?
Thank you so much.

pgRouting. pgr_ksp() returns -1 as id3


How to make a routable network topology?

$
0
0

We are trying to make a network topology for pgRouting from our own data. But there is a problem we could not resolve; our road data look like this;

enter image description here

The red edge is a single geometry, so we can not make a route between these nodes.
How can we convert this data to be able to make routing work?
Thanks in advance.

Create a route by using points (pgRouting)

$
0
0

I’m currently working on a routing model for 17th and 18th century voyages. These routes are based on data from old shipping logbooks.

I made a separate layer in QGIS which contains point. I exported those points to a shape layer, converted them to a sql structure. I am following the pgRouting workshop, and I am currently trying to convert it to a network topology using the pgr_createTopology function.

However, at the moment I’m getting an error (false) and I am wondering whether this is point-model is the right way to approach it. Especially since the osm examples seem to use a closed model where every line is connected.

Edit:
idea

So this is basically what I’m looking for. I have a different network of places inside a table, and I have a table with the routes that are basically points. They do not overlap. By using a function I want to connect the points to the route and make a simple polyline. Apparently this isn’t possible with pgrouting, but maybe it still is possible by just using postgis? I need the routes of 20 000 voyages, so the network doesn’t need to contain more complicated stuff such as bidirectional. I do need this (for instance the Brouwer Route only goes from The Cape/Cape Town to Indonesia and not vice versa), but I could probably cheat this by making different networks and writing queries that specify different voyages.

pgrouting pgr_dijkstra forcing route along a selected edge?

$
0
0

I have routing working where a user clicks 2 points on the map, I find the closest vertex to those 2 clicked locations and pass that to a pgr_dijkstra function.

What I would like is give the edge a user clicks a dynamically lower cost, which would force the route to go that way, even if there is another route that is the same cost, or maybe even less.

So given this image, a user clicks A & B, the route could go either way, as the length/cost is the same. But since the user clicked the “B” on the left edge, that should be given priority.

enter image description here
Is this possible?

This is my query now:

SELECT id, geom, trailid, cost, reverse_cost, source, target, ST_Reverse(geom) AS flip_geom 
FROM pgr_dijkstra(
   'SELECT id, source::int, target::int, length::float AS cost, reverse_cost::float AS reverse_cost 
   FROM edge_table 
   WHERE ST_Contains(bbox, 4326), geom)', 
source, target, false, true), edge_table 
WHERE id2 = id ORDER BY seq;

This is a prime example, I could do either way, but I would prefer to route up the green climbing trail.
http://www.trailforks.com/region/vedder-mountain/planner/?waypoints=49.076580,-121.986912:49.070167,-122.000207

PGR_Djikstra function error

$
0
0

I’m trying to find a shortest path between 2 nodes using djikstra method but it keeps saying

ERROR: Error, query must return columns ‘id’, ‘source’, ‘target’ and ‘cost’

this is my query:

SELECT seq, id1 AS node, id2 AS edge, cost
FROM pgr_dijkstra('SELECT gid, source,target, ST_Length(geom2d) as cost FROM  public.canada_rail_net', 1, 200, false, false);

is there anything wrong with column names?

How to connect QGIS to PostGIS? [duplicate]

pgRouting: weight routes with similar attributes higher

$
0
0

I am creating my own routable network using pgRouting. I would like my shortest path queries to prefer a route made up of the same attribute.

For example, in the image below (please excuse my terrible drawing), I don’t necessarily care if the best route is made up of either A or B as long as it’s only A or only B.

enter image description here

Retrieving the data from PgRouting

$
0
0

Currently i have created the layer in Geoserver by configuring a native SQL statement, there i am getting the route geometry details from DB by following query

   SELECT ST_MakeLine(route.geom) FROM (
    SELECT geom FROM pgr_fromAtoB('ways', %x1%, %y1%, %x2%, %y2%
  ) ORDER BY seq) AS route

this will give me linestring but along with this i want some other details i.e name of the node etc for that i just modified the query as

 SELECT ST_MakeLine(route.geom),text(route.name) FROM (
    SELECT geom,name FROM pgr_fromAtoB('ways', %x1%, %y1%, %x2%, %y2%
  ) ORDER BY seq) AS route

name above is nothing but the name of the route and text() will returns me the name if each route.But this gives syntax error.How to overcome this issue?


Dijkstra's Algorithm direction issue

$
0
0

I am trying to implement Dijkstra’s algorithm, my query is working fine if I set direction parameter to false. I have to consider the direction of travel while calculating route from point A to point B. This algorithm determines the direction by the order of the coordinates in the linestring, this is where I am facing problem. If I am traveling from north to south and the linestring coordinates of that route are digitized from south to north then the algorithm is not returning the correct route. I am using geometry length as cost and reverse_cost.

So, my question is how can I solve this problem? I have another column in my table containing Boolean value if the road is one way or not, can I use this column somehow to find the correct route?

Adding Special Nodes to OSM Data for pgRouting

$
0
0

I am currently creating a system to route pedestrian to open areas. I used OSM data and osm2po to parse the data for pgRouting. Now, I want to create new nodes with an additional attribute or flag to mark that it is an open area. Are there tools that can help me do that? Thank you!

Overlapping line aggregation+trim in PostGIS, while summing values

$
0
0

I’m sorry for the vague title, I didn’t really know how to name this. I am working on a project that visualizes and simulates 17th century trade. For this project we also use so called Minard lines to show how certain goods are distributed. To clarify I made this screenshot:

http://i.imgur.com/nzI7mUv.jpg

However, as you can see every route is a seperate line (Amsterdam <> Jakarta and Rotterdam <> Jakarta for instance) and they all overlap with each other. This does give a nice Tron effect, but we actually want to merge them. We want to merge the lines that overlap (while adding their values), and segmentize those that don’t (while remaining their values).

I’ve been thinking a lot about this on how to do this in postgres and postgis, but I’m having real troubles with it. Functions like ST_union aggregates everything, Could anyone point me in the right direction? Here’s a screenshot of the database:

http://i.imgur.com/bcnWbOm.png

Multiple queries in pgrouting (length as result)

$
0
0

I am facing the following problem with two tables:

The first table contains all my streetnetwork with cost, source, target. Another table contains 60 nodes (from the above network). Between all these 60 nodes I want to calculate the length of the path (e.g.: from node 1 to 2,3,4…60, from node 2 to 1,3,4,5…60 etc.)

Do you have a solution which could automatically calculate the length of all paths? Until now I can just calculate manually one path, then save the table and calculate the length.

But as you can imagine I don’t wanna calculate 60*60 relations manually.

See my SQL-query so far:

SELECT seq, id1 AS node, id2 AS edge, a.cost, geom_way
FROM pgr_dijkstra(
'SELECT id, source, target, km as cost, km as reverse_cost FROM 
routing where cost is not null',
(SELECT nodes FROM nodelist WHERE id =1),
(SELECT nodes FROM nodelist WHERE id =2), 
false, 
true
) AS a
Join routing b
ON a.id2 = b.id
ORDER BY seq;

GeoServer Sql view setup error?

$
0
0

The problem goes like this: I am trying to route osm data with the help of this tutorial: (http://workshop.pgrouting.org) but in the step where I create the sql view and add the parameters it fails to create the layer boundaries.

This is the error I get:

java.lang.RuntimeException: java.io.IOException: Error occured calculating bounds for pgrouting
at org.geotools.jdbc.JDBCFeatureSource.getBoundsInternal(JDBCFeatureSource.java:540)…..
aused by: java.io.IOException: Error occured calculating bounds for pgrouting
at org.geotools.jdbc.JDBCDataStore.getBounds(JDBCDataStore.java:1309)
at org.geotools.jdbc.JDBCFeatureSource.getBoundsInternal(JDBCFeatureSource.java:533)
… 116 more
Caused by: org.postgresql.util.PSQLException: ERROR: Support for id,source,target columns only of type: integer. Support for Cost: double precision
Where: PL/pgSQL function pgr_dijkstra(text,bigint,bigint,boolean,boolean) line 6 at assignment
PL/pgSQL function pgr_fromatob(character varying,double precision,double precision,double precision,double precision) line 35 at FOR over EXECUTE statement
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)

Does anyone have a clue?

Viewing all 47 articles
Browse latest View live