[gdal-dev] please help me check my code about transforming pg data
to shape
windwxc at sina.com
windwxc at sina.com
Thu Apr 24 22:43:02 EDT 2008
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.
public void GetGeometry(string sqltext)
{
// open the postgis and read data according sqltext
try {
try
{
Ogr.RegisterAll();
}
catch (Exception e)
{ MessageBox.Show(e.Message); }
try
{
OSGeo.OGR.DataSource pgconn = OSGeo.OGR.Ogr.Open ("PG:host=localhost dbname='stdb' user='stuser' password=123", 1);
OSGeo.OGR.Layer myOGRLay = pgconn.ExecuteSQL(sqltext,null, "tt");
FeatureDefn myDef = myOGRLay.GetLayerDefn();
int TT = myDef.GetFieldCount();
int ftcount = myOGRLay.GetFeatureCount(1);
OSGeo.OGR.DataSource shpconn;
OSGeo.OGR.Driver shpdriver;
shpdriver=Ogr.GetDriverByName("ESRI Shapefile");
string path = Application.StartupPath + "\\database\\yantian2005.shp";
OSGeo.OGR.SpatialReference myspatial = new SpatialReference("");
myspatial.ImportFromProj4("+proj=Transverse_Mercator +zone=48N+datum=GCS_WGS_1984");
shpconn = shpdriver.CreateDataSource(path, null);
OSGeo.OGR.Layer myshplay = shpconn.CreateLayer("yantian2005", null , OSGeo.OGR.wkbGeometryType.wkbPolygon, null);
if (myshplay == null)
{
MessageBox.Show("create layer failor!");
}
OSGeo.OGR.Feature myOGRFeat = null;
for (int i = 0; i < TT; i++)
{
FieldDefn fdef = myDef.GetFieldDefn(i);
if (myshplay.CreateField(fdef, 1) == OSGeo.OGR.Ogr.OGRERR_NONE)
{
}
}
int count = 1;
OSGeo.OGR.FeatureDefn myFDefn = myOGRLay.GetLayerDefn();
while ((myOGRFeat = myOGRLay.GetNextFeature()) != null)
{
OSGeo.OGR.Feature myOGRft = null;
myOGRft = myOGRLay.GetFeature(count);
OSGeo.OGR.Geometry myGeom = myOGRFeat.GetGeometryRef();
myOGRft.SetGeometry(myGeom);
for (int i = 0; i < myFDefn.GetFieldCount(); i++)
{
OSGeo.OGR.FieldDefn myFieD = myFDefn.GetFieldDefn(i);
if (myFieD.GetFieldType() == OSGeo.OGR.FieldType.OFTString)
{
myOGRft.SetField(i, myOGRFeat.GetFieldAsString(i));
}
else if (myFieD.GetFieldType() == OSGeo.OGR.FieldType.OFTInteger)
{
myOGRft.SetField(i, myOGRFeat.GetFieldAsDouble(i));
}
else if (myFieD.GetFieldType() == OSGeo.OGR.FieldType.OFTInteger)
{
myOGRft.SetField(i, myOGRFeat.GetFieldAsInteger(i));
}
}
myOGRft = myOGRFeat.Clone();
myOGRft.SetFrom(myOGRFeat, 1);
myshplay.CreateFeature(myOGRft);
count = count + 1;
myOGRft.Dispose();
}
axMapControl1.ActiveView.FocusMap.ClearLayers();
axMapControl1.AddLayerFromFile(Application.StartupPath + "\\database\\yantian2005.shp",0);
axMapControl1.ActiveView.Refresh();
//pgconn.Dispose();
}
catch (Exception e)
{ MessageBox.Show( e.Message);
// conn.Dispose();
Application.Exit();
}
}
catch (Exception e)
{
MessageBox.Show( e.Message);
}
}
i know some my code is rudundant and wish you can point out.
thank you
best regard.
windsnow
-------------------------------------------------------------------
ÏëÍæµÃ¾«²Ê£¿¿ìÀ´»ÝÆÕ"ÎҵĵçÄÔ ÎÒµÄÎę̀"( http://d1.sina.com.cn/sina/limeng3/mail_zhuiyu/2008/mail_zhuiyu_20080421.html )
-------------------------------------------------------------------
×¢²áÐÂÀË2GÃâ·ÑÓÊÏ䣨http://mail.sina.com.cn/£©
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20080425/5a11b8fd/attachment-0001.html
More information about the gdal-dev
mailing list