Getting an Attribute value for a specific Geometry

Topics: Data Access, General Topics, SharpMap v2.0
Jan 29, 2009 at 8:24 PM
This is my first post.  I would like to state that SharpMap is Great!  Even though the learning curve has been steep for me.(I am learning C# as I go) 

I am doing a web project in that shapefiles are drawn and queried upon a virtual earth map.  I am also providing tools to work with the data.  I do have some experience with ArcObjects and VB.. but stuff is done much differently in SharpMap.  I am posting this to help other folks because I found a solution and to solicite from the experts a better or more efficient way of accomplishing this task.

I am using Juan DoNeblo template for using Sharpmap to read a shapefile and display the drawing on a Virtual Earth Map through a web service call and javascript.  My client wanted to color code lines from a shapefile based upon a data attribute field.  In arcobjects this easy because the feature is linked through the FID.  I could not figure out how to access the FID through the geometry.  Basically I am using Sharpmap to read a shapefile and convert all the vertices to points and send them via webservice back to the client from which I use the VE API to draw if points or polygons lines between vertices.  So when I make my call to the web service I am sending the path to the shapefile and the fieldname to  which to attach the value for each feature sent back.  My first attempt using the Intersections methods would result in set of multiple features .  I had no way of telling which record went with what geometry (did not know how to access the FID through the Geometry collection.)  Looking at the files I noticed that the FeatureDataRow had a geometry property.  So I reconfigured  and looped through the BoundBox results for Geometries that were equal.  If they were I then queried attibute value and sent that along with a point collect (list) so that VE could draw the shape. 

Is there a more efficient or better way of doing this? --- I would feel much better equating FID's or Object ID as in the ESRI framework.  

<code snippet of web service>

if

 

(sf.ShapeType != ShapeType.Polygon && sf.ShapeType != ShapeType.PolyLine &&
     sf.ShapeType !=
ShapeType.MultiPoint && sf.ShapeType != ShapeType.Point)
     return null;
   // Initializing the list of Shapes to be returned
   List<Shape> layer = new List<Shape>();
   // Retrieving all the geometries in the Shapefile
   BoundingBox ext = sf.GetExtents();
   IEnumerable<Geometry> geometries = sf.ExecuteGeometryIntersectionQuery(ext);
   // Iterating through all the geometries in the shapefile, converting them
   // to Shape objects and adding them to the output list
   // along with the value of the attribute field (further classify the features)
     
foreach (Geometry geometry in geometries){
        
List<PointF> points = new List<PointF>();
        
string gClass = "";
       
// Eval only if a Field name is passed.
       
if (classify.Length > 0){
            FeatureDataSet ds = new FeatureDataSet();
            // fill a dataset with those items within the individual geometry
            // bounding box
            sf.ExecuteIntersectionQuery(geometry.GetBoundingBox(), ds);
             
FeatureDataTable dt = ds.Tables[0];
          
// Check to see if the intersection results in multiple records
          
// ideally should be one but geometry shape can be such that multiple
          
// other parts will fall in the bounding box.
         
if (dt.Rows.Count > 0){
            
// Check to see if the attribute data features geometry matches
            
// the Geometry which we are loop through. If so then
            
// assign the value of the field to the returning variable
           
// this way I am sure the right data is being assign to the correct
            
// geometry
                
foreach (FeatureDataRow row in dt.Rows){
                    
if (geometry.Equals(row.Geometry))
                        gClass = row[classify].ToString();
                    }
         }
}

 

 

 

// For Polygons
if (sf.ShapeType == ShapeType.Polygon)
......