1. 程式人生 > >C#中模型欄位取值的欄位計算

C#中模型欄位取值的欄位計算

定義一個資源模型,包含Shape、LEN_PIPE等屬性。需要我們在儲存資源至GIS資料圖層時,如已指定Shape,則LEN_PIPE自動計算(Shape是一條多段線,長度向下取整為LEN_PIPE的值),而當我們從GIS圖層中讀取資料時,如LEN_PIPE已有值,則取已有值,否則自動按Shape長度賦值。為此,模型內對LEN_PIPE的屬性設定及其get、set訪問器實現如下:

    /// <summary>光纜段模型
    /// 
    /// </summary>
    public class Cable : IGeographicalEntity
    {
        /// <summary>資源資料型別
        /// 
        /// </summary>
        public ResourceDataTypeEnum RESOURCETYPE => ResourceDataTypeEnum.CableLine;
		
        private int _shapeLength = 0;
        /// <summary>皮長
        ///
        /// </summary>
        public int LEN_PIPE
        {
            get
            {
                if (null != Shape && 0 >= _shapeLength)
                {
                    _shapeLength = GeometryFactory.CalculateLength(Shape);
                }
                return _shapeLength;
            }
            set => _shapeLength = value;
        }

        /// <summary>圖形
        /// 
        /// </summary>
        public GeoJSON.Net.Geometry.MultiLineString Shape { get; set; }
    }

其中,計算長度的方法如下:

        public static int CalculateLength(IGeometryObject pGeometryObject)
        {
            var nLength = 0;
            try
            {
                nLength = (int)((IPolyline)CreateEsriGeometryFromGeoJson(pGeometryObject)).Length;
            }
            catch
            {
                // ignored
            }
            return nLength;
        }