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

3 Answers 3

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

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

You may have already discovered how trivial a task ArcPy makes this sort of question. Expanding further on MappaGnosis' answer, to get the full extents of the feature class using ArcPy, call the GetExtent() method of the Layer object:

fc = r"C:\some_folder\my_subfolder\myShapefile.shp"
ex = arcpy.mapping.Layer(fc).getExtent()
print "\t".join(map(str, [ex.XMin, ex.YMin, ex.XMax, ex.YMax]))
del fc, ex

The arcpy.mapping.Layer() method creates a reference to a feature class from a shapefile to an ArcSDE feature class. The getExtent() method returns an Extent object with all kinds of properties including the coordinates we're after. Last, the string.join(map(str, list)) is a shorthand way to print a tab-delimited list of the coordinates.

To list the extents for each feature, open a search cursor on the feature class and loop through it:

fc = r"C:\some_folder\my_subfolder\myShapefile.shp"
for ftr in arcpy.da.SearchCursor(fc, ["SHAPE@", "RefName"]):
    ex = ftr[0].extent
    print "\t".join(map(str, [ftr[1], ex.XMin, ex.YMin, ex.XMax, ex.YMax]))
del fc, ftr, ex

The optional, second argument to the SearchCursor() method specifies what field values to return for each feature; if the argument is left out, then all field values will be returned.

share|improve this answer

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.