Select to view content in your preferred language

Load Local Shapefile with Symbology and Attributes

4215
24
07-14-2010 01:10 PM
by Anonymous User
Not applicable
Original User: tbrophy

All,
This is a question that I hope has been asked before:
Is there any way for my users to load a shapefile into my SFV while retaining the attributes and the symbology of the layer file?

I downloaded and am using �??Shape File Overlay for Sample Viewer�?� from the ArcGIS resource Center, submitted 3-4-2010 by �??HardWork�?�. It works great, but I need it go a step further.
//
Shape File Overlay for Sample Viewer
http://resources.esri.com/arcgisserver/apis/flex/index.cfm?fa=codeGalleryDetails&scriptID=16846
//

I really need the symbology and attributes to make it complete. Has anyone done this? Any suggestions would be incredibly appreciated!!!

I have 2 applications using Web Mercator & WGS84
Sample Flex Viewer: flexviewer-src-1.0.zip (Build 06.12.09)
agslib-1.2.swc
flex_sdk_3.2
Required Adobe Flash Player version 10

Attached are 2 sample shapefiles (and .lyr files) in both projections: (102100, 4326)
0 Kudos
24 Replies
by Anonymous User
Not applicable
Original User: EJAY_L

Hi coomsie,

Um...it didn't click. Sorry I didn't express myself well, I guess. In order to isntall the widget to the Flex view 2.0, I thought I need to open the zip file you provided above. So here's what I did. I download the file and try to unzip it to see what's inside and to install the widget but can't open it getting a "not valid zip file error message." Any further detailed instruction?

Thanks.


Hi,

it need the file to be a zip one currently,

i.e.

a normal zip file with all the normal gis file within it.

+ *.dbf
+ *.shp
+ *.shx


you could change the code to look for the files unzipped.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
YiJia LAI,

   That is a common error with this forum and zip files when you use IE to download. Try downloading the zip from Firefox or chrome and it will be fine.
0 Kudos
by Anonymous User
Not applicable
Original User: EJAY_L

YiJia LAI,

   That is a common error with this forum and zip files when you use IE to download. Try downloading the zip from Firefox or chrome and it will be fine.


Thanks Robert, you made my day. Can't believe this will happen though. What a tip for the IT help desk.
0 Kudos
AimeiChen
Occasional Contributor
Hi,

thanks for the code,

I've quickly converted to the SFV 2.0 Beta 2, if anyone wants it.

I've included the compiled version and source code.

DISCLAIMER: A quick modify of the existing widget to bring it up to flex 2.0, code by others ....

p.s. it would be interesting to extend this to have the same graphics colors etc control as the advanced graphic widget in sfv 1.3 (we have one for SFV 2.0 - using flex 2.0 api) and have the ability to push the graphic (which is now geometry) into a feature layer as a new edit feature ... 🙂 

Anyone thinking of doing that?

Cheers
Coomsie


Hi, Coomsie
Thanks for sharing the code.
It's been over a year now since you converted it to 2.0. I've loaded it into flexviewer 2.4 and found that it only works with point shape files, not polyine and polygon features. Do you have any plan to upgrade the source code for SFV 2.4?

Achen
0 Kudos
by Anonymous User
Not applicable
Original User: bfoster266

I'm also attempting to get this recompiled in 2.4, using the shape upload widget for 2.2 found at http://www.arcgis.com/home/item.html?id=bc6a88cf51364d9ea767347954363d7d.

I'm getting errors related to 'import org.vanrijkom.dbf.DbfTools;' and 'import org.vanrijkom.dbf.DbfRecord;', apparently this code is handled differently in 2.4. 

Here are the relevant lines:

import org.vanrijkom.dbf.DbfTools;
import org.vanrijkom.dbf.DbfRecord;
const dbfHeader:DbfHeader = new DbfHeader(dbfByteArray);
var dbfRecord:DbfRecord = DbfTools.getRecord(dbfByteArray, dbfHeader, index++);
var dbfRecord:DbfRecord = DbfTools.getRecord(dbfByteArray, dbfHeader, index++);
var dbfRecord:DbfRecord = DbfTools.getRecord(dbfByteArray, dbfHeader, index++);

Is there an easy way to rewrite these to compile correctly in 2.4?

Here is the code in context, with '---' added to the front to more easily identify the lines.

import mx.collections.*;
   import mx.controls.Alert;
   import mx.rpc.AsyncResponder;
   import mx.rpc.events.ResultEvent;  
  
   //
   import mx.collections.ArrayCollection;
   //import com.esri.ags.Graphic;
   import mx.utils.StringUtil;

---import org.vanrijkom.dbf.DbfTools;
---import org.vanrijkom.dbf.DbfRecord;                                       import widgets.ShapeFiles.ShpPolygon;
   import widgets.ShapeFiles.ShpLine;
   import widgets.ShapeFiles.ShpPoint;
---import org.vanrijkom.dbf.DbfHeader;
---import org.vanrijkom.dbf.DbfField;
   import widgets.ShapeFiles.ShpReader;
   import widgets.ShapeFiles.ShpHeader;
   import widgets.ShapeFiles.nochump.util.zip.ZipEntry;
   import widgets.ShapeFiles.nochump.util.zip.ZipFile;
  
  
   private var m_fileReference:FileReference;
  
   private var repPattern:RegExp = / /g;
  
   /* WC 7.22.2010 - graphiclayer graphics array */
   private var array:Array = [];
  
   //private var graphicsLayer:GraphicsLayer;
  
   private const ICON_URL:String = "widgets/ShapeFiles/assets/images/";
  
   private const TEXT_MODE:String = "text";
  
   private function init():void
   {
    wTemplate.addTitlebarButton(ICON_URL + "i_bookmark_add.png", "Load Shapefile", showStateLoad);
    wTemplate.addTitlebarButton(ICON_URL + "i_tools.png", "Display Preferences", showStateColors); 
   }  
  
   public function loadShapefile_clickHandler():void
   {
    m_fileReference = new FileReference();
    m_fileReference.addEventListener(Event.SELECT, selectHandler);
    m_fileReference.addEventListener(Event.CANCEL, cancelHandler);
    m_fileReference.browse([new FileFilter("Zip Files", "*.zip")]);
   
   }
  
   private function selectHandler(event:Event):void
   {
    m_fileReference.addEventListener(Event.COMPLETE, completeHandler);
    m_fileReference.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    m_fileReference.load();
   
   }
  
   private function completeHandler(event:Event):void
   {
    var shpByteArray:ByteArray;
    var dbfByteArray:ByteArray;
   
    const zipFile:ZipFile =  new ZipFile(m_fileReference.data);               
    for each (var entry:ZipEntry in zipFile.entries)
    {
     const name:String = entry.name.toLowerCase();
     // trace( name );
     if (name.indexOf("__macosx") > -1)
     {
      continue;
     }
     if (name.indexOf(".shp") === name.length - 4)
     {
      shpByteArray = zipFile.getInput(entry);
     }
     if (name.indexOf(".dbf") === name.length - 4)
     {
      dbfByteArray = zipFile.getInput(entry);
     }
    }
    if (shpByteArray && shpByteArray.length > 0 && dbfByteArray && dbfByteArray.length > 0)
    {
     loadShapefile(shpByteArray, dbfByteArray);
    }
    m_fileReference = null;
   }
  
   private function loadShapefile(shpByteArray:ByteArray, dbfByteArray:ByteArray):void
   {
    var index:int = 0;
    const shpReader:ShpReader = new ShpReader(shpByteArray);
---const dbfHeader:DbfHeader = new DbfHeader(dbfByteArray);
    /* WC 7.21.2010 - get shapefile type */
    var shapeType:int = shpReader.readShpType();
    while (shpReader.hasMore()) {
     if (shapeType == 1) {
      /* WC 7.21.2010 - shapefile type = mappoint */
      var shpPoint:ShpPoint = shpReader.readShpPoint();

   ---var dbfRecord:DbfRecord = DbfTools.getRecord(dbfByteArray, dbfHeader, index++);
      /* WC 7.22.2010 - setup label with first text field attribute */
      var txtSym:TextSymbol = new TextSymbol;
      txtSym.placement = "start";
      var fldIdx:int;
      var doneFlag:Boolean = false;
      for (fldIdx = 0; fldIdx < dbfHeader.fields.length; fldIdx++) {
       if (doneFlag == false && dbfHeader.fields[fldIdx].type == 67) {
        txtSym.text = StringUtil.trim(dbfRecord.values[dbfHeader.fields[fldIdx].name]);
        doneFlag = true;
       }
      }
      txtSym.xoffset = 3;
      var fBold:Boolean = bBold.selected;
      var fItalic:Boolean = bIta.selected;
      var fUnder:Boolean = bUnd.selected;
      txtSym.textFormat = new TextFormat("Verdana",sizeFont.value,cpFont.selectedColor,fBold,fItalic,fUnder);
      array.push(new Graphic(shpPoint.toPoint(), new SimpleMarkerSymbol(styleMark.text.replace(repPattern,"").toLowerCase(),sizeMark.value,cpMark.selectedColor,alphaMark.value/100), dbfRecord.values));
      array.push(new Graphic(shpPoint.toPoint(), txtSym, dbfRecord.values));
     } else if (shapeType == 3) {
      /* WC 7.21.2010 - shapefile type = polyline */
      var shpLine:ShpLine = shpReader.readShpLine();
   ---var dbfRecord:DbfRecord = DbfTools.getRecord(dbfByteArray, dbfHeader, index++);
      /* WC 7.22.2010 - setup label with first text field attribute */
      var txtSym:TextSymbol = new TextSymbol;
      txtSym.placement = "start";
      var doneFlag:Boolean = false;
      for (fldIdx = 0; fldIdx < dbfHeader.fields.length; fldIdx++) {
       if (doneFlag == false && dbfHeader.fields[fldIdx].type == 67) {
        txtSym.text = StringUtil.trim(dbfRecord.values[dbfHeader.fields[fldIdx].name]);
        doneFlag = true;
       }
      }
      var fBold:Boolean = bBold.selected;
      var fItalic:Boolean = bIta.selected;
      var fUnder:Boolean = bUnd.selected;
      txtSym.textFormat = new TextFormat("Verdana",sizeFont.value,cpFont.selectedColor,fBold,fItalic,fUnder);
      array.push(new Graphic(shpLine.toLine(), new SimpleLineSymbol(styleLine.text.replace(repPattern,"").toLowerCase(),cpLine.selectedColor,alphaLine.value/100,sizeLine.value), dbfRecord.values));
      array.push(new Graphic(new MapPoint(shpLine.extent.xmax, shpLine.extent.ymax), txtSym, dbfRecord.values));
     } else if (shapeType == 5) {
      /* WC 7.21.2010 - shapefile = polygon */
      var shpPolygon:ShpPolygon = shpReader.readShpPolygon();
   ---var dbfRecord:DbfRecord = DbfTools.getRecord(dbfByteArray, dbfHeader, index++);
      /* WC 7.22.2010 - setup label with first text field attribute */
      var txtSym:TextSymbol = new TextSymbol;
      txtSym.placement = "start";
      var doneFlag:Boolean = false;
      for (fldIdx = 0; fldIdx < dbfHeader.fields.length; fldIdx++) {
       if (doneFlag == false && dbfHeader.fields[fldIdx].type == 67) {
        txtSym.text = StringUtil.trim(dbfRecord.values[dbfHeader.fields[fldIdx].name]);
        doneFlag = true;
0 Kudos