X
2973

Creating Polyline Shapefiles Using IDLffShape


This article demonstrates how a polyline shapefile can be created using the IDLffShape object class.
To create a polyline shapefile several steps need to be followed:

1) Create a new shapefile object
2) Define the entity and attribute definitions
3) Add data to the file

The following sample data set can be used for the file:

44.269500 -107.684000
44.260753 -107.708720
44.258756 -107.749328
44.206913 -107.755205
44.215770 -107.726061
44.205779 -107.722249
44.206648 -107.685550
44.216383 -107.685894

PRO ex_shapefile_polyline_file

; To create a polygon shapefile several steps need to be followed:
; 1) Create a new shapefile object
; 2) Define the entity and attribute definitions
; 3) Add data to the file

; The following program creates a new shapefile (polyline_test.shp),
; defines the entity type as 'polyline' and defines 2 attributes and
; then adds the entity to the new file

compile_opt strictarr

;Create the new polygon shapefile and define the entity type to Polygon.
opolygonshape=obj_new('IDLffShape', filepath('polyline_test.shp'), $
 /update, entity_type=3)

; Set the attribute definitions for the new Shapefile
; i.e.(name, type, width,precision) 7= string, 5= double,float
opolygonshape->idlffshape::addattribute, 'Shape_Type', 7, 25
opolygonshape->idlffshape::addattribute, 'Name', 7, 25
opolygonshape->idlffshape::addattribute, 'Vertices', 5, 25

;
;********** Get the data file **********

;Dialog box to get *.txt file return if no file chosen
filter=['*.txt']
poly_file = dialog_pickfile(title='Choose your vertex file', filter=filter)
if (poly_file eq '') then return

;Determine the number of vertices
n_recs = file_lines(poly_file)
print, 'Number of vertices:',n_recs

;Define data format to columns of *.txt data file
;Start loop to read the vertex coords
f1= (f0 = 0.0D)
lon= dblarr(n_recs)
lat= dblarr(n_recs)

;Open the file for reading
openr, 1, poly_file

;Read the contents of the text data file.
for i = 0, n_recs-1 do begin
	readf, 1, f0, f1
	lat[i]  = f0
	lon[i]  = f1
    poly_file=[lat[i], lon[i]]
endfor

;Close the file.
close, 1

;
;********** End getting the data file **********

; Create structure for new entity.
entNew = {idl_shape_entity}

;Define the values for the new entity
entNew.Shape_Type= 3   ;polyline
entNew.N_Vertices= n_recs
entNew.Vertices=ptr_new(/allocate_heap)

entNew.Bounds[0] = -107.00000
entNew.Bounds[1] = 44.000000
entNew.Bounds[2] = 0.00000000
entNew.Bounds[3] = 0.00000000
entNew.Bounds[4] = -108.00000
entNew.Bounds[5] = 40.500000
entNew.Bounds[6] = 0.00000000
entNew.Bounds[7] = 0.00000000

;Holder for vertex data
verts=dblarr(2, n_recs)

verts[0,*]=lon
verts[1,*]=lat
*entNew.VERTICES = verts

;Create structure for new attributes.
attrNew = opolygonshape->IDLffShape::GetAttributes( $
/attribute_structure)

;Define the values for the new attributes.
attrNew.Attribute_0 = 'Polyline'
attrNew.Attribute_1 = 'Test_Polyline'
attrNew.Attribute_2= n_recs

;Add the new entity to new shapefile.
opolygonshape->IDLffShape::PutEntity, entNew

; Free the pointer to the vertices
ptr_free, entNew.vertices

; Add the attributes to new shapefile.
opolygonshape->IDLffShape::SetAttributes, 0, attrNew

; Close the shapefile.
obj_destroy, opolygonshape

; End the program with a print statement
print, 'Your polyline shape file has been created.'

END

; ***********************************