Friday, September 18, 2009

Map Layer From Local Shapefile

In this post, I will demo how to load a local shapefile from your hard drive, and overlay it on an map whose base layer is derived from a remote ArcGIS server. Using the Flash Player 10 FileReference API, this task is fairly easy. However, I did not want the user to be prompted for each imported file as a shapefile is composed of a .shp file holding the geometry and a .dbf file holding the attributes. So, the idea is to zip the shp and dbf and load the zip, and then let the application unzip the content and parse the shp and dbf entries. The application is using the Flex API for AGS and makes use of two wonderful as3 library; the shp library from Edwin van Rijkom, and a zip library from David Chang. In this application, I created a custom geometry to take advantage of the coordinate array format in the shapefile reducing the impedance and taking advantage of the "compression". Well, this not truly compressed - this an array of x,y,x,y,etc... rather than an array of MapPoint instances :-) and since this a custom geometry, well then you need a custom symbol to render that geometry. In this demo, I wanted to load and display the output of a plume model/process, and here is the result:

You can download the zip file from here, and you can see the application in action here. And like usual, the source code is here.
10/10/09 - I've updated the source to be a bit more robust - BTW, this only handles polygon shapes.

28 comments:

Yann said...

"ESRI Daily Build" : Some info on what's new ? :)

thunderhead said...

Ha - u say that :-( we are working on version 2 for AGS 9.4 - this will be based on flex 4 and has a feature layer, editing, native clustering, more geometry functions, bug fixes and performance enhancements; these are the things that I can think of now :-)

Yann said...

Maybe a release date ? can't wait :)

thunderhead said...

Not sure yet - we might release a 1.3 version before 2.

Yann said...

Great, thanks ! Can't wait to see demos of the new API features :)

CG said...

Is there any way to load shapefiles without ever contacting the remote rest service?

thunderhead said...

This _is_ not connecting to the remote rest service.

Unknown said...

[flexcoders] NFC (Near Field Communication) API in FlashPlayer 10.1
Vote for this !!

http://bugs.adobe.com/jira/browse/FP-2909

Mike Vanhook said...

Could this be tweaked a bit to allow zip and / or layer packages (lpk)? This would prevent the user from needing to create zip packages since a layer package is a zip format. Thanks- Mike

Casey Bentz said...

Do you have any idea if it is possible to load a dwg file in a simalar fashion?

thunderhead said...

Sure could be adjusted - have to find out the file format :-)

Unknown said...

Mansour,

This has tons of value for us!

Thank you for posting the idea, description and source code!

Ole

thunderhead said...

I've updated the source so it is a bit more robust - check it out and tell me - BTW - this works only on Polygon shapes

Mike Vanhook said...

Thanks - we'll deploy it on a site and give it a test.

Unknown said...

Thanks for the source code. I loaded it in Flash Builder 4 and I got an error message about missing the agslib-SNAPSHOT.swc file in the Referenced Libraries.
Error
Description Resource Path Location Type
unable to open '\Users\mraad\ArcGIS\ArcGIS\RIA\FlexAPI\api\dist\agslib-SNAPSHOT.swc' ShpOverlay Unknown Flex Problem

thunderhead said...

Please use the swc off the www.esri.com/flex - sorry about that :-(

Unknown said...

How can we label based on an attribute within the dbf?

csri said...

Hi Mansoor, Thanks for the awesome application. If I need to write the app so that it allows users to add shapefile of any spatial refrence. Do I need to first project the polygons to match the SR of the base map viewer and then add the projected graphics? Thanks in advance for your help.

thunderhead said...

U r welcome - what projection do u wanna go to ?

csri said...

Hi Mansoor, Our app needs to let the user overlay shapefile on top of base maps (arcgis online street-wgs84). So am thinking since I don't have control on the SR of the shapefile that the user overlays using this tool, should I first reproject it to wgs 84 in my app using geometry service and then add the projected graphic? Could you suggest some way please ? Thank you in advance..

thunderhead said...

Well if the shp file is in WGS84 - then there is no issue - u can use GS - but, u will have to send all the data to the server and back - however - u can project all the data _client_ side - but will will need to know the original SR - have library that can share if u r interested for a small $$ into my swiss bank account (Just kidding :-)

Jenn & Joel said...

Is there an easier way to add a local shapefile in 10. I get an error at the line

Jenn & Joel said...

at this line
shp:ShpSymbol id="shpSymbol"

Unknown said...

hai Mansour Raad this is RAMPRASAD from india tamilnadu very recently i saw your video FLEX API session in ESRI video library,it was really amazing.

i want know how to overlay the shape file and mdb file contains point,line&polygons features.using flash builder flex api did you have any special MXML source code & step by step procedure for this

Please Help me thank you

thunderhead said...

Glad that u like the app - all the source code (a bit old) all available for you to download at the bottom of the page - in addition, check out http://resources.arcgis.com/en/communities/flex-api/ for in depth information.
Hope this helps.

Adriana C said...

Hi!
I'm implementing a load shape file as a tool on my flex app, but I'd like to know in which API is possible to take the attributes from *.lyr file. I'm using API 1.3 and know it works fine... just the chiefs asked me about it.

Regards, thanks in advance

Adriana C said...

Hi!
I'm implementing a load shape file as a tool on my flex app, but I'd like to know in which API is possible to take the attributes from *.lyr file. I'm using API 1.3 and know it works fine... just the chiefs asked me about it.

Regards, thanks in advance

thunderhead said...

Have not done such an implementation - sorry.