Node sorting algorithm

51 views Asked by At

I'm currently developing an algorith that recives a network with nodes positionated in real lifes coords and must return a network with new positions for each node. The main idea is take a real life positions nodes and make a reduced map of an electrical network, preserving the relations and positions for each node to another. (Ideally, return grided positions, for example x:340, Y:670) For this, we can use some of the st functions or the posgis library.

I tryed reducing the real coords network and then aplying a st_snaptogrid function. Then moving the nodes that are positionated in the same position, but this way is deforming the network. Is there some algorithm that we can use? Every idea is welcome.

CREATE OR REPLACE FUNCTION reduce_nodes(
    p_origin_geom    geometry,
    p_factor         float8)
RETURNS void AS $$
DECLARE
    v_node_geom     geometry;
    v_node_id       core_object_id;
    v_x_offset      float8;
    v_y_offset      float8;
    v_node_pin_geom geometry;
BEGIN
    FOR v_node_id, v_node_geom IN SELECT node_id, node_geom FROM tmp_node_positions
    LOOP
        v_node_geom := st_centroid(v_node_geom);
        v_x_offset := (st_x(v_node_geom) - st_x(p_origin_geom)) * 0.9;
        v_y_offset := (st_y(v_node_geom) - st_y(p_origin_geom)) * 0.9;
        v_node_pin_geom :=
            st_snaptogrid(
                st_makepoint(
                    st_x(v_node_geom) - v_x_offset,
                    st_y(v_node_geom) - v_y_offset
                ),
                10
            )
        ;
        UPDATE tmp_node_positions
        SET
            node_geom = v_node_pin_geom,
            x_log     = st_x(v_node_pin_geom),
            y_log     = st_y(v_node_pin_geom)
        WHERE node_id = v_node_id;
    END LOOP;
END;
$$ LANGUAGE plpgsql;
0

There are 0 answers