/// <summary> /// 将wkb对象转换为Geometry对象 /// </summary> /// <param name="wkb"></param> /// <returns></returns> public static IGeometry ConvertWkbToGeometry(byte[] wkb) { IGeometry geom; int countin = wkb.GetLength(0); IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3; factory.CreateGeometryFromWkbVariant(wkb, out geom, out countin); return(geom); }
private void button1_Click(object sender, EventArgs e) { //当确定按钮被点击时 //连接数据库,路径规划 //string sqlstr = "select ST_asbinary(ST_Union(geom)) as route from pgr_fromAtoB('whu_road_feature2line'::text,114.353,30.539,114.367,30.544) ;"; string sqlstr = null; if ((startX * startY * endX * endY) != 0) { sqlstr = "select ST_asbinary(ST_Union(geom)) as route from pgr_fromAtoB('whu_road_feature2line'::text," + startX + "," + startY + "," + endX + "," + endY + ") ;"; } else { MessageBox.Show("起始点或终止点不能为空"); return; } Byte[] routeWKB = DAO.executeRouteQuery(sqlstr); IGeometry geom; int countin = routeWKB.GetLength(0); //地图容器,创建临时元素 IMap pMap = mMapControl.Map; IActiveView pActiveView = pMap as IActiveView; IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer; if (pElement != null) { pGraphicsContainer.DeleteElement(pElement); } //转换wkb为IGeometry IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3; factory.CreateGeometryFromWkbVariant(routeWKB, out geom, out countin); IPolyline pLine = (IPolyline)geom; //定义要素symbol ISimpleLineSymbol pLineSym = new SimpleLineSymbol(); IRgbColor pColor = new RgbColor(); pColor.Red = 11; pColor.Green = 120; pColor.Blue = 233; pLineSym.Color = pColor; pLineSym.Style = esriSimpleLineStyle.esriSLSSolid; pLineSym.Width = 2; //线元素symbol绑定 ILineElement pLineElement = new LineElementClass(); pLineElement.Symbol = pLineSym; //添加geom pElement = pLineElement as IElement; pElement.Geometry = pLine; //加入地图并刷新 pGraphicsContainer.AddElement(pElement, 0); pActiveView.Refresh(); //object symbol = pLineSym as object; //mMapControl.DrawShape(pLine, ref symbol); }
/// <summary> /// /// </summary> /// <param name="dbGeometry"></param> /// <returns></returns> public static IGeometry dbGeometryToGeometry(DbGeometry dbGeometry) { if (dbGeometry == null) { return(null); } byte[] wkb = dbGeometry.AsBinary(); IGeometry geomOut = new GeometryBag(); IGeometryFactory gf = new GeometryEnvironment() as IGeometryFactory; int readCount = 0; gf.CreateGeometryFromWkbVariant(wkb, out geomOut, out readCount); return(geomOut); }
private void LoadFeatureclass(IFeatureClass featureclass) { try { SpatialLiteDB db = new SpatialLiteDB(this.DatabasePath); db.Open(); string sql = "SELECT AsBinary(" + Table.GeometryColumnName + ") as wkb,* FROM " + this.TableName; using (SQLiteCommand command = new SQLiteCommand(sql, db.SQLiteDatabaseConn)) { using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(command)) { System.Data.DataTable table = new System.Data.DataTable(); adapter.Fill(table); IFeatureCursor cursor = featureclass.Insert(true); foreach (DataRow row in table.Rows) { IFeatureBuffer feature = featureclass.CreateFeatureBuffer(); for (int i = 0; i < feature.Fields.FieldCount; i++) { IField field = feature.Fields.get_Field(i); Debug.WriteLine("FieldName=" + field.Name); if (field.Type != esriFieldType.esriFieldTypeGeometry & !field.Name.Equals("wkb", StringComparison.CurrentCultureIgnoreCase) & !field.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase) & !field.Name.Equals(featureclass.OIDFieldName, StringComparison.CurrentCultureIgnoreCase)) { try { Debug.WriteLine("Spatialite Field Value: " + row[field.Name].ToString()); feature.set_Value(i, row[field.Name]); } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); } } else if (field.Type == esriFieldType.esriFieldTypeGeometry) { byte[] wkb = (byte[])row["wkb"]; int bytesRead; object missing = Type.Missing; IGeometry outGeometry; object byteArrayObject = row["wkb"]; IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3; factory.CreateGeometryFromWkbVariant(byteArrayObject, out outGeometry, out bytesRead); if (outGeometry != null) { try { feature.Shape = outGeometry; } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); } } } } cursor.InsertFeature(feature); } } } db.Close(); } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); throw; } }