TitlePages

Showing posts with label StoryTellingMaps. Show all posts
Showing posts with label StoryTellingMaps. Show all posts

Thursday, 15 January 2015

Modifying the Vertices of a Polyline in ArcMap using ArcObjects

Modifying the Vertices of a Polyline using ArcObjects


1.Start the Editing Mode in the Editor Toolbar

2.This will create an edit session

3.Using the editor tool select the polyline feature you want to edit

4.Click on our Custom tool

5.The vertices of our polyline will be visible

6.Save the edits

7.Stop editing and complete the session

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;


namespace ToolTest
{
    public class poli : ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        public static Boolean mousedown = false;
        public static IEditor3 meditor=null;
        IEditSketch3 editsketch = meditor as IEditSketch3;

        public poli()
        {
        }

        protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            if (mousedown == false)
            {

                meditor = GetEditorFromArcMap(ArcMap.ThisApplication) as IEditor3;
                //IWorkspace workspace = (GetFeatureLayerFromLayerIndexNumber((ArcMap.Application.Document as IActiveView), 1).FeatureClass as IWorkspace);
                //if(meditor.EditState!=esriEditState.esriStateEditing)
                //meditor.StartEditing(workspace);






                IEditSketch3 editsketch = meditor as IEditSketch3;
                editsketch.GeometryType = esriGeometryType.esriGeometryPolyline;


                 IEditTaskSearch editTaskSearch = meditor as IEditTaskSearch;
 IEditTask editTask = editTaskSearch.get_TaskByUniqueName("GarciaUI_ModifyFeatureTask");

  //Set the task returned to the current edit task.
 if (editTask != null)
 {
     meditor.CurrentTask = editTask;
 }
                ISketchOperation sk = new SketchOperationClass();
                sk.Start(meditor);

             

             
            }
            if(mousedown==true)
            {
                if(editsketch.Geometry==null)
                {
                    IPolyline poly;
                    poly = editsketch as IPolyline;
                    editsketch.FinishSketch();

                }
                else
                {
                    mousedown=false;
                }


            }
       

         
        }


        protected override void OnMouseUp(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
         
        }

public ESRI.ArcGIS.Editor.IEditor2 GetEditorFromArcMap(ESRI.ArcGIS.ArcMapUI.IMxApplication mxApplication)
{
  if(mxApplication == null)
  {
    return null;
  }
  ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
  uid.Value = "{F8842F20-BB23-11D0-802B-0000F8037368}";
  ESRI.ArcGIS.Framework.IApplication application = mxApplication as ESRI.ArcGIS.Framework.IApplication; // Dynamic Cast
  ESRI.ArcGIS.esriSystem.IExtension extension = application.FindExtensionByCLSID(uid);
  ESRI.ArcGIS.Editor.IEditor2 editor2 = extension as ESRI.ArcGIS.Editor.IEditor2; // Dynamic Cast

  return editor2;
}




public ESRI.ArcGIS.Carto.IFeatureLayer GetFeatureLayerFromLayerIndexNumber(ESRI.ArcGIS.Carto.IActiveView activeView, System.Int32 layerIndex)
{
    if (activeView == null || layerIndex < 0)
    {
        return null;
    }
    ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
    if (layerIndex < map.LayerCount && map.get_Layer(layerIndex) is ESRI.ArcGIS.Carto.IFeatureLayer)
    {
        return (ESRI.ArcGIS.Carto.IFeatureLayer)activeView.FocusMap.get_Layer(layerIndex); // Explicit Cast
    }
    else
    {
        return null;
    }
}
public ESRI.ArcGIS.Geometry.IPointCollection4 modifyFirstVertexOfAPolyline
(ESRI.ArcGIS.Geometry.IGeometryCollection geometryCollection_Polyline,
System.Double searchRadius, System.Double offsetX, System.Double offsetY)
{
    ESRI.ArcGIS.Geometry.IPolyline polyline = (ESRI.ArcGIS.Geometry.IPolyline)
        geometryCollection_Polyline;
    ESRI.ArcGIS.Geometry.IPoint queryPoint = polyline.FromPoint;

    ESRI.ArcGIS.Geometry.IPoint hitPoint = new ESRI.ArcGIS.Geometry.PointClass();

    //Define and initialize the variables that will get populated from the .HitTest() method.
    System.Double hitDistance = 0;
    System.Int32 hitPartIndex = 0;
    System.Int32 hitSegmentIndex = 0;
    System.Boolean rightSide = false;

    ESRI.ArcGIS.Geometry.IHitTest hitTest = (ESRI.ArcGIS.Geometry.IHitTest)
        geometryCollection_Polyline;
    System.Boolean foundGeometry = hitTest.HitTest(queryPoint, searchRadius,
        ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex,
        hitPoint, ref hitDistance, ref hitPartIndex, ref hitSegmentIndex, ref
        rightSide);

    if (foundGeometry == true)
    {
        ESRI.ArcGIS.Geometry.IGeometry geometry =
            geometryCollection_Polyline.get_Geometry(hitPartIndex);
        ESRI.ArcGIS.Geometry.IPointCollection4 pointCollection =
            (ESRI.ArcGIS.Geometry.IPointCollection4)geometry;
        ESRI.ArcGIS.Geometry.IPoint transformPoint = pointCollection.get_Point
            (hitSegmentIndex);

        ESRI.ArcGIS.Geometry.ITransform2D transform2D =
            (ESRI.ArcGIS.Geometry.ITransform2D)transformPoint;
        transform2D.Move(offsetX, offsetY);

        ESRI.ArcGIS.Geometry.IPoint afterMovePoint = (ESRI.ArcGIS.Geometry.IPoint)
            transform2D;

        //The point is not updated in the polyline until the next line is called.
        pointCollection.UpdatePoint(hitSegmentIndex, (ESRI.ArcGIS.Geometry.IPoint)
            transform2D);

        return pointCollection;
    }

    return null;

}

        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }
    }

}

Wednesday, 26 November 2014

Custom Geoprocessing Tool


Arcobjects Geoprocessing Procedure:



1. Create a geoprocessor object and set its properties.
2. Set the appropriate values for the tool’s parameters.
3. Execute a tool using the geoprocessor object’s Execute() method.

In order to create a geoprocessor object, the IGeoProcessor2 interface can be used. This interface
is defined in the geoprocessing library of ArcObjects and is implemented by the GeoProcessor
CoClass.

IGeoProcessor2 gp = new GeoProcessorClass();
//add the result of geoprocessing as a new layer to Map
gp.AddOutputsToMap = true;
//if output of geoprocessing exists before the execution of tool
//it will be overwritten
gp.OverwriteOutput = true;

Assume that you want to create Thiessen polygons for the cities FeatureClass to create proximal
zones for all cities. The proximal zones represent full areas where any location inside the zone is
closer to its associated city than any other city. Based on the Create Thiessen Polygons reference
page, you have to provide at least an input point FeatureLayer and the path to the output
FeatureClass.
In order to create and set each parameter, the IVariantArray interface of the System library must
be used.

IVariantArray parameters = new VarArrayClass();

Each parameter has to be added to the IVariantArray interface in the exact order that is specified
on the tool’s reference page.

//in_features
parameters.Add(@"D:\DataFolder\fileGDB.gdb\cities");
//out_feature_class
parameters.Add(@"D:\DataFolder\fileGDB.gdb\citiesThiessen");
//fields_to_copy(Optional)
parameters.Add("ALL");

As it is illustrated on the Create Thiessen Polygons reference page, the third parameter is optional.
You can simply not add any value to IVariantArray or, as shown in the preceding code, you can
provide an appropriate value for the optional parameter. You can skip the optional parameter
using an empty string as input to IVariantArray’s Add() method. For example, look at the
reference page of the Buffer tool; you can see that three of the seven available parameters are
mandatory. The following code demonstrates how to skip the fourth parameter and specify the
fifth parameter:

//1-in_features
parameters.Add(@"D:\test.gdb\cities");
//2-out_feature_class
parameters.Add(@"D:\test.gdb\citiesBuffer");
//3-buffer_distance_or_field
parameters.Add("50 kilometers");
//4-line_side(Optional)
parameters.Add("");
//5-line_end_type(Optional)
parameters.Add("ROUND");
//6 &7 there is no need to provide empty string
//for the rest of parameters since you don't want to set them
After setting all the required parameters, all you need to run a tool is to call the Execute() method
of the geoprocessor object. The Execute() method solicits the name of the tool and its parameters.

gp.Execute("CreateThiessenPolygons_analysis", parameters, null);
The following code shows the complete code for this example. In order to run the code, you need to
add references to the Geoprocessing and System libraries of ArcObjects:
IGeoProcessor2 gp = new GeoProcessorClass();
//add the result of geoprocessing as a new layer to Map
gp.AddOutputsToMap = true;
//if output of geoprocessing exists before the execution of tool
//it will be overwritten
gp.OverwriteOutput = true;
IVariantArray parameters = new VarArrayClass();
//in_features
parameters.Add(@"D:\DataFolder\fileGDB.gdb\cities");
//out_feature_class
parameters.Add(@"D:\DataFolder\fileGDB.gdb\citiesThiessen");
//fields_to_copy(Optional)
parameters.Add("ALL");
//or parameters.Add("");
gp.Execute("CreateThiessenPolygons_analysis", parameters, null);


The Geoprocessing library of ArcObjects is accessible through the ESRI.ArcGIS.Geoprocessing
namespace. This library contains a few hundred types which can be used to run and manage tools
and GIS workfl ows. You learned earlier in this section that IGeoProcessor2 is the main interface of
this library and the easiest way to run a geoprocessing tool is to call its Execute() method.
However, using IGeoProcessor2 is not the only approach to run a geoprocessing tool or model.
There are some managed assemblies created by Esri to performing geoprocessing in a managed
way. A managed way means there is a native .NET assembly (the Geoprocessor assembly) that is
a wrapper for some types in the Geoprocessing library of ArcObjects, and there are other .NET
assemblies for each system toolbox.
These native .NET assemblies provide an even easier way to run a system tool. In general, the
procedure for running a tool using the Geoprocessor-managed assembly is the same as running a tool