[GRASS-dev] r.distance problem: in our out?

Hi,

for an application I have to calculate the distance between
raster "objects". While this generally works, I have found
that r.distance happily calculates the distance even if
one object is within the other (say, raster pixel over
raster polygon in second map). I would need an indication
somehow that we are *in* (to ignore the distance in this
case).

Spearfish example:

g.region -dp

# extract field #25:
r.mapcalc "myarea=if(fields == 25, 1,null())"

# this archsite falls into field 25:
v.extract archsites out=mypixel list=9
v.to.rast mypixel out=mypixel use=val val=1
r.distance mypixel,myarea
# -> reports two distant points

# zoom:
g.region n=4926990 s=4925856 w=599850 e=601376 align=myarea
d.mon x0
d.rast myarea
d.vect mypixel col=blue
# visualize distance (d.graph give sometimes random output!):
r.distance mypixel,myarea | awk -F: '{print "move",$4,$5,"\ndraw",$6,$7}' | d.graph -m

# generate points from distance for easier verification:
r.distance mypixel,myarea | awk -F: '{print $4,"|",$5,"\n",$6,"|",$7}' | sed 's+ ++g' | v.in.ascii out=distance
d.vect distance col=yellow

# cleanup:
g.remove rast=myarea,mypixel
g.remove vect=mypixel,distance

###
Problem: There is no indication that we are *within* the area. Maybe a flag is
needed to optionally suppress the result in this case?
Better ideas?

Second problem: The result seems to be underestimated by half a cell
since not the cell edge is taken from the raster polygon boundary
but the cell center.

Appreciating help,
Markus

hallo,

On Wed, Oct 04, 2006 at 01:38:25PM +0200, Markus Neteler wrote:

Hi,

for an application I have to calculate the distance between
raster "objects". While this generally works, I have found
that r.distance happily calculates the distance even if
one object is within the other (say, raster pixel over
raster polygon in second map). I would need an indication
somehow that we are *in* (to ignore the distance in this
case).

i did not found any G_is_null(raster_map_name,x,y) or similar function.
is there something like this?

thank you

jachym

one cell from mypixel is "surrounded"

Spearfish example:

g.region -dp

# extract field #25:
r.mapcalc "myarea=if(fields == 25, 1,null())"

# this archsite falls into field 25:
v.extract archsites out=mypixel list=9
v.to.rast mypixel out=mypixel use=val val=1
r.distance mypixel,myarea
# -> reports two distant points

# zoom:
g.region n=4926990 s=4925856 w=599850 e=601376 align=myarea
d.mon x0
d.rast myarea
d.vect mypixel col=blue
# visualize distance (d.graph give sometimes random output!):
r.distance mypixel,myarea | awk -F: '{print "move",$4,$5,"\ndraw",$6,$7}' | d.graph -m

# generate points from distance for easier verification:
r.distance mypixel,myarea | awk -F: '{print $4,"|",$5,"\n",$6,"|",$7}' | sed 's+ ++g' | v.in.ascii out=distance
d.vect distance col=yellow

# cleanup:
g.remove rast=myarea,mypixel
g.remove vect=mypixel,distance

###
Problem: There is no indication that we are *within* the area. Maybe a flag is
needed to optionally suppress the result in this case?
Better ideas?

Second problem: The result seems to be underestimated by half a cell
since not the cell edge is taken from the raster polygon boundary
but the cell center.

Appreciating help,
Markus

_______________________________________________
grass-dev mailing list
grass-dev@grass.itc.it
http://grass.itc.it/mailman/listinfo/grass-dev

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
Department of Geoinformation Technologies
Zemedelska 3
613 00, Brno
Czech Republick
e-mail: xcepicky@node.mendelu.cz
URL: http://mapserver.mendelu.cz
Tel.: +420 545 134 514

On Tuesday 10 October 2006 06:33, Jachym Cepicky wrote:

hallo,

On Wed, Oct 04, 2006 at 01:38:25PM +0200, Markus Neteler wrote:
> Hi,
>
> for an application I have to calculate the distance between
> raster "objects". While this generally works, I have found
> that r.distance happily calculates the distance even if
> one object is within the other (say, raster pixel over
> raster polygon in second map). I would need an indication
> somehow that we are *in* (to ignore the distance in this
> case).

i did not found any G_is_null(raster_map_name,x,y) or similar function.
is there something like this?

thank you

jachym

one cell from mypixel is "surrounded"

> Spearfish example:
>
> g.region -dp
>
> # extract field #25:
> r.mapcalc "myarea=if(fields == 25, 1,null())"
>
> # this archsite falls into field 25:
> v.extract archsites out=mypixel list=9
> v.to.rast mypixel out=mypixel use=val val=1
> r.distance mypixel,myarea
> # -> reports two distant points
>
> # zoom:
> g.region n=4926990 s=4925856 w=599850 e=601376 align=myarea
> d.mon x0
> d.rast myarea
> d.vect mypixel col=blue
> # visualize distance (d.graph give sometimes random output!):
> r.distance mypixel,myarea | awk -F: '{print "move",$4,$5,"\ndraw",$6,$7}'
> | d.graph -m
>
> # generate points from distance for easier verification:
> r.distance mypixel,myarea | awk -F: '{print $4,"|",$5,"\n",$6,"|",$7}' |
> sed 's+ ++g' | v.in.ascii out=distance d.vect distance col=yellow
>
> # cleanup:
> g.remove rast=myarea,mypixel
> g.remove vect=mypixel,distance
>
> ###
> Problem: There is no indication that we are *within* the area. Maybe a
> flag is needed to optionally suppress the result in this case?
> Better ideas?
>
> Second problem: The result seems to be underestimated by half a cell
> since not the cell edge is taken from the raster polygon boundary
> but the cell center.
>
> Appreciating help,
> Markus
>

This is an interesting proposition, with a very nice example of using d.graph
(have to save that one). How hard would it be to implement something like
what Jachym mentioned G_is_null(raster_map_name,x,y) OR something that
explicitly tests for a non-null raster value at some x,y coordinate:

r.distance ---------

// test for overlap between inputs
if( G_overlap_raster(input_a, input_b, x, y) == 1 )
{
//feature is overlapping, return zero distance
dist = 0 ;
continue;
}

//normal distance calculation from here...

-------------------------

cheers,

--
Dylan Beaudette
Soils and Biogeochemistry Graduate Group
University of California at Davis
530.754.7341

hi
On Wed, Oct 04, 2006 at 01:38:25PM +0200, Markus Neteler wrote:

Hi,

for an application I have to calculate the distance between
raster "objects". While this generally works, I have found
that r.distance happily calculates the distance even if
one object is within the other (say, raster pixel over
raster polygon in second map). I would need an indication
somehow that we are *in* (to ignore the distance in this
case).

Spearfish example:

g.region -dp

# extract field #25:
r.mapcalc "myarea=if(fields == 25, 1,null())"

# this archsite falls into field 25:
v.extract archsites out=mypixel list=9
v.to.rast mypixel out=mypixel use=val val=1
r.distance mypixel,myarea
# -> reports two distant points

# zoom:
g.region n=4926990 s=4925856 w=599850 e=601376 align=myarea
d.mon x0
d.rast myarea
d.vect mypixel col=blue
# visualize distance (d.graph give sometimes random output!):
r.distance mypixel,myarea | awk -F: '{print "move",$4,$5,"\ndraw",$6,$7}' | d.graph -m

# generate points from distance for easier verification:
r.distance mypixel,myarea | awk -F: '{print $4,"|",$5,"\n",$6,"|",$7}' | sed 's+ ++g' | v.in.ascii out=distance
d.vect distance col=yellow

# cleanup:
g.remove rast=myarea,mypixel
g.remove vect=mypixel,distance

###
Problem: There is no indication that we are *within* the area. Maybe a flag is
needed to optionally suppress the result in this case?
Better ideas?

Second problem: The result seems to be underestimated by half a cell
since not the cell edge is taken from the raster polygon boundary
but the cell center.

Appreciating help,
Markus

# your overlapping example
r.distance mypixel,myarea --q
1:1:90:601005:4926315:601095:4926315

r.distance mypixel,myarea --q -o
1:1:0:601005:4926315:601005:4926315

# not overlapping one
r.distance mypixel2,myarea --q
1:1:330:600075:4926405:600405:4926405

r.distance mypixel2,myarea --q -o
1:1:330:600075:4926405:600405:4926405

this patch introduces new function int null_distance(char *map1, char map2*, int *, int *)
which goes trough all cells and if both map values on the same place are
non-NULL, that it returns 1, else 0;

i'm shure, that it is not the efficientest way, how to do it, but it
works

could you test this patch, and eventually post your comments, please?

thanks

jachym

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
Department of Geoinformation Technologies
Zemedelska 3
613 00, Brno
Czech Republick
e-mail: xcepicky@node.mendelu.cz
URL: http://mapserver.mendelu.cz
Tel.: +420 545 134 514

(attachments)

r.distance.diff (7.25 KB)

hallo,

Dylan tested and found no error. Patch commited, r.distance has -o flag
for taking care on overlapping rasters

    jachym

On Tue, Oct 10, 2006 at 03:33:56PM +0200, Jachym Cepicky wrote:

hallo,

On Wed, Oct 04, 2006 at 01:38:25PM +0200, Markus Neteler wrote:
> Hi,
>
> for an application I have to calculate the distance between
> raster "objects". While this generally works, I have found
> that r.distance happily calculates the distance even if
> one object is within the other (say, raster pixel over
> raster polygon in second map). I would need an indication
> somehow that we are *in* (to ignore the distance in this
> case).

i did not found any G_is_null(raster_map_name,x,y) or similar function.
is there something like this?

thank you

jachym

one cell from mypixel is "surrounded"
>
> Spearfish example:
>
> g.region -dp
>
> # extract field #25:
> r.mapcalc "myarea=if(fields == 25, 1,null())"
>
> # this archsite falls into field 25:
> v.extract archsites out=mypixel list=9
> v.to.rast mypixel out=mypixel use=val val=1
> r.distance mypixel,myarea
> # -> reports two distant points
>
> # zoom:
> g.region n=4926990 s=4925856 w=599850 e=601376 align=myarea
> d.mon x0
> d.rast myarea
> d.vect mypixel col=blue
> # visualize distance (d.graph give sometimes random output!):
> r.distance mypixel,myarea | awk -F: '{print "move",$4,$5,"\ndraw",$6,$7}' | d.graph -m
>
> # generate points from distance for easier verification:
> r.distance mypixel,myarea | awk -F: '{print $4,"|",$5,"\n",$6,"|",$7}' | sed 's+ ++g' | v.in.ascii out=distance
> d.vect distance col=yellow
>
> # cleanup:
> g.remove rast=myarea,mypixel
> g.remove vect=mypixel,distance
>
> ###
> Problem: There is no indication that we are *within* the area. Maybe a flag is
> needed to optionally suppress the result in this case?
> Better ideas?
>
> Second problem: The result seems to be underestimated by half a cell
> since not the cell edge is taken from the raster polygon boundary
> but the cell center.
>
> Appreciating help,
> Markus
>
> _______________________________________________
> grass-dev mailing list
> grass-dev@grass.itc.it
> http://grass.itc.it/mailman/listinfo/grass-dev

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
Department of Geoinformation Technologies
Zemedelska 3
613 00, Brno
Czech Republick
e-mail: xcepicky@node.mendelu.cz
URL: http://mapserver.mendelu.cz
Tel.: +420 545 134 514

_______________________________________________
grass-dev mailing list
grass-dev@grass.itc.it
http://grass.itc.it/mailman/listinfo/grass-dev

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
Department of Geoinformation Technologies
Zemedelska 3
613 00, Brno
Czech Republick
e-mail: xcepicky@node.mendelu.cz
URL: http://mapserver.mendelu.cz
Tel.: +420 545 134 514

Hi,

backported to GRASS 6.2-CVS for 6.2.2.

Markus

hallo,

Dylan tested and found no error. Patch commited, r.distance has -o flag
for taking care on overlapping rasters

    jachym

On Tue, Oct 10, 2006 at 03:33:56PM +0200, Jachym Cepicky wrote:

hallo,

On Wed, Oct 04, 2006 at 01:38:25PM +0200, Markus Neteler wrote:
> Hi,
>
> for an application I have to calculate the distance between
> raster "objects". While this generally works, I have found
> that r.distance happily calculates the distance even if
> one object is within the other (say, raster pixel over
> raster polygon in second map). I would need an indication
> somehow that we are *in* (to ignore the distance in this
> case).

i did not found any G_is_null(raster_map_name,x,y) or similar function.
is there something like this?

thank you

jachym

one cell from mypixel is "surrounded"
>
> Spearfish example:
>
> g.region -dp
>
> # extract field #25:
> r.mapcalc "myarea=if(fields == 25, 1,null())"
>
> # this archsite falls into field 25:
> v.extract archsites out=mypixel list=9
> v.to.rast mypixel out=mypixel use=val val=1
> r.distance mypixel,myarea
> # -> reports two distant points
>
> # zoom:
> g.region n=4926990 s=4925856 w=599850 e=601376 align=myarea
> d.mon x0
> d.rast myarea
> d.vect mypixel col=blue
> # visualize distance (d.graph give sometimes random output!):
> r.distance mypixel,myarea | awk -F: '{print
"move",$4,$5,"\ndraw",$6,$7}' | d.graph -m
>
> # generate points from distance for easier verification:
> r.distance mypixel,myarea | awk -F: '{print $4,"|",$5,"\n",$6,"|",$7}' |
sed 's+ ++g' | v.in.ascii out=distance
> d.vect distance col=yellow
>
> # cleanup:
> g.remove rast=myarea,mypixel
> g.remove vect=mypixel,distance
>
> ###
> Problem: There is no indication that we are *within* the area. Maybe a
flag is
> needed to optionally suppress the result in this case?
> Better ideas?
>
> Second problem: The result seems to be underestimated by half a cell
> since not the cell edge is taken from the raster polygon boundary
> but the cell center.
>
> Appreciating help,
> Markus

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
Department of Geoinformation Technologies
Zemedelska 3
613 00, Brno
Czech Republick
e-mail: xcepicky@node.mendelu.cz
URL: http://mapserver.mendelu.cz
Tel.: +420 545 134 514

--
Jachym Cepicky
e-mail: jachym.cepicky@centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/jachym_cepicky-gpg_public_key.asc
-----------------------------------------
OFFICE:
Department of Geoinformation Technologies
Zemedelska 3
613 00, Brno
Czech Republick
e-mail: xcepicky@node.mendelu.cz
URL: http://mapserver.mendelu.cz
Tel.: +420 545 134 514

--
View this message in context: http://www.nabble.com/r.distance-problem%3A-in-our-out--tf2381876.html#a10836579
Sent from the Grass - Dev mailing list archive at Nabble.com.