-
Notifications
You must be signed in to change notification settings - Fork 9
/
CIAWFBFixer.scala
95 lines (74 loc) · 3.3 KB
/
CIAWFBFixer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io._
import java.util._
import scala.collection.JavaConversions._
object CIAWFBFixer extends App {
val countriesToCoords = new HashMap[String, (Double, Double)]
var country = ""
for(line <- scala.io.Source.fromFile(args(0)).getLines) {
if(line.endsWith(":")) {
country = line.dropRight(1).toLowerCase
if(country.equals("korea, south"))
country = "south korea"
else if(country.equals("korea, north"))
country = "north korea"
else if(line.contains(",")) {
country = line.slice(0, line.indexOf(",")).toLowerCase
}
//println(country)
}
if(line.length >= 5 && line.startsWith(" ")) {
val tokens = line.trim.split("[^0-9SWNE]+")
if(tokens.length >= 6) {
val lat = (tokens(0).toDouble + tokens(1).toDouble / 60.0) * (if(tokens(2).equals("S")) -1 else 1)
val lon = (tokens(3).toDouble + tokens(4).toDouble / 60.0) * (if(tokens(5).equals("W")) -1 else 1)
countriesToCoords.put(country, (lat, lon))
if(country.contains("bosnia"))
countriesToCoords.put("bosnia", (lat, lon))
if(country.equals("yugoslavia"))
countriesToCoords.put("serbia", (lat, lon))
if(country.equals("holy see (vatican city)"))
countriesToCoords.put("vatican", (lat, lon))
}
}
}
countriesToCoords.put("montenegro", (42.5,19.1)) // from Google
//countriesToCoords.foreach(p => println(p._1 + " " + p._2._1 + "," + p._2._2))
val lineRE = """^(.*lat=\")([^\"]+)(.*long=\")([^\"]+)(.*)$""".r
//val line2RE = """^(.*long=\")([^\"]+)(.*lat=\")([^\"]+)(.*)$""".r
//val lineRE = """^(.*lat=\")([^\"]+)(.*long=\")([^\"]+)(.*humanPath=\")([^\"]+)(.*)$""".r
val countryNameRE = """^.*humanPath=\"([^\"]+).*$""".r
val inDir = new File(if(args(1).endsWith("/")) args(1).dropRight(1) else args(1))
val outDir = new File(if(args(2).endsWith("/")) args(2).dropRight(1) else args(2))
for(file <- inDir.listFiles.filter(_.getName.endsWith(".xml"))) {
val out = new BufferedWriter(new FileWriter(outDir+"/"+file.getName))
/*var beg = ""
var lat0 = ""
var mid = ""
var lon0 = ""
var end = ""*/
for(line <- scala.io.Source.fromFile(file).getLines) {
if(line.contains("CIAWFB") && lineRE.findFirstIn(line) != None) {// && line.contains("long=\"-0\"")) {
//line match {
/*case lineRE => */val lineRE(beg, lat0, mid, lon0, end) = line//; beg = begr; lat0 = lat0r; mid = midr; lon0 = lon0r; end = endr;
//case line2RE => val line2RE(begr, lon0r, midr, lat0r, endr) = line; beg = begr; lat0 = lat0r; mid = midr; lon0 = lon0r; end = endr;
//}
val countryNameRE(countryName) = line
//val countryName = "hi"
//println(line)
var lat = lat0//.toDouble
var lon = lon0//.toDouble
if(countriesToCoords.contains(countryName.toLowerCase)) {
lat = countriesToCoords(countryName.toLowerCase)._1.toString
lon = countriesToCoords(countryName.toLowerCase)._2.toString
}
//if(countryName.toLowerCase.equals("vatican"))
// lat = countriesToCoords(countryName.toLowerCase)._1
out.write(beg+lat+mid+lon+end+"\n")
//println(beg+lat+mid+lon+humpath+countryName+end+"\n")
}
else
out.write(line+"\n")
}
out.close
}
}