You are here: > ESRI Forums > arcgis engine forums > Thread Replies

ArcGIS Engine Forums

ArcGIS Engine: ArcObjects .NET API forum

Get Polygon extents via FeatureClass   Norman Paterson Feb 07, 2007
Re: Get Polygon extents via FeatureClass   Melita Kennedy Feb 07, 2007
Re: Get Polygon extents via FeatureClass   Norman Paterson Feb 07, 2007
Re: Get Polygon extents via FeatureClass   Michael Knight Feb 07, 2007
Re: Get Polygon extents via FeatureClass   Norman Paterson Feb 08, 2007
Re: Get Polygon extents via FeatureClass   Norman Paterson Feb 14, 2007
Re: Get Polygon extents via FeatureClass   Norman Paterson Feb 14, 2007
Re: Get Polygon extents via FeatureClass   Michael Knight Feb 14, 2007
Re: Get Polygon extents via FeatureClass   Norman Paterson Feb 15, 2007
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Get Polygon extents via FeatureClass 
Author Norman Paterson 
Date Feb 07, 2007 
Message I have a layer of polygons. I need to loop through each polygon on the layer, and return the minx and miny value of the polygon to a custom function. The code attached is my first look at the FeatureClass - I noticed that the SpatialReference has this data - perhaps you know of an alternate method to get this, with this code I get a cast error "Unable to cast COM object of type System_ComObject to class ESRI.ArcGIS.ADF.ArcGISServer.SpatialReference 
 
            ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)args.Control;
            //================================================================
            // Map Query
            //================================================================
            // Get MapFunctionality from the control...
            MapFunctionality mapfunc = (MapFunctionality)mapctrl.GetFunctionality(mapctrl.MapResourceManagerInstance.ResourceItems.Count - 1);
            MapResourceLocal mapres = (MapResourceLocal)mapfunc.MapResource;
            IServerContext sc = mapres.ServerContextInfo.ServerContext;
            IMapServer map = mapres.MapServer;
            IMapServerObjects mapobj = (IMapServerObjects)map;
            IMap fgmap = mapobj.get_Map(map.DefaultMapName);
            IFeatureLayer fl = (IFeatureLayer)fgmap.get_Layer(2);
            IFeatureClass fc = fl.FeatureClass;
            IQueryFilter queryFilter = new QueryFilterClass(); 
            queryFilter.WhereClause = "SEVERITY='High'";
            ISelectionSet selectionSet =  (ESRI.ArcGIS.Geodatabase.SelectionSet)fc.Select(queryFilter,  esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);
            int mycount = selectionSet.Count;
            string sFCType = "";
            string sFieldData = "";
            if (fc.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
            {
                sFCType = "Polygon";
            }
            else
            {
                sFCType = "NA";
            }
            IFields pFlds = fc.Fields;
            IField pFld;
            //IGeometryDef iGeoDef;
            ESRI.ArcGIS.ADF.ArcGISServer.GeometryDef iGeoDef;
            SpatialReference sRef;
            string sX, sY;
            for (int x = 1;x < pFlds.FieldCount;x++)
            {
                pFld = pFlds.get_Field(x);
                sFieldData = pFld.Name;
                iGeoDef = (ESRI.ArcGIS.ADF.ArcGISServer.GeometryDef)pFld.GeometryDef;
                sRef = iGeoDef.SpatialReference;
                sX = sRef.XOrigin.ToString();
            }
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Melita Kennedy 
Date Feb 07, 2007 
Message Actually you don't want the spatial reference. Instead, check out IGeometry::Envelope.

The properties in the spatial reference are used for geodatabase storage and when performing topological operations.

Melita 
  Melita Kennedy
ESRI Product Specialist 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Norman Paterson 
Date Feb 07, 2007 
Message Thanks for your help Melita. The following function is what I came up with, it returns a datatable with all the polygon x/y coords. I plan to revise it to return an envelope and fire a function passing in that envelope.

Because the layer contains only polygons I simply have to find the envelope info for each shape in the shapefile. FYI the polygons contained in the layer in question is basically a grid laid out using polygon shapes. 
 
private DataTable IterateGridCells(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl)
        {
            System.Data.DataTable dt = new DataTable();
            dt.Columns.Add("OID");
            dt.Columns.Add("XMin");
            dt.Columns.Add("YMax");
            MapFunctionality mapfunc = (MapFunctionality)mapctrl.GetFunctionality(mapctrl.MapResourceManagerInstance.ResourceItems.Count - 1);
            MapResourceLocal mapres = (MapResourceLocal)mapfunc.MapResource;
            IServerContext sc = mapres.ServerContextInfo.ServerContext;
            IMapServer map = mapres.MapServer;
            IMapServerObjects mapobj = (IMapServerObjects)map;
            IMap fgmap = mapobj.get_Map(map.DefaultMapName);
            IFeatureLayer fl = (IFeatureLayer)fgmap.get_Layer(2);
            IFeatureClass fc = fl.FeatureClass;
            IQueryFilter queryFilter = new QueryFilterClass();
            queryFilter.WhereClause = "SEVERITY='High'";
           // queryFilter.WhereClause = "1=1";
            IFeatureCursor fcursor = fc.Search(queryFilter, false);
            IFeature ifeats = fcursor.NextFeature();
            while (ifeats != null)
            {
                string sy = ifeats.Shape.Envelope.XMin.ToString();
                string sx = ifeats.OID.ToString();

                IFeature feat = fc.GetFeature(ifeats.OID);
                double dXMin = feat.Shape.Envelope.XMin;
                double dYMax = feat.Shape.Envelope.YMax;

                //This is what I left working on, want to pass an envelope rather than x/y
                //because we are dealing with a polygon:
                IEnvelope env = new EnvelopeClass();
                env.PutCoords(dXMin, 1 ,1, dYMax);

                dt.Rows.Add(sx.ToString(),dXMin.ToString(),dYMax.ToString());
                ifeats = fcursor.NextFeature();
            }
            return dt;
        }
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Michael Knight 
Date Feb 07, 2007 
Message If you actually want the coordinates of the polygons corners, the envelopes min/max x/y aren't it (the envelope is actually a larger area surrounding the shape). You will have to cast the shape to IPolygon and/ or IPointCollection to get the actual points (x,y) of each corner of each polygon in the grid. 
  Michael Knight
GIS Solutions Architect
Contract Land Staff, LLC
Stafford, Texas

http://www.contractlandstaff.com

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Norman Paterson 
Date Feb 08, 2007 
Message I thought that was the case, thank you so much for your feedback, it's invaluable. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Norman Paterson 
Date Feb 14, 2007 
Message Weird, even thought my code was similar, near identical, when I ported this vb function from the ArcGIS Snippet Finder to C# and used it it finally works! 
 
private IFeatureCursor SpatialQuery(ESRI.ArcGIS.Geodatabase.IFeatureClass fc,
            ESRI.ArcGIS.Geometry.IGeometry geo, ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum se,
            string WhereClause)
        {
            ESRI.ArcGIS.Geodatabase.ISpatialFilter sf = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
            sf.Geometry = geo;
            string nameOfShapeField = fc.ShapeFieldName;
            sf.GeometryField = nameOfShapeField;
            sf.SpatialRel = se;
            sf.WhereClause = WhereClause;
            ESRI.ArcGIS.Geodatabase.IQueryFilter qf = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
            ESRI.ArcGIS.Geodatabase.IFeatureCursor featcr = fc.Search(qf,false);
            return featcr;
        }
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Norman Paterson 
Date Feb 14, 2007 
Message Well, I thought it solved my problem but it doesn't. For some stupid reason, the sample code converts the spatial query to an IQueryFilter. Seems that the result of this is it filters the results WITHOUT the spatial attributes - because I get all my shapes in the entire map layer returned. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Michael Knight 
Date Feb 14, 2007 
Message Try using the spatial filter in place of the query filter:

ESRI.ArcGIS.Geodatabase.IFeatureCursor featcr = fc.Search(sf,false);
 
  Michael Knight
GIS Solutions Architect
Contract Land Staff, LLC
Stafford, Texas

http://www.contractlandstaff.com

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Get Polygon extents via FeatureClass 
Author Norman Paterson 
Date Feb 15, 2007 
Message Thanks for your help. I noticed an error in the coding, and tried that. The feature cursor contains no data, just empty fields. I'm going to try the ITopologicalOperator Simplfy on the geometry object to see if that corrects this, I can't believe it's not working. I wonder if it has something to do with the fact that the polygon passed to the SpatialFilter resides in a different layer than the layer the SpatialFilter is run against? Or possibly the server context? I've checked that the source polygon has valid attributes (only problem I see is it has no Z attribute, but its only 2 dimmensional - when I view the IGeometry object in debug mode, it indicates that Envelope.Depth threw an exception - that the geometry is not Z-Aware - but this exception is not raised in the program), that I'm query the right layers, etc. 
 
private void GetDistressData(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl, ESRI.ArcGIS.Geometry.IGeometry mappoly)
        {
            MapFunctionality mapfunc = (MapFunctionality)mapctrl.GetFunctionality(2);//Distress Layer
            MapResourceLocal mapres = (MapResourceLocal)mapfunc.MapResource;
            IServerContext sc = mapres.ServerContextInfo.ServerContext;
            IMapServer map = mapres.MapServer;
            IMapServerObjects mapobj = (IMapServerObjects)map;
            IMap fgmap = mapobj.get_Map(map.DefaultMapName);
            IFeatureLayer fl = (IFeatureLayer)fgmap.get_Layer(2);//Distress Polygon Layer
            IFeatureClass fc = fl.FeatureClass;
            //ISpatialReference ir = (ESRI.ArcGIS.Geometry.ISpatialReference)fgmap.SpatialReference;
            ESRI.ArcGIS.Geodatabase.IFeatureCursor fcursor = SpatialQuery(fc, mappoly,
                ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains, "1=1");
            //Create a datatable to hold the results:
            System.Data.DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("SEVERITY");
            dt.Columns.Add("STRTYPE");
            dt.Columns.Add("DATEINSP");
            dt.Columns.Add("INSPECTR");
            dt.Columns.Add("COMMENT");
            //end data table definition
            IFeature pFeature;
            while ((pFeature = fcursor.NextFeature()) != null)
            {
                //int id = pFeature.Fields.FindField("STRTYPE");
                //string sID = pFeature.get_Value(id).ToString();
                string iID = pFeature.get_Value(pFeature.Fields.FindField("ID")).ToString();
                string Severity = pFeature.get_Value(pFeature.Fields.FindField("SEVERITY")).ToString();
                string sType = pFeature.get_Value(pFeature.Fields.FindField("STRTYPE")).ToString();
                string sDate = pFeature.get_Value(pFeature.Fields.FindField("DATEINSP")).ToString();
                string sInspector = pFeature.get_Value(pFeature.Fields.FindField("INSPECTR")).ToString();
                string sComment = pFeature.get_Value(pFeature.Fields.FindField("COMMENT")).ToString();
                dt.Rows.Add(iID,Severity,sType,sDate,sInspector,sComment);
            }
            int iomg = dt.Rows.Count;
            //-----------------------------------------------------------------
            if (dt.Rows.Count > 0)
            {
                string returnstring = null;
                GridView gvPoly = (GridView)mapctrl.Page.FindControl("gvPoly");
                gvPoly.DataSource = dt;
                gvPoly.DataBind();
                gvPoly.Visible = true;
                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    HtmlTextWriter htw = new HtmlTextWriter(sw);
                    gvPoly.RenderControl(htw);
                    htw.Flush();
                    returnstring = returnstring + sw.ToString();
                }
                CallbackResult cr = new CallbackResult("div", "griddiv", "innercontent", returnstring);
                mapctrl.CallbackResults.Add(cr);
            }
        }
        private ESRI.ArcGIS.Geodatabase.IFeatureCursor SpatialQuery(ESRI.ArcGIS.Geodatabase.IFeatureClass fc,
            ESRI.ArcGIS.Geometry.IGeometry geo, ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum se,
            string WhereClause)
        {
            ESRI.ArcGIS.Geodatabase.ISpatialFilter sf = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
            //ESRI.ArcGIS.Geometry.IEnvelope env = geo.Envelope;
            sf.Geometry = geo;
            string nameOfShapeField = fc.ShapeFieldName;
            sf.GeometryField = nameOfShapeField;
            sf.SpatialRel = se;
            //sf.SpatialRelDescription = "T**TF****";
            sf.WhereClause = WhereClause;
            //Line below is from sample code, except it returns all data, without filtering
            //by polygon dimmensions:
            ESRI.ArcGIS.Geodatabase.IQueryFilter qf = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
            qf = (ESRI.ArcGIS.Geodatabase.IQueryFilter)sf;
            ESRI.ArcGIS.Geodatabase.IFeatureCursor featcr;
            featcr = fc.Search(sf,false);
    
            return featcr;
        }