AnsweredAssumed Answered

Problem with memory when load GML file

Question asked by kireandreev on Mar 17, 2019

try
{
Symbol symbol = null;
SimpleLineSymbol LineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.FromArgb(0, 0, 0), 1);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, System.Drawing.Color.FromArgb(59, 179, 0x00), LineSymbol);
SimpleFillSymbol fillSymbol1 = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, System.Drawing.Color.FromArgb(59, 200, 0x00), LineSymbol);
XmlDocument xmldoc = new XmlDocument();
//Set Folder permission
SetFullAccessPermissionsForEveryone(Path.GetDirectoryName(zipPath));
using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
{

//GlobalVar.InnerBoundBuild.Clear();
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.Name == "buildings.gml")
{
ZipArchiveEntry zipEntry = archive.GetEntry(entry.Name);
int count = 0;
char[] delimiters = new char[] { ',' };
//first string od json parser
string str1_Poly = "{" + @"""rings"":[[[";
string str2 = "]]]," + @"""spatialReference"":{" + @"""wkt"":";
string str3 = "}}";
string WKID1 = @"""PROJCS[\""" + "Bessel_1841_Transverse_Mercator" + @"\"",GEOGCS[\""GCS_Bessel_1841\"",DATUM[\""D_Bessel_1841\"",SPHEROID[\""Bessel_1841\"",6377397.155,299.1528128]],PRIMEM[\""Greenwich\"",0.0],UNIT[\""Degree\"",0.0174532925199433]],PROJECTION[\""Transverse_Mercator\""],PARAMETER[\""False_Easting\"",7500000.0],PARAMETER[\""False_Northing\"",0.0],PARAMETER[\""Central_Meridian\"",21.0],PARAMETER[\""Scale_Factor\"",0.9999],PARAMETER[\""Latitude_Of_Origin\"",0.0],UNIT[\""Meter\"",1.0]]""";
string str = null;
using (XmlReader reader = XmlReader.Create(new StreamReader(zipEntry.Open())))
{

using (Stream stream = entry.Open())
{
xmldoc.Load(stream);
}
XmlNodeList nodes = xmldoc.GetElementsByTagName("gml:featureMember");
//Get first child of gml:featureMember
XmlNode node1 = nodes.Item(0).FirstChild;
//Parent node of gml:coordinates
XmlNodeList GeoPoly = xmldoc.GetElementsByTagName("gml:LinearRing");
XmlNodeList outerBoundaryIs = xmldoc.GetElementsByTagName("gml:outerBoundaryIs");

while (reader.Read())
{
if (reader.IsStartElement(node1.Name))
{
for (int i = 0; i < node1.ChildNodes.Count; i++)
{
if (reader.ReadToFollowing(node1.ChildNodes[i].Name))
{
//Get part of string which is after ":" symbol
int index = reader.Name.IndexOf(":") + 1;
string name = reader.Name.Substring(index);
//Getting attribute from gml
if (name != "GEOMETRY")
{
string value = reader.ReadString();
if (name == "BUILDNO")
{
GetSetNumber.Number = value;
}
if (name == "MAIN_PARCEL")
{
GetSetMainParc.MainParc = value;
}

graphAttr.Add(name, value);

}
// //Getting coordinates and create json string.After create json string with function createGraphic we create graphic from json string.
if (name == "GEOMETRY")
{
str = str1_Poly + GeoPoly.Item(count).FirstChild.InnerText.Trim().Replace(" ", "],[") + str2 + WKID1 + str3;
GetSetCoordinate.Coord = str;
}
}
}
if (GetSetMainParc.MainParc == "0" )
symbol = fillSymbol;
if (GetSetMainParc.MainParc == "1" || (GetSetMainParc.MainParc == ""))
symbol = fillSymbol1;
Graphic graphic = new Graphic(Geometry.FromJson(GetSetCoordinate.Coord), symbol);
foreach (var attr in graphAttr)
{
graphic.Attributes.Add(attr.Key, attr.Value);
}

_overlay_Graphic.Graphics.Add(graphic);

MapPoint label = GeometryEngine.LabelPoint(Geometry.FromJson(str) as Polygon);
Graphic graphic_Label = new Graphic(label, Symbol.FromJson(styleObject.SymbolKO_Build_ToJson(GetSetNumber.Number)));
_overlay_Graphic_Label.Graphics.Add(graphic_Label);
graphAttr.Clear();
count = count + 1;
}
}
}
}
}
}
zipToOpen.Close();
}
}
catch
(Exception ex)
{ MessageBox.Show(ex.ToString()); }

 

This is function for reading gml file from zip archive. Application work OK for gml file with several graphics.When load gml with more of 100 graphics applications is very slow and may to crash.I know that start garbage collector because in loops is initialize Graphic class constructor.

When initialize Graphic class constructor on begin of function out of loops appear next message:

How to solved this problem with garbage collector and my application to work fast?

Outcomes