SVG建筑平面图中米与点数的正确转换方法问询
米与SVG点数的转换问题
问题背景
我有一份SVG格式的建筑平面图,其尺寸属性为:
width="594.75pt" height="841.5pt"
该平面图对应的实际物理尺寸是40×52米。请问将米转换为SVG点数的正确方法是什么?
我的尝试代码
XmlTextReader reader = new XmlTextReader(pathToSvg); while (reader.Read() && string.IsNullOrEmpty(width) && string.IsNullOrEmpty(height)) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.Name == @"svg") { width = reader.GetAttribute(@"width"); height = reader.GetAttribute(@"height"); } break; } } // 移除宽高字符串中的pt单位 width = width.Replace("pt", string.Empty); height = height.Replace("pt", string.Empty); // 转换为double类型数值 double widthInPoint = double.Parse(width, CultureInfo.InvariantCulture); double heightInPoint = double.Parse(height, CultureInfo.InvariantCulture); // 计算两个维度的米/点数比例 <=== 这部分逻辑对吗? // 594.75pt => 40米 // 1pt => X米 double ratioX = mapHorizontalMeterSize / widthInPoint; double ratioY = mapVerticalMeterSize / heightInPoint; // 计算信标在点数坐标系中的位置 double radiusInPoint = Math.Round(radius / ratioX, 2); double beaconXPositionOnMapInPt = Math.Round((customMapX / ratioX) - (radius / ratioX), 2); double yPos = Math.Round((customMapY / ratioY) - (radius / ratioY), 2); // SVG默认以左上角为原点,我们的坐标系是左下角为原点(originCornerId == 0) double beaconYPositionOnMapInPt = originCornerId == 0 ? heightInPoint - yPos : yPos; string tmpPath; var reaaderSettings = new XmlReaderSettings(); reaaderSettings.DtdProcessing = DtdProcessing.Parse; using (var svgReader = XmlReader.Create(path, reaaderSettings)) { XDocument doc = XDocument.Load(svgReader); var xmlns = doc.Root.GetDefaultNamespace(); var xlinkns = doc.Root.GetNamespaceOfPrefix("xlink"); // 添加圆形标记 doc.Root.Add(new XElement(xmlns + "circle", new XAttribute("stroke", "blue"), new XAttribute("stroke-width", "3"), new XAttribute("cx", $"{beaconXPositionOnMapInPt.ToString(CultureInfo.InvariantCulture)}pt"), new XAttribute("cy", $"{beaconYPositionOnMapInPt.ToString(CultureInfo.InvariantCulture)}pt"), new XAttribute("r", $"{radiusInPoint.ToString(CultureInfo.InvariantCulture)}"), new XAttribute("fill-opacity", "0.1") )); // 添加信标图片 //XNamespace xlinkns = "https://www.w3.org/1999/xlink"; doc.Root.Add(new XElement(xmlns + "image", new XAttribute("x", $"{beaconXPositionOnMapInPt.ToString(CultureInfo.InvariantCulture)}pt"), new XAttribute("y", $"{yPos.ToString(CultureInfo.InvariantCulture)}pt"), new XAttribute(xlinkns + "href", $"data:image/{iconFormat};base64,{icon}") )); tmpPath = FileHelpers.NextAvailableFilename(path); doc.Save(tmpPath); }
问题
按照上述代码处理后,结果完全不符合预期。该SVG文件大小约3MB,无法在此展示。
内容的提问来源于stack exchange,提问作者anon
相关产品推荐
相关产品推荐

