Newer
Older
EMS_REFACTOR / ClassSearchAnalysis.cs
nn-203 on 26 Jul 2017 17 KB first commit
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using GeoScene.Data;
using GeoScene.Engine;
using GeoScene.Globe;

namespace Cyberpipe
{
    class ClassSearchAnalysis
    {
        /// <summary>
        /// SQL语句查询
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="m_globeControl"></param>
        /// <param name="layerName"></param>
        /// <param name="filedObject"></param>
        /// <param name="filedValue"></param>
        /// <returns></returns>
        public static void ResultDataTable(out DataTable dt,GSOGlobeControl m_globeControl,string layerName,
            string filedObject,string filedValue)
        {
            string sql = "";
            if (filedObject == "")
            {
                sql = "select " + GetpipeLineFields.GetFields(layerName, m_globeControl)
                        + " from " + layerName;
            }
            else
            {
                sql = "select " + GetpipeLineFields.GetFields(layerName, m_globeControl)
                        + " from " + layerName + " where " + filedObject + " like '%"
                        + filedValue + "%'";
            }
            dt = OledbHelper.QueryTable(sql);
            //return dt;
        }
        /// <summary>
        /// SQL语句查询
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="m_globeControl"></param>
        /// <param name="layerName"></param>
        /// <param name="filedObject"></param>
        /// <param name="mathT"></param>
        /// <param name="leftFiledValue"></param>
        /// <param name="rightFiledValue"></param>
        /// <returns></returns>
        public static void ResultDataTable(out DataTable dt, GSOGlobeControl m_globeControl, string layerName,
            string filedObject,string mathT, string leftFiledValue, string rightFiledValue)
        {
            string sql = "";
            if (mathT == "区间")
            {
                sql = "select " + GetpipeLineFields.GetFields(layerName, m_globeControl)
                + " from " + layerName + " where " + filedObject + ">" + leftFiledValue
                + "and "+filedObject+"<" + rightFiledValue;
            }
            else
            {
                sql = "select " + GetpipeLineFields.GetFields(layerName, m_globeControl)
                + " from " + layerName + " where " + filedObject + mathT + rightFiledValue;
            }
            dt = OledbHelper.QueryTable(sql);
            //return dt;
        }
        /// <summary>
        /// SQL查询不重复的值
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="layerName"></param>
        /// <param name="filedObject"></param>
        /// <returns></returns>
        public static void ResultDistinctDataTable(out DataTable dt,string layerName,string filedObject)
        {
            string sql = "select distinct " + filedObject + " from " + layerName + " order by " + filedObject;
            dt = OledbHelper.QueryTable(sql);
            //return dt;
        }
        /// <summary>
        /// SQL查询,返回包含NULL的Table
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="m_globeControl"></param>
        /// <param name="layerName"></param>
        /// <param name="filedObject"></param>
        /// <param name="fileValueList"></param>
        /// <returns></returns>
        public static void ResultDataTableContainNull(out DataTable dt,GSOGlobeControl m_globeControl, 
            string layerName,string filedObject,List<string> filedValueList)
        {
            string sql = "select " + GetpipeLineFields.GetFields(layerName, m_globeControl)
                    + " from " + layerName + " where " + filedObject + " = '";

            for (int i = 0; i < filedValueList.Count; i++)
            {
                if (i == 0)
                {
                    if (filedValueList[i] == "无")
                    {
                        sql = sql.Replace("= '", "is null");
                    }
                    else
                    {
                        sql += filedValueList[i] + "' ";
                    }
                }
                else
                {
                    if (filedValueList[i] == "无")
                    {
                        sql += " or "+filedObject+" is null";
                    }
                    else
                    {
                        sql += " or " + filedObject + " = '" + filedValueList[i] + "'";
                    }
                }
            }

            dt = OledbHelper.QueryTable(sql);
            //return dt;
        }
        /// <summary>
        /// SQL查询,参数待有where语句
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="m_globeControl"></param>
        /// <param name="layerName"></param>
        /// <param name="whereSql"></param>
        /// <returns></returns>
        public static void ResultDataTable(out DataTable dt,GSOGlobeControl m_globeControl, string layerName, string whereSql)
        {
            string sql = "select " + GetpipeLineFields.GetFields(layerName, m_globeControl)
                    + " from " + layerName + " where " + whereSql;

            dt = OledbHelper.QueryTable(sql);
            //return dt;
        
        }

        /// <summary>
        /// 关联查询
        /// </summary>
        /// <param name="m_globeControl"></param>
        /// <param name="feature"></param>
        /// <param name="layer"></param>
        /// <param name="valueLayerNames"></param>
        /// <param name="workwellLayerNames"></param>
        /// <param name="instrumenLayerNames"></param>
        /// <param name="pipefittingLayerNames"></param>
        /// <param name="valueAllowance"></param>
        public static void ResultRelationAnalysis(GSOGlobeControl m_globeControl,GSOFeature feature,
            List<string> valueLayerNames,List<string> workwellLayerNames,List<string> instrumenLayerNames,
            List<string> pipefittingLayerNames,List<string> m_PipelineLayerNames,double valueAllowance)
        {
            List<string> pointLayerNames = new List<string>();
            for (int i = 0; i < valueLayerNames.Count; i++)
            {
                if (pointLayerNames.Contains(valueLayerNames[i]) == false)
                {
                    pointLayerNames.Add(valueLayerNames[i]);
                }
            }
            for (int i = 0; i < workwellLayerNames.Count; i++)
            {
                if (pointLayerNames.Contains(workwellLayerNames[i]) == false)
                {
                    pointLayerNames.Add(workwellLayerNames[i]);
                }
            }
            for (int i = 0; i < instrumenLayerNames.Count; i++)
            {
                if (pointLayerNames.Contains(instrumenLayerNames[i]) == false)
                {
                    pointLayerNames.Add(instrumenLayerNames[i]);
                }
            }
            for (int i = 0; i < pipefittingLayerNames.Count; i++)
            {
                if (pointLayerNames.Contains(pipefittingLayerNames[i]) == false)
                {
                    pointLayerNames.Add(pipefittingLayerNames[i]);
                }
            }
            if (feature.Geometry.Type == EnumGeometryType.GeoPolyline3D)
            {
                LineRelation(m_globeControl, feature, pointLayerNames, valueAllowance);
            }
            else
            {
                PointRelation(m_globeControl, feature, m_PipelineLayerNames, valueAllowance);
            }
        }
        /// <summary>
        /// 线关联
        /// </summary>
        /// <param name="m_globeControl"></param>
        /// <param name="feature"></param>
        /// <param name="valueAllowance"></param>
        /// <param name="pointLayerNames"></param>
        private static void LineRelation(GSOGlobeControl m_globeControl,GSOFeature feature,List<string> pointLayerNames,
            double valueAllowance)
        {
            GSOFeatures featuresAllResult = new GSOFeatures();
            GSOGeoPolyline3D line = feature.Geometry as GSOGeoPolyline3D;
            double lineLenght = line.GetSpaceLength(false, 6378137.0);

            //头
            GSOPoint3d headPoint = line[0][0];
            GSOGeoPolyline3D newline = new GSOGeoPolyline3D();
            GSOPoint3ds part = new GSOPoint3ds();
            part.Add(headPoint);
            headPoint.X = headPoint.X - 0.0000000005;//?
            part.Add(headPoint);
            newline.AddPart(part);
            GSOGeoPolygon3D buffer = newline.CreateBuffer(valueAllowance, true, 5, true, false);

            for (int i = 0; i < pointLayerNames.Count; i++)
            {
                GSOLayer pointLayer = m_globeControl.Globe.Layers.GetLayerByCaption(pointLayerNames[i]);
                if (pointLayer == null)
                {
                    continue;
                }
                GSOFeatures features = pointLayer.FindFeaturesInPolygon(buffer, false);
                if (features != null && features.Length > 0)
                {
                    for (int m = 0; m < features.Length; m++)
                    {
                        featuresAllResult.Add(features[m]);
                    }
                }
            }

            //尾
            GSOPoint3d tailPoint = line[line.PartCount - 1][line[line.PartCount - 1].Count - 1];
            newline = new GSOGeoPolyline3D();
            part = new GSOPoint3ds();
            part.Add(tailPoint);
            tailPoint.X = tailPoint.X - 0.0000000005;
            part.Add(tailPoint);
            newline.AddPart(part);
            buffer = newline.CreateBuffer(valueAllowance, true, 5, true, false);
            for (int i = 0; i < pointLayerNames.Count; i++)
            {
                GSOLayer pointLayer = m_globeControl.Globe.Layers.GetLayerByCaption(pointLayerNames[i]);
                if (pointLayer == null)
                {
                    continue;
                }
                GSOFeatures features = pointLayer.FindFeaturesInPolygon(buffer, false);
                if (features != null && features.Length > 0)
                {
                    for (int m = 0; m < features.Length; m++)
                    {
                        featuresAllResult.Add(features[m]);
                    }
                }
            }

            //所有结果高亮
            for (int i = 0; i < featuresAllResult.Length; i++)
            {
                GSOFeature featureResult = featuresAllResult[i];
                if (featureResult != null)
                {
                    featureResult.HighLight = true;
                }
            }
        }
        /// <summary>
        /// 点关联
        /// </summary>
        /// <param name="m_globeControl"></param>
        /// <param name="feature"></param>
        /// <param name="m_PipelineLayerNames"></param>
        /// <param name="valueAllowance"></param>
        private static void PointRelation(GSOGlobeControl m_globeControl, GSOFeature feature, 
            List<string> m_PipelineLayerNames, double valueAllowance)
        {
            GSOPoint3d point = feature.Geometry.GeoCenterPoint;
            if (point != null)
            {
                GSOGeoPolyline3D line = new GSOGeoPolyline3D();
                GSOPoint3ds part = new GSOPoint3ds();
                part.Add(point);
                point.X = point.X - 0.0000000005;
                part.Add(point);
                line.AddPart(part);
                GSOGeoPolygon3D buffer = line.CreateBuffer(valueAllowance, true, 5, true, false);

                for (int i = 0; i < m_PipelineLayerNames.Count; i++)
                {
                    GSOLayer lineLayer = m_globeControl.Globe.Layers.GetLayerByCaption(m_PipelineLayerNames[i]);
                    if (lineLayer == null)
                    {
                        continue;
                    }
                    GSOFeatures features = lineLayer.FindFeaturesInPolygon(buffer, false);
                    if (features != null && features.Length > 0)
                    {
                        for (int m = 0; m < features.Length; m++)
                        {
                            GSOFeature featureResult = features[m];
                            if (featureResult != null)
                            {
                                featureResult.HighLight = true;
                            }
                        }
                    }
                }
            }
        }
        
        /// <summary>
        /// 地球场景中添加管线标注
        /// </summary>
        /// <param name="glb"></param>
        /// <param name="feature"></param>
        public static void AddMakerToLineFeature(GSOGlobeControl glb, GSOFeature feature)
        {
            glb.Globe.MemoryLayer.RemoveAllFeature();
            if (feature == null) return;

            GSOFeature radiusMarkerFeature=new GSOFeature();
            GSOGeoPoint3D pt = new GSOGeoPoint3D();
            GSOPoint3d point3d = new GSOPoint3d();

            if (feature.Geometry.Type == EnumGeometryType.GeoModel)
            {
                GSOGeoModel model = feature.Geometry as GSOGeoModel;
                pt.Position = model.Position;
                point3d.X = pt.X;
                point3d.Y = pt.Y;
                point3d.Z = pt.Z;
            }
            else
            {
                GSOGeoPolyline3D selLine = feature.Geometry as GSOGeoPolyline3D;
                if (selLine[0].Count > 1)
                {
                    point3d = selLine.GeoCenterPoint;
                    pt.X = point3d.X;
                    pt.Y = point3d.Y;
                    pt.Z = point3d.Z;
                }
            }
            glb.Globe.JumpToPosition(point3d, EnumAltitudeMode.Absolute, 50);
            createLabel(out radiusMarkerFeature, pt, feature.Name);
            radiusMarkerFeature.Visible = true;
            glb.Globe.MemoryLayer.AddFeature(radiusMarkerFeature);

            glb.Refresh();
        }
        /// <summary>
        /// 创建LABEL
        /// </summary>
        /// <param name="feature"></param>
        /// <param name="point"></param>
        /// <param name="labelName"></param>
        /// <returns></returns>
        private static GSOFeature createLabel(out GSOFeature feature, GSOGeoPoint3D point, string labelName)
        {
            feature = new GSOFeature();

            point.AltitudeMode = EnumAltitudeMode.RelativeToGround;
            feature.Geometry = point;
            GSOLabel newLabel = new GSOLabel();
            newLabel.Text = labelName;
            newLabel.Style = new GSOLabelStyle();
            newLabel.Style.Opaque = 0.8;
            newLabel.Style.OutlineColor = Color.Gray;
            newLabel.Style.OutlineWidth = 1;
            newLabel.Style.TracktionLineWidth = 3;
            newLabel.Style.TractionLineColor = Color.Green;

            Color color1 = Color.FromArgb(60, 187, 206, 230);
            Color color2 = Color.FromArgb(150, 187, 200, 250);
            newLabel.Style.BackBeginColor = color1;
            newLabel.Style.BackEndColor = color2;

            feature.Label = newLabel;
            return feature;
        }

        #region wang
        /// <summary>
        /// 
        /// </summary>
        /// <param name="features"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public static DataTable ConvertGsoFeatures2DataTable(GSOFeatures features, string[] field)
        {
            if (features == null || features.Length == 0 || field.Length == 0) return null;
            DataTable result = new DataTable();
            for (int j = 0; j < field.Length; j++)
            {
                result.Columns.Add(field[j], typeof(String));
            }
            for (int i = 0; i < features.Length; i++)
            {
                GSOFeature feature = features[i];
                string[] values = new string[field.Length];
                for (int j = 0; j < field.Length; j++)
                {
                    string value = feature.GetFieldAsString(field[j]);
                    values[j] = value;
                }
                result.Rows.Add(values);
            }
            return result;
        }

        public static string[] GetFields(string caption, GSOGlobeControl mGlobeControl)
        {
            GSOLayer mLayer = mGlobeControl.Globe.Layers.GetLayerByCaption(caption);//获取当前选择的layer图层
            if (mLayer == null) return null;
            var ds = mLayer.Dataset;
            var sourcefDataset = ds as GSOFeatureDataset;
            if (sourcefDataset != null)
            {
                sourcefDataset.Open();
                string fields = "";
                for (int j = 0; j < sourcefDataset.FieldCount; j++)
                {
                    GSOFieldAttr fieldef = sourcefDataset.GetField(j);
                    string fieldName = fieldef.Name;
                    if (j < sourcefDataset.FieldCount - 1)
                    {
                        fields += fieldName + ",";
                    }
                    else
                        fields += fieldName;
                }
                return fields.Trim().Split(',');
            }
            return null;
        }
        #endregion

    }
}