[GRASS-user] Extracting centroids as a raster (and are these centroids really at the center?)

Dear all,

I need to extract the center of raster areas as another raster.

Since my script is totaly raster-based (and I am not used to using vectors),
I tried to find some kind of r.thin that would generate the centroids,
instead of trying to "force" the area into lines.

I believe there is no such module (please correct me if I'm wrong), so I had
to use vectors.

I then did:
- r.to.vect input=raster_areas output=vectorized_areas feature=area
- v.to.points in=vectorized_areas out=vector_centroids type=centroid
- v.to.rast input=vector_centroids output=raster_centroids use=val

I was succesfull, but I have to questions:
1- Is there a better way to accomplish this?
2 - How are these centroids calculated? I ask this because they don't seem
to be the actual visual centroid, as the attached image hopefully clarifies
to you. The image shows three example areas with the centroids both as an
"x" and as a raster in pink.

I thank you all very much for any help and wish you a great new year with a
lot of intelectual challenges.

Best,
Marcello.

http://osgeo-org.1803224.n2.nabble.com/file/n7146840/areas.png

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Extracting-centroids-as-a-raster-and-are-these-centroids-really-at-the-center-tp7146840p7146840.html
Sent from the Grass - Users mailing list archive at Nabble.com.

On Tue, Jan 3, 2012 at 3:15 PM, Marcello Gorini <gorini@gmail.com> wrote:

Dear all,

I need to extract the center of raster areas as another raster.

Since my script is totaly raster-based (and I am not used to using vectors),
I tried to find some kind of r.thin that would generate the centroids,
instead of trying to "force" the area into lines.

find a script attached which calculated centroids from raster areas.
You may expand it with a final r.in.xyz step to write out the calculated
centroid(s) as raster map.

Hope this helps,
Markus

(attachments)

r.centroid (3.86 KB)

Marcello:

Dear all,

I need to extract the center of raster areas as another raster.

Since my script is totaly raster-based (and I am not used to using
vectors),
I tried to find some kind of r.thin that would generate the centroids,
instead of trying to "force" the area into lines.

Markus:

find a script attached which calculated centroids from raster areas.
You may expand it with a final r.in.xyz step to write out the calculated
centroid(s) as raster map.

Hope this helps,
Markus

Thanks A LOT !!

Now the centroids are in the center :slight_smile:

You can see in the attached image the new ones in pink. Great!

But I encountered a few problems to get there. I don't know if it is
something with me or my system (6.4.0svn on old ubuntu 9.1), but the script
wasn't looping correctly over the different areas.

It seemed that r.category was ignoring the mask created in the loop and that
was causing trouble. So, instead of using r.mask, I actually created a
temporary raster containing only the selected area in the loop. I also put
the area calculation with r.stats inside the loop.

Also, as you instructed, I made the program output the centroid coordinates
to a file and then use it as input to r.in.xyz to create the raster. It
worked as a charm!

Thanks again,
Marcello.

P.S.; I also attached the modified r.centroid.new if you would like to take
a look. I hope I didn't deform your script too much :slight_smile:

http://osgeo-org.1803224.n2.nabble.com/file/n7168100/centroids.png

http://osgeo-org.1803224.n2.nabble.com/file/n7168100/r.centroid.new
r.centroid.new

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Extracting-centroids-as-a-raster-and-are-these-centroids-really-at-the-center-tp7146840p7168100.html
Sent from the Grass - Users mailing list archive at Nabble.com.

On Mon, Jan 9, 2012 at 3:49 PM, Marcello Gorini <gorini@gmail.com> wrote:

Now the centroids are in the center :slight_smile:
You can see in the attached image the new ones in pink. Great!

Excellent.

But I encountered a few problems to get there. I don't know if it is
something with me or my system (6.4.0svn on old ubuntu 9.1), but the script
wasn't looping correctly over the different areas.

Honestly, I tried only with a one area map and updated the script to the
posted stage after midnight :stuck_out_tongue:

It seemed that r.category was ignoring the mask created in the loop and that
was causing trouble. So, instead of using r.mask, I actually created a
temporary raster containing only the selected area in the loop. I also put
the area calculation with r.stats inside the loop.

Yes, right.

Also, as you instructed, I made the program output the centroid coordinates
to a file and then use it as input to r.in.xyz to create the raster. It
worked as a charm!

Thanks again,
Marcello.

P.S.; I also attached the modified r.centroid.new if you would like to take
a look. I hope I didn't deform your script too much :slight_smile:

...

http://osgeo-org.1803224.n2.nabble.com/file/n7168100/r.centroid.new

excellent!

Cheers
Markus

Markus:

Honestly, I tried only with a one area map

and updated the script to the
posted stage after midnight :stuck_out_tongue:

All I can do is to thank you very much for your effort in updating the script, i.e., in helping me.

It really did… a lot!

Cheers,
Marcello.