[gdal-dev] please help me check my code about transforming pg
data to shape
abhay menon
abhay.menon at gmail.com
Sat Apr 26 05:13:08 EDT 2008
Skipped content of type multipart/alternative-------------- next part --------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using OSGeo.OGR;
namespace DataExtract
{
class AppUtils
{
public int CreateLayer(String dataSource, String ExecSQL, String LayerName, String fShpFile)
{
Ogr.RegisterAll();
DataSource ds = Ogr.Open(dataSource, 0);
if (ds == null)
{
Console.WriteLine("Can't open " + 0);
return -1;
}
Driver drv = Ogr.GetDriverByName("ESRI Shapefile");
if (drv == null)
{
Console.WriteLine("Can't get driver.");
return -1;
}
Layer layer = ds.ExecuteSQL(ExecSQL, null, "PostGIS");
if (layer == null)
{
Console.WriteLine("FAILURE: Couldn't fetch advertised layer : " + LayerName);
return -1;
}
if (File.Exists(fShpFile))
drv.DeleteDataSource(fShpFile);
DataSource dsDec = drv.CreateDataSource(fShpFile, new string[] { });
if (dsDec == null)
{
Console.WriteLine("Can't open " + 0);
return -1;
}
Layer layerDec;
FeatureDefn def = layer.GetLayerDefn();
layerDec = dsDec.CreateLayer(LayerName, null, def.GetGeomType(), new string[] { });
if (layerDec == null)
{
Console.WriteLine("Layer creation failed.");
return -1;
}
SpatialReference sr = layer.GetSpatialRef();
string srs_wkt;
if (sr != null)
{
sr.ExportToPrettyWkt(out srs_wkt, 1);
String prjDesc = dsDec.name;
prjDesc = prjDesc.Replace(".shp", ".prj");
using (TextWriter tx = new StreamWriter(prjDesc))
{
tx.Write(srs_wkt);
}
}
for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++)
{
FieldDefn fdef = def.GetFieldDefn(iAttr);
layerDec.CreateField(fdef, 1);
}
OSGeo.OGR.Feature feat;
while ((feat = layer.GetNextFeature()) != null)
{
layerDec.CreateFeature(feat);
feat.Dispose();
}
layerDec.Dispose();
dsDec.Dispose();
return 0;
}
}
}
More information about the gdal-dev
mailing list