Nordpil
September 16, 2012

js2shapefile

js2shapefile

I am currently preparing a web map application for a client - an interface to a huge geospatial database with geographic information systems (GIS) data. The map is using the geoinformatics mapping software ESRI ArcGIS Server, and the excellent ArcGIS Javascript API, provided by ESRI.

A new requirement that the client asked me to look into was the possibility for downloads of GIS data in the form of shapefiles out of selections that have been done interactively in the map (in the browser).

Some search lead me to the excellent Javascript js2shapefile library, that I found in a repository on Google Code. The helper class was written by Harry Gibson at CEH Wallingford and is copyright NERC. This JavaScript code prepares the export in the browser, all on the client side, so no trips to the server are necessary. Either the files are provided as one zip archive, or as separate downloads for the dbf, shp and shx files that make up the ESRI shapefile format.

There is not much documentation provided on how to use the files, but it was fairly easy to set it up and get it working with the ArcGIS Javascript API, dojo and my webserver. I modified a sample, and managed to set up the export without too much trouble.

At first I was a bit worried about the amount of processing done on the client - since all the calculation and assembly is done in the browser, but it seems to work quite well - even for large selections. For some reason the file preparation takes much more time in Microsoft Internet Explorer, compared to Mozilla Firefox or Google Chrome.

One thing that I needed to do - js2shapefile needs graphics objects for it's processing, and I had an array of features from a selection. This little snippet took care of that and prepared a new array of graphics that I could feed into the Shapefile class. Since the results array looks different if it comes from an identify or query task - I had to handled that in this routine as well.

An alternative method would be to set up a geoprocessing service, and program that in Python, using ArcPy (ESRI ArcGIS Python programming extensions). The request would then pass a group of id's and the name of the layer and the server would return the link to a zip-file with the specified data. I haven't yet set up any service like that, and it would have been interesting to play with that!