1. 程式人生 > >ArcGIS Pro-使用Core程式集開發獨立的App

ArcGIS Pro-使用Core程式集開發獨立的App

介紹

有時候我們並不需要把功能定製到ArcGIS Pro框架上,只需要寫個小的工具處理下資料,處理資料時只需雙擊執行exe就行,簡單方便,那麼我們可以使用arcgis pro的資料庫和圖形操作介面嗎?

ArcGIS.Core.dll提供地理資料庫和圖形操作的介面,在呼叫了Host.Initialize方法後此程式集能在獨立的console或WPF應用程式中使用。

前提條件

1、安裝了ArcGIS Pro

2、配置了授權

Host.Initialize檢查項

  • 程式是64位的嗎?
  • 程序COM執行緒模型是單執行緒公寓(STA)嗎?
  • ArcGIS Pro安裝了嗎?
  • ArcGIS Pro授權可以初始化嗎?

獨立模式(非Pro Addin模式下)使用編輯

在獨立模式下編輯資料時,需要使用ArcGIS.Core.Data.Geodatabase.ApplyEdits方法。

using (RowCursor rowCursor = featureClass.Search(new QueryFilter { WhereClause = "NAME = 'Kern'" }, false))
{
  if (!rowCursor.MoveNext())
    return;

  geodatabase.ApplyEdits(() =>
  {
    using (Row row = rowCursor.Current)
    {
      row["NAME"] = "Kern-Another";
      row.Store();
    }

    using (RowBuffer rowBuffer = featureClass.CreateRowBuffer())
    {
      rowBuffer[FileGDBPathsAndNames.CountyNameColumn] = "Santa Ana";
      rowBuffer[FileGDBPathsAndNames.StateNameColumn]  = "California";

      using (Feature firstFeature = featureClass.CreateRow(rowBuffer))
      {
      }
    }

    using (RowBuffer rowBuffer = featureClass.CreateRowBuffer())
    {
      rowBuffer[FileGDBPathsAndNames.CountyNameColumn] = "Irvine";
      rowBuffer[FileGDBPathsAndNames.StateNameColumn]  = "California";

      using (Feature secondFeature = featureClass.CreateRow(rowBuffer))
      {
      }
    }
  });
}

示例:WPF應用程式

程式碼結構

執行結果

原始碼

1、App.xaml.cs檔案中呼叫Host.Initialize方法

  public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                Host.Initialize();
                MainWindow frmMain = new ProCoreHostDemo.MainWindow();
                MainVM sourceVM = new ProCoreHostDemo.MainVM();
                sourceVM.LoadData();
                frmMain.DataContext = sourceVM;
                frmMain.ShowDialog();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Initialization failed: {0}", ex.Message));
            }
        }
    }

2、Xaml介面程式碼

<Window x:Class="ProCoreHostDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ProCoreHostDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView  ItemsSource="{Binding ListTableInfo}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="表名" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
                    <GridViewColumn Header="別名" DisplayMemberBinding="{Binding AliasName}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

3、資料來源

namespace ProCoreHostDemo
{
    public class MainVM
    {
        private string m_GeoDasePath = @"XXXX.gdb";

        private List<TableInfo> m_ListTableInfo;

        public List<TableInfo> ListTableInfo
        {
            get
            {
                return m_ListTableInfo;
            }
        }

        /// <summary>
        /// 資料載入
        /// </summary>
        /// <returns></returns>
        public bool LoadData()
        {
            try
            {
                Geodatabase gdb = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(m_GeoDasePath)));
                IReadOnlyList<TableDefinition> definitions = gdb.GetDefinitions<FeatureClassDefinition>();
                m_ListTableInfo = definitions.Select((item) =>
                 {
                     return new TableInfo()
                     {
                         Name = item.GetName(),
                         AliasName = item.GetAliasName()
                     };
                 }).ToList();
                
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }

    public class TableInfo
    {
        public string Name { get; set; }

        public string AliasName { get; set; }
    }
}