1

I am trying to to extract sdf file from geodatabase.As the new sdf file created ,the memory usage by program increases.To overcome this issue I have tried to reaseing connection for sdf file and also tried to release resources by using GC.Collect() method but still the problem persist.

/// <summary>
/// Get feature data from geodatabase file
/// </summary>
/// <param name="dictionaryProperty"> Schema of table </param>
/// <param name="table">table connection of gdb file</param>
/// <param name="sdffilepath">sdf file path</param>
static void GetFeatureData(Dictionary<string, Property> dictionaryProperty, Table table, string sdffilepath)
{
    int filecount = 1; ;
    List<Property> propertyList = new List<Property>();
    IEnumerable<string> propertycoll = dictionaryProperty.Select(i => i.Value).Where(i => i.DataType == "OID").ToList().Select(i => i.PropertyName);
    string propertyname = null;
    foreach (string item in propertycoll)
    {
        propertyname = item;
    }
    for (int rowNo = 1; rowNo <= table.RowCount; rowNo++)
    {
        foreach (Row row in table.Search("*", propertyname + "=" + rowNo.ToString(), RowInstance.Recycle))
        {
            foreach (Property property in dictionaryProperty.Values)
            {
                Property objproperty = new Property();
                objproperty.PropertyName = property.PropertyName;
                objproperty.DataType = property.DataType;
                switch (property.DataType)
                {
                    case "SmallInteger":
                        if (row.IsNull(property.PropertyName))
                        {
                            objproperty.PropertyData = "";
                        }
                        else
                        {
                            objproperty.PropertyData = row.GetShort(property.PropertyName).ToString();
                        }
                        break;

                    case "Geometry":
                        switch (row.GetGeometry().geometryType.ToString())
                        {
                            case "Point":
                                PointShapeBuffer pointGeometry = row.GetGeometry();
                                objproperty.GeometryType = property.GeometryType;
                                objproperty.GeometryData = pointGeometry.point.x + " " + pointGeometry.point.y;
                                break;

                                objproperty.GeometryData = coord;
                                objproperty.PropertyData = coord;
                                objproperty.GeometryType =   property.GeometryType;
                                coord = string.Empty;
                                break;
                            case "Polygon":

                                break;
                        }
                        break;
                    case "GlobalID":

                        break;
                }
                propertyList.Add(objproperty);
            }
            InsertFeatureData(table, propertyList, sdffilepath);
            propertyList.Clear();
        }
    }
    Console.WriteLine(filecount + "file completed");
    filecount++;
}
/// <summary>
/// Insert feature data to sdf file path
/// </summary>
/// <param name="table"></param>
/// <param name="propertyList">Schema defination</param>
/// <param name="sdffilepath"></param>
static void InsertFeatureData(Table table, List<Property> propertyList, string sdffilepath)
{
    IConnection con = OpenFDOSDFConnection(sdffilepath);
    IInsert insertCommand = (IInsert)con.CreateCommand(OSGeo.FDO.Commands.CommandType.CommandType_Insert);
    insertCommand.SetFeatureClassName(GetTableName(table));
    foreach (Property objProperty in propertyList)
    {
        switch (objProperty.DataType)
        {
            case "OID":
                if (!string.IsNullOrEmpty(objProperty.PropertyData))
                {
                    insertCommand.PropertyValues.Add(new PropertyValue(objProperty.PropertyName, new Int64Value(Convert.ToInt64(objProperty.PropertyData))));
                }
                else
                {
                    insertCommand.PropertyValues.Add(new PropertyValue(objProperty.PropertyName, null));
                }
                break;

            case "Geometry":
                switch (objProperty.GeometryType)
                {
                    case "Point":
                        if (!(objProperty.GeometryData == ""))
                        {
                            FgfGeometryFactory factory = new FgfGeometryFactory();
                            DirectPositionCollection pcollection = new DirectPositionCollection();
                            string[] points = objProperty.GeometryData.Split();
                            insertCommand.PropertyValues.Add(new PropertyValue("Geometry", new GeometryValue(factory.GetFgf(factory.CreatePoint(factory.CreatePositionXY(Convert.ToDouble(points[0]), Convert.ToDouble(points[1])))))));
                        }   
        }
    }
    insertCommand.Execute();
    insertCommand.Dispose();
    con.Close();
}
RashM
  • 19
  • 2
  • at the bottom of the for loop `propertyList` instead of doing `propertyList.Clear()` why not set it to Null or if it implements `IDisposable` the do something like this `((IDisposible)propertyList).Dispose()` since you are creating a new instance in the for loop. I would personally step through your own code and try to see where you could free your created resources – MethodMan Jul 02 '15 at 05:19
  • In addition to what above: Select().Where().ToList().Select()is terribly inefficient. For very large collections it can make a difference. Let's change it to .Where().Select()! After this...profile! – Adriano Repetti Jul 02 '15 at 06:18
  • ((IDisposible)propertyList).Dispose() throwing InvalidCastException – RashM Jul 02 '15 at 07:13
  • What is the meaning of: `foreach (string item in propertycoll){ propertyname = item;}` – leppie Jul 02 '15 at 08:41
  • foreach (string item in propertycoll){ propertyname = item;} retrieve column name having OID as a datatype. OID is similar to Identity column in sql. – RashM Jul 02 '15 at 10:35

0 Answers0