Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance to nearest polling station on a map #3

Open
tdamsma opened this issue Mar 23, 2017 · 4 comments
Open

Distance to nearest polling station on a map #3

tdamsma opened this issue Mar 23, 2017 · 4 comments

Comments

@tdamsma
Copy link

tdamsma commented Mar 23, 2017

Inspired by @hpfast presentation on distance to nearest polling station in Utrecht, I had a quick go at this issue using pgroute. Just wanted to share my findings:

I loaded the Rotterdam metro area (300k nodes), and defined 33 facilities (facility being the polling station or some other Point of Interest). Using the function pgr_drivingdistance it is possible to do the entire query in one go (define distance to multiple sources), reducing query time for my case to just under 10 seconds on my laptop.

The query is as follows:

--DROP MATERIALIZED VIEW distances;
CREATE MATERIALIZED VIEW distances AS (

WITH 
--first map the facilities to the nearest node on the network
node_facility AS (
    SELECT
		facilities.id AS facilities_id,
        (SELECT 
            nodes.id AS node_id
        FROM ways_vertices_pgr AS nodes
        ORDER BY facilities.geom <#> nodes.the_geom
        LIMIT 1)
        FROM facilities
    ),
--aggregate all nodes in opne array
nodes AS (
    SELECT array_agg(node_id) AS nodes FROM node_facility
    ),
--then calculate the distance from every point on the network to each of the 
--nodes. Stop searching after max distance (10km in this example) is reached.
distance AS (
    SELECT *
    FROM nodes, pgr_drivingDistance('
        SELECT gid AS id,
            source::int4,
            target::int4,
            length_m::float8 AS cost
        FROM ways',
        nodes.nodes,
        10000::FLOAT,
        false,
        false))
--use the result to select the single nearest facility
SELECT DISTINCT ON (nodes.id) 
    nodes.id, 
    nodes.the_geom, 
	node_facility.facilities_id,
    distance.agg_cost 
  FROM ways_vertices_pgr AS nodes
  JOIN distance ON distance.node = nodes.id
  JOIN node_facility ON node_facility.node_id = distance.from_v
  ORDER BY nodes.id, agg_cost ASC
  )
@hpfast
Copy link

hpfast commented Mar 23, 2017

there we go, that's more like it :)

thanks for sharing this.

@tdamsma
Copy link
Author

tdamsma commented Mar 23, 2017

Almost forgot, based on: http://gis.stackexchange.com/questions/156916/driving-time-to-the-nearest-facility-using-pgrouting/233269#233269

Another thing came to mind: did you consider using nearest neighbour interpolation instead of inverse distance? I think that might be slightly more representative and that would produce nice discontinuous jumps in the gradient where highways and rivers cut through the network

@hpfast
Copy link

hpfast commented Mar 23, 2017 via email

@hpfast
Copy link

hpfast commented Mar 23, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants