[GRASS-user] finding bounding box of polygons

Hamish hamish_nospam at yahoo.com
Sun Sep 30 22:15:47 EDT 2007


> Jerry Nelson wrote:
> > I'd like to construct a list of the bounding box coordinates for all
> > the polygons in a vector file. I could find nothing in the manual
> > that seemed to do that. Any tips appreciated.
..
> What I'm trying to do is get the bounding boxes around each country in
> the world, so I can create a region file for each. This would make doing
> graphics by country much easier. For example, we have constructed a
> raster at 1 km resolution of the number of amphibian species ranges at
> each location for the whole world. There are times when I just want,
> say the counts in India. We have a nice little script that generates a
> png automatically based on a region file. It would be nice to have such
> a file for each country in the world.
> 
> If I read your example correctly, ogrinfo gives me the box around the
> entire shapefile, not individual polygons.

ogrinfo will give the box around all the data, as will 'v.info -g'.

> I suppose an alternative would be to somehow export each country
> polygon as a separate shapefile and get the region of that.

you could write a bash loop which cycled through all the cat values
v.extract

MAP=yourmap

for CAT in `v.category in=roads op=print | sort -n | uniq` ; do
  unset north south east west
  echo "[$CAT]"
  v.extract in=$MAP out=tmp_map_$$ list=$CAT
  eval `v.info -g tmp_map_$$`
  echo "$CAT: n=$north s=$south e=$east w=$west"
  g.remove tmp_map_$$
done

# I forget the better way to do that loop ?

I believe a number of vector modules use the polygon bounding boxes
internally (see v.in.ogr section of doc/vector/TODO in the source code),
so using a SWIG interface to get those directly with a vector library
fn would be a lot faster than extracting each with v.extract.

For 250 or so countries, the above script shouldn't take too long.


Hamish




More information about the grass-user mailing list