<P> hi,every one. I need read the spatial data in postgresql and transform it to shape file and show it by axmapcontrol in my project. My code can run but when i use arcmap open the new shape file i find it cannot show any graphics and the attribute table is empty. the size of the shape isnot 0k. I run same code but only once the shape file can show graphic and can be seen the arrtibute. The following is code and i wish you can help me find where is error.</P>
<P>public void GetGeometry(string sqltext)</P>
<P> {<BR> // open the postgis and read data according sqltext </P>
<P> try { <BR> <BR> try<BR> {<BR> Ogr.RegisterAll();<BR> }<BR> catch (Exception e)<BR> { MessageBox.Show(e.Message); }</P>
<P> try<BR> {<BR> OSGeo.OGR.DataSource pgconn = OSGeo.OGR.Ogr.Open ("PG:host=localhost dbname='stdb' user='stuser' password=123", 1);<BR> OSGeo.OGR.Layer myOGRLay = pgconn.ExecuteSQL(sqltext,null, "tt");<BR> FeatureDefn myDef = myOGRLay.GetLayerDefn();<BR> int TT = myDef.GetFieldCount();<BR> int ftcount = myOGRLay.GetFeatureCount(1);</P>
<P> OSGeo.OGR.DataSource shpconn;<BR> OSGeo.OGR.Driver shpdriver;<BR> shpdriver=Ogr.GetDriverByName("ESRI Shapefile");<BR> string path = Application.StartupPath + "<A href="file://\\database\\yantian2005.shp" target=_blank>\\database\\yantian2005.shp</A>";<BR> <BR> OSGeo.OGR.SpatialReference myspatial = new SpatialReference("");<BR> myspatial.ImportFromProj4("+proj=Transverse_Mercator +zone=48N+datum=GCS_WGS_1984");</P>
<P> shpconn = shpdriver.CreateDataSource(path, null);<BR> OSGeo.OGR.Layer myshplay = shpconn.CreateLayer("yantian2005", null , OSGeo.OGR.wkbGeometryType.wkbPolygon, null);<BR> if (myshplay == null)<BR> {<BR> MessageBox.Show("create layer failor!");<BR> }<BR> <BR> OSGeo.OGR.Feature myOGRFeat = null;</P>
<P> for (int i = 0; i < TT; i++)<BR> {<BR> FieldDefn fdef = myDef.GetFieldDefn(i);<BR> if (myshplay.CreateField(fdef, 1) == OSGeo.OGR.Ogr.OGRERR_NONE)<BR> {<BR> }<BR> } <BR> <BR> int count = 1;<BR> OSGeo.OGR.FeatureDefn myFDefn = myOGRLay.GetLayerDefn();<BR> while ((myOGRFeat = myOGRLay.GetNextFeature()) != null)<BR> {<BR> <BR> OSGeo.OGR.Feature myOGRft = null;<BR> myOGRft = myOGRLay.GetFeature(count);</P>
<P> OSGeo.OGR.Geometry myGeom = myOGRFeat.GetGeometryRef();<BR> myOGRft.SetGeometry(myGeom);</P>
<P> for (int i = 0; i < myFDefn.GetFieldCount(); i++)<BR> {<BR> OSGeo.OGR.FieldDefn myFieD = myFDefn.GetFieldDefn(i);</P>
<P> if (myFieD.GetFieldType() == OSGeo.OGR.FieldType.OFTString)<BR> {<BR> myOGRft.SetField(i, myOGRFeat.GetFieldAsString(i));<BR> }<BR> else if (myFieD.GetFieldType() == OSGeo.OGR.FieldType.OFTInteger)<BR> {<BR> myOGRft.SetField(i, myOGRFeat.GetFieldAsDouble(i));<BR> }<BR> else if (myFieD.GetFieldType() == OSGeo.OGR.FieldType.OFTInteger)<BR> {<BR> myOGRft.SetField(i, myOGRFeat.GetFieldAsInteger(i));<BR> }<BR> }</P>
<P> myOGRft = myOGRFeat.Clone();<BR> myOGRft.SetFrom(myOGRFeat, 1);<BR> <BR> myshplay.CreateFeature(myOGRft);<BR> <BR> count = count + 1;<BR> myOGRft.Dispose();<BR> } <BR> <BR> axMapControl1.ActiveView.FocusMap.ClearLayers();<BR> axMapControl1.AddLayerFromFile(Application.StartupPath + "<A href='file://\\database\\yantian2005.shp",0' target=_blank>\\database\\yantian2005.shp",0</A>);<BR> axMapControl1.ActiveView.Refresh();<BR> //pgconn.Dispose();<BR> }<BR> catch (Exception e)<BR> { MessageBox.Show( e.Message);<BR> // conn.Dispose();<BR> Application.Exit();</P>
<P> }<BR> }<BR> catch (Exception e)<BR> {<BR> MessageBox.Show( e.Message);<BR> } <BR> }</P>
<P>i know some my code is rudundant and wish you can point out.</P>
<P>thank you </P>
<P>best regard.</P>
<P> windsnow</P><br />
<br />
-------------------------------------------------------------------<br />
想玩得精彩?快来惠普"我的电脑 我的舞台"( <a href=http://d1.sina.com.cn/sina/limeng3/mail_zhuiyu/2008/mail_zhuiyu_20080421.html target=_blank>http://d1.sina.com.cn/sina/limeng3/mail_zhuiyu/2008/mail_zhuiyu_20080421.html</a> )<br />
<br />
-------------------------------------------------------------------<br />
注册新浪2G免费邮箱(<a href=http://mail.sina.com.cn/ target=_blank>http://mail.sina.com.cn/</a>)