/// <summary> /// 标准差 /// </summary> /// <param name="dzxLayer"></param> /// <param name="aue"></param> /// <param name="bzc"></param> public static void getBZC(OSGeo.OGR.Layer dzxLayer, out double aue, out double bzc) { //获取Featuer数 int featCount = dzxLayer.GetFeatureCount(0); // 1 拿到每个Featuer的Value double[] values = new double[featCount]; for (int i = 0; i < featCount; i++) { OSGeo.OGR.Feature fileFeat = dzxLayer.GetFeature(i); values[i] = fileFeat.GetFieldAsDouble("EVE"); fileFeat.Dispose(); } // 2 求Values的平均值 aue = values.Average(); // 3 求values与平均值差的平方和 double pingFangHe = 0; for (int i = 0; i < featCount; i++) { pingFangHe += (values[i] - aue) * (values[i] - aue); } // 4 每个值与平均值的差相加,除Featuer数.再开方,得到标准差 bzc = Math.Sqrt(pingFangHe / featCount); }
/// <summary> /// Ogr to DotSpatial at layer level /// </summary> /// <param name="layer">Ogr Layer</param> /// <returns>DotSpatial IFeatureSet</returns> public static DotSpatial.Data.IFeatureSet Ogr2DSLayer(OSGeo.OGR.Layer layer) { DotSpatial.Data.IFeatureSet featureSet = new DotSpatial.Data.FeatureSet(); featureSet.FeatureType = Ogr2DSGeometryType(layer.GetGeomType()); featureSet.DataTable = Ogr2DSDataTable(layer.GetLayerDefn()); featureSet.Projection = Ogr2DSProjection(layer.GetSpatialRef()); for (int i = 0; i < layer.GetFeatureCount(0); i++) { OSGeo.OGR.Geometry geometry = layer.GetFeature(i).GetGeometryRef(); DotSpatial.Data.IFeature feature = new DotSpatial.Data.Feature(Ogr2DSGeometry(geometry)); OSGeo.OGR.Feature ogrFeature = layer.GetFeature(i); feature.DataRow = featureSet.DataTable.NewRow(); for (int j = 0; j < layer.GetLayerDefn().GetFieldCount(); j++) { object value = GetOgrValue(ogrFeature, j); if (value == null) { value = DBNull.Value; } feature.DataRow[j] = value; } featureSet.Features.Add(feature); } return(featureSet); }
/// <summary> /// 获取Feature的最大值和最小值 范围不准 /// </summary> /// <param name="dsmDs"></param> /// <param name="oriFeat"></param> /// <param name="bufFeat"></param> /// <param name="subRasterOff_Size"></param> private static void getMaxMinValue(OSGeo.GDAL.Dataset dsmDs, OSGeo.OGR.Feature oriFeat, OSGeo.OGR.Feature bufFeat, int[] subRasterOff_Size) { //拿到当前Feature对应的栅格值(数组) double[] rastValue = new double[subRasterOff_Size[2] * subRasterOff_Size[3]]; //把SUBimg的值读进数组 lock (dsmDs) { dsmDs.GetRasterBand(1).ReadRaster( subRasterOff_Size[0], subRasterOff_Size[1], subRasterOff_Size[2], subRasterOff_Size[3], rastValue, subRasterOff_Size[2], subRasterOff_Size[3], 0, 0); } double max = rastValue.Max(); double min = rastValue.Min(); double cay = max - min; oriFeat.SetField("TEMP", max - min); oriFeat.SetField("MIN", 0); oriFeat.SetField("MAX", min); oriFeat.SetField("HIGHT", max); }
static void getMinOutLineFromLayerToLayer(OSGeo.OGR.Layer inLayer) { shpDataSet.deleteLayerByName("minOutLine"); OSGeo.OGR.Layer minoutline = shpDataSet.CreateLayer("minOutLine", inLayer.GetSpatialRef(), inLayer.GetGeomType(), null); OSGeo.OGR.Feature aFeat = null; StaticTools.msgLine("getMinOutline..."); int featCount = inLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { aFeat = inLayer.GetFeature(i); OSGeo.OGR.Geometry oGeometry = aFeat.GetGeometryRef(); OSGeo.OGR.Geometry ogeo = oGeometry.GetGeometryRef(0); QTGeometry qtGeo = new QTGeometry(ogeo); minoutline.CreateFeature(qtGeo.GetSMBR()); StaticTools.progress((i + 1) * 100 / featCount, $"{i} / {featCount}"); } minoutline.Dispose(); //inLayer.ResetReading(); //while ((aFeat = inLayer.GetNextFeature()) != null) //{ // OSGeo.OGR.Geometry oGeometry = aFeat.GetGeometryRef(); // OSGeo.OGR.Geometry ogeo = oGeometry.GetGeometryRef(0); // QTGeometry qtGeo = new QTGeometry(ogeo); // minoutline.CreateFeature(qtGeo.GetSMBR()); //} }
/// <summary> /// 将OSGeo.OGR.Feature转为Polygon /// </summary> /// <param name="feature">OSGeo.OGR.Feature对象</param> /// <returns>Polygon对象</returns> private List <Polygon> ConvertFeature2Polygon(OSGeo.OGR.Feature feature) { List <Polygon> polys = new List <Polygon>(); if (feature != null) { ConvertFromFeature convert = new ConvertFromFeature(); GeneralGeometry geo = convert.Feature2Geometry(feature); switch (geo.GeometryType) { case WKBGeometryType.wkbPolygon: Polygon poly = geo.MyPolygon; poly.Id = _id++; polys.Add(poly); break; case WKBGeometryType.wkbMultiPolygon: polys = geo.MyMultiPolygon; for (int i = 0; i < polys.Count; i++) { polys[i].Id = _id++; } break; default: break; } } return(polys); }
/// <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(); }
/*************************************************************************************************/ /***************************读入SHP文件***************************/ /// <summary> /// shpPolygon To List<Point[]> /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static List <Point[]> shpPolygonToPointGL(string filePath) { List <Point[]> allFeature = new List <Point[]>(); 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 fileLayer = ds.GetLayerByIndex(0); int FeatCount = fileLayer.GetFeatureCount(0); for (int u = 0; u < FeatCount; u++) { OSGeo.OGR.Feature fileFeature = fileLayer.GetFeature(u); OSGeo.OGR.Geometry fileGeom = fileFeature.GetGeometryRef(); OSGeo.OGR.Geometry subGeom = fileGeom.GetGeometryRef(0); int k = subGeom.GetPointCount(); Point[] aFeat = new Point[k]; for (int i = 0; i < k; i++) { aFeat[i].X = subGeom.GetX(i); aFeat[i].Y = subGeom.GetY(i); aFeat[i].Z = subGeom.GetZ(i); } allFeature.Add(aFeat); } return(allFeature); }
public static object GetFieldValue(this OSGeo.OGR.Feature feature, string fieldName) { if (feature == null || string.IsNullOrEmpty(fieldName)) { throw new Exception("参数设置错误"); } using var fieldDefn = feature.GetFieldDefnRef(fieldName); object value; switch (fieldDefn.GetFieldType()) { case OSGeo.OGR.FieldType.OFTString: value = feature.GetFieldAsString(fieldName); break; case OSGeo.OGR.FieldType.OFTInteger: value = feature.GetFieldAsInteger(fieldName); break; case OSGeo.OGR.FieldType.OFTInteger64: value = feature.GetFieldAsInteger64(fieldName); break; case OSGeo.OGR.FieldType.OFTReal: value = feature.GetFieldAsDouble(fieldName); break; default: throw new NotImplementedException(); } return(value); }
public static object GetFieldValue(this OSGeo.OGR.Feature feature, int index) { if (feature == null || index < 0 || index >= feature.GetFieldCount()) { throw new Exception("参数设置错误"); } using var fieldDefn = feature.GetFieldDefnRef(index); object value; switch (fieldDefn.GetFieldType()) { case OSGeo.OGR.FieldType.OFTString: value = feature.GetFieldAsString(index); break; case OSGeo.OGR.FieldType.OFTInteger: value = feature.GetFieldAsInteger(index); break; case OSGeo.OGR.FieldType.OFTInteger64: value = feature.GetFieldAsInteger64(index); break; case OSGeo.OGR.FieldType.OFTReal: value = feature.GetFieldAsDouble(index); break; default: throw new NotImplementedException(); } return(value); }
private static void _标准差(string dzx, out double aue, out double bzc) { 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); //获取Featuer数 int featCount = dzxLayer.GetFeatureCount(0); // 1 拿到每个Featuer的Value double[] values = new double[featCount]; for (int i = 0; i < featCount; i++) { OSGeo.OGR.Feature fileFeat = dzxLayer.GetFeature(i); OSGeo.OGR.Geometry fileGeom = fileFeat.GetGeometryRef(); values[i] = fileFeat.GetFieldAsDouble("EVE"); fileGeom.Dispose(); fileFeat.Dispose(); } dzxDS.Dispose(); // 2 求Values的平均值 aue = values.Average(); // 3 求values与平均值差的平方和 double pingFangHe = 0; for (int i = 0; i < featCount; i++) { pingFangHe += (values[i] - aue) * (values[i] - aue); } // 4 每个值与平均值的差相加,除Featuer数.再开方,得到标准差 bzc = Math.Sqrt(pingFangHe / featCount); }
//分块清理,提高效率 public static OSGeo.OGR.Layer cleanLayer_Cut(OSGeo.OGR.Layer pdLayer, double minArea, double maxArea, double maxCha = 1) { List <CutBox> Tree = buildTree(pdLayer); shpDataSet.deleteLayerByName("pdClear"); OSGeo.OGR.Layer outLayer = shpDataSet.CreateLayer("pdClear", pdLayer.GetSpatialRef(), pdLayer.GetGeomType(), null); OSGeo.OGR.Envelope oriEnve = new OSGeo.OGR.Envelope(); OSGeo.OGR.Envelope nextEnve = new OSGeo.OGR.Envelope(); StaticTools.msgLine("clean SlopeLine..."); for (int c = 0; c < Tree.Count; c++) { CutBox cb = Tree[c]; int featCount = cb.pdxIDs.Count; for (int i = 0; i < featCount - 1; i++) { bool isOnly = true; OSGeo.OGR.Feature ori = pdLayer.GetFeature(cb.pdxIDs[i]); double oriArea = ori.GetGeometryRef().GetArea(); if (oriArea < minArea || oriArea > maxArea) { //判断当前要素的面积,过小或过大时,认为其有相同的图形,不加入新的Layer isOnly = false; } else { //判断当前要素与其它要素的包围盒的对应边,差值都小于阈值时,认为是相同的图形,需要跳过 //没有相同的图型时,isOnly为true ,塞到新的layer里 ori.GetGeometryRef().GetEnvelope(oriEnve); for (int j = i + 1; j < featCount; j++) { pdLayer.GetFeature(cb.pdxIDs[j]).GetGeometryRef().GetEnvelope(nextEnve); if (Math.Abs(oriEnve.MaxX - nextEnve.MaxX) < maxCha && Math.Abs(oriEnve.MaxY - nextEnve.MaxY) < maxCha && Math.Abs(oriEnve.MinX - nextEnve.MinX) < maxCha && Math.Abs(oriEnve.MinY - nextEnve.MinY) < maxCha) { isOnly = false; break; } } } if (isOnly) { outLayer.CreateFeature(ori); } StaticTools.progress((i + 2) * 100 / featCount, $"{i + 1} / {featCount}"); } } if (IsDelete) { shpDataSet.deleteLayerByName(pdLayer.GetName()); } return(outLayer); }
public static OSGeo.OGR.Feature ToFeature(this IFeature feature) { OSGeo.OGR.Feature destFeature = null; if (feature is GdalFeature gdalFeature) { destFeature = gdalFeature.Feature; } return(destFeature); }
public static IFeature ToFeature(this OSGeo.OGR.Feature feature) { IFeature destFeature = null; if (feature != null) { destFeature = new GdalFeature(feature); } return(destFeature); }
/// <summary> /// Returns a FeatureDataRow based on a RowID /// </summary> /// <param name="rowId"></param> /// <returns>FeatureDataRow</returns> public FeatureDataRow GetFeature(uint rowId) { FeatureDataTable fdt = new FeatureDataTable(); _ogrLayer.ResetReading(); ReadColumnDefinition(fdt, _ogrLayer); OgrFeature feature = _ogrLayer.GetFeature((int)rowId); return(OgrFeatureToFeatureDataRow(fdt, feature)); }
private static void getH(OSGeo.OGR.Layer oriLayer) { //创建一个BUFFER,BUFFER距离为1米 OSGeo.OGR.Layer bufLayer = bufferFile(oriLayer, 1); //判断原文件中是否有以下字段,没有就创建 if (oriLayer.FindFieldIndex("MIN", 1) == -1) { OSGeo.OGR.FieldDefn min = new OSGeo.OGR.FieldDefn("MIN", OSGeo.OGR.FieldType.OFTReal); oriLayer.CreateField(min, 1); } if (oriLayer.FindFieldIndex("MAX", 1) == -1) { OSGeo.OGR.FieldDefn max = new OSGeo.OGR.FieldDefn("MAX", OSGeo.OGR.FieldType.OFTReal); oriLayer.CreateField(max, 1); } if (oriLayer.FindFieldIndex("HIGHT", 1) == -1) { OSGeo.OGR.FieldDefn hight = new OSGeo.OGR.FieldDefn("HIGHT", OSGeo.OGR.FieldType.OFTReal); oriLayer.CreateField(hight, 1); } if (oriLayer.FindFieldIndex("TEMP", 1) == -1) { OSGeo.OGR.FieldDefn hight = new OSGeo.OGR.FieldDefn("TEMP", OSGeo.OGR.FieldType.OFTReal); oriLayer.CreateField(hight, 1); } double[] transfrom = new double[6]; dsmDataset.GetGeoTransform(transfrom); int allX = dsmDataset.RasterXSize; int allY = dsmDataset.RasterYSize; StaticTools.msgLine("getH..."); //开始计算每个Feature需要读取的栅格参数 int featCount = oriLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { OSGeo.OGR.Feature oriFeat = oriLayer.GetFeature(i); OSGeo.OGR.Feature bufFeat = bufLayer.GetFeature(i); int[] subRasterOff_Size = subRasterInfo(transfrom, allX, allY, bufFeat); // getMaxMinValue(dsmDataset, oriFeat, bufFeat, subRasterOff_Size); oriLayer.SetFeature(oriFeat); oriFeat.Dispose(); bufFeat.Dispose(); StaticTools.progress((i + 1) * 100 / featCount, $"{i} / {featCount}"); } if (IsDelete) { shpDataSet.deleteLayerByName(bufLayer.GetName()); } }
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); }
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> /// 根据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); }
/******************************** 等值线转换为多边形 ***********************************************/ /// <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); }
/// <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); } }
/// <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(); }
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); }
/// <summary> /// 清理Layer中重复的,面积过小的featuer,返回一个新的Layer /// </summary> /// <param name="inLayer">源layer</param> /// <param name="andArea">是否判断最小面积</param> /// <param name="minArea">最小面积</param> /// <returns></returns> public static OSGeo.OGR.Layer cleanLayer_FF(OSGeo.OGR.Layer inLayer) { string oldLayerName = inLayer.GetName(); string newLayerName = oldLayerName + "Clear"; shpDataSet.deleteLayerByName(newLayerName); OSGeo.OGR.Layer outLayer = shpDataSet.CreateLayer(newLayerName, srs, inLayer.GetGeomType(), null); int featCount = inLayer.GetFeatureCount(0); StaticTools.msgLine($"clean resLine... before {featCount}"); for (int i = 0; i < featCount - 1; i++) { bool isOnly = true; OSGeo.OGR.Feature ori = inLayer.GetFeature(i); OSGeo.OGR.Envelope oriEnve = new OSGeo.OGR.Envelope(); ori.GetGeometryRef().GetEnvelope(oriEnve); double maxCha = 1; for (int j = i + 1; j < featCount; j++) { OSGeo.OGR.Feature next = inLayer.GetFeature(j); OSGeo.OGR.Envelope nextEnve = new OSGeo.OGR.Envelope(); next.GetGeometryRef().GetEnvelope(nextEnve); if (Math.Abs(oriEnve.MaxX - nextEnve.MaxX) < maxCha && Math.Abs(oriEnve.MaxY - nextEnve.MaxY) < maxCha && Math.Abs(oriEnve.MinX - nextEnve.MinX) < maxCha && Math.Abs(oriEnve.MinY - nextEnve.MinY) < maxCha) { isOnly = false; break; } } if (isOnly) { outLayer.CreateFeature(ori); } ori.Dispose(); StaticTools.progress((i + 2) * 100 / featCount, $"{i + 1} / {featCount}"); } if (IsDelete) { shpDataSet.deleteLayerByName(inLayer.GetName()); } StaticTools.msgLine($"clean resLine... after {outLayer.GetFeatureCount(0)}"); return(outLayer); }
public OGRCursor(OGRDataset parent, String whereClause, System.Array esriQueryFieldMap, IEnvelope env = null, int onlyOneID = -1) { m_isFinished = false; m_pDataset = parent; m_whereClause = whereClause; if (m_whereClause != null && m_whereClause.Length > 0) { m_pDataset.ogrLayer.SetAttributeFilter(m_whereClause); } else { m_pDataset.ogrLayer.SetAttributeFilter(null); } m_envelope = env; if (m_envelope != null) { m_envelope.Project(m_pDataset.SpatialReference); m_pDataset.ogrLayer.SetSpatialFilterRect(m_envelope.XMin, m_envelope.YMin, m_envelope.XMax, m_envelope.YMax); } else { m_pDataset.ogrLayer.SetSpatialFilter(null); } m_esriQueryFieldMap = esriQueryFieldMap; m_pDataset.ogrLayer.ResetReading(); m_currentOGRFeature = null; m_onlyOneID = onlyOneID; if (m_onlyOneID == -1) { NextRecord(); } else { RetrieveOnlyOneRecord(); } }
/// <summary> /// Spatial filter using OGR /// </summary> /// <param name="featureSet">FeatureSet</param> /// <param name="geometry">Spatial geometry</param> /// <returns>Indexs of selected features</returns> public static List <int> OgrSpatialFilter(IFeatureSet featureSet, IGeometry geometry) { List <int> result = new List <int>(); OSGeo.OGR.Layer layer = GIS.GDAL.VectorConverter.DS2OrgLayer(featureSet); OSGeo.OGR.Geometry geo = GIS.GDAL.VectorConverter.DS2OgrGeometry(geometry, featureSet); layer.SetSpatialFilter(geo); OSGeo.OGR.Feature feature = layer.GetNextFeature(); while (feature != null) { result.Add(Convert.ToInt32(feature.GetFID())); feature = layer.GetNextFeature(); } return(result); }
/// <summary> /// 清理等值线上的点 /// </summary> /// <param name="filePath"></param> public static void claenPoint(string filePath) { 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(filePath.Substring(0, filePath.LastIndexOf(".")) + "_targ.shp")) { // UsefullTools.deleteFiles(filePath.Substring(0, filePath.LastIndexOf(".")) + "_targ.shp"); } OSGeo.OGR.DataSource targDs = dr.CreateDataSource(filePath.Substring(0, filePath.LastIndexOf(".")) + "_targ.shp", 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 OSGeo.OGR.Feature newFeat = JID(aFeat); if (newFeat != null) { targLayer.CreateFeature(newFeat); } } oriDs.Dispose(); targDs.Dispose(); }
public void NextRecord() { if (m_isFinished) //error already thrown once { return; } m_currentOGRFeature = m_pDataset.ogrLayer.GetNextFeature(); if (m_currentOGRFeature == null) { m_isFinished = true; throw new COMException("End of OGR Plugin cursor", S_FALSE); } return; }
/// <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(); }
/************************************** 清理等值线 *****************************************/ /// <summary> /// 通过线长,值 清理等值线 /// </summary> /// <param name="filePath"></param> public static string cleanDS(string dzx) { Console.WriteLine("开始清理等值线!"); double aue, bzc; _标准差(dzx, out aue, out bzc); double minLength = 50; double maxLength = 2600; double minValue = aue - bzc * 2; double maxValue = aue + bzc * 2; //open dzx 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); //new a shp string cleanline = StaticTools.tempFilePath("shp", "清理后的等值线"); OSGeo.OGR.DataSource newdzxDS = dr.CreateDataSource(cleanline, null); OSGeo.OGR.Layer newdzxLayer = newdzxDS.CreateLayer(dzxLayer.GetName(), dzxLayer.GetSpatialRef(), dzxLayer.GetGeomType(), null); for (int i = 0; i < dzxLayer.GetFeatureCount(0); i++) { OSGeo.OGR.Feature fileFeat = dzxLayer.GetFeature(i); OSGeo.OGR.Geometry fileGeom = fileFeat.GetGeometryRef(); double FeatLength = fileGeom.Length(); bool s1 = FeatLength > minLength && FeatLength < maxLength; double featValue = fileFeat.GetFieldAsDouble("EVE"); bool s2 = featValue > minValue && featValue < maxValue; bool isR = fileGeom.IsRing(); if (s1 && s2 && isR) { newdzxLayer.CreateFeature(fileFeat); } fileFeat.Dispose(); } newdzxDS.Dispose(); dzxDS.Dispose(); Console.WriteLine("清理等值线完成!"); return(cleanline); }
/// <summary> /// 判断两个Featuer是否重复,两外接矩形相同位置的边差小于1时为true /// </summary> /// <param name="ori"></param> /// <param name="next"></param> /// <returns></returns> public static bool isSame(OSGeo.OGR.Feature ori, OSGeo.OGR.Feature next, double maxCha = 1) { OSGeo.OGR.Envelope oriEnve = new OSGeo.OGR.Envelope(); ori.GetGeometryRef().GetEnvelope(oriEnve); OSGeo.OGR.Envelope nextEnve = new OSGeo.OGR.Envelope(); next.GetGeometryRef().GetEnvelope(nextEnve); if (Math.Abs(oriEnve.MaxX - nextEnve.MaxX) < maxCha && //外接矩形差 Math.Abs(oriEnve.MaxY - nextEnve.MaxY) < maxCha && Math.Abs(oriEnve.MinX - nextEnve.MinX) < maxCha && Math.Abs(oriEnve.MinY - nextEnve.MinY) < maxCha) { return(true); } else { return(false); } }
public OGRCursor(OGRDataset parent, String whereClause, System.Array esriQueryFieldMap, IEnvelope env = null, int onlyOneID = -1) { m_isFinished = false; m_pDataset = parent; m_whereClause = whereClause; if (m_whereClause != null && m_whereClause.Length > 0) m_pDataset.OGRLayer.SetAttributeFilter(m_whereClause); else m_pDataset.OGRLayer.SetAttributeFilter(null); m_envelope = env; if (m_envelope != null) { m_envelope.Project(m_pDataset.SpatialReference); m_pDataset.OGRLayer.SetSpatialFilterRect(m_envelope.XMin, m_envelope.YMin, m_envelope.XMax, m_envelope.YMax); } else { m_pDataset.OGRLayer.SetSpatialFilter(null); } m_esriQueryFieldMap = esriQueryFieldMap; m_pDataset.OGRLayer.ResetReading(); m_currentOGRFeature = null; m_onlyOneID = onlyOneID; if (m_onlyOneID == -1) NextRecord(); else { RetrieveOnlyOneRecord(); } }
public void RetrieveOnlyOneRecord() { m_currentOGRFeature = m_pDataset.OGRLayer.GetFeature(m_onlyOneID); m_isFinished = true; }
public void NextRecord() { if (m_isFinished) //error already thrown once return; m_currentOGRFeature = m_pDataset.OGRLayer.GetNextFeature(); if (m_currentOGRFeature == null) { m_isFinished = true; throw new COMException("End of OGR Plugin cursor", S_FALSE); } return; }
/// <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(); }