Pages

Showing posts with label ArcGIS .Net WebADF. Show all posts
Showing posts with label ArcGIS .Net WebADF. Show all posts

Mar 7, 2012

How to get fields & assigned domains of FeatureClass + ESRI + C#

here is the code to get fields of a featureclass and check for the assigned domain 

IFields fields = featureClass.Fields;
string domainName = string.Empty;
for (int i = 0; i < fields.FieldCount; i++)
{
if (fields.get_Field(i).DomainFixed)
domainName = fields.get_Field(i).Domain.Name;
}

Thanks
Mukund

Mar 6, 2012

How to get feature class from the shape file on Disk.

Below is the method that gets the feature class from the shape file
/// Get the FeatureClass from a Shapefile on disk (hard drive).
/// A System.String that is the directory where the shapefile is located.
Example: "C:\data\USA"

/// A System.String that is the shapefile name.
 Note: the shapefile extension's
///(.shp, .shx, .dbf, etc.) is not provided! Example: "States"</param>

public ESRI.ArcGIS.Geodatabase.IFeatureClass GetFeatureClassFromShapefileOnDisk
(System.String string_ShapefileDirectory, System.String string_ShapefileName)
{
     System.IO.DirectoryInfo directoryInfo_check = new System.IO.DirectoryInfo(                         
                                                                                 string_ShapefileDirectory);
      if (directoryInfo_check.Exists)
    {
        //We have a valid directory, proceed
        System.IO.FileInfo fileInfo_check = new System.IO.FileInfo(string_ShapefileDirectory + "\\" +           
                                                                     string_ShapefileName + ".shp");
    if (fileInfo_check.Exists)
   {
        //We have a valid shapefile, proceed

         ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory =
                                                 new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); 
         ESRI.ArcGIS.Geodatabase.IWorkspace workspace =      
                                                 workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0); 
         ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace =
                                                  (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
       // Explict Cast

        ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = 
                                                featureWorkspace.OpenFeatureClass(string_ShapefileName);
        return featureClass;
}
else
{
     //Not valid shapefile
      return null;
}
}
 else
{
    // Not valid directory
       return null;
}
}

How to find feature count that intersect an envelope in Geodatabase + C#

In our previous post, we discussed about connecting the File GeoDB and returning the Workspace.

Now will see how to find feature count that intersect an envelope in the File GeoDB

// The method FileGdbWorkspaceFromPropertySet definition is present in our previous post

IWorkspace featureWorkspace = FileGdbWorkspaceFromPropertySet(gdbPath);
        IFeatureWorkspace fws = (IFeatureWorkspace)featureWorkspace;

// Method that Gets Features Count From a FeatureClass

private static int GetCountFromFeatureClass(IFeatureWorkspace fws, string parcelName, IEnvelope envelope)
    {
        // Open the feature classes used by the queries.
        IFeatureClass parcelsFeatureClass = fws.OpenFeatureClass(parcelName);      

        //// Create the spatial filter. Note that the SubFields property specifies that only
        //// the Shape field is retrieved, since the features' attributes aren't being inspected.
        ISpatialFilter spatialFilter = new SpatialFilterClass();
        spatialFilter.Geometry = envelope;
        spatialFilter.GeometryField = parcelsFeatureClass.ShapeFieldName;
        spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
        spatialFilter.SubFields = "Shape";

        // Use IFeatureClass.FeatureCount to get a parcel count.
        return parcelsFeatureClass.FeatureCount(spatialFilter);

    }


Feb 15, 2012

How to connect to a ESRI Geodatabase and return workspace.

Initialize Product License with License Initializer

Connecting to GeoDB is the first step of querying the GeoDB. Inorder to work with the ArcObjects, the ArcGIS license has to be initialized. ESRI provides a License initializer project downloaded from the below link.


http://edn.esri.com/index.cfm?fa=codeExch.sampleDetail&pg=/arcobjects/9.1/Samples/Licensing_and_Extension_Checking/InitializeProductLicenseWithLicenseInitializer/InitializeProducLicenseWithLicenseInitializer.htm


Download the Project and add it to your solution which uses arcObjects. Set the Project InitializeProductLicense as the startup and run the project. Debug and see what is the license status returned.


below are the different cases returned

//esriLicenseAvailable                 
//esriLicenseNotLicensed
//esriLicenseFailure                   
//esriLicenseAlreadyInitialized                  
//esriLicenseNotInitialized
//esriLicenseCheckedOut                    
//esriLicenseCheckedIn
IF the return value is isriLicenseCheckedOut, then license is intialized.     
Below is the method that get path of the geoDB say C:\\test\\Test.gdb and returns the workspace that can be queried for different scenarios.              
Namespace Used:

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;


public static IWorkspace FileGdbWorkspaceFromPropertySet(String path)
        {
            //Initialize the application.
            esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
            IAoInitialize m_AoInitialize = new AoInitializeClass();
            licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
          
            Type factoryType = Type.GetTypeFromProgID(
            "esriDataSourcesGDB.FileGDBWorkspaceFactory");
            IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)Activator.CreateInstance
                (factoryType);
            return workspaceFactory.OpenFromFile(path, 0);
        }


In the next post will see how to query parcels intersecting a envelope in the GeoDB.

Feb 8, 2010

How to draw a Geometry on the Map + ArcGIS + .Net Web ADF

This is a common requirement in the Mapping Applications to Draw the People's Area of Interest(AOI) in different geometry forms like Rectangle, Polygon etc.

Below is the method that allow you draw the AOI on the map and zoom to it, when you pass the bounds as inputs.

public void DrawRectangleOnMap(double minX,double minY,double maxX,double maxY)
{
ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality adfGraphicsMapFunctionality = null;
ElementGraphicsLayer elementGraphicsLayer = null;
GraphicsDataSet graphicsDataSet = null;

 
try
{
foreach (IMapFunctionality mapFunctionality in Map1.GetFunctionalities())
{
// If multiple graphics resources are available, use the resource name to distinguish
if (mapFunctionality.Resource.Name == "ADFGraphicsResource")
{
adfGraphicsMapFunctionality = mapFunctionality as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality;
break;
}
}


// Return if there is no Graphic Resource.
if (adfGraphicsMapFunctionality == null)
return;
elementGraphicsLayer = new ElementGraphicsLayer();
elementGraphicsLayer.TableName = Guid.NewGuid().ToString();

 // Add graphics layer to map functionality graphics dataset
graphicsDataSet = adfGraphicsMapFunctionality.GraphicsDataSet;
graphicsDataSet.Tables.Add(elementGraphicsLayer);


// Create a Geometry using the bound values from the QueryString .
ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
 adfPolygon.Rings.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Ring
(new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minX, minY, maxX, maxY)));

 // Fill the Geometry with color and style.
ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol simpleFillSymbol =
new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol();
simpleFillSymbol.Transparency = 100;
simpleFillSymbol.BoundaryColor = System.Drawing.Color.Red;

// Loads the Geometry to the GraphicElement.
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement =
new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPolygon, simpleFillSymbol);


// Add the GraphicElement to the GraphicsLayer.
elementGraphicsLayer.Add(graphicElement);

// Zooms to the AOI
Map1.Zoom(adfPolygon);
}
catch (Exception)
{
throw;
}
finally
{
elementGraphicsLayer = null;
graphicsDataSet = null;
adfGraphicsMapFunctionality = null;
}
}





Jan 23, 2010

How to find the layers that intersect with an envelope or geometry + ArcGIS .Net web ADF

Layers Intersection with an Envelope:
As we know Layers intersection is a common task in GIS, here we achieve this using Identity method.
Below is the code used to find the layers in the mapresource which are being intersected 
with an envelope.


Method Call:
DataTable dt  = GetIntersectionLayers(Map1.GetFunctionalities(0),x1,y1,x2,y2);


Method Definition:
public DataTable[] GetIntersectionLayers(
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality gisFunctionality, double minX, 
double minY, double maxX, double maxY)
    {
        int pixelTolerance = 1;
        DataTable[] dt = null;
        ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisResource =   gisFunctionality.Resource;
        
        ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality queryFunctionality =
            (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality
            (typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);


        string[] layerIDs = null;
        string[] layerNames = null;
        queryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames);            


        ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope =
            new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minX, minY, maxX, maxY);            


        dt = queryFunctionality.Identify(gisFunctionality.Name, adfEnvelope,
            pixelTolerance, ESRI.ArcGIS.ADF.Web.IdentifyOption.AllLayers, layerIDs);
        return dt;                
    } 


The method returns the data table with a collection of intersected layers.

Jan 2, 2010

how to zoom to a particular coordinate location in ArcGIS .Net webADF

Below is the method used to zoom in to a particular location using the coordinates. Unlike MapExtreme, ArcGIS uses ZoomFactor to zoom in and zoom out instead of ZoomWidth in MapExtreme.
Method:
private void ZoomToCoordinates()
    {
        ESRI.ArcGIS.ADF.Web.Geometry.Point center = null;
        try
        {            
            center = new ESRI.ArcGIS.ADF.Web.Geometry.Point(17.7244, 1.1883);
           // Numeric value is the Zoom factor(1-10 ZoomsIn & 0 to 1 ZoomsOut)
            Map1.Zoom(2, center);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            center = null;
        }
    }

How to add map resource dynamically to MapResourceManager + ArcGIS 9.3 and 9.3.1 .Net WebADF

In ArcGIS webADF, After publishing the services using ArcGIS SERVER(say map service), we can just add a map resource to the MapResourceManager manually in design time.


Now lets see how to add the map resource dynamically through code. Because mostly we may have multiple map resource that has to be shown only on request.


Below is the code through which we can achieve this.


Method call:
AddMapResourceDynamically("GlobalService", "Layers@GlobalService", false);
- GlobalService is the service name.
- Layers@GlobalService is the service definition(i.e Layers@Service_Name is the format)
- true or false to list the resource in the TOC Control.


Method Definition:
private void AddMapResourceDynamically(string resName, string resDefinition, bool IsTOC)
    {
        string m_ResourceName = string.Empty;
        MapResourceItem mapResourceItem = null;
        GISResourceItemDefinition resDef = null;
        DisplaySettings displaySettings = null;
        try
        {
            mapResourceItem = new MapResourceItem();
            resDef = new GISResourceItemDefinition();
            mapResourceItem.Name = resName;


            // The services url which we use to fetch the services.
            resDef.DataSourceDefinition = @"http://hostname/arcgis/services";
            resDef.DataSourceType = "ArcGIS Server Internet";
            resDef.ResourceDefinition = resDefinition;
            resDef.DataSourceShared = true;
            mapResourceItem.Parent = MapResourceManager1;
            mapResourceItem.Definition = resDef;


            displaySettings = new DisplaySettings();
            displaySettings.Transparency = 0;
            displaySettings.Visible = true;
            displaySettings.DisplayInTableOfContents = IsTOC;
            displaySettings.ImageDescriptor.TransparentColor = System.Drawing.Color.White;
            displaySettings.ImageDescriptor.TransparentBackground = true;
            mapResourceItem.DisplaySettings = displaySettings;


            // Insert the new resource item at the beginning (top).             MapResourceManager1.ResourceItems.Insert(MapResourceManager1.ResourceItems.Count, mapResourceItem);


            // Create the map resource and return a reference in case it will be the 
            // primary map resource.
            IMapResource mapResource =       MapResourceManager1.CreateResource(mapResourceItem);


            // If no other resource items are present, set the new map resource item as the 
            // primary map resource.
            if (MapResourceManager1.ResourceItems.Count < 1)
            {
                Map1.PrimaryMapResource = mapResourceItem.Name;
                Map1.Focus();
                Map1.InitializeFunctionalities();
                Map1.InitializeTileFunctionalities();
            }
            Toc1.Refresh();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            mapResourceItem = null;
            resDef = null;
            displaySettings = null;
        }         
    }


The above code will add the map resource dynamically to the MapResourceManager and display it on the map on runtime.