Shapefile Programs Run Slowly
QUESTION: I have been using the GSHHS Shoreline Database shapefiles as overlays on my map projections, but my programs run unbelievably slowly. It takes a minute or more for the programs to run, although they seem to draw their continental outlines much faster than that. Is there some way to speed things up?
ANSWER: There is an excellent chance that the slowness you are experiencing has nothing whatsoever to do with the drawing of the shapefile polygons. Rather, it is probably the result of the way you are freeing the shapefile pointers in your IDL program.
When you get the "entities" or polygons out of the shapefile, IDL returns an array of polygon structures (an IDL_Shape_Entity structure), with each structure representing a single polygon. Each polygon structure contains ten tags or fields. Five of these fields are pointers. This means that even in a fairly low resolution GSHHS file, which may have, say 35,000 polygons, that you have created well over 150,000 pointers!
IDL> polygons = ShapefileObj -> GetEntity(/ALL) IDL> Help, polygons POLYGONS STRUCT = -> IDL_SHAPE_ENTITY Array
Since these pointers are, as you can imagine, hard to keep track of, the generally accepted way of freeing these pointers when you are finished with them is to use the IDL comamnd Heap_Free.
IDL> Heap_Free, polygons
The Heap_Free command recursively searches for and destroys any heap variable (pointer or object) that is found in its argument. However, this can be a slow process. To find and free up 150,000 pointers like this can take between one and two minutes!
It is much faster (at least 10 times faster!) to destroy the pointers as you go through the polygons to display them. Your code might look something like this.
FOR j=0,N_Elements(polygons)-1 DO BEGIN thisPolygon = polygons[j] Draw_Polygon, thisPolygon Ptr_Free, thisPolygon.vertices Ptr_Free, thisPolygon.measure Ptr_Free, thisPolygon.parts Ptr_Free, thisPolygon.part_types Ptr_Free, thisPolygon.attributes ENDFOR
Using this free-as-you-go approach in the Coyote Library program, cgDrawShapes resulted in a 10-fold speed-up in program execution using the intermediate level resolution GSHHS shapefiles.
Version of IDL used to prepare this article: IDL 8.1.