/// <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> /// 获取SHP文件的层 /// </summary> /// <param name="sfilename"></param> /// <param name="oLayer"></param> /// <returns></returns> public bool GetShpLayer(string sfilename) { if (null == sfilename || sfilename.Length <= 3) { oLayer = null; return(false); } if (oDerive == null) { LoggerHelper.Logger.Info("文件不能打开,请检查"); } DataSource ds = oDerive.Open(sfilename, 1); if (null == ds) { oLayer = null; return(false); } int iPosition = sfilename.LastIndexOf("\\"); string sTempName = sfilename.Substring(iPosition + 1, sfilename.Length - iPosition - 4 - 1); oLayer = ds.GetLayerByName(sTempName); if (oLayer == null) { ds.Dispose(); return(false); } return(true); }
/************************************ 对比并筛选 ***************************************************/ /// <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文件的层 /// </summary> /// <param name="sfilename"></param> /// <param name="oLayer"></param> /// <returns></returns> public bool GetShpLayer(string sfilename) { if (sfilename == null || sfilename.Length <= 3) { oLayer = null; return(false); } //if (oDriver == null) //{ // MessageBox.Show("文件不能打开,请检查"); //} DataSource ds = oDriver.Open(sfilename, 1); if (ds == null) { oLayer = null; return(false); } int iPosition = sfilename.LastIndexOf("\\"); string sTempName = sfilename.Substring(iPosition + 1, sfilename.Length - iPosition - 4 - 1); oLayer = ds.GetLayerByName(sTempName); if (oLayer == null) { ds.Dispose(); return(false); } return(true); }
/// <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> /// 清理重复的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(); }
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++; }); }
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> /// 清理重复的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 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> /// 提取平角点到文件 /// </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(); }
/// <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); }
/************************************* 清理点簇 (未完成)*****************************************/ 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 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(); }
public static void selectFromPoint(string point, string slopPoly) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource poiDS = dr.Open(point, 0); OSGeo.OGR.DataSource slopDS = dr.Open(slopPoly, 1); OSGeo.OGR.Layer poiLayer = poiDS.GetLayerByIndex(0); OSGeo.OGR.Layer slopLayer = slopDS.GetLayerByIndex(0); List <int> staIds = new List <int>(); int poiCount = WorkFlow.pointIds.Count; int slopCount = slopLayer.GetFeatureCount(0); for (int i = 0; i < poiCount; i++) { OSGeo.OGR.Feature poiFeat = poiLayer.GetFeature(WorkFlow.pointIds[i]); OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef(); for (int t = 0; t < slopCount; t++) { OSGeo.OGR.Feature slopFeat = slopLayer.GetFeature(t); OSGeo.OGR.Geometry slopGeom = slopFeat.GetGeometryRef(); if (poiGeom.Within(slopGeom)) { staIds.Add(t); WorkFlow.pointIds[i] = -1; } slopFeat.Dispose(); } poiFeat.Dispose(); } Console.WriteLine("【本次提取到{0}个要素】", staIds.Count); while (WorkFlow.pointIds.IndexOf(-1) > -1) { WorkFlow.pointIds.Remove(-1); } for (int i = 0; i < slopCount; i++) { if (staIds.IndexOf(i) == -1) { slopLayer.DeleteFeature(i); } } slopDS.deleteFeatUpdate(); slopDS.Dispose(); poiDS.Dispose(); }
/************************************* 点角式 *****************************************/ /// <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(); }
public static void selectDZXFromPoint(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.DataSource dzxDS = dr.Open(dzx, 0); if (File.Exists(savePath)) { File.Delete(savePath); } OSGeo.OGR.DataSource newDS = dr.CreateDataSource(savePath, null); OSGeo.OGR.Layer poiLayer = poiDS.GetLayerByIndex(0); OSGeo.OGR.Layer dzxLayer = dzxDS.GetLayerByIndex(0); OSGeo.OGR.Layer newLayer = newDS.CreateLayer("", dzxLayer.GetSpatialRef(), dzxLayer.GetGeomType(), null); int poiCount = poiLayer.GetFeatureCount(0); int dzxCount = dzxLayer.GetFeatureCount(0); for (int i = 0; i < poiCount; i++) { OSGeo.OGR.Feature poiFeat = poiLayer.GetFeature(i); OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef(); for (int t = 0; t < dzxCount; t++) { OSGeo.OGR.Feature dzxFeat = dzxLayer.GetFeature(t); OSGeo.OGR.Geometry dzxGeom = dzxFeat.GetGeometryRef(); if (poiGeom.Within(dzxGeom)) { newLayer.CreateFeature(dzxFeat); } dzxFeat.Dispose(); Console.WriteLine("getFeatureByPoint:{0}/{1}", i, poiCount); } poiFeat.Dispose(); } Console.WriteLine("【本次提取到{0}个要素】", newLayer.GetFeatureCount(0)); newDS.Dispose(); dzxDS.Dispose(); poiDS.Dispose(); }
/// <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(); }
static void Test2() { Ogr.RegisterAll(); OSGeo.OGR.Driver driver = Ogr.GetDriverByName("ESRI Shapefile"); //var dataSource = driver.Open(@"C:\test\continents.shp", 0); OpenFileDialog openFileDialog = new OpenFileDialog(); var result = openFileDialog.ShowDialog(); string filePath = @"C:\test\continents.shp"; if (result == DialogResult.Yes || result == DialogResult.OK) { filePath = openFileDialog.FileName; } var dataSource = driver.Open(filePath, 0); }
public static void getHM(string DSMfile, string oriShp) { Console.WriteLine("开始提取建筑高度!"); Stopwatch aTime = new Stopwatch(); aTime.Start(); Ogr.RegisterAll(); Gdal.AllRegister(); OSGeo.OGR.Driver dr = Ogr.GetDriverByName("ESRI shapefile"); DataSource oriDs = dr.Open(oriShp, 0); Layer oriLayer = oriDs.GetLayerByIndex(0); //判断原文件中是否有以下字段,没有就创建 if (oriLayer.FindFieldIndex("MIN", 1) == -1) { FieldDefn min = new FieldDefn("MIN", FieldType.OFTReal); oriLayer.CreateField(min, 1); } if (oriLayer.FindFieldIndex("MAX", 1) == -1) { FieldDefn max = new FieldDefn("MAX", FieldType.OFTReal); oriLayer.CreateField(max, 1); } if (oriLayer.FindFieldIndex("HIGHT", 1) == -1) { FieldDefn hight = new FieldDefn("HIGHT", FieldType.OFTReal); oriLayer.CreateField(hight, 1); } int featCount = oriLayer.GetFeatureCount(0); tickTime = 0; oriDs.Dispose(); for (int i = 0; i < featCount; i++) { multi(DSMfile, oriShp, i); } while (tickTime < featCount) { Console.WriteLine("提取建筑高度,当前完成:{0}/{1}.", tickTime, featCount); Thread.Sleep(2000); } aTime.Stop(); Console.WriteLine("建筑高度值提取完成!用时:{0}", aTime.Elapsed.ToString()); MessageBox.Show("提取高度完成!"); }
/// <summary> /// 清理原文件 /// </summary> /// <param name="filePath"></param> public static void claenPoint(string filePath, double jiaodu, int cishu) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource oriDs = dr.Open(filePath, 1); OSGeo.OGR.Layer oriLayer = oriDs.GetLayerByIndex(0); int featCount = oriLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { OSGeo.OGR.Feature oriFeat = oriLayer.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); oriLayer.SetFeature(oriFeat); } Console.WriteLine("cleanPoint已完成{0}/{1}", i, featCount); oriFeat.Dispose(); } oriDs.Dispose(); }
//get a ogr datasorce by path string private static OSGeo.OGR.DataSource GetOGRDataSource(SHPGeoDataBase thisdb, String path) { OSGeo.OGR.Ogr.RegisterAll(); //to support chinese path OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); //to support chinese field name OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", ""); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); if (dr == null) { return(null); } //第二参数为0表明只读模式 OSGeo.OGR.DataSource ds = dr.Open(path, 0); thisdb.iDses.Add(path, ds); return(ds); }
public static void ConvertSHPtoGJSON(string shapefilePath, out StringBuilder gJSON) { string shpPath = shapefilePath; Ogr.RegisterAll(); OSGeo.OGR.Driver drv = Ogr.GetDriverByName("ESRI Shapefile"); var ds = drv.Open(shpPath, 0); /* * Driver geoJSONDriver = Ogr.GetDriverByName("GeoJSON"); * * string geojsonfilepath = @"c:\temp\us_counties_test.json"; * * if (System.IO.File.Exists(geojsonfilepath)) * System.IO.File.Delete(geojsonfilepath); * * geoJSONDriver.CreateDataSource(@"c:\temp\us_counties_test.json", null); */ OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); OSGeo.OGR.Feature f; layer.ResetReading(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); var options = new[] { "-f", "GeoJSON", "-lco", "RFC7946=YES" }; while ((f = layer.GetNextFeature()) != null) { var geom = f.GetGeometryRef(); if (geom != null) { var geometryJson = geom.ExportToJson(options); sb.AppendLine(geometryJson); } } gJSON = sb; }
public static string addPdx(string point, string inDSM, int lev) { //读取点ID OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource pointDs = dr.Open(point, 0); OSGeo.OGR.Layer pointLayer = pointDs.GetLayerByIndex(0); int pointCount = pointLayer.GetFeatureCount(0); for (int i = 0; i < pointCount; i++) { pointIds.Add(i); } //提升分辨率,计算坡度图 List <string> subSlop = new List <string>(); string upDataDSM = StaticTools.tempFilePath("img", "UpDataDSM"); CutImg.ImgReProject(inDSM, upDataDSM, 2); string addSolpeMap = StaticTools.tempFilePath("img", "addSolpeMap"); SlopeDem.Instance.Sloping(upDataDSM, addSolpeMap); //循环坡度下限值 for (int i = 0; i < lev; i++) { //没点了就退出 if (pointIds.Count == 0) { Console.WriteLine("所有目标都已找到匹配元素!"); break; } Console.WriteLine("【开始第{0}次循环,还有{1}个点待提取!】", i, pointIds.Count); string _outShpPath = StaticTools.tempFilePath("shp", "SlopAdd" + i.ToString() + "Times"); GetOutlines.Instance.MyGetOutlines(upDataDSM, addSolpeMap, _outShpPath, 80 - i * 5); 补充要素.AddFeatuer.selectFromPoint(point, _outShpPath); subSlop.Add(_outShpPath); } Console.WriteLine("还有{0}个目标没有找到对应要素!", pointIds.Count.ToString()); return(OutLine.CutImg.zhengHePdx(subSlop, 10)); }
/// <summary>初始数据源 /// </summary> /// <param name="strFilename">文件名</param> /// <param name="oFileEncoding">文件编码类型</param> /// <param name="nLayerindex">层序号</param> /// <param name="bUpdata">是否可以修改</param> /// <returns></returns> public virtual bool InitDataSource(string strFilename, Encoding oFileEncoding, int nLayerindex = 0, bool bUpdata = false) { if (_Driver == null) { return(false); } if (null == strFilename || strFilename.Length <= 3) { return(false); } //清除图层及数据源 if (_Layer != null) { _Layer.Dispose(); _Layer = null; } if (_DataSource != null) { _DataSource.Dispose(); _DataSource = null; } _Feilds = new Dictionary <string, FieldType>(); _FeatureCount = 0; _Coordiantes = ""; _FileEncoding = oFileEncoding; //载入数据源 _DataSource = _Driver.Open(strFilename, Convert.ToInt32(bUpdata)); if (null == _DataSource) { return(false); } //载入图层 if (nLayerindex >= 0) { return(this.InitLayer(nLayerindex)); } return(true); }
public RasterRepair(string imgPath, string shpPath, Action callback = null) { _imgPath = imgPath; _shpPath = shpPath; double[] geoTransform = new double[6]; Dataset imgDs = Gdal.Open(_imgPath, Access.GA_Update); imgDs.GetGeoTransform(geoTransform); Band _band = imgDs.GetRasterBand(1); OSGeo.GDAL.Driver d = Gdal.GetDriverByName("HFA"); //Dataset dsout = d.Create(_orderPath, _band.XSize, _band.YSize, 1, DataType.GDT_Float32, null); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); DataSource ds = dr.Open(_shpPath, 0); Layer lyr = ds.GetLayerByIndex(0); thr tr = new thr(imgDs, imgPath, geoTransform, lyr); tr.Repair(callback); }
public string getSHPLayer(string filename) { if (null == filename || filename.Length <= 3) { oLayer = null; return(null); } if (oDriver == null) { // MessageBox.Show("文件不能打开,请检查"); } DataSource ds = oDriver.Open(filename, 1); if (null == ds) { oLayer = null; return(null); } int position = filename.LastIndexOf("\\"); string tempName = filename.Substring(position + 1, filename.Length - position - 4 - 1); //获取图层数目 int layerCount = ds.GetLayerCount(); if (layerCount > 1) { // MessageBox.Show("该SHP文件不止一个图层"); } //根据名字索引图层 oLayer = ds.GetLayerByName(tempName); if (oLayer == null) { ds.Dispose(); return(null); } return(tempName); }
public bool CreateFromSHP(string shpfile) { Ogr.RegisterAll(); OSGeo.OGR.Driver dr = Ogr.GetDriverByName("ESRI shapefile"); DataSource ds = dr.Open(shpfile, 0); Layer layer = ds.GetLayerByIndex(0); List <double> points = new List <double>(); //判断数据是否可用 int FeatCount = layer.GetFeatureCount(0); wkbGeometryType geoType = layer.GetLayerDefn().GetGeomFieldDefn(0).GetFieldType(); if (FeatCount > 2 && geoType.ToString().Contains("wkbPoint")) { int indexZ = layer.GetLayerDefn().GetFieldIndex("Z"); if (indexZ > -1) //优先使用 Z 字段 { for (int i = 0; i < FeatCount - 1; i++) { points.Add(layer.GetFeature(i).GetGeometryRef().GetX(0)); points.Add(layer.GetFeature(i).GetGeometryRef().GetY(0)); points.Add(layer.GetFeature(i).GetFieldAsDouble(indexZ)); } } else if (geoType == wkbGeometryType.wkbPoint25D) { for (int i = 0; i < FeatCount; i++) { points.Add(layer.GetFeature(i).GetGeometryRef().GetX(0)); points.Add(layer.GetFeature(i).GetGeometryRef().GetY(0)); points.Add(layer.GetFeature(i).GetGeometryRef().GetZ(0)); } } } ds.Dispose(); return(CreateMesh(points.ToArray())); }