[GRASS-user] finding bounding box of polygons

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.

Thanks, Jerry

Gerald C. Nelson

Professor

217-333-6465 - office

217-390-7888 - cell

https://netfiles.uiuc.edu/gnelson/www/

Hi,

On 9/27/07, Jerry Nelson <gnelson@uiuc.edu> 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.

Not a GRASS-centric response :), but if it is just a box you're after,
you can probably use ogrinfo for this, as it gives you the Extent of
your file. As an example, using it with the world.shp Shapefile
results in:
$ ogrinfo -so python/world.shp world
INFO: Open of `python/world.shp'
      using driver `ESRI Shapefile' successful.

Layer name: world
Geometry: Polygon
Feature Count: 244
Extent: (-180.000000, -89.900000) - (180.000000, 83.674733)
Layer SRS WKT:
(unknown)
NAME: String (40.0)
CAPITAL: String (64.0)
APPROX: Real (15.0)
AREA: Real (20.15)
SOURCETHM: String (16.0)

Hope that helps

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. I suppose an alternative would be to
somehow export each country polygon as a separate shapefile and get the
region of that.

Any tips appreciated.
Thanks, Jerry

-----Original Message-----
From: Jose Gomez-Dans [mailto:jgomezdans@gmail.com]
Sent: Thursday, September 27, 2007 4:59 PM
To: Jerry Nelson
Cc: grassuser@grass.itc.it
Subject: Re: [GRASS-user] finding bounding box of polygons

Hi,

On 9/27/07, Jerry Nelson <gnelson@uiuc.edu> 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.

Not a GRASS-centric response :), but if it is just a box you're after,
you can probably use ogrinfo for this, as it gives you the Extent of
your file. As an example, using it with the world.shp Shapefile
results in:
$ ogrinfo -so python/world.shp world
INFO: Open of `python/world.shp'
      using driver `ESRI Shapefile' successful.

Layer name: world
Geometry: Polygon
Feature Count: 244
Extent: (-180.000000, -89.900000) - (180.000000, 83.674733)
Layer SRS WKT:
(unknown)
NAME: String (40.0)
CAPITAL: String (64.0)
APPROX: Real (15.0)
AREA: Real (20.15)
SOURCETHM: String (16.0)

Hope that helps

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

Couldn't you dissolve the polygons for each region of interest
into points (using v.type and v.to.points) and then just calculate
the convex hull (v.hull) for the result points cloud and take that as
your region envelope?

Benjamin

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. I suppose an alternative would be to
somehow export each country polygon as a separate shapefile and get the
region of that.

Any tips appreciated.
Thanks, Jerry

-----Original Message-----
From: Jose Gomez-Dans [mailto:jgomezdans@gmail.com]
Sent: Thursday, September 27, 2007 4:59 PM
To: Jerry Nelson
Cc: grassuser@grass.itc.it
Subject: Re: [GRASS-user] finding bounding box of polygons

Hi,

On 9/27/07, Jerry Nelson <gnelson@uiuc.edu> 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.

Not a GRASS-centric response :), but if it is just a box you're after,
you can probably use ogrinfo for this, as it gives you the Extent of
your file. As an example, using it with the world.shp Shapefile
results in:
$ ogrinfo -so python/world.shp world
INFO: Open of `python/world.shp'
      using driver `ESRI Shapefile' successful.

Layer name: world
Geometry: Polygon
Feature Count: 244
Extent: (-180.000000, -89.900000) - (180.000000, 83.674733)
Layer SRS WKT:
(unknown)
NAME: String (40.0)
CAPITAL: String (64.0)
APPROX: Real (15.0)
AREA: Real (20.15)
SOURCETHM: String (16.0)

Hope that helps

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