我编写了一个perl脚本,用于从一些KML文件中提取坐标。像这样:
if ($start ne 6)
{
$sheet = substr($line,$start,$end-$start);
}
$start = index($line,"<coordinates>") + 13;
$end = index($line,"</coordinates>");
$coords = substr($line,$start,$end-$start);
if ($start ne 12)
{
@values = split (' ',$coords);
foreach (@values) {}
my (@lat, @long);
( $lat[@lat], $long[@long] )
= split /,/ for @values;
这将从此行提取坐标:
<Polygon><outerBoundaryIs><LinearRing><coordinates>-4.799999989183125,54.479999998144478 -5.399999989687073,54.479999998084068 -5.409999989614521,54.739999998345262 -4.809999989103597,54.749999998414239 -4.799999989183125,54.479999998144478</coordinates></LinearRing></outerBoundaryIs></Polygon>
但是当我有多个环形多边形在一行中多次重复<LinearRing><coordinates>
个标签时,它只会检索第一组坐标(正如您所期望的那样)。
所以我的问题是:有没有办法可以修改我已经拥有的多余部分,或者我必须使用不同的解析技术?
感谢。
答案 0 :(得分:0)
我通过用反向索引替换索引来修复它,然后删除&lt;之间的任何内容。和&gt; :
while ( <INPUT> ) {
chomp();
$line = $_;
$start = index($line,"SHEET") + 7;
$end = index($line,"</SimpleData>");
if ($start ne 6)
{
$sheet = substr($line,$start,$end-$start);
}
$start = index($line,"<coordinates>") + 13;
$end = rindex($line,"</coordinates>");
$coords = substr($line,$start,$end-$start);
$coords =~ s/<.*>//;
if ($start ne 12)
{
@values = split (' ',$coords);