Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

i have a shapefile of country subdivisions and i would like to extract an array of bounding lat and lon for each division.. is it possible to do?

share|improve this question
    
Please clarify: do you have a shapefile or an Excel file? –  whuber Nov 24 '10 at 15:08
1  
The title doesn't really reflect your question, please think about editing it. –  DavidF Nov 24 '10 at 15:21
    
i have a shapefile –  mossplix Nov 24 '10 at 15:43
add comment

4 Answers

up vote 18 down vote accepted

Using the ogr Python module from OSGEO, this example will give you a tuple containing the coords that define an envelope for each feature.

from osgeo import ogr

ds = ogr.Open("mn_counties.shp")
lyr = ds.GetLayerByName("mn_counties")

lyr.ResetReading()

for feat in lyr:
    # get bounding coords in minx, maxx, miny, maxy format
    env = feat.GetGeometryRef().GetEnvelope()
    # get bounding coords in minx, miny, maxx, maxy format
    bbox = [env[0], env[2], env[1], env[3]]
    print env
    print bbox
    print
share|improve this answer
    
+1 for python :D –  mossplix Nov 24 '10 at 15:45
2  
... and free geospatial python at that ; / –  DavidF Nov 24 '10 at 15:56
add comment

One possible way to proceed using SAGA GIS http://www.saga-gis.org After opening your shapefile run these 3 modules: 1. Modules\Shapes\Tools\Get Shapes extent

  1. Modules\Shapes\Tools\Points\Points from lines [contrary to what the name suggest you can also use this to get points from a polygon]

  2. Modules\Shapes\Tools\Points\Add Coordinates to points This will give you a table which contains the x and y coordinates of the 4 corners of the bounding box of your polygon file.

share|improve this answer
add comment

In arcgis, here's python code. result is a list of minx, miny, maxx, maxy, minM, maxM, minZ, maxZ (

import arcpy
for feat in arcpy.SearchCursor(r"c:\data\f.gdb\counties"):
    print feat.Shape.extent

-2.66852727251546 49.4265363633626 -2.52848181818121 49.5079454546192 NaN NaN NaN NaN
-10.463336363782 51.4455454544593 -6.01305454583045 55.3799909091533 NaN NaN NaN NaN
-4.77778181827614 54.0555454544593 -4.35347272688468 54.4100000000002 NaN NaN NaN NaN
share|improve this answer
add comment

Here's an R version, using example data from the rgdal package:

library(rgdal)
dsn <- system.file("vectors/ps_cant_31.MIF", package = "rgdal")[1]
d <- readOGR(dsn = dsn, layer="ps_cant_31")

## transform if this is not longlat
if (is.projected(d)) d <- spTransform(d, CRS("+proj=longlat +ellps=WGS84"))

for (i in 1:nrow(d)) {
  print(bbox(d[i,]))    
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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