#!/usr/bin/perl 

use strict;

my @text;
my $result;
my $in_osm=$ARGV[0]; #MAP.osm.xml
my $in_map=$ARGV[1]; #MAP.net.xml
my $out_map=$ARGV[2]; #MAP.net.xml
my $tlstime=$ARGV[3]; #紅綠燈時間

#先將.osm->.net.xml並且移除原本的紅綠燈以及不需要的物件
system "netconvert", "--osm-files", "$in_osm", "-o", "$out_map","--geometry.remove","--roundabouts.guess","--ramps.guess","--junctions.join","--tls.discard-loaded","--no-internal-links","--remove-edges.isolated";

#開啟地圖檔，存入text array
open FILE, $in_map or die "Loading error!";
@text = <FILE>;
close FILE;

#以下為將需要加入紅綠燈的路口id取出
#將array合併為字串處理
$result = join("",@text);
#過濾出junction id="...."
@text = ($result =~ m/junction id="[0-9a-z_]+"/g);
#利用,連接id
$result = join(",",@text);
#刪除不需要的字元
$result =~ s/junction id=//g;
$result =~ s/"//g;

#執行netconvert加入紅綠燈
system "netconvert", "--sumo-net-file", $in_map, "-o", $out_map, "--tls.set", $result, "--tls.green.time", $tlstime;
