postgresql+postgis+pgrouting implementation of Shortest Path query (1)---line data processing and building topology

Source: Internet
Author: User
Tags postgis

Prepare a line SHP data and import it into the postgres inside, postgres install PostGIS and pgrouting two plugins (see http://www.cnblogs.com/nidaye/p/4553522.html for a method). The fields of the line data are as follows: note the name of the field, save the unnecessary hassle of appearing.

1, ALTER TABLE beijing_line ADD COLUMN source integer;

ALTER TABLE beijing_line ADD COLUMN target integer;

ALTER TABLE beijing_line ADD COLUMN length double precision;

UPDATE beijing_line SET length = st_length (THE_GEOM);

PS: Perform the following three-sentence SQL query against the database before executing the createtopology function:

CREATE EXTENSION PostGIS;

CREATE EXTENSION postgis_topology;

CREATE EXTENSION Fuzzystrmatch;

2. Select Pgr_createtopology (' Beijing_line ', 0.001,source:= ' source ', id:= ' gid ', target:= ' target ', the_geom:= ' the_ Geom ');

Select Pgr_createtopology (' Beijing_line ', 0.1,source:= ' source ', id:= ' gid ', target:= ' target ', the_geom:= ' the_geom ');

Setting the tolerance to a larger point may result in better results

3. SELECT * from Pgr_dijkstra ('

SELECT GID as ID,

Source::integer,

Target::integer,

Length::d ouble Precision as cost

From Beijing_line ',

(), false, false);

No query results do the following to try, or there may be no path between two points

Select source from Beijing_line;

Select target from Beijing_line;

Select length from Beijing_line;

4, all the points after the query:

SELECT St_astext (The_geom) from Pgr_dijkstra ('

SELECT GID as ID,

Source::integer,

Target::integer,

Length::d ouble Precision as cost

From Beijing_line ',

+, False, false) as Di

Join Beijing_line PT

on di.id2 = Pt.gid;

5. Write the path in a geometry file:

SELECT seq, id1 as node, id2 as Edge, Cost,the_geom into Dijkstra_res from Pgr_dijkstra ('

SELECT GID as ID,

Source::integer,

Target::integer,

Length::d ouble Precision as cost

From Beijing_line ',

+, False, false) as Di

Join Beijing_line PT

on di.id2 = Pt.gid;

Experienced the above steps, if all goes well, should be able to see the results, here no longer. At this point the preliminary preparations have been completed.

Refer to the following blog post ():

1, one of path planning based on pgrouting

2, pgrouting the introduction of Path planning two (1 and 2 is a shortest path to the blog series, very good to see, but may be because of the different machine configuration environment, may not be able to run other people's code, must understand the principle, and then to the previous code improvement)

3, pgrouting use the foundation

4. Using pgrouting2.0 to find the shortest path

postgresql+postgis+pgrouting implementation of Shortest Path query (1)---line data processing and building topology

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.