Hi All,
Today we are going to tell, how to connect and retrive the shape file information through FDO using .net.
• Download the latest version of FDO providers from internet.
• Extract and copy the dll in to your local machine.
• Open the visual studio .net 2005
• Create new c# windows application project.
• Right click on the references and add the following dlls to your project from FDO Bin folder
• Create a new class say “FDOShapeFileClass”
• Below we have shown the code connect and read the shape file geometry
public class FDOShapeFileClass:IDisposable
{
private IProviderRegistry FDORegistry = FeatureAccessManager.GetProviderRegistry();
private IConnectionManager FDOManager = FeatureAccessManager.GetConnectionManager();
private IConnection FDOConnection = FeatureAccessManager.GetConnectionManager().CreateConnection("OSGeo.SHP.3.3"); // Replace the version of DLL your using…
public string OpenedFile;
public ConnectionState Constate;
public FDOShapeFileClass(string _ShapeFile)
{
OpenedFile = _ShapeFile;
FDOConnection.ConnectionInfo.ConnectionProperties.SetProperty("DefaultFileLocation", _ShapeFile);
Constate = FDOConnection.Open();
}
// To check the connection state of the Shape file…
public ConnectionState CheckConnected
{
get
{
return Constate;
}
}
// By using below function we can retrive the all geometry in the shape file at once..
// this function will return the Geomtrycollection… of the shape file.
public GeometryCollection SelectAllQuery()
{
ISelect sel = (ISelect)FDOConnection.CreateCommand(OSGeo.FDO.Commands.CommandType.CommandType_Select);
sel.SetFeatureClassName(System.IO.Path.GetFileNameWithoutExtension(OpenedFile));
IFeatureReader FDOReader = sel.Execute();
GeometryCollection Geo_Collection = new GeometryCollection();
FgfGeometryFactory GeoFac = new FgfGeometryFactory();
while (FDOReader.ReadNext())
{
Byte[] Tmppts = FDOReader.GetGeometry("Geometry");
IGeometry Geo = GeoFac.CreateGeometryFromFgf(Tmppts);
Geo_Collection.Add((IGeometry)Geo);
}
return Geo_Collection;
}
// By using this function we can the extents of shape file in the form of ipolygon.
public IPolygon GetExtents()
{
IPolygon retpolygon;
ISelectAggregates pselagree = (ISelectAggregates)FDOConnection.CreateCommand(CommandType.CommandType_SelectAggregates);
pselagree.SetFeatureClassName(System.IO.Path.GetFileNameWithoutExtension(OpenedFile));
IdentifierCollection props = pselagree.PropertyNames;
Expression exp = Expression.Parse("SpatialExtents(Geometry)");
ComputedIdentifier se = new ComputedIdentifier("Extents", exp);
props.Add(se);
IDataReader FDOReader = pselagree.Execute();
FgfGeometryFactory GeoFac = new FgfGeometryFactory();
FDOReader.ReadNext();
Byte[] Tmppts = FDOReader.GetGeometry("Extents");
IGeometry Geo = GeoFac.CreateGeometryFromFgf(Tmppts);
retpolygon = (IPolygon)Geo;
return retpolygon;
}
// we will give this function in the next post…
public void UpdateQuery()
{
throw new System.NotImplementedException();
}
public void DeleteQuery()
{
throw new System.NotImplementedException();
}
public void Dispose()
{
Close();
FDORegistry.Dispose();
FDOManager.Dispose();
FDOConnection.Dispose();
}
private void Close()
{
FDOConnection.Close();
}
• Based on your feedback in the next post we will tell you how to use this retrived geometry in to your application.
Thank You,
Showing posts with label Shape File. Show all posts
Showing posts with label Shape File. Show all posts
Tuesday, January 13, 2009
FDO-Shape file .Net
Labels:
.net,
Dot net,
FDO,
Shape File
Subscribe to:
Posts (Atom)