. Net in ArcGIS calls Python

Source: Internet
Author: User
About arcpyArcpy is a site package that comes with any ArcGIS product. the functions are built upon the fine-grain ArcObjects to simplify your geoprocessing tasks. its goal is to create the cornerstone for a useful and productive way to perform geographic data analysis, data conversion, data management, and map automation with python. this package provides a rich and native Python experience. arcGIS applications written with arcpy benefit from the development of additional modules in numerous niches of python by geographic information system (GIS) sionsionals and programmers from your different disciplines. arcpy is well-formed ented in the ArcGIS Desktop user help system. to get started, see a quick tour of arcpy. arcpy provides access to geoprocessing tools as well as additional functions, classes, and modules that allow you to create simple or complex workflows quickly and easily. broadly, arcpy is organized in tools, functions, classes, and modules. technically speaking, geoprocessing tools are functions available from arcpy. there are also functions non-tool functions, such as utility functions like listfeatureclasses (). although some of these functions are available in. net via the geoprocessor or the igputilities object, except other functions are not. for example, in. net you need fine-grain ArcObjects to use cursors, whereas arcpy has functions that are very easy to cursor through data. when you use arcpy, you do not have to worry about navigating through many ArcObjects interfaces. for more information, see alphabetical list of arcpy functions in the ArcGIS Desktop user help system. when possible, developers shoshould take advantage of the power of the arcpy module, then if necessary, the Python code can be integrated. NET applications. Leveraging arcpy in. netThe following are the ways you can leverage arcpy:
  • You can call any Python script (*. PY) from. net. this includes des code that uses Python packages designed for analytical and mathematical purposes, such as R statistics, complex matrix algebra, scipy, or any other script.
  • Similarly, you can use simplified arcpy functions as well. The following are several modules that you can take advantage:
    • Mapping module-For more information see, geoprocessing scripts for map document management and output in the ArcGIS Desktop user help system.
    • Geostatistical analyst module-For more information, see a quick tour of the geostatistical analyst arcpy classes in the ArcGIS Desktop user help system.
    • Spatial Analyst module-For more information, see an overview of spatial analyst classes in the ArcGIS Desktop user help system.
    There are using classes in an arcpy package that you can integrate in your code.
  • You can use the. Net geoprocessor to execute a script tool that is implemented with python. This is the way gis analysts use it.
Using Geometry as tool InputYou can use the geometry class to perform specific operations using arcpy without creating on-disk feature classes. for example, in. net you cannot use a geometry object, such as a polygon as tool input. in our geoprocessing workflows, you might need to run a specific operation using coordinate and geometry information but do not necessarily want to go through the process of creating a (temporary) feature class, populating the feature class with cursors, using the feature class, then deleting the temporary feature class. geometry objects can be used instead for both input and output to make geoprocessing simpler. geometry objects can be created from scratch using geometry, multipoint, pointgeometry, polygon, or polyline classes. the following Python code shows how you can use geometry as tool input. it creates a polygon geometry object that was created using a list of X, Y coordinates. the clip tool is then used to clip a feature class with the polygon geometry object. import arcpy # create an array object.
#
Array = arcpy. Array () # list of coordinates.
#
Coordlist = ['1. 0; 1.0 ', '1. 0; 10.0 ', '10. 0; 10.0 ', '10. 0; 1.0 '] # For each coordinate set, create a point object and add the X-and
# Y-coordinates to the point object, then add the Point Object
# To the array object.
#
For coordpair in coordlist:
X, Y = coordpair. Split (";")
PNT = arcpy. Point (x, y)
Array. Add (PNT) # Add in the first point of the array again to close the polygon boundary.
#
Array. Add (array. GetObject (0) # create a polygon geometry object using the array object.
#
Boundarypolygon = arcpy. polygon (array) # Use the geometry to clip an input feature class.
#
Arcpy. clip_analysis ("C:/data/rivers. SHP ", boundarypolygon," C:/data/rivers_clipped.shp ") the previous script is saved to disk as a Python script (*. py file ). you can then call the Python script from. NET application. assuming the script is saved as geom_input.py in the C: \ scripts folder, the following. net code example shows how you can run the script. the Code invokes the CMD process specifying the command to be executed. the command parameter of the executecommand function is a string that you pass to cmd.

[C #]

// Executes a shell command synchronously.// Example of command parameter value is// "python " + @"C:\scripts\geom_input.py".//public static void ExecuteCommand(object command){    try    {        // Create the ProcessStartInfo using "cmd" as the program to be run,        // and "/c " as the parameters.        // "/c" tells cmd that you want it to execute the command that follows,        // then exit.        System.Diagnostics.ProcessStartInfo procStartInfo = new            System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);        // The following commands are needed to redirect the standard output.        // This means that it will be redirected to the Process.StandardOutput StreamReader.        procStartInfo.RedirectStandardOutput = true;        procStartInfo.UseShellExecute = false;        // Do not create the black window.        procStartInfo.CreateNoWindow = true;        // Now you create a process, assign its ProcessStartInfo, and start it.        System.Diagnostics.Process proc = new System.Diagnostics.Process();        proc.StartInfo = procStartInfo;        proc.Start();        // Get the output into a string.        string result = proc.StandardOutput.ReadToEnd();        // Display the command output.        Console.WriteLine(result);    }    catch (Exception objException)    {        Console.WriteLine(objException.Message);        // Log the exception and errors.    }}

[VB. NET]

' Executes a shell command synchronously.' Example of command parameter value is' "python " + "C:\scripts\geom_input.py".'Sub ExecuteCommandSync(ByVal Command As Object)        Try        ' Create the ProcessStartInfo using "cmd" as the program to be run,    ' and "/c " as the parameters.    ' "/c" tells cmd that you want it to execute the command that follows,    ' then exit.    Dim procStartInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("cmd", "/c " + Command)        ' The following commands are needed to redirect the standard output.    ' This means that it will be redirected to the Process.StandardOutput StreamReader.    procStartInfo.RedirectStandardOutput = True    procStartInfo.UseShellExecute = False    procStartInfo.CreateNoWindow = True        ' Now you create a process, assign its ProcessStartInfo, and start it.    Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()    proc.StartInfo = procStartInfo    proc.Start()        ' Get the output into a string.    Dim result As String = proc.StandardOutput.ReadToEnd()        ' Display the command output.    Console.WriteLine(result)        Catch ex As Exception    Console.WriteLine(ex.Message)    ' Log the exception and errors.        End Try    End Sub

Adding the python application to the path variableIf you want to run a Python script from the command line in windows, specify the full path of the Python application or add Python's installation location to the path system environment variable. do the following to set Python to the path variable:
  1. Right-click my computer and click Properties. The System Properties dialog box appears.
  2. On the System Properties dialog box, click the Advanced tab, then click the environment variables button. The environment variables dialog box appears. See the following screen shot:

  3. On the environment variables dialog box, under the system variables section, double-click the PATH variable. the Edit System variable dialog box appears. type the path of the Python application. see the following screen shot:

    In this example, python is installed in the C: \ python26 directory location. if you accept all defaults while installing ArcGIS, your path can resemble c: \ python26 \ arcgisx (where X is the version number for ArcGIS ).

From: http://resources.arcgis.com/content/search-result? Searchkeyword = currentcontentsview & searchtaxonomy = Resource + centers % 2 fnet % 2 fdocumentation % 2 fdeveloper & searchproduct = ArcObjects + SDK + for + the + Microsoft +. net + Platform + 10.0 & searchrc = All & sourceurl = http % 3A % 2f % 2fhelp.arcgis.com % 2fen % 2 fsdk % 2f10. 0% 2farcobjects_net % 2 fcomponenthelp % 2f & sourceproductname =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.