Example #1
0
        /// <summary>
        /// 通过ID数组提取出所有要素
        /// </summary>
        /// <param name="dzLine"></param>
        /// <param name="pdLing"></param>
        /// <param name="savePath"></param>
        public static void selectFeat(string dzLine, string pdLing, string savePath)
        {
            //获得数组
            int[] a = getMinIdGroup(dzLine, pdLing);

            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            //读取原Layer
            OSGeo.OGR.DataSource dzDS    = dr.Open(dzLine, 0);
            OSGeo.OGR.Layer      dzLayer = dzDS.GetLayerByIndex(0);
            //新建Layer
            if (System.IO.File.Exists(savePath))
            {
                System.IO.File.Delete(savePath);
            }
            OSGeo.OGR.DataSource ds        = dr.CreateDataSource(savePath, null);
            OSGeo.OGR.Layer      fileLayer = ds.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null);
            //get featuer by ID
            for (int i = 0; i < a.Length; i++)
            {
                OSGeo.OGR.Feature  dzFeat   = dzLayer.GetFeature(a[i]);
                OSGeo.OGR.Geometry dzGeom   = dzFeat.GetGeometryRef();
                OSGeo.OGR.Geometry fileGeom = dzGeom;
                OSGeo.OGR.Feature  fileFeat = new OSGeo.OGR.Feature(dzFeat.GetDefnRef());
                fileFeat.SetGeometry(fileGeom);
                fileLayer.CreateFeature(dzFeat);
            }
            fileLayer.Dispose();
            ds.Dispose();
            dzDS.Dispose();
        }
Example #2
0
 static OSGeo.OGR.Feature polyFeat(OSGeo.OGR.Geometry lineGeom)
 {
     OSGeo.OGR.Feature  polyFeat = new OSGeo.OGR.Feature(new OSGeo.OGR.FeatureDefn(""));
     OSGeo.OGR.Geometry polyGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
     OSGeo.OGR.Geometry subGeom  = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
     for (int s = 0; s < lineGeom.GetPointCount(); s++)
     {
         subGeom.AddPoint(lineGeom.GetX(s), lineGeom.GetY(s), lineGeom.GetZ(s));
     }
     polyGeom.AddGeometry(subGeom);
     polyFeat.SetGeometry(polyGeom);
     return(polyFeat);
 }
Example #3
0
        /// <summary>
        /// 根据dsm的坐标划分区域
        /// </summary>
        /// <param name="tileSize">区块尺寸</param>
        /// <returns></returns>
        static OSGeo.OGR.Layer createCutLayer(double tileSize)
        {
            shpDataSet.deleteLayerByName("cutLayer");
            OSGeo.OGR.Layer cutLayer = shpDataSet.CreateLayer("cutLayer", srs, OSGeo.OGR.wkbGeometryType.wkbPolygon, null);
            double[]        trans    = new double[6];
            dsmDataset.GetGeoTransform(trans);
            //四边坐标
            double top    = trans[3];
            double left   = trans[0];
            double right  = trans[0] + dsmDataset.RasterXSize * trans[1];
            double bottom = trans[3] + dsmDataset.RasterYSize * trans[5];
            //子块角点
            Point LB, LT, RT, RB;

            //横竖块数
            int hCount = (int)((top - bottom) / tileSize) + 1;
            int wCount = (int)((right - left) / tileSize) + 1;

            for (int h = 0; h < hCount; h++)
            {
                for (int w = 0; w < wCount; w++)
                {
                    OSGeo.OGR.Geometry subCutGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);

                    LB.X = left + tileSize * w;
                    LB.Y = bottom + tileSize * h;
                    subCutGeom.AddPoint(LB.X, LB.Y, 0);

                    LT.X = LB.X;
                    LT.Y = LB.Y + tileSize;
                    subCutGeom.AddPoint(LT.X, LT.Y, 0);

                    RT.X = LB.X + tileSize;
                    RT.Y = LT.Y;
                    subCutGeom.AddPoint(RT.X, RT.Y, 0);

                    RB.X = RT.X;
                    RB.Y = LB.Y;
                    subCutGeom.AddPoint(RB.X, RB.Y, 0);
                    //封闭poly
                    subCutGeom.AddPoint(LB.X, LB.Y, 0);

                    OSGeo.OGR.Geometry cutGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
                    cutGeom.AddGeometry(subCutGeom);
                    OSGeo.OGR.Feature cutFeat = new OSGeo.OGR.Feature(new OSGeo.OGR.FeatureDefn(""));
                    cutFeat.SetGeometry(cutGeom);
                    cutLayer.CreateFeature(cutFeat);
                }
            }
            return(cutLayer);
        }
Example #4
0
        private static void TKDataF(OSGeo.OGR.Layer slopePolyLayer, int index)
        {
            double[] geoTansform = new double[6];
            dsmDataset.GetGeoTransform(geoTansform);

            int xTimes = (dsm_Xsize - 300) / 500;
            int xRemainder = (dsm_Xsize - 300) % 500;
            int yTimes = (dsm_Ysize - 300) / 500;
            int yRemainder = (dsm_Ysize - 300) % 500;
            int ax = xTimes, by = yTimes;

            if (xRemainder > 10)
            {
                ax++;
            }
            int x = index % ax;
            int y = index / ax;
            int xsize = 800, ysize = 800;

            if (x * 500 + 800 > dsm_Xsize)
            {
                xsize = dsm_Xsize - x * 500;
            }
            if (y * 500 + 800 > dsm_Ysize)
            {
                ysize = dsm_Ysize - y * 500;
            }

            double[] buffer      = new double[xsize * ysize];
            double[] slopebuffer = new double[xsize * ysize];
            dsmDataset.GetRasterBand(1).ReadRaster(x * 500, y * 500, xsize, ysize, buffer, xsize, ysize, 0, 0);
            slopeDataSet.GetRasterBand(1).ReadRaster(x * 500, y * 500, xsize, ysize, slopebuffer, xsize, ysize, 0, 0);

            BasicUnit bUnit = new BasicUnit(x * 500, y * 500, xsize, ysize, ImprotLevel, buffer, slopebuffer, geoTansform, imgNodata);

            List <OSGeo.OGR.Geometry> geolist = bUnit.Identify2();

            lock (slopePolyLayer)
            {
                foreach (OSGeo.OGR.Geometry item in geolist)
                {
                    if (item.GetGeometryType() == OSGeo.OGR.wkbGeometryType.wkbPolygon25D ||
                        item.GetGeometryType() == OSGeo.OGR.wkbGeometryType.wkbPolygon)
                    {
                        OSGeo.OGR.Feature oFea = new OSGeo.OGR.Feature(slopePolyLayer.GetLayerDefn());
                        oFea.SetGeometry(item);
                        slopePolyLayer.CreateFeature(oFea);
                    }
                }
            }
        }
        /********************************   等值线转换为多边形   ***********************************************/

        /// <summary>
        /// 等值线转为POLYGON
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string dzPoly(string filePath)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");

            string a = StaticTools.tempFilePath("shp", "等值线POLY");

            OSGeo.OGR.DataSource newDS     = dr.CreateDataSource(a, null);
            OSGeo.OGR.Layer      polyLayer = newDS.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null);
            OSGeo.OGR.FieldDefn  fieldDf0  = new OSGeo.OGR.FieldDefn("LID", OSGeo.OGR.FieldType.OFTInteger);
            OSGeo.OGR.FieldDefn  fieldDf1  = new OSGeo.OGR.FieldDefn("EVE", OSGeo.OGR.FieldType.OFTReal);
            polyLayer.CreateField(fieldDf0, 1); //ID
            polyLayer.CreateField(fieldDf1, 1); //Value
            OSGeo.OGR.FeatureDefn featDF = new OSGeo.OGR.FeatureDefn("");
            Console.WriteLine("开始等值线转POLY!");
            OSGeo.OGR.DataSource cleanDS    = dr.Open(filePath, 0);
            OSGeo.OGR.Layer      cleanLayer = cleanDS.GetLayerByIndex(0);
            for (int i = 0; i < cleanLayer.GetFeatureCount(0); i++)
            {
                OSGeo.OGR.Feature  lineFeat = cleanLayer.GetFeature(i);
                OSGeo.OGR.Geometry lineGeom = lineFeat.GetGeometryRef();

                OSGeo.OGR.Feature  polyFeat = new OSGeo.OGR.Feature(featDF);
                OSGeo.OGR.Geometry polyGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
                OSGeo.OGR.Geometry subGeom  = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
                int u = lineGeom.GetPointCount();
                for (int s = 0; s < u; s++)
                {
                    double x = lineGeom.GetX(s);
                    double y = lineGeom.GetY(s);
                    double z = lineGeom.GetZ(s);
                    subGeom.AddPoint(x, y, z);
                }
                polyGeom.AddGeometry(subGeom);
                polyFeat.SetGeometry(polyGeom);
                polyLayer.CreateFeature(polyFeat);
                lineGeom.Dispose();
                polyGeom.Dispose();
                subGeom.Dispose();
                lineFeat.Dispose();
                polyFeat.Dispose();
            }
            cleanLayer.Dispose();
            polyLayer.Dispose();
            cleanDS.Dispose();
            newDS.Dispose();
            Console.WriteLine("等值线转POLY完成!");
            return(a);
        }
Example #6
0
        /// <summary>
        /// DotSpatial to Ogr at layer level
        /// </summary>
        /// <param name="featureSet">DotSpatial IFeatureSet</param>
        /// <returns>Ogr Layer</returns>
        public static OSGeo.OGR.Layer DS2OrgLayer(DotSpatial.Data.IFeatureSet featureSet)
        {
            using (OSGeo.OGR.Driver driver = OSGeo.OGR.Ogr.GetDriverByName("Memory"))
            {
                OSGeo.OGR.DataSource  dataSource  = driver.CreateDataSource(featureSet.Name, null);
                OSGeo.OGR.Layer       layer       = dataSource.CreateLayer("Result", DS2OgrProjection(featureSet.Projection), DS2OgrGeometryType(featureSet.FeatureType), new string[] { });
                OSGeo.OGR.FeatureDefn featureDefn = DS2OgrDataTable(featureSet.DataTable);

                for (int k = 0; k < featureDefn.GetFieldCount(); k++)
                {
                    layer.CreateField(featureDefn.GetFieldDefn(k), 0);
                }

                for (int i = 0; i < featureSet.NumRows(); i++)
                {
                    DotSpatial.Data.IFeature feature    = featureSet.GetFeature(i);
                    OSGeo.OGR.Feature        ogrFeature = new OSGeo.OGR.Feature(featureDefn);
                    ogrFeature.SetGeometry(DS2OgrGeometry(feature.Geometry, featureSet));
                    for (int j = 0; j < feature.DataRow.ItemArray.Length; j++)
                    {
                        #region Set Value to Feature

                        object value = feature.DataRow.ItemArray.GetValue(j);
                        if (value is int)
                        {
                            ogrFeature.SetField(j, (int)value);
                        }
                        else if (value is double)
                        {
                            ogrFeature.SetField(j, (double)value);
                        }
                        else if (value is string)
                        {
                            ogrFeature.SetField(j, (string)value);
                        }
                        else if (value is DateTime)
                        {
                            DateTime dateTime = (DateTime)value;
                            ogrFeature.SetField(j, dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, (float)dateTime.Second, 0);
                        }

                        #endregion
                    }
                    layer.CreateFeature(ogrFeature);
                }
                dataSource.FlushCache();
                return(layer);
            }
        }
Example #7
0
 /// <summary>
 /// LineD to shpLine
 /// </summary>
 /// <param name="savePath"></param>
 /// <param name="aLine"></param>
 public static void LineDtoshpLine(string savePath, LineD aLine)
 {
     OSGeo.OGR.Ogr.RegisterAll();
     OSGeo.OGR.Driver      dr        = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
     OSGeo.OGR.DataSource  ds        = dr.CreateDataSource(savePath, null);
     OSGeo.OGR.Layer       fileLayer = ds.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbLineString, null);
     OSGeo.OGR.FeatureDefn FeatDf    = new OSGeo.OGR.FeatureDefn("");
     OSGeo.OGR.Feature     fileFeat  = new OSGeo.OGR.Feature(FeatDf);
     OSGeo.OGR.Geometry    fileGeom  = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLineString);
     fileGeom.AddPoint(aLine.Start.X, aLine.Start.Y, aLine.Start.Z);
     fileGeom.AddPoint(aLine.End.X, aLine.End.Y, aLine.End.Z);
     fileFeat.SetGeometry(fileGeom);
     fileLayer.CreateFeature(fileFeat);
     fileLayer.Dispose();
     ds.Dispose();
 }
Example #8
0
        public static OSGeo.OGR.Layer DS2OrgLayer(DotSpatial.Data.IFeatureSet featureSet, OSGeo.OGR.Layer layer)
        {
            using (OSGeo.OGR.FeatureDefn featureDefn = DS2OgrDataTable(featureSet.DataTable))
            {
                for (int k = 0; k < featureDefn.GetFieldCount(); k++)
                {
                    layer.CreateField(featureDefn.GetFieldDefn(k), 0);
                }

                for (int i = 0; i < featureSet.NumRows(); i++)
                {
                    DotSpatial.Data.IFeature feature = featureSet.GetFeature(i);
                    using (OSGeo.OGR.Feature ogrFeature = new OSGeo.OGR.Feature(featureDefn))
                    {
                        ogrFeature.SetGeometry(DS2OgrGeometry(feature.Geometry, featureSet));
                        for (int j = 0; j < feature.DataRow.ItemArray.Length; j++)
                        {
                            #region Set Value to Feature

                            object value = feature.DataRow.ItemArray.GetValue(j);
                            if (value is int)
                            {
                                ogrFeature.SetField(j, (int)value);
                            }
                            else if (value is double)
                            {
                                ogrFeature.SetField(j, (double)value);
                            }
                            else if (value is string)
                            {
                                ogrFeature.SetField(j, (string)value);
                            }
                            else if (value is DateTime)
                            {
                                DateTime dateTime = (DateTime)value;
                                ogrFeature.SetField(j, dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, (float)dateTime.Second, 0);
                            }

                            #endregion
                        }
                        layer.CreateFeature(ogrFeature);
                    }
                }
            }
            return(layer);
        }
Example #9
0
 /// <summary>
 /// Point[] to shpPoint
 /// </summary>
 /// <param name="savePath"></param>
 /// <param name="allFeat"></param>
 public static void pointGToShpPoint(string savePath, Point[] allFeat)
 {
     OSGeo.OGR.Ogr.RegisterAll();
     OSGeo.OGR.Driver     dr        = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
     OSGeo.OGR.DataSource ds        = dr.CreateDataSource(savePath, null);
     OSGeo.OGR.Layer      fileLayer = ds.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPoint, null);
     for (int i = 0; i < allFeat.Length; i++)
     {
         OSGeo.OGR.FeatureDefn FeatDf   = new OSGeo.OGR.FeatureDefn("");
         OSGeo.OGR.Feature     fileFeat = new OSGeo.OGR.Feature(FeatDf);
         OSGeo.OGR.Geometry    fileGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPoint);
         fileGeom.AddPoint(allFeat[i].X, allFeat[i].Y, allFeat[i].Z);
         fileFeat.SetGeometry(fileGeom);
         fileLayer.CreateFeature(fileFeat);
     }
     fileLayer.Dispose();
     ds.Dispose();
 }
Example #10
0
        public static void claenPoint(this OSGeo.OGR.Layer resLayer, double jiaodu, int cishu)
        {
            int featCount = resLayer.GetFeatureCount(0);

            for (int i = 0; i < featCount; i++)
            {
                OSGeo.OGR.Feature  oriFeat = resLayer.GetFeature(i);
                OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef();
                OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0);

                int pointCount = subGeom.GetPointCount();

                Point[] aFeat = new Point[pointCount];

                for (int c = 0; c < pointCount; c++)
                {
                    aFeat[c].X = subGeom.GetX(c);
                    aFeat[c].Y = subGeom.GetY(c);
                    aFeat[c].Z = subGeom.GetZ(c);
                }

                OSGeo.OGR.Geometry newGeom = null;
                if (aFeat.Length > cishu * 3)
                {
                    newGeom = JID(aFeat, jiaodu, cishu);
                }
                else
                {
                    oriFeat.Dispose();
                    continue;
                }
                if (newGeom != null)
                {
                    oriFeat.SetGeometry(newGeom);
                    resLayer.SetFeature(oriFeat);
                }
                oriFeat.Dispose();
            }
        }
Example #11
0
        /// <summary>
        /// 创建一个Buffer file,用于获取最小高度,juli是buffer的距离
        /// </summary>
        /// <param name="infile"></param>
        /// <param name="juli"></param>
        /// <returns></returns>
        private static OSGeo.OGR.Layer bufferFile(OSGeo.OGR.Layer inLayer, int juli)
        {
            string buf = inLayer.GetName() + "buf";

            shpDataSet.deleteLayerByName(buf);
            OSGeo.OGR.Layer bufferLayer = shpDataSet.CreateLayer(buf, inLayer.GetSpatialRef(), inLayer.GetGeomType(), null);

            int featCount = inLayer.GetFeatureCount(0);

            for (int i = 0; i < featCount; i++)
            {
                OSGeo.OGR.Feature  inFeat  = inLayer.GetFeature(i);
                OSGeo.OGR.Geometry inGeom  = inFeat.GetGeometryRef();
                OSGeo.OGR.Geometry outGeom = inGeom.Buffer(juli, 0);
                OSGeo.OGR.Feature  outFeat = new OSGeo.OGR.Feature(new OSGeo.OGR.FeatureDefn(""));
                outFeat.SetGeometry(outGeom);
                bufferLayer.CreateFeature(outFeat);
                inFeat.Dispose();
                outFeat.Dispose();
            }
            return(bufferLayer);
        }
Example #12
0
 /// <summary>
 /// List<Point[]> To shpLine
 /// </summary>
 /// <param name="savePath"></param>
 /// <param name="allFeat"></param>
 public static void pointGtoshpLine(string savePath, List <Point[]> allFeat)
 {
     OSGeo.OGR.Ogr.RegisterAll();
     OSGeo.OGR.Driver     dr        = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
     OSGeo.OGR.DataSource ds        = dr.CreateDataSource(savePath, null);
     OSGeo.OGR.Layer      fileLayer = ds.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbLineString, null);
     for (int u = 0; u < allFeat.Count; u++)
     {
         Point[] a = allFeat[u];
         OSGeo.OGR.FeatureDefn FeatDf   = new OSGeo.OGR.FeatureDefn("");
         OSGeo.OGR.Feature     fileFeat = new OSGeo.OGR.Feature(FeatDf);
         OSGeo.OGR.Geometry    fileGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLineString);
         for (int i = 0; i < a.Length; i++)
         {
             fileGeom.AddPoint(a[i].X, a[i].Y, a[i].Z);
         }
         fileFeat.SetGeometry(fileGeom);
         fileLayer.CreateFeature(fileFeat);
     }
     fileLayer.Dispose();
     ds.Dispose();
 }
Example #13
0
        private void pdfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < mapBox1.Map.Layers.Count; i++)
            {
                if (mapBox1.Map.Layers[i].LayerName == SelectedLayer)
                {
                    var layer = mapBox1.Map.Layers[i] as VectorLayer;

                    var dataSource = layer.DataSource as GeometryFeatureProvider;

                    OSGeo.OGR.Driver driverSH = OSGeo.OGR.Ogr.GetDriverByName("PDF");



                    var referance = new SpatialReference("PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],EXTENSION[\"PROJ4\",\"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs\"],AUTHORITY[\"EPSG\",\"3857\"]]");


                    OSGeo.OGR.DataSource dataSourceSH = driverSH.CreateDataSource(Application.StartupPath + "\\" + SelectedLayer + ".pdf", new string[] { "ENCODING=UTF-16" });


                    var layerSH = dataSourceSH.CreateLayer("PointLayer", referance, OSGeo.OGR.wkbGeometryType.wkbPoint, new string[] { "ENCODING=UTF-16" });


                    layerSH.StartTransaction();

                    for (int k = 0; k < dataSource.Features.Columns.Count; k++)
                    {
                        OSGeo.OGR.FieldDefn field = new OSGeo.OGR.FieldDefn(dataSource.Features.Columns[k].Caption, OSGeo.OGR.FieldType.OFTString);


                        layerSH.CreateField(field, 1);
                    }

                    for (int k = 0; k < dataSource.Features.Rows.Count; k++)
                    {
                        OSGeo.OGR.Feature feature = new OSGeo.OGR.Feature(layerSH.GetLayerDefn());

                        if (!string.IsNullOrEmpty(dataSource.Features.Rows[k]["geom_wkt"].ToString()))
                        {
                            OSGeo.OGR.Geometry geom = OSGeo.OGR.Geometry.CreateFromWkt(dataSource.Features.Rows[k]["geom_wkt"].ToString());
                            feature.SetGeometry(geom);
                        }

                        for (int x = 0; x < dataSource.Features.Columns.Count; x++)
                        {
                            var data = dataSource.Features.Rows[k][x].ToString();

                            feature.SetField(dataSource.Features.Columns[x].Caption, data);
                        }
                        try
                        {
                            layerSH.CreateFeature(feature);
                            feature.Dispose();
                        }
                        catch (Exception)
                        {
                        }
                    }
                    layerSH.CommitTransaction();
                    layerSH.SyncToDisk();
                    layerSH.Dispose();

                    dataSourceSH.FlushCache();
                    dataSourceSH.Dispose();

                    driverSH.Dispose();
                }
            }
        }
Example #14
0
        /// <summary>
        /// 三点夹角的判定条件,输出为满足条件的成员的ID所组成的ID数组
        /// </summary>
        /// <param name="aFeat"></param>
        /// <returns></returns>
        private static OSGeo.OGR.Feature JID(Point[] aFeat)
        {
            int            userSet  = 165;
            int            seleTime = 10;
            List <Point[]> pjGroupL = new List <Point[]>();
            List <Point[]> zjGroupL = new List <Point[]>();

            List <Point> pjGroup = new List <Point>(); //平角
            List <Point> zjGroup = new List <Point>(); //平角

            for (int i = 0; i < aFeat.Length; i++)
            {
                int    frontId, thisId, backId;
                bool[] yon = new bool[seleTime];
                for (int t = 1; t <= seleTime; t++)
                {
                    if (i < t)
                    {
                        frontId = aFeat.Length - 1 + i - t;
                    }
                    else
                    {
                        frontId = i - t;
                    }

                    thisId = i;

                    if (i > aFeat.Length - 1 - t)
                    {
                        backId = i - (aFeat.Length - 1) + t;
                    }
                    else
                    {
                        backId = i + t;
                    }
                    double jiaodu = cosCalculator(aFeat[frontId], aFeat[thisId], aFeat[backId]);//求角度
                    yon[t - 1] = jiaodu > userSet;
                }

                //判定条件为:以上两条件满足其一则被选为平角,并输出为结果

                //当两有任意一个角度大于 160度. 则认为是平角
                if (yon.Contains(true))
                {
                    //if (zjGroup.Count != null)
                    //{
                    //    zjGroupL.Add(zjGroup.ToArray());
                    //    zjGroup.Clear();
                    //}
                    pjGroup.Add(aFeat[i]);
                }
                //否则认为是直角
                else
                {
                    //if (pjGroup.Count >3 )
                    //{
                    //    pjGroupL.Add(pjGroup.ToArray());
                    //    pjGroup.Clear();
                    //}
                    zjGroup.Add(aFeat[i]);
                }
            }

            ///输出Featuer

            OSGeo.OGR.FeatureDefn featDf  = new OSGeo.OGR.FeatureDefn("");
            OSGeo.OGR.Feature     outFeat = new OSGeo.OGR.Feature(featDf);
            OSGeo.OGR.Geometry    outGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
            OSGeo.OGR.Geometry    subGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);

            for (int g = 0; g < zjGroup.Count(); g++)
            {
                Point a = zjGroup[g];
                subGeom.AddPoint(a.X, a.Y, a.Z);
            }
            if (subGeom.GetPointCount() < 4)
            {
                return(null);
            }
            subGeom.CloseRings();
            outGeom.AddGeometry(subGeom);
            outFeat.SetGeometry(outGeom);
            return(outFeat);
        }
Example #15
0
        /// <summary>
        /// Creates an OGR data source from a FeatureDataTable
        /// </summary>
        /// <param name="table">The name of the table</param>
        /// <param name="geometryType">The geometry type</param>
        /// <param name="driver">The driver</param>
        /// <param name="connection">The connection string</param>
        /// <param name="driverOptions">The options for the driver</param>
        /// <param name="layerOptions">The options for the layer</param>
        public static void CreateFromFeatureDataTable(FeatureDataTable table,
                                                      OgcGeometryType geometryType, int srid, string driver, string connection, string[] driverOptions = null, string[] layerOptions = null)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            if (table.Rows.Count == 0)
            {
                throw new ArgumentException("The table contains no rows", "table");
            }

            if (geometryType < OgcGeometryType.Point || geometryType > OgcGeometryType.MultiPolygon)
            {
                throw new ArgumentException("Invalid geometry type", "geometryType");
            }

            if (string.IsNullOrWhiteSpace(driver))
            {
                throw new ArgumentException("No driver specified", "driver");
            }

            var dr = OSGeo.OGR.Ogr.GetDriverByName(driver);

            if (dr == null)
            {
                throw new Exception(string.Format("Cannot load driver '{0}'!", driver));
            }

            //if (!dr.TestCapability("ODrCCreateDataSource"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a data source!", driver));

            // Create the data source
            var ds = dr.CreateDataSource(connection, driverOptions);
            //if (!ds.TestCapability("ODsCCreateLayer"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a layer!", driver));

            // Create the spatial reference
            var sr = new OSGeo.OSR.SpatialReference(string.Empty);

            sr.ImportFromEPSG(srid);

            // Create the layer
            var lyr = ds.CreateLayer(table.TableName, sr, (OgrGeometryType)geometryType, layerOptions);

            sr.Dispose();

            //lyr.GetSpatialRef();
            foreach (System.Data.DataColumn dc in table.Columns)
            {
                using (var fldDef = GetFieldDefinition(dc))
                    lyr.CreateField(fldDef, 0);
            }

            using (var ld = lyr.GetLayerDefn())
            {
                foreach (FeatureDataRow fdr in table.Rows)
                {
                    if ((int)fdr.Geometry.OgcGeometryType != (int)geometryType)
                    {
                        continue;
                    }

                    using (var feature = new OgrFeature(ld))
                    {
                        feature.SetGeometry(OgrGeometry.CreateFromWkb(fdr.Geometry.AsBinary()));
                        var idx = -1;
                        foreach (System.Data.DataColumn dc in table.Columns)
                        {
                            idx++;
                            var      fd = ld.GetFieldDefn(idx);
                            DateTime dt;
                            switch (fd.GetFieldType())
                            {
                            case OgrFieldType.OFTBinary:
                                //Nothing
                                break;

                            case OgrFieldType.OFTDate:
                                dt = ((DateTime)fdr[dc]).Date;
                                feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                break;

                            case OgrFieldType.OFTDateTime:
                                dt = (DateTime)fdr[dc];
                                feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                break;

                            case OgrFieldType.OFTTime:
                                var tod = ((DateTime)fdr[dc]).TimeOfDay;
                                feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0);
                                break;

                            case OgrFieldType.OFTInteger:
                                feature.SetField(idx, Convert.ToInt32(fdr[dc]));
                                break;

                            case OgrFieldType.OFTIntegerList:
                                var il = GetIntegerList(fdr[dc], dc.DataType);
                                feature.SetFieldIntegerList(idx, il.Length, il);
                                break;

                            case OgrFieldType.OFTReal:
                                feature.SetField(idx, Convert.ToDouble(fdr[dc]));
                                break;

                            case OgrFieldType.OFTRealList:
                                var dl = GetDoubleList(fdr[dc], dc.DataType);
                                feature.SetFieldDoubleList(idx, dl.Length, dl);
                                break;

                            case OgrFieldType.OFTString:
                                feature.SetField(idx, Convert.ToString(fdr[dc]));
                                break;

                            case OgrFieldType.OFTStringList:
                                var sl = (string[])fdr[dc];
                                feature.SetFieldStringList(idx, sl);
                                break;
                            }
                            fd.Dispose();
                        }
                        lyr.CreateFeature(feature);
                        feature.Dispose();
                    }
                    //ld.Dispose();
                }
            }

            lyr.Dispose();
            ds.Dispose();
            dr.Dispose();
        }
Example #16
0
        /// <summary>
        /// Creates an OGR data source from a FeatureDataTable
        /// </summary>
        /// <param name="table">The name of the table</param>
        /// <param name="geometryType">The geometry type</param>
        /// <param name="driver">The driver</param>
        /// <param name="connection">The connection string</param>
        /// <param name="driverOptions">The options for the driver</param>
        /// <param name="layerOptions">The options for the layer</param>
        public static void CreateFromFeatureDataTable(FeatureDataTable table, 
            OgcGeometryType geometryType, int srid, string driver, string connection, string[] driverOptions = null, string[] layerOptions = null)
        {
            if (table == null)
                throw new ArgumentNullException("table");

            if (table.Rows.Count == 0)
                throw new ArgumentException("The table contains no rows", "table");

            if (geometryType < OgcGeometryType.Point || geometryType > OgcGeometryType.MultiPolygon)
                throw new ArgumentException("Invalid geometry type", "geometryType");

            if (string.IsNullOrWhiteSpace(driver))
                throw new ArgumentException("No driver specified", "driver");

            var dr = OSGeo.OGR.Ogr.GetDriverByName(driver);
            if (dr == null)
                throw new Exception(string.Format("Cannot load driver '{0}'!", driver));

            //if (!dr.TestCapability("ODrCCreateDataSource"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a data source!", driver));

            // Create the data source
            var ds = dr.CreateDataSource(connection, driverOptions);
            //if (!ds.TestCapability("ODsCCreateLayer"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a layer!", driver));

            // Create the spatial reference
            var sr = new OSGeo.OSR.SpatialReference(string.Empty);
            sr.ImportFromEPSG(srid);

            // Create the layer
            var lyr = ds.CreateLayer(table.TableName, sr, (OgrGeometryType)geometryType, layerOptions);
            sr.Dispose();

            //lyr.GetSpatialRef();
            foreach (System.Data.DataColumn dc in table.Columns)
            {
                using (var fldDef = GetFieldDefinition(dc))
                    lyr.CreateField(fldDef, 0);
            }

            using (var ld = lyr.GetLayerDefn())
            {
                foreach (FeatureDataRow fdr in table.Rows)
                {
                    if ((int)fdr.Geometry.OgcGeometryType != (int)geometryType)
                        continue;

                    using (var feature = new OgrFeature(ld))
                    {
                        feature.SetGeometry(OgrGeometry.CreateFromWkb(fdr.Geometry.AsBinary()));
                        var idx = -1;
                        foreach (System.Data.DataColumn dc in table.Columns)
                        {
                            idx++;
                            var fd = ld.GetFieldDefn(idx);
                            DateTime dt;
                            switch (fd.GetFieldType())
                            {
                                case OgrFieldType.OFTBinary:
                                    //Nothing
                                    break;
                                case OgrFieldType.OFTDate:
                                    dt = ((DateTime)fdr[dc]).Date;
                                    feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                    break;
                                case OgrFieldType.OFTDateTime:
                                    dt = (DateTime)fdr[dc];
                                    feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                    break;
                                case OgrFieldType.OFTTime:
                                    var tod = ((DateTime)fdr[dc]).TimeOfDay;
                                    feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0);
                                    break;
                                case OgrFieldType.OFTInteger:
                                    feature.SetField(idx, Convert.ToInt32(fdr[dc]));
                                    break;
                                case OgrFieldType.OFTIntegerList:
                                    var il = GetIntegerList(fdr[dc], dc.DataType);
                                    feature.SetFieldIntegerList(idx, il.Length, il);
                                    break;
                                case OgrFieldType.OFTReal:
                                    feature.SetField(idx, Convert.ToDouble(fdr[dc]));
                                    break;
                                case OgrFieldType.OFTRealList:
                                    var dl = GetDoubleList(fdr[dc], dc.DataType);
                                    feature.SetFieldDoubleList(idx, dl.Length, dl);
                                    break;
                                case OgrFieldType.OFTString:
                                    feature.SetField(idx, Convert.ToString(fdr[dc]));
                                    break;
                                case OgrFieldType.OFTStringList:
                                    var sl = (string[])fdr[dc];
                                    feature.SetFieldStringList(idx, sl);
                                    break;

                            }
                            fd.Dispose();
                        }
                        lyr.CreateFeature(feature);
                        feature.Dispose();
                    }
                    //ld.Dispose();
                }
            }

            lyr.Dispose();
            ds.Dispose();
            dr.Dispose();
        }