I’ve recently become interested in loading shapefiles into Flash applications. Doing so opens up many possibilities.
- Cartograms
- Projections
- Flash-based GIS
Perhaps best of all, it may mean that, for simple mapping tasks, I could do without ArcGIS altogether. I could load a shapefile into my Flash application, project it into one of dozens of projections, zoom into a continent, apply a choropleth, dot density, or proportional symbol symbology, and then export to SVG or EPS format for later touch-ups in Illustrator.
But we’re a long way from that. Two packages that I’ve recently stumbled onto make this easier.
- Actionscript 3 SHP Library by Edwin Vanrijkom (who works for Adobe)
- PHP Class Shapefile Reader by Juan Carlos Gonzalez
The former uses only Actionscript 3, thanks to the new ByteArray class. It easily loads and parses shapefiles, and has built-in functions to draw the imported polygons, polylines, or points. He also has classes for loading DBF files, in which shapefiles store their attribute data. And it’s blazing fast. I loaded a highly detailed world countries shapefile and drew it in less than a second. That’s nice.
So why would one bother with the PHP route, if one can do everything in the Flash environment? Great question. For simply loading and displaying shapefiles, you probably wouldn’t. But for more complicated, processor-intensive applications such as point transformation-based projections or diffusion-based cartograms it is nice to do the hard math and thousands of loops on the server side.
Mr. Gonzalez’s Shapefile Reader class gives us a good start. I’ve already set up an AMFPHP service to send the shapefile information back to Flash after the PHP class has done it’s heavy lifting. Time-wise, this approach has nothing on Mr. Vanrijkom’s AS-only approach. Loading the same shapefile as above in PHP, transmitting it via AMFPHP, and drawing it in Flash took about 5 seconds. Computing a point transformation for the hundreds of thousands of points in this shapefile (to construct a Mercator projection) on the server side, however, took only an additional second, and added one second more to the AMFPHP side. I haven’t tried to compute the Mercator projection in AS yet, but I will, and will report on the findings here. I’m guessing the AS-only approach will still be better/faster, but by a smaller margin.
Why PHP? Wouldn’t Python or C be much faster? Yes! But PHP is easier to use in Flash Remoting and many Flash developers (including myself) have little experience with these other languages. One advantage of the server-side technique, though, is that when I become more comfortable in Python, I could easily port my PHP to Python, and the Flash code would change very little.
So for now I’m going to be working with the PHP class, trying to optimize it for AMFPHP transmission to Flash, and seeing how easy it is to add additional projections, build topologies, and perhaps even tackle a cartogram in the PHP code. I’ll post some experimentation soon.
2 Trackbacks
[...] Below is linked a little teaser from my recent experimentation with loading shapefiles. [...]
[...] language like php or python. Zachary Forest Johnson from indiemaps.com compared both techniques and suggested the server-side method and that’s also what we’ll use in this [...]