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 want to extract a rectangle corner coordinates of a shape file in python script..please suggest me a solution.

Thanks in advance, Bhavya

share|improve this question
add comment

2 Answers

A lot depends on what APIs you have installed. Since you haven't tagged this post as 'ArcGIS' or 'ESRI', I assume you are not using ArcPy. Probably the most common Python GIS APIs is OGR. In OGR do this:

import ogr
ds = ogr.Open(r"C:\some_folder\my_subfolder\myShapefile.shp")
lyr = ds.GetLayer(0)
print(lyr.GetExtent())

This gives the extent of the entire shapefile. To get the extent of individual features you must drill down and get their envelopes.

PS: I should have said that the above code give you the extent as (xMin, xMax, yMin, yMax)

share|improve this answer
    
Hi Sylvester Sneekly, for above code i got the following error..Traceback (most recent call last): File "C:/Documents and Settings/vamsi/.qgis/python/plugins\Transform\dlgTransform.py", line 80, in GetExtent print(lyr.GetExtent()) IOError: [Errno 9] Bad file descriptor........please help..thanks –  vamsi Apr 4 '12 at 7:00
    
That's a very odd error to get here. Are you diverting the stdout to a file? BTW I only suggested to print the extent to screen as a quick demo. You could store it in a file for consumption later in your program instead e.g. "myExtent = lyr.GetExtent()" –  MappaGnosis Apr 4 '12 at 16:31
    
Thank u so much Sylvester for the answer –  vamsi Apr 5 '12 at 4:26
    
Hi.. Sylvester Sneekly, how can i add the extent values into existing fields like llx,lly lrx lry ulx uly urx ury –  vamsi Apr 7 '12 at 7:24
    
You need this tutorial: gis.usu.edu/~chrisg/python/2009. It is excellent and will really get you going with GDAL/OGR. –  MappaGnosis Apr 7 '12 at 19:42
show 1 more comment

I'm assuming you have ArcGIS. There is a very good worked example at http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Dataset_properties/000v0000002m000000/

Otherwise a common python library for geo-related tasks is Shapely. However, I think you might have more luck with OGR/GDAL. There is a tutorial here, including how to get the extent (see page 18)

share|improve this answer
    
Hi Stev, i have QGis and want to develope python plugins to QGis..i referred the tutorial u suggested. But i dint get the statement "os.chdir('f:/data/classes/python/data')" please clarify..Thanks for your reply :) –  vamsi Apr 3 '12 at 10:42
    
If you just want to know the extent in QGIS go to the layer properties and look at the Metadata tab. –  MappaGnosis Apr 3 '12 at 10:51
    
Hi..Sylvester Sneekly, thanks for your answer and i got the extent..but i want it by using the python script.. –  vamsi Apr 3 '12 at 11:00
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.