Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Created April 23, 2017 20:21
Show Gist options
  • Save AlexArcPy/a5c9dec7c138682a09f0d988ad4c9697 to your computer and use it in GitHub Desktop.
Save AlexArcPy/a5c9dec7c138682a09f0d988ad4c9697 to your computer and use it in GitHub Desktop.
Calling ArcGIS Pro class library from Python
'''call ArcGIS Pro class library to get the ObjectID field name of a given shapefile'''
import clr
import sys
import pythoncom
pythoncom.CoInitialize()
path = r'C:\Projects\SimplePro\SimplePro\bin\x64\Debug'
sys.path.append(path)
clr.AddReference('SimplePro')
from SimplePro import DataManagement
c = DataManagement()
res = c.GetObjectIDField(r"C:\GIS\Temp\arcgisprodata", "Shopping")
print res
using System;
using ArcGIS.Core.Hosting;
using ArcGIS.Core.Data;
namespace SimplePro
{
public class DataManagement
{
public static string GetObjectIDField(string path, string shapefile)
{
//Call Host.Initialize before constructing any objects from ArcGIS.Core
try
{
Host.Initialize();
Console.WriteLine("ArcGIS License initialized");
System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(path);
Shapefile shp = new Shapefile(folder);
TableDefinition easementsDefinition = shp.GetDefinition(shapefile);
return easementsDefinition.GetObjectIDField();
}
catch (Exception e)
{
// Error (missing installation, no license, 64 bit mismatch, etc.)
Console.WriteLine(string.Format(e.Message));
return "error";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment