private void CreateShp(string outpath, Dictionary <Point2d, Geometry> geoList, OSGeo.OSR.SpatialReference srt) { //注册ogr库 string pszDriverName = "ESRI Shapefile"; OSGeo.OGR.Ogr.RegisterAll(); //调用对shape文件读写的Driver接口 OSGeo.OGR.Driver poDriver = OSGeo.OGR.Ogr.GetDriverByName(pszDriverName); if (poDriver == null) { MessageBox.Show("驱动错误!"); } //创建河网shp string shpPath = outpath; OSGeo.OGR.DataSource poDs; poDs = ShpHelp.GetShpDriver().CreateDataSource(shpPath, null); //创建图层 OSGeo.OGR.Layer poLayer; //OSGeo.OSR.SpatialReference srt = new OSGeo.OSR.SpatialReference(Const.WGS84); poLayer = poDs.CreateLayer("rivernetline.shp", srt, wkbGeometryType.wkbLineString, null); //创建一个Feature OSGeo.OGR.Feature feature = new OSGeo.OGR.Feature(poLayer.GetLayerDefn()); foreach (var geo in geoList) { feature.SetGeometry(geo.Value); poLayer.CreateFeature(feature); } feature.Dispose(); poDs.Dispose(); }
/************************************ 对比并筛选 ***************************************************/ /// <summary> /// 通过ID数组提取出所有要素,创建动作 /// </summary> /// <param name="dzLine"></param> /// <param name="pdLing"></param> /// <param name="savePath"></param> public static void selectFeat(string dzx, string pdx, string savePath) { Stopwatch ssww = new Stopwatch(); ssww.Start(); //获得数组 int[] a = getMinIdGroupM(dzx, pdx); Console.WriteLine("ID to Feater..."); OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource dzDS = dr.Open(dzx, 0); OSGeo.OGR.Layer dzLayer = dzDS.GetLayerByIndex(0); 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++) { fileLayer.CreateFeature(dzLayer.GetFeature(a[i])); dzLayer.GetFeature(a[i]).Dispose(); } dzLayer.Dispose(); dzDS.Dispose(); fileLayer.Dispose(); ds.Dispose(); _2_坡度线.OutLine.CutImg.cleanPdxM(savePath); ssww.Stop(); Console.WriteLine("提取轮廓线完成!用时:{0}", ssww.Elapsed.ToString()); }
/// <summary> /// 在指定路径下构建shp数据源(此操作不会生成实际的shp文件) /// </summary> /// <param name="shpFilePath">shp文件路径</param> /// <returns></returns> public static DataSource CreateShapefileSource(string shpFilePath) { Driver driver = Ogr.GetDriverByName("ESRI Shapefile"); if (driver == null) { throw new Exception("ESRI Shapefile 驱动不可使用"); } Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // 为了支持中文路径 DataSource dataSource = driver.CreateDataSource(shpFilePath, null); if (dataSource == null) { throw new Exception("创建SHP文件" + shpFilePath + "失败"); } DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(shpFilePath)); if (!directoryInfo.Exists) { directoryInfo.Create(); } driver.Dispose(); return(dataSource); }
/// <summary> /// 通过是否相交和面积差,获得最小面积差相交要素的ID数组--------多线OK /// </summary> /// <param name="dzx"></param> /// <param name="pdx"></param> /// <returns></returns> private static int[] getMinIdGroupM(string dzx, string pdx) { Console.WriteLine("起用多线程提取轮廓线ID...."); OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource pdDS = dr.Open(pdx, 0); OSGeo.OGR.Layer pdLayer = pdDS.GetLayerByIndex(0); int pdCount = pdLayer.GetFeatureCount(0); for (int pdi = 0; pdi < pdCount; pdi++) { OSGeo.OGR.Feature pdFeat = pdLayer.GetFeature(pdi); select(pdFeat, dzx); } while (tickTime < pdCount) { Console.WriteLine("第{0}个完成,共{1}个", tickTime, pdCount); Thread.Sleep(2000); } pdLayer.Dispose(); pdDS.Dispose(); Console.WriteLine("多线程提取ID完成....."); return(minIdG.ToArray()); }
/// <summary> /// 创建一个Buffer file,用于获取最小高度,juli是buffer的距离 /// </summary> /// <param name="infile"></param> /// <param name="juli"></param> /// <returns></returns> private static string bufferFile(string infile, int juli) { Ogr.RegisterAll(); string bufferFile = StaticTools.tempFilePath("shp", "Buf"); OSGeo.OGR.Driver dr = Ogr.GetDriverByName("ESRI shapefile"); DataSource infileDs = dr.Open(infile, 0); Layer inLayer = infileDs.GetLayerByIndex(0); DataSource bufferDs = dr.CreateDataSource(bufferFile, null); Layer bufferLayer = bufferDs.CreateLayer(inLayer.GetName(), inLayer.GetSpatialRef(), inLayer.GetGeomType(), null); int featCount = inLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { Feature inFeat = inLayer.GetFeature(i); Geometry inGeom = inFeat.GetGeometryRef(); Geometry outGeom = inGeom.Buffer(juli, 0); Feature outFeat = new Feature(new FeatureDefn("")); outFeat.SetGeometry(outGeom); bufferLayer.CreateFeature(outFeat); inFeat.Dispose(); outFeat.Dispose(); } infileDs.Dispose(); bufferDs.Dispose(); return(bufferFile); }
/// <summary> /// Creates a points shapefile /// </summary> /// <param name="FileName"></param> private static void TestCreatePointsShapeFile(string FileName) { var points = new List <string>() { "P1", "P2", "P3", "P4" }; OSGeo.OGR.Driver drv = Ogr.GetDriverByName("ESRI Shapefile"); using (var ds = drv.CreateDataSource(FileName, new string[] {})) { var src = new OSGeo.OSR.SpatialReference(""); src.ImportFromEPSG(23030); using (var layer = ds.CreateLayer("SENSORS", src, wkbGeometryType.wkbPoint, new string[] {})) { FieldDefn fdefn = new FieldDefn("ID", FieldType.OFTString); fdefn.SetWidth(32); layer.CreateField(fdefn, 1); fdefn = new FieldDefn("FIELD_2", FieldType.OFTReal); layer.CreateField(fdefn, 1); foreach (var s in points) { Feature feature = new Feature(layer.GetLayerDefn()); feature.SetField("ID", s); feature.SetField("FIELD_2", 123.4d); var geom = new Geometry(wkbGeometryType.wkbPoint); double X = 123.4; double Y = 123.4; geom.AddPointZM(X, Y, 123.4d, 0d); feature.SetGeometry(geom); layer.CreateFeature(feature); } } ds.FlushCache(); } }
async static void multi(string dsm, string oriShp, int i) { await Task.Run(() => { Ogr.RegisterAll(); Gdal.AllRegister(); Dataset rastDs = Gdal.Open(dsm, Access.GA_ReadOnly); var geoTransform = new double[6]; rastDs.GetGeoTransform(geoTransform); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); DataSource featDs = dr.Open(oriShp, 1); Layer oriLayer = featDs.GetLayerByIndex(0); Feature oriFeat = oriLayer.GetFeature(i); Feature bufFeat = new Feature(new FeatureDefn("")); bufFeat.SetGeometry(oriFeat.GetGeometryRef().Buffer(1, 0)); int[] subRasterOff_Size = subRasterInfo(geoTransform, rastDs.RasterXSize, rastDs.RasterYSize, bufFeat); getMaxMinValue(rastDs, oriFeat, bufFeat, subRasterOff_Size); oriLayer.SetFeature(oriFeat); bufFeat.Dispose(); rastDs.Dispose(); featDs.Dispose(); tickTime++; }); }
/// <summary> /// Prints initialized Ogr drivers. /// </summary> private static void PrintDriversOgr() { if (!Usable) { throw new Exception(string.Format(Strings.UnableToPrintDrivers, "OGR")); } int driverCount = Ogr.GetDriverCount(); for (int index = 0; index < driverCount; index++) { try { using (OSGeo.OGR.Driver driver = Ogr.GetDriver(index)) { Console.WriteLine($"OGR {index}: {driver.GetName()}", "Debug"); } } catch (Exception exception) { throw new Exception(string.Format(Strings.UnableToPrintDrivers, "OGR"), exception); } } }
/// <summary> /// 整合坡度多边形(Polygon),并清除重叠部份 /// </summary> /// <param name="subPdx"></param> /// <returns></returns> public static string zhengHePdx(List <string> subPdx) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); string res = StaticTools.tempFilePath("shp", "整合"); OSGeo.OGR.DataSource newDS = dr.CreateDataSource(res, null); //////////////待关闭1 has Layer newLayer = newDS.CreateLayer("podu", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null); //////////////待关闭2 for (int i = 0; i < subPdx.Count; i++) { OSGeo.OGR.DataSource ds = dr.Open(subPdx[i], 0);//////////////待关闭3 has Layer layer = ds.GetLayerByIndex(0); for (int j = 0; j < layer.GetFeatureCount(0); j++) { OSGeo.OGR.Feature subFeat = layer.GetFeature(j); OSGeo.OGR.Geometry subGeom = subFeat.GetGeometryRef(); double thisArea = subGeom.GetArea(); // 清理过小Featuer if (thisArea > 200) { newLayer.CreateFeature(layer.GetFeature(j)); } } ds.Dispose(); Console.WriteLine("完成整合{0}/{1}", i + 1, subPdx.Count); } newDS.Dispose(); cleanPdx(res); return(res); }
async static void getDoubFeat(string filePath, int s, int ss) { await Task.Run(() => { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource ds = dr.Open(filePath, 0); OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); OSGeo.OGR.Feature oriFeat = layer.GetFeature(s); for (int i = s + 1; i < ss; i++) { OSGeo.OGR.Feature nextFeat = layer.GetFeature(i); if (StaticTools.isSame(oriFeat, nextFeat, 1)) { ids.Add(s); break; } nextFeat.Dispose(); } oriFeat.Dispose(); layer.Dispose(); ds.Dispose(); tickTime++; } ); }
/// <summary> /// 整合坡度线(Line) /// </summary> /// <param name="subPdx"></param> /// <returns></returns> public static string zhengHePdLine(List <string> subPdx) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); string newPath = StaticTools.tempFilePath("shp", "整合线"); OSGeo.OGR.DataSource newDs = dr.CreateDataSource(newPath, null);///////////////// OSGeo.OGR.Layer newLayer = newDs.CreateLayer("Lines", dr.Open(subPdx[0], 0).GetLayerByIndex(0).GetSpatialRef(), dr.Open(subPdx[0], 0).GetLayerByIndex(0).GetGeomType(), null); for (int i = 0; i < subPdx.Count; i++) { OSGeo.OGR.DataSource dsLine = dr.Open(subPdx[i], 0);/////////////////////// OSGeo.OGR.Layer lineLayer = dsLine.GetLayerByIndex(0); for (int j = 0; j < lineLayer.GetFeatureCount(0); j++) { newLayer.CreateFeature(lineLayer.GetFeature(j)); } dsLine.Dispose(); } newDs.Dispose(); return(newPath); }
/// <summary> /// 清理重复的Featuer----------多线程----与单线程效率差别很小,不推荐 /// </summary> /// <param name="filePath"></param> public static void cleanPdxM(string filePath) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource ds = dr.Open(filePath, 1); OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); int featCount = layer.GetFeatureCount(0); var sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (int i = 0; i < featCount - 1; i++) { getDoubFeat(filePath, i, featCount); } while (tickTime != featCount - 1) { Thread.Sleep(2000); Console.WriteLine(tickTime.ToString() + " /" + featCount.ToString() + "/ deletePoint " + ids.Count().ToString()); } sw.Stop(); Console.WriteLine("多线程清理重复Featuer用时:" + sw.Elapsed.ToString()); for (int i = 0; i < ids.Count; i++) { layer.DeleteFeature(ids[i]); } string a = "REPACK " + layer.GetName(); ds.ExecuteSQL(a, null, ""); ds.Dispose(); }
async static void subAdd(Feature poiFeat, string dzx, Layer newLayer) { await Task.Run(() => { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource dzxDS = dr.Open(dzx, 0); OSGeo.OGR.Layer dzxLayer = dzxDS.GetLayerByIndex(0); int dzxCount = dzxLayer.GetFeatureCount(0); OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef(); for (int i = 0; i < dzxLayer.GetFeatureCount(0); i++) { OSGeo.OGR.Feature dzxFeat = dzxLayer.GetFeature(i); OSGeo.OGR.Geometry dzxGeom = dzxFeat.GetGeometryRef(); if (poiGeom.Within(dzxGeom)) { newLayer.CreateFeature(dzxFeat); } dzxGeom.Dispose(); dzxFeat.Dispose(); } poiFeat.Dispose(); dzxDS.Dispose(); tickTime++; }); }
/// <summary> /// 提取平角点到文件 /// </summary> /// <param name="inFile"></param> /// <param name="outFile"></param> public static void newGeom(string inFile, string outFile, double jiao = 175, int times = 3) { Ogr.RegisterAll(); OSGeo.OGR.Driver dr = Ogr.GetDriverByName("ESRI shapefile"); DataSource ds = dr.Open(inFile, 0); Layer layer = ds.GetLayerByIndex(0); var pointDs = dr.CreateDataSource(outFile, null); var pointLayer = pointDs.CreateLayer("PointLayer", null, wkbGeometryType.wkbPoint, null); int featCount = layer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { Feature afeat = layer.GetFeature(i); List <int> a = _平角点ID集(afeat, jiao, times); int straightCount = a.Count; for (int j = 0; j < straightCount; j++) { Geometry geom = afeat.GetGeometryRef(); Geometry subGeom = geom.GetGeometryRef(0); Feature pointFeat = new Feature(new FeatureDefn("")); Geometry pointGeom = new Geometry(wkbGeometryType.wkbPoint); pointGeom.AddPoint_2D(subGeom.GetX(j), subGeom.GetY(j)); pointFeat.SetGeometry(pointGeom); pointLayer.CreateFeature(pointFeat); pointFeat.Dispose(); } afeat.Dispose(); } ds.Dispose(); pointDs.Dispose(); }
public static void selectDZXFromPointM(string point, string dzx, string savePath) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource poiDS = dr.Open(point, 0); OSGeo.OGR.Layer poiLayer = poiDS.GetLayerByIndex(0); if (File.Exists(savePath)) { File.Delete(savePath); } OSGeo.OGR.DataSource newDS = dr.CreateDataSource(savePath, null); OSGeo.OGR.Layer newLayer = newDS.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null); tickTime = 0; int poiCount = poiLayer.GetFeatureCount(0); for (int i = 0; i < poiCount; i++) { subAdd(poiLayer.GetFeature(i), dzx, newLayer); } while (tickTime < poiCount) { Thread.Sleep(2222); Console.WriteLine("getFeatureByPoint:{0}/{1}", tickTime, poiCount); } Console.WriteLine("【本次提取到{0}个要素】", newLayer.GetFeatureCount(0)); newDS.Dispose(); poiDS.Dispose(); }
/// <summary> /// 清理重复的Featuer----------单线程 /// </summary> /// <param name="pdx"></param> public static void cleanPdx(string pdx) { Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); DataSource ds = dr.Open(pdx, 1); Layer newLayer = ds.GetLayerByIndex(0); int 重复的 = 0; for (int i = 0; i < newLayer.GetFeatureCount(0) - 1; i++) { Feature ori = newLayer.GetFeature(i); for (int j = i + 1; j < newLayer.GetFeatureCount(0); j++) { Feature next = newLayer.GetFeature(j); bool a = StaticTools.isSame(ori, next, 1); if (a) { newLayer.DeleteFeature(i); 重复的++; Console.WriteLine("已删除{0}个重复Featuer,allFeat is {1}/{2}", 重复的, i + 1, newLayer.GetFeatureCount(0)); break; } next.Dispose(); } ori.Dispose(); } string layerName = newLayer.GetName(); ds.ExecuteSQL("REPACK " + layerName, null, ""); ds.Dispose(); }
/************************************* 清理点簇 (未完成)*****************************************/ public static void getCleanLins(string inFile, string outFile) { Ogr.RegisterAll(); OSGeo.OGR.Driver dr = Ogr.GetDriverByName("ESRI shapefile"); DataSource inds = dr.Open(inFile, 0); Layer inLayer = inds.GetLayerByIndex(0); if (File.Exists(outFile)) { File.Delete(outFile); } DataSource outds = dr.CreateDataSource(outFile, null); Layer outLayer = outds.CreateLayer("", null, wkbGeometryType.wkbPolygon, null); int featCount = inLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { Feature inFeat = inLayer.GetFeature(i); Geometry inGeom = inFeat.GetGeometryRef(); Geometry inSubG = inGeom.GetGeometryRef(0); Feature outFeat = new Feature(new FeatureDefn("")); Geometry outGeom = new Geometry(wkbGeometryType.wkbPolygon); outGeom.AddGeometry(getCleanGeom(inSubG)); outFeat.SetGeometry(outGeom); outLayer.CreateFeature(outFeat); Console.WriteLine("{0}/{1}", i, featCount); } inds.Dispose(); outds.Dispose(); }
public void testStaticPartition() { Gdal.AllRegister(); Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); Gdal.SetConfigOption("SHAPE_ENCODING", ""); Ogr.RegisterAll(); //读取文件 DataSource ds = Ogr.Open(fromPath, 0); Layer oLayer = ds.GetLayerByIndex(0); // 写入文件 OSGeo.OGR.Driver oDriver = Ogr.GetDriverByName("ESRI Shapefile"); // 创建数据源 DataSource oDS; if (Ogr.Open(toPath, 0) != null) { oDS = Ogr.Open(toPath, 1); oDS.DeleteLayer(0); } else { oDS = oDriver.CreateDataSource(toPath, null); } Layer toLayer = oDS.CreateLayer("POINT", oLayer.GetSpatialRef(), wkbGeometryType.wkbPoint, null); Random ran = new Random(); Feature oFeature = null; Geometry lines = null; FeatureDefn oDefn = oLayer.GetLayerDefn(); FieldDefn oFieldID = new FieldDefn("HEIGHT_G", FieldType.OFTReal); toLayer.CreateField(oFieldID, 1); FieldDefn oFieldName = new FieldDefn("PWLs", FieldType.OFTReal); toLayer.CreateField(oFieldName, 1); while ((oFeature = oLayer.GetNextFeature()) != null) { //read current feature lines = oFeature.GetGeometryRef(); Feature feature = new Feature(oDefn); feature.SetGeometry(Line.getPoint(lines, lines.Centroid())); feature.SetField(0, 4.0); feature.SetField(1, ran.Next(40, 120)); toLayer.CreateFeature(feature); } oDS.SyncToDisk(); }
/************************************* 点角式 *****************************************/ /// <summary> /// 清理等后生成新的文件 /// /// </summary> public static void claenPoint(string filePath, string savefile, double jiaodu, int cishu) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); //进来的Layer OSGeo.OGR.DataSource oriDs = dr.Open(filePath, 0); OSGeo.OGR.Layer oriLayer = oriDs.GetLayerByIndex(0); //出去的Layer if (System.IO.File.Exists(savefile)) { savefile.deleteFiles(); } OSGeo.OGR.DataSource targDs = dr.CreateDataSource(savefile, null); OSGeo.OGR.Layer targLayer = targDs.CreateLayer("targ", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null); int featCount = oriLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { ///进来的Featuer OSGeo.OGR.Feature oriFeat = oriLayer.GetFeature(i); ///把一个Featuer转为点数组 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); } ///调选点方法,得到一个新的Featuer Geometry newGeom = null; if (aFeat.Length > cishu * 3) { newGeom = JID(aFeat, jiaodu, cishu); } else { oriFeat.Dispose(); continue; } if (newGeom != null) { Feature temp = new Feature(new FeatureDefn("")); temp.SetGeometry(newGeom); targLayer.CreateFeature(temp); temp.Dispose(); } oriFeat.Dispose(); Console.WriteLine("已完成{0}/{1}", i, featCount); } oriDs.Dispose(); targDs.Dispose(); }
/// <summary> /// 线声源离散 /// </summary> /// <param name="sourcePath">声源路径</param> /// <param name="resultPath">离散后路径</param> /// <param name="splitLength">分割长度</param> /// <param name="timeType">时段</param> /// <param name="frequency">频率</param> /// <returns></returns> private Layer getSource() { String sourceResultPath = m_resultPath + "\\SourceTemp.shp";//输出路径 // 写入文件 OSGeo.OGR.Driver resultDriver = Ogr.GetDriverByName("ESRI Shapefile"); // 创建数据源 DataSource resultDataSource; if (Ogr.Open(sourceResultPath, 0) != null) { resultDataSource = Ogr.Open(sourceResultPath, 1); resultDataSource.DeleteLayer(0); } else { resultDataSource = resultDriver.CreateDataSource(sourceResultPath, null); } //投影信息 OSGeo.OSR.SpatialReference projection = new OSGeo.OSR.SpatialReference(""); projection.ImportFromEPSG(3395); //构建声源点图层 Layer resultLayer = resultDataSource.CreateLayer("POINT", projection, wkbGeometryType.wkbPoint, null); //构建属性 resultLayer.CreateField(new FieldDefn("HEIGHT_G", FieldType.OFTReal), 1); //高度 resultLayer.CreateField(new FieldDefn("PWLs", FieldType.OFTReal), 1); //单一频率声功率级 resultLayer.CreateField(new FieldDefn("FREQUENCY", FieldType.OFTReal), 1); //频率 ISource sourceBean = null; DataSource sourceDataSource; foreach (KeyValuePair <String, String> item in m_sourcePath) { switch (item.Key) { case "road": sourceBean = new RoadSourrce(); break; case "rainway": sourceBean = new RainwaySource(); break; case "subway": sourceBean = new RoadSourrce(); break; } //源文件 sourceDataSource = Ogr.Open(item.Value, 0); resultLayer = sourceBean.getSource(sourceDataSource.GetLayerByIndex(0), resultLayer, m_splitLength, m_timeType, m_frequency); } return(resultLayer); }
public GdalRead_GeoJson() { _Driver = null; _DataSource = null; _Layer = null; _Feilds = new Dictionary <string, FieldType>(); _Coordiantes = null; this.InitinalGdal(); }
/// <summary> /// Prints available OGR drivers in DEBUG mode. /// </summary> private static void PrintDriversOgr() { int count = Ogr.GetDriverCount(); for (int i = 0; i < count; i++) { OSGeo.OGR.Driver driver = Ogr.GetDriver(i); Console.WriteLine($"OGR {i}: {driver.name}"); } }
public static HandleRef getCPtrAndSetReference(Driver obj, object parent) { if (obj != null) { obj.swigParentRef = parent; return obj.swigCPtr; } else { return new HandleRef(null, IntPtr.Zero); } }
public static HandleRef getCPtrAndDisown(Driver obj, object parent) { if (obj != null) { obj.swigCMemOwn = false; obj.swigParentRef = parent; return obj.swigCPtr; } else { return new HandleRef(null, IntPtr.Zero); } }
private static List <string> GetSupportVectorExtensions() { int driverCount = Ogr.GetDriverCount(); List <string> extensions = new List <string>(); for (int i = 0; i < driverCount; i++) { using (OSGeo.OGR.Driver driver = Ogr.GetDriver(i)) { } } return(extensions); }
public void InitGDAL() { Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); Gdal.SetConfigOption("SHAPE_ENCODING", ""); Gdal.AllRegister(); Ogr.RegisterAll(); oDriver = Ogr.GetDriverByName("ESRI Shapefile"); if (oDriver == null) { // MessageBox.Show("文件不能打开,请检查"); } }
/// <summary> /// 子线程提取轮廓线ID /// </summary> /// <param name="pdFeat"></param> /// <param name="dzx"></param> async static void select(Feature pdFeat, string dzx) { await Task.Run(() => { double afterCha = -1; int yesID = -1; OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource dzDS = dr.Open(dzx, 0); OSGeo.OGR.Layer dzxLayer = dzDS.GetLayerByIndex(0); OSGeo.OGR.Geometry pdGeom = pdFeat.GetGeometryRef(); int dzCount = dzxLayer.GetFeatureCount(0); for (int dzi = 0; dzi < dzCount; dzi++) { OSGeo.OGR.Feature dzFeat = dzxLayer.GetFeature(dzi); OSGeo.OGR.Geometry dzGeom = dzFeat.GetGeometryRef(); if (pdGeom.Intersect(dzGeom)) { double cha = Math.Abs(dzGeom.GetArea() - pdGeom.GetArea()); if (afterCha == -1) { afterCha = cha; yesID = dzi; } else if (cha < afterCha) { afterCha = cha; yesID = dzi; } } dzGeom.Dispose(); dzFeat.Dispose(); } if (yesID != -1) { minIdG.Add(yesID); } dzDS.Dispose(); dzxLayer.Dispose(); pdGeom.Dispose(); pdFeat.Dispose(); tickTime++; }); }
/******************************* 算法一 *************************************/ /// /// 2 调用点矩法提出断点,标记在CUT字段中 /// /// 3 调用点角法提出平角点,标计在straight中 /// /// 4 通过cut字段提取分段点集,再通过straight提取选用点集 /// /// 5 用选出的点集拟合直线并保留参数 /// 6 用参数集进行求交 /// 7 主要参数:点距法中的最大点距、点角法中的最大角度和迭代次数 /// /******************************* 工作流 *************************************/ public static void getDLG(string inFile, string outFile) { //注册 Ogr.RegisterAll(); Gdal.AllRegister(); //驱动 OSGeo.OGR.Driver dr = Ogr.GetDriverByName("ESRI shapefile"); //原数据 DataSource inDS = dr.Open(inFile, 0); Layer inLayer = inDS.GetLayerByIndex(0); //out数据 if (File.Exists(outFile)) { File.Delete(outFile); } DataSource outDS = dr.CreateDataSource(outFile, null); Layer outLayer = outDS.CreateLayer("outLayer", null, wkbGeometryType.wkbPolygon, null); int featCount = inLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { Feature aFeatuer = inLayer.GetFeature(i); Geometry subGeom = aFeatuer.GetGeometryRef().GetGeometryRef(0); //工作流 2 , 返回所有断点的ID,有重要参数! List <int> cutIDS = _点距法提断点._提取断点(aFeatuer, 2); //工作流 3, 返回所有平角点的ID,有重要参数! List <int> straightIDS = _提取平角点._平角点ID集(aFeatuer, 175, 5); //输出准备工作 Feature newFeat = new Feature(new FeatureDefn("")); Geometry newGeom = new Geometry(wkbGeometryType.wkbPolygon); //工作流 4 , 通过cut字段提取分段点集,再通过straight提取选用点集 newGeom.AddGeometry(ForDLG._提取断平拟合直线(aFeatuer, cutIDS, straightIDS)); //收! newFeat.SetGeometry(newGeom); outLayer.CreateFeature(newFeat); newFeat.Dispose(); aFeatuer.Dispose(); } inLayer.Dispose(); outLayer.Dispose(); inDS.Dispose(); outDS.Dispose(); }
/// <summary> /// 初始化Gdal /// </summary> public void InitinalGdal() { // 为了支持中文路径 Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); // 为了使属性表字段支持中文 Gdal.SetConfigOption("SHAPE_ENCODING", ""); Gdal.AllRegister(); Ogr.RegisterAll(); oDerive = Ogr.GetDriverByName("ESRI Shapefile"); if (oDerive == null) { LoggerHelper.Logger.Info("文件不能打开,请检查"); } }
/// <summary> /// 初始化Gdal /// </summary> public void InitinalGdal() { // 为了支持中文路径 OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // 为了使属性表字段支持中文 Gdal.SetConfigOption("SHAPE_ENCODING", ""); Gdal.AllRegister(); Ogr.RegisterAll(); oDerive = Ogr.GetDriverByName("ESRI Shapefile"); if (oDerive == null) { MessageBox.Show("文件不能打开,请检查"); } }
private void DatasetCopy(string srcPath, string trgPath, string driverName) { var dataset = Ogr.OpenShared(srcPath, 0); var layers = CommonUtils.GetDatasetLayers(dataset); OSGeo.OGR.Driver driver = Ogr.GetDriverByName(driverName); var trgDataset = driver.CreateDataSource(trgPath, null); foreach (var srcLayer in layers) { trgDataset.CopyLayer(srcLayer, srcLayer.GetName(), null); } trgDataset.Dispose(); }
/// <summary> /// 初始化Gdal /// </summary> public void InitinalGdal() { // 为了支持中文路径 Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // 为了使属性表字段支持中文 Gdal.SetConfigOption("SHAPE_ENCODING", ""); //Gdal.AllRegister(); Ogr.RegisterAll(); oDriver = Ogr.GetDriverByName("ESRI Shapefile"); if (oDriver == null) { Console.WriteLine("驱动不可用,请检查"); } }
public static HandleRef getCPtr(Driver obj) { return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; }