I am using post-gis to create a polyline that describes the flow direction of water in a landscape/catchment, to use in a hydrological model. To do that I start with a regular grid (as polygon) and connect the centre of each cell to the centre each of the 8 cells around it, using ST_Intersect. (afterwards I delete the irrelevant lines, based on the elevation of the grid). When I do this, most connections are made as I want, except that only half of the diagonal connections are made:

enter image description here

In the figure the green squares is my grid, and the white lines are the output polyline

here is a code sample I use:

INSERT INTO test_olaf.olafmain(id,id_in,id_out,ngf_in,ngf_out,the_geom,geom_in,geom_out SELECT DISTINCT ON (ST_Intersection(parcel_a.the_geom,parcel_b.the_geom) CAST(CAST(parcel_a.id as TEXT)||CAST(parcel_b.id as TEXT) as INTEGER), 

FROM test_olaf.grid as parcel_a, test_olaf.grid as parcel_b -- test_olaf.grid is my grid (based on a DEM, converted into a polygon)
WHERE ST_Intersects(parcel_a.the_geom,parcel_b.the_geom)


Does anyone know how I can connect my cell-centre to all the cells around it, or why the ST_intersect function does not work for all the cells?

thanks! Annette


Try adding a small buffer to one of the polygons to avoid floating-point errors.

If the units are degrees, 0.00001 will buffer by a meter or two. If the units are meters, use 1.0 or 0.1.

ST_Intersection(ST_Buffer(parcel_a.the_geom, 0.00001),parcel_b.the_geom)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.