Tutorial: Reading ESRI Shapefiles in ActionScript

This tutorial is the second part of a tutorial series about customized map projection in flash/actionscript. It shows how to import a shapefile into ActionScript via the SHP library by Edwin van Rijkom. **1** Reading Shapefiles in PHP, 2 Reading Shapefiles in ActionScript.

So, after Zachary Forest Johnson told me that he changed is mind about server-side reading of shapefiles, I decided to create another tutorial showing the client-side method using Edwin van Rijkom’s SHP library.

This is obviosly exactly the same as in the first tutorial: We start by downloading a shapefile from finder.geocommons.com which is a large portal for free map datasets. I decided to use the dataset World Countries Boundary File, World, 2002 because it includes not only information about the country boundaries but also some useful meta information like ISO codes etc. Here is a direct link to the zip-file containing the shapefiles. The zip-package contains three files, one shp-file containing all shape coordinates, one dbf-file containing all shape meta data and one shx-file containing the shape index file. You find more information on the structure shapefiles in the wikipedia.

# Step 2: Importing the shapefiles into flash

At first we have to load the shapefile data into the flash movie. I chose to do this using the excellent BulkLoader library, but you can also do this by hand by using the native URLLoader class instead.

This is the part where Edwin van Rijkom’s SHP library comes into the game. I first tried to extract the shape records using the class ShpTools:

This attempt resulted in an exception. After a while I figured out that one has to create an instance of ShpHeader before the parsing of the records works.

Now we got an array full of ShpRecord instances. Since our shapefile contains only polygons we can access the information by casting the ShpObject instances to ShpPolygon.

# Step 3: Reading the meta information

The access to the shape meta information (which are stored in the dbf file) can be done using the class DbfTools. But just to be sure I created a DbfHeader instance at first.

We can allocate the right dbf record to a shape record by using the number property. We have to decrease it by one since the getRecord method starts the counting by zero while the counting of number starts by one (seems to be a little inconsistent at this point).

So that’s it! The trace output will look something like this:

You can again download a zip file containing the full actionscript class and the shapefiles used in this tutorial.

In the next tutorial I’ll show how to project and display a static map out of these coordinates.

lionlancer (Jun 03, 2011)

Thanks a lot! I was looking all over the web for this! I have absolutely no idea on how to use the library. __

Great work!

suresh (Feb 24, 2012)

how to display a static map out of these coordinates?

Alex (Mar 07, 2012)

Hi!

Great job, very useful 😃
Only one problem, I couldn’t find the DbfTools and DbfHeader classes in the lib… Do you know what happend to it? If you have it, could you post it on your blog?

Cheers,

Alex

Alex (Mar 07, 2012)

OK, I found it, I forgot to checkout it from SVN…
Thanks again!