AnsweredAssumed Answered

Problem with memory when write to GML file

Question asked by kireandreev on Mar 17, 2019

try
{
//Set Folder permission
SetFullAccessPermissionsForEveryone(Path.GetTempPath());
string tempPath = Path.Combine(Path.GetTempPath(), "Temp");
string tempPath1 = Path.Combine(tempPath, "buildings.gml");

if (File.Exists(tempPath1))
{
File.Delete(tempPath1);
}
List<double> CoordX = new List<double>();
List<double> CoordY = new List<double>();
int k = 0;

char[] delimiters = new char[] { ',' };
for (int i = 0; i < _overlay_Graphic.Graphics.Count; i++)
{
if (_overlay_Graphic.Graphics[i].Attributes.Count > 0)
{
var coords = _overlay_Graphic.Graphics[i].Geometry as Multipart;
var points = coords.Parts.First().Points;
for (int j = 0; j < points.Count; j++)
{
MapPoint point = new MapPoint(Math.Round(Convert.ToDouble(points[j].X),2), Math.Round(Convert.ToDouble(points[j].Y),2), new SpatialReference(WKID));
MapPoint pointUTM = (MapPoint)GeometryEngine.Project(point, new SpatialReference(WKID));
CoordX.Add(pointUTM.X);
CoordY.Add(pointUTM.Y);
}
}
}
XNamespace akn = "http://www.openplans.org/topp";
XNamespace wfs = "http://www.opengis.net/wfs";
XNamespace xs = "http://www.w3.org/2001/XMLSchema";
XNamespace arec = "http://mak.arec/";
XNamespace gml = "http://www.opengis.net/gml";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace schemaLocation = XNamespace.Get("http://www.openplans.org/topp http://128.128.0.105/MakEditProxy/dzgr_parc_trans.xsd http://www.opengis.net/wfs http://128.128.0.105/MakEditProxy/WFS-basic.xsd");
XDocument xDoc1 = new XDocument(new XDeclaration("1.0", "UTF-8", "no"));
XElement dSigEnvelope = new XElement(new XElement(wfs + "FeatureCollection",
new XAttribute("xmlns", wfs),
new XAttribute(XNamespace.Xmlns + "wfs", wfs),
new XAttribute(XNamespace.Xmlns + "akn", akn),
new XAttribute(XNamespace.Xmlns + "gml", gml),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", schemaLocation),
new XElement(gml + "description", "Geographic Markup Language (GML)"),
new XElement(gml + "name", "buildings"),
new XElement(gml + "boundedBy",
new XElement(gml + "Box",
new XElement(gml + "coordinates", CoordX.Min() + "," + CoordY.Min() + " " + CoordX.Max() + "," + CoordY.Max())))));
for (int i = 0; i < _overlay_Graphic.Graphics.Count; i++)
{
if (_overlay_Graphic.Graphics[i].Attributes.Count > 0)
{
k = k + 1;
XElement Polygon = new XElement(gml + "Polygon");
String str1 = _overlay_Graphic.Graphics[i].Geometry.ToJson().Replace(@"""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 str2 = str1.Replace("]]]," + @"""spatialReference"":{" + @"""wkt"":", "");
String str3 = str2.Replace("}}", "");
String str4 = str3.Replace(@"""x"":", "");
String str5 = str4.Replace(@",""z"":0", "");
String str6 = str5.Replace("{" + @"""rings"":[[[", "");
String str7 = str6.Replace("],[", " ");
String coordinate = str7.Replace(@"""y"":", "");
XElement gml1 = new XElement(gml + "featureMember",
new XElement(akn + "BUILDINGS_TRANS", new XAttribute("fid", "BUILDINGS_TRANS." + k),
from keyValue in _overlay_Graphic.Graphics[i].Attributes
select new XElement(akn + keyValue.Key, keyValue.Value),
new XElement(akn + "GEOMETRY",
new XElement(gml + "Polygon",
new XElement(gml + "outerBoundaryIs",
new XElement(gml + "LinearRing",
new XElement(gml + "coordinates", coordinate))))))
);
if (GlobalVar.CommandBP == "holeBuilding")
{
foreach (KeyValuePair<string, List<String>> item in GlobalVar.InnerBoundBuild)
{
if (_overlay_Graphic.Graphics[i].Attributes["BUILDNO"].ToString() == item.Value[0] && _overlay_Graphic.Graphics[i].Attributes["PARCNO"].ToString() == item.Value[1])
{
XElement innerBoundaryIs = new XElement(gml + "innerBoundaryIs");
XElement coordinates1 = new XElement(gml + "coordinates", item.Key);
XElement LinearRing1 = new XElement(gml + "LinearRing");
Polygon.Add(innerBoundaryIs);
innerBoundaryIs.Add(LinearRing1);
LinearRing1.Add(coordinates1);
}
}
}
dSigEnvelope.Add(gml1);
}
}
xDoc1.Add(dSigEnvelope);
if (!File.Exists(tempPath1))
{
xDoc1.Save(tempPath1);
GlobalVar.InnerBoundBuild.Clear();
}
}
catch

(Exception ex)
{ MessageBox.Show(ex.ToString()); }

 

This is function for writing to gml file Graphic. Application work OK for writing  several graphics to GML. When write  more of 100 graphics applications is very slow and may to crash.I know that start garbage collector because in loops have several initialize on XElement constructors.

 

How to solved this problem? I would like to have only one initialize for each XElement.

 

Best Regard

Outcomes