使用正则表达式从 .kml 转换边界点
Converting boundary points from a .kml using regex
我有从 kml 收到的这个边界,我能够向下挖掘 xml 并只获取边界点。我需要从中转换点数:
-92.25968002689014,30.7180061776264,0 -92.25976564548085,30.71751889774971,0 -92.25992462712097,30.71670626485147,0 -92.26006418327708,30.71604891951008,0 -92.26018466460856,30.71558863525373,0 -92.26037301574165,30.71498469610939,0 -92.26054805030229,30.71444051930294,0 -92.26065861561004, 30.71411636559884,0
为此:
POLYGON((-92.25968002689014 30.7180061776264, -92.25976564548085,30.71751889774971, -92.25992462712097 30.71670626485147, -92.26006418327708,30.71604891951008, -92.26018466460856 30.71558863525373, -92.26037301574165,30.71498469610939, -92.26054805030229 30.71444051930294, -92.26065861561004,30.71411636559884))
我使用的正则表达式模式是:",[0-9.-]* *"
我的计划是使用正则表达式替换来替换任何逗号后跟任意数量的数字、句点或减号后跟一个或多个空格以及冒号等字符。然后用空格替换所有逗号,然后用逗号替换所有冒号。但出于某种原因,我无法让它工作。任何建议将不胜感激。
你可以试试这个:
([-\d.]+),([-\d.]+),([-\d.]+)\s+([-\d.]+),([-\d.]+),([-\d.]+)\s*;
示例 C# 代码:
String polygon(String input)
{
string pattern = @"([-\d.]+),([-\d.]+),([-\d.]+)\s+([-\d.]+),([-\d.]+),([-\d.]+)\s*";
RegexOptions options = RegexOptions.Singleline | RegexOptions.Multiline;
String finalString = "POLYGON((";
int count = 0;
foreach (Match m in Regex.Matches(input, pattern, options))
{
if (count > 0)
finalString += ",";
finalString += m.Groups[1] + " " + m.Groups[2] + ", " + m.Groups[4] + "," + m.Groups[5];
count = 1;
}
finalString += "))";
return finalString;
}
输出:
POLYGON((-92.25968002689014 30.7180061776264, -92.25976564548085,30.71751889774971,-92.25992462712097 30.71670626485147,
-92.26006418327708,30.71604891951008,-92.26018466460856 30.71558863525373, -92.26037301574165,30.71498469610939,-92.260
54805030229 30.71444051930294, -92.26065861561004,30.71411636559884))
我有从 kml 收到的这个边界,我能够向下挖掘 xml 并只获取边界点。我需要从中转换点数:
-92.25968002689014,30.7180061776264,0 -92.25976564548085,30.71751889774971,0 -92.25992462712097,30.71670626485147,0 -92.26006418327708,30.71604891951008,0 -92.26018466460856,30.71558863525373,0 -92.26037301574165,30.71498469610939,0 -92.26054805030229,30.71444051930294,0 -92.26065861561004, 30.71411636559884,0
为此:
POLYGON((-92.25968002689014 30.7180061776264, -92.25976564548085,30.71751889774971, -92.25992462712097 30.71670626485147, -92.26006418327708,30.71604891951008, -92.26018466460856 30.71558863525373, -92.26037301574165,30.71498469610939, -92.26054805030229 30.71444051930294, -92.26065861561004,30.71411636559884))
我使用的正则表达式模式是:",[0-9.-]* *" 我的计划是使用正则表达式替换来替换任何逗号后跟任意数量的数字、句点或减号后跟一个或多个空格以及冒号等字符。然后用空格替换所有逗号,然后用逗号替换所有冒号。但出于某种原因,我无法让它工作。任何建议将不胜感激。
你可以试试这个:
([-\d.]+),([-\d.]+),([-\d.]+)\s+([-\d.]+),([-\d.]+),([-\d.]+)\s*;
示例 C# 代码:
String polygon(String input)
{
string pattern = @"([-\d.]+),([-\d.]+),([-\d.]+)\s+([-\d.]+),([-\d.]+),([-\d.]+)\s*";
RegexOptions options = RegexOptions.Singleline | RegexOptions.Multiline;
String finalString = "POLYGON((";
int count = 0;
foreach (Match m in Regex.Matches(input, pattern, options))
{
if (count > 0)
finalString += ",";
finalString += m.Groups[1] + " " + m.Groups[2] + ", " + m.Groups[4] + "," + m.Groups[5];
count = 1;
}
finalString += "))";
return finalString;
}
输出:
POLYGON((-92.25968002689014 30.7180061776264, -92.25976564548085,30.71751889774971,-92.25992462712097 30.71670626485147,
-92.26006418327708,30.71604891951008,-92.26018466460856 30.71558863525373, -92.26037301574165,30.71498469610939,-92.260
54805030229 30.71444051930294, -92.26065861561004,30.71411636559884))