public static bool WriteOsmDataToXml(OsmData osmData, string outputFileName, double minLon, double minLat, double maxLon, double maxLat) { if (!Directory.Exists(Constants.Constants.FileFolder)) { Directory.CreateDirectory(Constants.Constants.FileFolder); } if (osmData == null) { return(false); } XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; using (XmlWriter outputWriter = XmlWriter.Create($"{Constants.Constants.FileFolder}{Path.DirectorySeparatorChar}{outputFileName}", settings)) { outputWriter.WriteStartElement("osm"); WriteBounds(outputWriter, osmData.bounds); WriteNodes(outputWriter, osmData.Nodes); WriteWays(outputWriter, osmData.Ways); WriteRelations(outputWriter, osmData.Relations); outputWriter.WriteEndElement(); } return(true); }
private OsmData[] SortData(string strInputData, double decible, double nLonOrg, double nLatOrg, string strAdminpwd) { //let's convert the OSM string points output to array and sort it according to QuickSort methodology string[] strArray = strInputData.Split(Environment.NewLine.ToCharArray()); strArray = strArray.Where(x => !string.IsNullOrEmpty(x)).ToArray(); OsmData[] array = new OsmData[strArray.Length]; for (int i = 0; i < strArray.Length - 1; i++) { var values = strArray[i + 1].Split(','); //let's parse all the items from the string in each line //and store each value in the dedicated variable array[i].ItemId = Convert.ToInt64(values[0]); array[i].nLat = Convert.ToDouble(values[2]); array[i].nLon = Convert.ToDouble(values[1]); array[i].Category = Convert.ToString(values[3]); array[i].SubCategory = Convert.ToString(values[4]); array[i].Name = Convert.ToString(values[5]); } //let's run quick sort algorithm //left side the strat of the array - right end of the array Quick_Sort(ref array, 0, array.Length - 1); //let's remove close points in the array to reduce point cloud intensity //OsmData[] arrayNoDuplicates = RemoveDuplicated(array); //let's calculate distances and decibles for each point //CalculateDistanceAndDecibleEqu(ref arrayNoDuplicates, nLonOrg, nLatOrg); CalculateDistanceAndDecibleEqu(ref array, decible, nLonOrg, nLatOrg); //return the data array (OSMdata format) return(array); }
private void GetOsmData() { new Thread(delegate() { OsmData tmpOsmData = OsmParser.Parse(ConfigManager.Instance.FilePath, ConfigManager.Instance.MinLon, ConfigManager.Instance.MinLat, ConfigManager.Instance.MaxLon, ConfigManager.Instance.MaxLat); lock (osmDataLock) { osmData = tmpOsmData; } }).Start(); }
private OsmData[] RemoveDuplicated(OsmData[] arr) { //let's remove close points from array according to the array sort and comparing between //following points in the array, in addition we will check for each new point the distance with //the prevoius points in the new array OsmData[] newArray = new OsmData[arr.Length]; //we will pass 0,0 coordinates and consider it as empty point int i = 0, j = 0; while (arr[i].nLat == 0 && arr[i].nLon == 0) { i++; } newArray[j] = arr[i]; while (i < arr.Length - 1) { //let's get the 1st point in the set and loop the following points to remove close points //and decrease the density of the points OsmData nPointValueOrg = arr[i]; OsmData nPointValueNext = arr[i]; while ((distance(nPointValueOrg.nLat, nPointValueOrg.nLon, nPointValueNext.nLat, nPointValueNext.nLon, "K") < 0.2) && (i < arr.Length - 1)) { i++; nPointValueNext = arr[i]; } newArray[j] = arr[i]; nPointValueOrg = newArray[j]; bool bExist = false; int k = j - 1; //let's check for close points before adding the new far point //this done because the quick sort is sorting 2 parameters in accuracy of ~70-80% //we still have cases where far points from the 1st set point could be closed to other //already inserted points from previous sets while ((!bExist) && k >= 0) { nPointValueNext = newArray[k]; bExist = (distance(nPointValueOrg.nLat, nPointValueOrg.nLon, nPointValueNext.nLat, nPointValueNext.nLon, "K") < 0.2); k--; } if (!bExist) { j++; } } //since we had a new smaller array after remove close points, let's resize the array accordingly Array.Resize <OsmData>(ref newArray, j); return(newArray); }
void Start() { osmDataLock = new object(); dataParserLock = new object(); dataParserStarted = false; dataParserFinished = false; dataManagerFinished = false; osmData = null; if (!ConfigManager.Instance.Load(Assets.Scripts.Constants.Constants.ConfigFile)) { DebugConsoleManager.Instance.UpdateUI(debugConsoleText); dataManagerFinished = true; } }
public void StartCutting() { Log = string.Empty; if (string.IsNullOrWhiteSpace(InputFileName)) { Log += $"Input file can't be empty { Environment.NewLine}"; return; } if (string.IsNullOrWhiteSpace(OutputFileName)) { Log += $"Output file name can't be empty { Environment.NewLine}"; return; } Log += $"Loading osm data form input file{ Environment.NewLine}"; OsmData osmData = null; try { osmData = OsmParser.Parse(InputFileName, _minLon, _minLat, _maxLon, _maxLat); } catch (FileNotFoundException e) { Log += $"input file not found { Environment.NewLine}"; return; } osmData.FillWaysNode(); osmData.RemoveRelationsWithoutMembers(); osmData.RemoveWaysWithoutNodes(); Log += $"Writing osm data to output file{Environment.NewLine}{AppDomain.CurrentDomain.BaseDirectory}{OSMtoSharp.FileManagers.Constants.Constants.FileFolder}{Path.DirectorySeparatorChar}{OutputFileName}{ Environment.NewLine}"; if (OsmXmlWriter.WriteOsmDataToXml(osmData, OutputFileName, _minLon, _minLat, _maxLon, _maxLat)) { Log += $"Done! { Environment.NewLine}"; } }
/// <summary> /// Standard constructor /// </summary> public BufferClass() { data = new OsmData(); }
public static OsmData Parse(string fileName, double minLon, double minLat, double maxLon, double maxLat) { OsmData osmData = new OsmData(); AbstractOsmNode.parent = osmData; OsmNode currentNode = null; OsmWay currentWay = null; OsmRelation currentRelation = null; bool currentIsNode = false; bool currentIsWay = false; bool currentIsRelation = false; if (!File.Exists(fileName)) { throw new FileNotFoundException(); } using (XmlReader reader = XmlReader.Create(fileName)) { while (reader.Read()) { if (reader.IsStartElement()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == Constants.Constants.osmNode) { try { double lat = double.Parse(reader.GetAttribute(Constants.Constants.LatString), CultureInfo.InvariantCulture); double lon = double.Parse(reader.GetAttribute(Constants.Constants.LonString), CultureInfo.InvariantCulture); if (osmData.bounds.MinLat <= lat && osmData.bounds.MaxLat >= lat && osmData.bounds.MinLon <= lon && osmData.bounds.MaxLon >= lon) { OsmNode newOsmNode = new OsmNode() { Id = long.Parse(reader.GetAttribute(Constants.Constants.IdString)), Point = new Point() { Lat = double.Parse(reader.GetAttribute(Constants.Constants.LatString), CultureInfo.InvariantCulture), Lon = double.Parse(reader.GetAttribute(Constants.Constants.LonString), CultureInfo.InvariantCulture) } }; osmData.Nodes.Add(newOsmNode.Id, newOsmNode); currentNode = newOsmNode; currentIsNode = true; currentIsWay = false; currentIsRelation = false; } else { currentIsNode = false; currentIsWay = false; currentIsRelation = false; } } catch (Exception ex) { #if VERBOSE // Console.WriteLine(ex.Message); #endif } } else if (reader.Name == Constants.Constants.osmWay) { try { OsmWay newOsmWay = new OsmWay() { Id = long.Parse(reader.GetAttribute(Constants.Constants.IdString)), }; osmData.Ways.Add(newOsmWay.Id, newOsmWay); currentWay = newOsmWay; currentIsNode = false; currentIsWay = true; currentIsRelation = false; } catch (Exception ex) { #if VERBOSE //Console.WriteLine(ex.Message); #endif } } else if (reader.Name == Constants.Constants.osmRelation) { try { OsmRelation newOsmRelation = new OsmRelation() { Id = long.Parse(reader.GetAttribute(Constants.Constants.IdString)) }; osmData.Relations.Add(newOsmRelation.Id, newOsmRelation); currentRelation = newOsmRelation; currentIsNode = false; currentIsWay = false; currentIsRelation = true; } catch (Exception ex) { #if VERBOSE //Console.WriteLine(ex.Message); #endif } } else if (reader.Name == Constants.Constants.osmTag) { try { string key = reader.GetAttribute(Constants.Constants.KeyString); string value = reader.GetAttribute(Constants.Constants.ValueString); TagKeyEnum tagKey = EnumExtensions.GetTagKeyEnum <TagKeyEnum>(key); if (tagKey != TagKeyEnum.None) { if (currentIsNode && currentNode != null) { currentNode.Tags[tagKey] = value; } else if (currentIsWay && currentWay != null) { currentWay.Tags[tagKey] = value; } else if (currentIsRelation && currentRelation != null) { currentRelation.Tags[tagKey] = value; } } } catch (Exception ex) { #if VERBOSE // Console.WriteLine(ex.Message); #endif } } else if (reader.Name == Constants.Constants.osmNd) { try { long refId = long.Parse(reader.GetAttribute(Constants.Constants.refString)); if (currentWay != null && currentIsWay) { currentWay.Nds.Add(refId); } } catch (Exception ex) { #if VERBOSE // Console.WriteLine(ex.Message); #endif } } else if (reader.Name == Constants.Constants.osmMember) { try { string role = reader.GetAttribute(Constants.Constants.roleString); RelationMemberRoleEnum roleEnum = EnumExtensions.GetTagKeyEnum <RelationMemberRoleEnum>(role); string type = reader.GetAttribute(Constants.Constants.typeString); RelationMemberTypeEnum typeEnum = EnumExtensions.GetTagKeyEnum <RelationMemberTypeEnum>(type); if (currentIsRelation && currentRelation != null) { currentRelation.Members.Add(new OsmMember() { Ref = long.Parse(reader.GetAttribute(Constants.Constants.refString)), Role = roleEnum, Type = typeEnum }); } } catch (Exception ex) { #if VERBOSE //Console.WriteLine(ex.Message); #endif } } else if (reader.Name == Constants.Constants.osmBounds) { double fileMinLat; double fileMinLon; double fileMaxLat; double fileMaxLon; try { fileMinLat = double.Parse(reader.GetAttribute(Constants.Constants.MinLatString), System.Globalization.CultureInfo.InvariantCulture); fileMinLon = double.Parse(reader.GetAttribute(Constants.Constants.MinLonString), System.Globalization.CultureInfo.InvariantCulture); fileMaxLat = double.Parse(reader.GetAttribute(Constants.Constants.MaxLatString), System.Globalization.CultureInfo.InvariantCulture); fileMaxLon = double.Parse(reader.GetAttribute(Constants.Constants.MaxLonString), System.Globalization.CultureInfo.InvariantCulture); } catch (Exception) { throw new Exception("FIle bad bounds"); } osmData.bounds = new OsmBounds(); if (fileMaxLat <= minLat && fileMaxLon <= minLon && fileMinLat >= maxLat && fileMinLon >= maxLon) { break; } if (fileMinLat >= minLat) { osmData.bounds.MinLat = fileMinLat; } else { osmData.bounds.MinLat = minLat; } if (fileMinLon >= minLon) { osmData.bounds.MinLon = fileMinLon; } else { osmData.bounds.MinLon = minLon; } if (fileMaxLat >= maxLat) { osmData.bounds.MaxLat = maxLat; } else { osmData.bounds.MaxLat = fileMaxLat; } if (fileMaxLon >= maxLon) { osmData.bounds.MaxLon = maxLon; } else { osmData.bounds.MaxLon = fileMaxLon; } } } } } } return(osmData); }