X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



3680 Rate this article:
5.0

Creating Polygon Shapefiles Using IDLffShape


This article demonstrates how a polygon shapefile can be created using the IDLffShape object class.
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 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
44.236127 -107.665883
44.269500 -107.684000
PRO ex_shapefile_polygon_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 (polygon_test.shp),
; defines the entity type as 'polygon' 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('polygon_test.shp'), $
 /update, entity_type=5)

; 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_Type', 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)
vert_ID= strarr(n_recs)
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= 5   ;polygon
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 = 'Arbitrary'
attrNew.Attribute_1 = 'Test_Polygon'
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

print, 'Your shape file has been created.'

END

; ***********************************
Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »