public static GeometryType ToGeometryType(this wkbGeometryType wkbGeometryType) { GeometryType geometryType = GeometryType.Unknown; string name = wkbGeometryType.ToString().Replace("wkb", ""); Enum.TryParse(name, out geometryType); return(geometryType); }
protected override async Task _draw() { RecordSet layer = GetMetadata(); if (layer.Properties.BBox != null) { features.SetSpatialFilterRect(layer.Properties.BBox[0], layer.Properties.BBox[1], layer.Properties.BBox[2], layer.Properties.BBox[3]); } SetCrs(OgrReader.getSR(features, layer)); using (OgrReader ogrReader = new OgrReader()) { await ogrReader.GetFeaturesAsync(features); foreach (Feature feature in ogrReader.features) { int geoCount = feature.GetDefnRef().GetGeomFieldCount(); for (int j = 0; j < geoCount; j++) { Geometry point = feature.GetGeomFieldRef(j); wkbGeometryType type = point.GetGeometryType(); string t = type.ToString(); if (point.GetGeometryType() == wkbGeometryType.wkbPoint || point.GetGeometryType() == wkbGeometryType.wkbPoint25D || point.GetGeometryType() == wkbGeometryType.wkbPointM || point.GetGeometryType() == wkbGeometryType.wkbPointZM) { point.TransformWorld(GetCrs()).ToList <Vector3>().ForEach(async item => await _drawFeatureAsync(item, feature)); } else if (point.GetGeometryType() == wkbGeometryType.wkbMultiPoint || point.GetGeometryType() == wkbGeometryType.wkbMultiPoint25D || point.GetGeometryType() == wkbGeometryType.wkbMultiPointM || point.GetGeometryType() == wkbGeometryType.wkbMultiPointZM) { int n = point.GetGeometryCount(); for (int k = 0; k < n; k++) { Geometry Point2 = point.GetGeometryRef(k); Point2.TransformWorld(GetCrs()).ToList <Vector3>().ForEach(async item => await _drawFeatureAsync(item, feature)); } } point.Dispose(); } } } if (layer.Transform != null) { transform.position = AppState.instance.map.transform.TransformPoint(layer.Transform.Position); transform.rotation = layer.Transform.Rotate; transform.localScale = layer.Transform.Scale; } }
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())); }
private void button2_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "打开ShapeFile数据"; dlg.Filter = "ShapeFile数据(*.shp)|*.shp"; if (dlg.ShowDialog() == DialogResult.OK) { Ogr.RegisterAll(); string strVectorFile = dlg.FileName; textBox1.Text = strVectorFile; //打开数据 DataSource ds = Ogr.Open(strVectorFile, 0); if (ds == null) { listBox1.Items.Add(string.Format("打开文件【{0}】失败!", strVectorFile)); return; } listBox1.Items.Add(string.Format("打开文件【{0}】成功!", strVectorFile)); // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个 int iLayerCount = ds.GetLayerCount(); // 获取第一个图层 Layer oLayer = ds.GetLayerByIndex(0); if (oLayer == null) { listBox1.Items.Add(string.Format("获取第{0}个图层失败!\n", 0)); return; } // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); // 通过属性表的SQL语句对图层中的要素进行筛选,这部分详细参考SQL查询章节内容 //oLayer.SetAttributeFilter("\"NAME99\"LIKE \"北京市市辖区\""); // 通过指定的几何对象对图层中的要素进行筛选 //oLayer.SetSpatialFilter(); // 通过指定的四至范围对图层中的要素进行筛选 //oLayer.SetSpatialFilterRect(); // 获取图层中的属性表表头并输出 listBox1.Items.Add("属性表结构信息:"); FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); listBox1.Items.Add(string.Format("{0}:{1} ({2}.{3})", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision())); } // 输出图层中的要素个数 listBox1.Items.Add(string.Format("要素个数 = {0}", oLayer.GetFeatureCount(0))); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { Geometry geo = oFeature.GetGeometryRef(); wkbGeometryType wkb = geo.GetGeometryType(); listBox1.Items.Add(string.Format("当前处理第要素值:{0}", wkb.ToString())); string strGml = geo.ExportToGML(); listBox1.Items.Add(strGml); listBox1.Items.Add(string.Format("当前处理第{0}个: \n属性值:", oFeature.GetFID())); // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsString(iField))); break; case FieldType.OFTReal: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsDouble(iField))); break; case FieldType.OFTInteger: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsInteger(iField))); break; default: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsString(iField))); break; } } // 获取要素中的几何体 Geometry oGeometry = oFeature.GetGeometryRef(); // 为了演示,只输出一个要素信息 break; } listBox1.Items.Add("数据集关闭!"); } }