Exemple #1
0
        /// <summary>
        /// 转换并纠偏,只使用与火星坐标纠偏
        /// </summary>
        /// <param name="dataTable">数据表,如果是点表中含X,Y字段,如果是线和面,表中含有PATH字段</param>
        /// <param name="filePath">存储路径</param>
        /// <param name="geoType">数据类型,暂时支持简单的点线面</param>
        //public static void saveShpFile(DataTable dataTable,string filePath,wkbGeometryType geoType)
        //{
        //    saveShpFile(dataTable, filePath, geoType, ProjectConvert.GCJ_WGS);
        //}
        /// <summary>
        /// 导出shp数据到表格
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>DataTable</returns>
        public static DataTable GetData(string path)
        {
            try
            {
                OSGeo.OGR.Ogr.RegisterAll();
                OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");

                if (dr == null)
                {
                    Console.WriteLine("启动驱动失败!\n");
                }

                OSGeo.OGR.DataSource ds    = dr.Open(path, 0);
                int             layerCount = ds.GetLayerCount();
                OSGeo.OGR.Layer layer      = ds.GetLayerByIndex(0);
                DataTable       dataTable  = new DataTable();
                FeatureDefn     fdf        = layer.GetLayerDefn();
                int             fieldCount = fdf.GetFieldCount();
                for (int i = 0; i < fieldCount; i++)
                {
                    dataTable.Columns.Add(fdf.GetFieldDefn(i).GetName(), Type.GetType("System.String"));
                }
                if (fdf.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbPoint)
                {
                    if (!dataTable.Columns.Contains("X"))
                    {
                        dataTable.Columns.Add("X", Type.GetType("System.String"));
                    }
                    if (!dataTable.Columns.Contains("Y"))
                    {
                        dataTable.Columns.Add("Y", Type.GetType("System.String"));
                    }
                }
                else if (fdf.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbPolygon || fdf.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbLineString)
                {
                    if (!dataTable.Columns.Contains("PATH"))
                    {
                        dataTable.Columns.Add("PATH", Type.GetType("System.String"));
                    }
                }
                OSGeo.OGR.Feature feat;
                while ((feat = layer.GetNextFeature()) != null)
                {
                    DataRow            row      = dataTable.NewRow();
                    OSGeo.OGR.Geometry geometry = feat.GetGeometryRef();
                    for (int i = 0; i < fieldCount; i++)
                    {
                        string value = feat.GetFieldAsString(i);
                        row[i] = value.Replace(",", "");
                    }
                    OSGeo.OGR.wkbGeometryType goetype = geometry.GetGeometryType();
                    if (goetype == OSGeo.OGR.wkbGeometryType.wkbPoint)
                    {
                        double x = geometry.GetX(0);
                        double y = geometry.GetY(0);
                        row["X"] = x.ToString();
                        row["Y"] = y.ToString();
                    }
                    else if (goetype == OSGeo.OGR.wkbGeometryType.wkbLineString)
                    {
                        string points = "";
                        for (int i = 0; i < geometry.GetPointCount(); i++)
                        {
                            double x = geometry.GetX(i);
                            double y = geometry.GetY(i);
                            points += x.ToString() + "|" + y.ToString() + ";";
                        }
                        if (points != "")
                        {
                            points.Substring(0, points.Length - 1);
                        }
                        row["PATH"] = points;
                    }
                    else if (goetype == OSGeo.OGR.wkbGeometryType.wkbPolygon)
                    {
                        int    count  = geometry.GetGeometryCount();
                        string points = "";
                        for (int j = 0; j < count; j++)
                        {
                            Geometry geo = geometry.GetGeometryRef(j);
                            for (int i = 0; i < geo.GetPointCount(); i++)
                            {
                                double x = geo.GetX(i);
                                double y = geo.GetY(i);
                                points += x.ToString() + "|" + y.ToString() + ";";
                            }
                            if (points != "")
                            {
                                points.Substring(0, points.Length - 1);
                            }
                            points += "|";
                        }
                        if (points != "")
                        {
                            points.Substring(0, points.Length - 1);
                        }
                        row["PATH"] = points;
                    }
                }
                layer.Dispose();
                ds.Dispose();
                dr.Dispose();
                return(dataTable);
            }
            catch
            {
                return(null);
            }
        }
Exemple #2
0
        public FeatureType GetFeatureType()
        {
            wkbGeometryType geomType = _ogrFeatureDefinition.GetGeomType();

            return(TranslateOgrGeometryType(geomType));
        }
Exemple #3
0
        ControlZone _zoneB = new ControlZone(); //管控区B

        public bool ConflictAnalysis()
        {
            IFeatureSet resultSet = null;

            try
            {
                #region 相交分析

                OSGeo.OGR.Layer layerA = null;
                if (_zoneA.FeatureSet != null)
                {
                    layerA = GIS.GDAL.VectorConverter.DS2OrgLayer(_zoneA.FeatureSet);
                }
                else
                {
                    layerA = GIS.GDAL.VectorConverter.GetOgrLayer(_zoneA.Address);
                }

                OSGeo.OGR.Layer layerB = null;
                if (_zoneB.FeatureSet != null)
                {
                    layerB = GIS.GDAL.VectorConverter.DS2OrgLayer(_zoneB.FeatureSet);
                }
                else
                {
                    layerB = GIS.GDAL.VectorConverter.GetOgrLayer(_zoneB.Address);
                }

                OSGeo.OGR.Layer resultLayer = null;
                using (OSGeo.OGR.Driver driver = Ogr.GetDriverByName("ESRI Shapefile"))
                {
                    if (driver == null)
                    {
                        System.Environment.Exit(-1);
                    }
                    string[] resultPath = _address.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                    string   resultName = resultPath[resultPath.Length - 1];
                    using (var dsResult = driver.CreateDataSource(_address, new string[] { }))
                    {
                        if (dsResult == null)
                        {
                            throw new Exception("Can't get to the datasoure.");
                        }

                        for (int i = 0; i < dsResult.GetLayerCount(); i++)
                        {
                            resultLayer = dsResult.GetLayerByIndex(i);
                            if (resultLayer != null && resultLayer.GetLayerDefn().GetName() == resultName)
                            {
                                dsResult.DeleteLayer(i);
                                break;
                            }
                        }
                        SpatialReference reference  = layerA.GetSpatialRef();
                        FeatureDefn      definition = layerA.GetLayerDefn();
                        wkbGeometryType  type       = definition.GetGeomType();
                        resultLayer = dsResult.CreateLayer("ResultLayer", layerA.GetSpatialRef(), layerA.GetLayerDefn().GetGeomType(), new string[] { });

                        bool intersectSuccess = GIS.GDAL.Overlay.Overlay.OverlayOperate(layerA, layerB, ref resultLayer, OverlayType.Intersects, null);

                        if (!intersectSuccess)
                        {
                            return(false);
                        }
                    }
                }

                #endregion

                resultSet = DotSpatial.Data.DataManager.DefaultDataManager.OpenFile(_address) as IFeatureSet;

                #region 添加转换要素的字段

                DataTable resultTable = resultSet.DataTable;

                DataColumn conflictColumn = new DataColumn();
                conflictColumn.DataType   = typeof(string);
                conflictColumn.ColumnName = "冲突类型";
                conflictColumn.MaxLength  = 100;
                resultTable.Columns.Add(conflictColumn);

                DataColumn conflictColumn2 = new DataColumn();
                conflictColumn2.DataType   = typeof(string);
                conflictColumn2.ColumnName = "处理意见";
                conflictColumn2.MaxLength  = 100;
                resultTable.Columns.Add(conflictColumn2);

                DataColumn conflictColumn3 = new DataColumn();
                conflictColumn3.DataType   = typeof(string);
                conflictColumn3.ColumnName = "备注";
                conflictColumn3.MaxLength  = 50;
                resultTable.Columns.Add(conflictColumn3);

                DataColumn conflictColumn4 = new DataColumn();
                conflictColumn4.DataType   = typeof(string);
                conflictColumn4.ColumnName = "用地类型";
                conflictColumn4.MaxLength  = 50;
                resultTable.Columns.Add(conflictColumn4);

                #endregion

                int index = resultTable.Columns.IndexOf("冲突类型");
                for (int i = 0; i < resultTable.Rows.Count; i++)
                {
                    DataRow dataRow = resultTable.Rows[i];
                    dataRow[index] = _conflictType;
                }

                resultSet.Save();
                if (resultSet.Projection == null)
                {
                }
                GIS.FrameWork.Application.App.Map.Layers.Add(resultSet);

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }