Thursday, June 16, 2011

Exporting .shp Files into XML with shp2text

I have gotten a few emails asking how to install and use shp2text, a program I used in a previous post.  This post will be a step by step guide on how to install shp2text on Mac OS X 10.4 - 10.6.  A version of Xcode must also be installed on the machine for the code to compile.  The original code provided by obviously.com will not compile properly, so here is my modified version that will compile on OS X.

First, unzip onto the Desktop.  Then open up a terminal and type:

cd ~/Desktop/shp2text/

This command will change the terminal directory to the shp2text directory.  If you input "ls" you should see the files in the shp2text directory like Makefile, dbfopen.c, and shapefil.h and etc.  Now simple type:

make

and you should see an output that looks like:

cc -g -c shpopen.c
cc -g -c dbfopen.c
cc -g shpdiff.c shpopen.o dbfopen.o  -o shpdiff
shpdiff.c: In function ‘compareDBF’:
shpdiff.c:539: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[512]’
shpdiff.c:539: warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘char (*)[512]’
cc -g shp2text.c shpopen.o dbfopen.o  -o shp2text

Now to test if the compilation was done correctly, I have included the sample .shp file provided by obviously.com.  To export the latitude and longitude information with shp2text type:

./shp2text --gpx test/bike-cape_islands-geo.shp 6 0 > test/output.xml

And you will find that an output.xml file, in the test directory, has been created with the .shp data in XML format.  I hope these quick instructions help the confused people out there.

Here is the modified shp2text source code again.