public void saveOpenStreetMapCropped(float minLat, float minLon, float maxLat, float maxLon, string fileNameNodes, string fileNameRoads, string fileNameSubRoads, string fileDistance) { StreamWriter file = new StreamWriter(fileNameNodes); StreamWriter metroToPixel = new StreamWriter(fileDistance); //file.WriteLine("##NODES##"); long nodeID = 1; Dictionary <long, long> hashIds = new Dictionary <long, long>(); //ADICIONADO POR MIM Dictionary <long, long> hashRoads = new Dictionary <long, long>(); //ADICIONADO POR MIM PointF minProjc = PlateCareePaintMapModel.GPSCoordinatesToProjection(new PointF(minLon, minLat)); PointF maxProjc = PlateCareePaintMapModel.GPSCoordinatesToProjection(new PointF(maxLon, maxLat)); double ration = (maxProjc.X - minProjc.X) / (maxProjc.Y - minProjc.Y); const double EARTH_RADIUOS = 6371000.0; double distancia_LON = EARTH_RADIUOS * Math.Cos((minProjc.Y + maxProjc.Y) / 2) * (maxProjc.X - minProjc.X); double distancia_LAT = EARTH_RADIUOS * (maxProjc.Y - minProjc.Y); if (ration <= (16.0 / 9.0)) { metroToPixel.WriteLine(distancia_LON / (1080 * ration)); metroToPixel.WriteLine(distancia_LAT / 1080); } else { metroToPixel.WriteLine(distancia_LON / 1920); metroToPixel.WriteLine(distancia_LAT / (1920 / ration)); } metroToPixel.Close(); foreach (KeyValuePair <long, Node> e in croppedNodes) { Node x = e.Value; hashIds.Add(e.Value.getId(), nodeID); float lat = x.getLat(); float lon = x.getLon(); PointF xy = PlateCareePaintMapModel.GPSCoordinatesToProjection(new PointF(lon, lat)); int xPoint, yPoint; if (ration <= 16.0 / 9.0) { xPoint = (int)(minProjc.X + (int)(1080 * ration) / (maxProjc.X - minProjc.X) * (xy.X - minProjc.X)); yPoint = 1080 - (int)(minProjc.Y + 1080 / (maxProjc.Y - minProjc.Y) * (xy.Y - minProjc.Y)); } else { xPoint = (int)(minProjc.X + 1920 / (maxProjc.X - minProjc.X) * (xy.X - minProjc.X)); yPoint = (int)(1920 / ration) - (int)(minProjc.Y + (int)(1920 / ration) / (maxProjc.Y - minProjc.Y) * (xy.Y - minProjc.Y)); } if (x.getIsRoadNode()) { file.WriteLine(nodeID.ToString() + ";" + xPoint.ToString(CultureInfo.InvariantCulture) + ";" + yPoint.ToString(CultureInfo.InvariantCulture)); } nodeID++; } file.Close(); file = new StreamWriter(fileNameSubRoads); //file.WriteLine("##SUBROADS##"); HashSet <KeyValuePair <long, long> > tempSubRoads = new HashSet <KeyValuePair <long, long> >(); Dictionary <long, Road> roadNames = new Dictionary <long, Road>(); firstNode = true; XmlTextReader textReader = new XmlTextReader(filePath); initOpenStreetRoadTypes(); String stateLvl1 = ""; long actualRoad = -1; string actualRoadName = ""; bool actualRoadTwoWay = false; RoadType actualRoadType = null; long lastNode = -1; bool isRoad = false; long idOfLine = -1; string lineName = ""; string maxspeed = ""; Dictionary <long, string> lineNames = new Dictionary <long, string>(); int id = 1; // ter ids de arrestas diferentes; long lastRoad = -1; while (textReader.Read()) { switch (textReader.NodeType) { case XmlNodeType.Element: // O nó é um elemento. switch (textReader.Name) { case "way": stateLvl1 = "way"; actualRoad = long.Parse(textReader.GetAttribute("id")); break; case "nd": if (stateLvl1 == "way") { long refe = long.Parse(textReader.GetAttribute("ref")); if (lastNode == -1) { if (croppedNodes.ContainsKey(refe)) // True { lastNode = refe; } } else { if (croppedNodes.ContainsKey(refe)) // True { tempSubRoads.Add(new KeyValuePair <long, long>(lastNode, refe)); lastNode = refe; } } } break; case "tag": if (stateLvl1 == "way") { if (textReader.GetAttribute("k") == "highway") { isRoad = true; string roadType = textReader.GetAttribute("v"); if (roadTypes.ContainsKey(roadType)) { if (actualRoad != -1) { actualRoadType = roadTypes[roadType]; } } } else if (textReader.GetAttribute("k") == "railway") { isRoad = true; string roadType = textReader.GetAttribute("v"); if (roadTypes.ContainsKey(roadType)) { if (actualRoad != -1) { actualRoadType = roadTypes[roadType]; } } } else if (textReader.GetAttribute("k") == "name") { if (actualRoad != -1) { actualRoadName = textReader.GetAttribute("v"); } } else if (textReader.GetAttribute("k") == "oneway") { if (actualRoad != -1) { actualRoadTwoWay = textReader.GetAttribute("v") != "yes"; } } else if (textReader.GetAttribute("k") == "maxspeed") { maxspeed = textReader.GetAttribute("v"); } else if (textReader.GetAttribute("k") == "maxspeed:type") { maxspeed = textReader.GetAttribute("v"); } else if (textReader.GetAttribute("k") == "source:maxspeed") { maxspeed = textReader.GetAttribute("v"); } } else if (stateLvl1 == "relation") { if (textReader.GetAttribute("k") == "ref") { lineName = textReader.GetAttribute("v"); } } break; case "relation": stateLvl1 = "relation"; idOfLine = long.Parse(textReader.GetAttribute("id")); break; case "member": if (textReader.GetAttribute("type") == "way") { long refe = long.Parse(textReader.GetAttribute("ref")); if (roadNames.ContainsKey(refe)) { roadNames[refe].addLine(idOfLine); } } break; } break; case XmlNodeType.EndElement: //Apresente o fim do elemento. if (textReader.Name == "way") { if (isRoad == true && actualRoad != -1 && actualRoadType != null && tempSubRoads.Count > 0) { foreach (KeyValuePair <long, long> e in tempSubRoads) { file.WriteLine((id) + ";" + hashIds[e.Key].ToString() + ";" + hashIds[e.Value].ToString() + ";");//id antes era actualRoad; if (lastRoad != actualRoad) { hashRoads.Add(actualRoad, id); lastRoad = actualRoad; } id++; } roadNames.Add(actualRoad, new Road(actualRoad, actualRoadName, actualRoadTwoWay, maxspeed)); } isRoad = false; stateLvl1 = ""; actualRoad = -1; lastNode = -1; actualRoadName = ""; maxspeed = ""; actualRoadType = null; tempSubRoads.Clear(); } if (textReader.Name == "relation") { if (lineNames.ContainsKey(idOfLine)) { lineNames[idOfLine] += lineName; } else { lineNames.Add(idOfLine, lineName); } lineName = ""; stateLvl1 = ""; idOfLine = -1; } break; } } file.Close(); file = new StreamWriter(fileNameRoads); //file.WriteLine("##ROADS##"); foreach (KeyValuePair <long, Road> e in roadNames) { file.WriteLine(hashRoads[e.Value.getId()].ToString() + ";" + e.Value.getName() + ";" + e.Value.isTwoWay() + ";" + e.Value.getMaxspeed()); } file.Close(); }
public void saveOpenStreetMapCropped(float minLat, float minLon, float maxLat, float maxLon, string fileNameNodes, string fileNameRoads, string fileNameSubRoads) { StreamWriter file = new StreamWriter(fileNameNodes); //file.WriteLine("##NODES##"); foreach (KeyValuePair <long, Node> e in croppedNodes) { Node x = e.Value; float lat = x.getLat(); float lon = x.getLon(); PointF xy = PlateCareePaintMapModel.GPSCoordinatesToProjection(new PointF(lon, lat)); if (x.getIsRoadNode()) { file.WriteLine(x.getId().ToString() + ";" + lat.ToString(CultureInfo.InvariantCulture) + ";" + lon.ToString(CultureInfo.InvariantCulture) + ";" + xy.X.ToString(CultureInfo.InvariantCulture) + ";" + xy.Y.ToString(CultureInfo.InvariantCulture)); } } file.Close(); file = new StreamWriter(fileNameSubRoads); //file.WriteLine("##SUBROADS##"); HashSet <KeyValuePair <long, long> > tempSubRoads = new HashSet <KeyValuePair <long, long> >(); HashSet <Road> roadNames = new HashSet <Road>(); firstNode = true; XmlTextReader textReader = new XmlTextReader(filePath); initOpenStreetRoadTypes(); String stateLvl1 = ""; long actualRoad = -1; string actualRoadName = ""; bool actualRoadTwoWay = false; RoadType actualRoadType = null; long lastNode = -1; bool isRoad = false; while (textReader.Read()) { switch (textReader.NodeType) { case XmlNodeType.Element: // O nó é um elemento. switch (textReader.Name) { case "way": stateLvl1 = "way"; actualRoad = long.Parse(textReader.GetAttribute("id")); break; case "nd": if (stateLvl1 == "way") { long refe = long.Parse(textReader.GetAttribute("ref")); if (lastNode == -1) { if (croppedNodes.ContainsKey(refe)) // True { lastNode = refe; } } else { if (croppedNodes.ContainsKey(refe)) // True { tempSubRoads.Add(new KeyValuePair <long, long>(lastNode, refe)); lastNode = refe; } } } break; case "tag": if (stateLvl1 == "way") { if (textReader.GetAttribute("k") == "highway") { isRoad = true; string roadType = textReader.GetAttribute("v"); if (roadTypes.ContainsKey(roadType)) { if (actualRoad != -1) { actualRoadType = roadTypes[roadType]; } } } else if (textReader.GetAttribute("k") == "name") { if (actualRoad != -1) { actualRoadName = textReader.GetAttribute("v"); } } else if (textReader.GetAttribute("k") == "oneway") { if (actualRoad != -1) { actualRoadTwoWay = textReader.GetAttribute("v") != "yes"; } } } break; } break; case XmlNodeType.EndElement: //Apresente o fim do elemento. if (textReader.Name == "way") { if (isRoad == true && actualRoad != -1 && actualRoadType != null && tempSubRoads.Count > 0) { foreach (KeyValuePair <long, long> e in tempSubRoads) { file.WriteLine(actualRoad + ";" + e.Key.ToString() + ";" + e.Value.ToString() + ";"); } roadNames.Add(new Road(actualRoad, actualRoadName, actualRoadTwoWay)); } isRoad = false; stateLvl1 = ""; actualRoad = -1; lastNode = -1; actualRoadName = ""; actualRoadType = null; tempSubRoads.Clear(); } break; } } file.Close(); file = new StreamWriter(fileNameRoads); //file.WriteLine("##ROADS##"); foreach (Road e in roadNames) { file.WriteLine(e.getId().ToString() + ";" + e.getName() + ";" + e.isTwoWay()); } file.Close(); }