Fanning Software Consulting

Extracting Shapes or Polygons from Shapefiles

Facebook Twitter RSS Google+

QUESTION: I have a shapefile and I would like to extract one of the polygons from the shapefile in order to do some analysis with it. How can I do this in IDL?

ANSWER: You can extract single shapes or polygons with the Coyote Library routine, cgExtractShape. This routine allows you to select a single shape or polygon from the shapefile based on the shapes's attribute name and value. If you don't know ahead of time what the shape's attribute name and value are, you can use the selection widget built into the program to browse the shapefile. You see an example of a Colorado country shapefile being browsed with the selection widget in the figure below. In this case the attribute name is "COUNTY" and the attribute value I am interested in is Grand country, which is named "GRAND". Once I hit the Accept button, the Grand country shape is read out of the file.

   IDL> file = 'colorado_counties.shp'
   IDL> grand = cgExtractShape(file)
Browsing the Colorado county shapefile with cgShapeInfo.
Browsing the Colorado county shapefile with cgShapeInfo.
 

An alternative way to select the Grand country shape from the shapefile is to specify the attribute name and value directly, like this. In this case, no selection widget appears.

   IDL> grand = cgExtractShape(file, 'county', 'grand')

The return value from the function is an IDLanROI object.

   IDL> Help, grand
        GRAND     OBJREF = <ObjHeapVar321141(IDLANROI)>

You can use the IDLanROI object in the usual way to compute statistics, create image masks, and so on. Note that if the shape requires more than one polygon to render the shape correctly, the return object will be an IDLanROIGroup object. There will be a separate IDLanROI object in the group object for each individual polygon that describes the shape.

   IDL> boulder = cgExtractShape(file, 'county', 'boulder')
   IDL> Help, boulder
        BOULDER    OBJREF = <ObjHeapVar321338(IDLANROIGROUP)>

In either case, however, the shape can be rendered with the Coyote Library routine cgDraw_ROI.

   IDL> cgDrawShapes, file, /AutoDraw
   IDL> cgDraw_ROI, boulder, Color='blue'
   IDL> cgDraw_ROI, grand, Color='red'

You see the results in the figure below. Keywords to cgDraw_ROI can change the way the shape is rendered.

Individual shapes are extracted and rendered.
Individual shapes are extracted and rendered.
 

If you need to get at the actual vertices of the polygon for some reason, you can extract this information from the IDLanROI object.

   IDL> grand -> GetProperty, DATA=polydata
   IDL> x = Reform(polydata[0,*])
   IDL> y = Reform(polydata[1,*])
   IDL> cgPlots, x, y, LineStyle=2, Color='blu4', Thick=3

If you are working in a version of IDL prior to IDL 8, please be sure to clean up your objects when you are finished with them. In IDL 8 and above, this kind of object clean-up is done automatically when you are though with your objects.

   IDL> Obj_Destroy, [grand, boulder]

Version of IDL used to prepare this article: IDL 8.2.3.

Written: 23 November 2013