1 | package ch.guggis.haiti.hospital;
|
---|
2 | import groovy.xml.MarkupBuilder
|
---|
3 | import java.io.File;
|
---|
4 | import java.text.SimpleDateFormat;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Input: a CSV export of the PAHO Health Facilities List
|
---|
8 | * Output: an OSM file with the PAHO Health Facilities
|
---|
9 | *
|
---|
10 | * !!! DO NOT upload the output file without careful examination !!!
|
---|
11 | * !!! which Health Facilities are already mapped in OSM. !!!
|
---|
12 | * !!! For each generated node, check whether there is already a node !!!
|
---|
13 | * !!! or a way with the same health_facility:paho_id !!!
|
---|
14 | *
|
---|
15 | * Usage:
|
---|
16 | * groovy ch.guggis.haiti.hospital.MasterToOsmConverter [options]
|
---|
17 | * Options:
|
---|
18 | * -h, --help show help information
|
---|
19 | * -i, --input-file the input file. Reads from stdin if missing
|
---|
20 | * -o, --output-file the output file. Writes to stdout if missing.
|
---|
21 | *
|
---|
22 | */
|
---|
23 | class MasterToOsmConverter {
|
---|
24 | def reader
|
---|
25 | def writer
|
---|
26 |
|
---|
27 | def process() {
|
---|
28 | def builder = new MarkupBuilder(writer)
|
---|
29 | def int id = 0
|
---|
30 | builder.mkp.xmlDeclaration([version:"1.0", encoding:"UTF-8"])
|
---|
31 | builder.mkp.comment("""
|
---|
32 | Automatically generated from v2 of the PAHO master list of Health Facilities.
|
---|
33 |
|
---|
34 | Generated on ${new SimpleDateFormat().format(new Date())}by MasterToOsmConverter"""
|
---|
35 | )
|
---|
36 | builder.osm(version:"0.6") {
|
---|
37 | def nonemptytag = {
|
---|
38 | k,v ->
|
---|
39 | if (v) {
|
---|
40 | tag(k:k, v:v)
|
---|
41 | }
|
---|
42 | }
|
---|
43 | def notnulltag = {
|
---|
44 | k,v ->
|
---|
45 | if (v && v != "0" ) {
|
---|
46 | tag(k:k, v:v)
|
---|
47 | }
|
---|
48 | }
|
---|
49 | reader.eachLine() {
|
---|
50 | line ->
|
---|
51 | def fields = line.split("\t")
|
---|
52 | fields = fields.collect {
|
---|
53 | v ->
|
---|
54 | v = v.replaceAll("^\"", "")
|
---|
55 | v = v.replaceAll("\"\$", "")
|
---|
56 | //v= "${i++}:" + v
|
---|
57 | v
|
---|
58 | }
|
---|
59 | if (fields[0] == "FID") {
|
---|
60 | // skip first line. This is a hack because
|
---|
61 | // reader.eachLine(1) doesn't work
|
---|
62 | //
|
---|
63 | return
|
---|
64 | }
|
---|
65 | id--
|
---|
66 | node(id:id, lat: fields[26], lon: fields[25], version:1) {
|
---|
67 | notnulltag("health_facility:commune_id",fields[4])
|
---|
68 | nonemptytag("health_facility:commune", fields[6])
|
---|
69 | notnulltag("health_facility:department", fields[5])
|
---|
70 | notnulltag("health_facility:region_id", fields[2])
|
---|
71 | notnulltag("health_facility:district_id", fields[3])
|
---|
72 | nonemptytag("health_facility:paho_id",fields[9])
|
---|
73 | nonemptytag("name", fields[10])
|
---|
74 | nonemptytag("pcode", fields[17])
|
---|
75 | nonemptytag("operator", fields[14].trim())
|
---|
76 | if (fields[15].trim()) {
|
---|
77 | switch(fields[15].trim()) {
|
---|
78 | case "DISP":
|
---|
79 | tag(k:"health_facility:type",v:"dispensary")
|
---|
80 | break
|
---|
81 | case "HOP":
|
---|
82 | tag(k:"amenity",v:"hospital")
|
---|
83 | tag(k:"health_facility:type",v:"hospital")
|
---|
84 | break
|
---|
85 | case "C/S":
|
---|
86 | tag(k:"health_facility:type",v:"cs_health_center")
|
---|
87 | break;
|
---|
88 | case "CSL":
|
---|
89 | tag(k:"health_facility:type",v:"cs_health_center")
|
---|
90 | tag(k:"bed", v:"no")
|
---|
91 | break;
|
---|
92 | case "CAL":
|
---|
93 | tag(k:"health_facility:type",v:"cs_health_center")
|
---|
94 | tag(k:"bed", v:"yes")
|
---|
95 | break;
|
---|
96 | case "Other":
|
---|
97 | case "Unknown":
|
---|
98 | tag(k:"health_facility:type",v:"unspecified")
|
---|
99 | }
|
---|
100 | }
|
---|
101 | nonemptytag("source:health_facility", fields[28])
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | def usage() {
|
---|
108 | println """
|
---|
109 | groovy ch.guggis.haiti.hospital.MasterToOsmConverter [options]
|
---|
110 | Options:
|
---|
111 | -h, --help show help information
|
---|
112 | -i, --input-file the input file. Reads from stdin if missing
|
---|
113 | -o, --output-file the output file. Writes to stdout if missing.
|
---|
114 | """
|
---|
115 | }
|
---|
116 |
|
---|
117 | def fail(msg) {
|
---|
118 | println msg
|
---|
119 | usage()
|
---|
120 | System.exit(1)
|
---|
121 | }
|
---|
122 |
|
---|
123 | def processCommandLineOptions(argArray) {
|
---|
124 | def inputFile
|
---|
125 | def outputFile
|
---|
126 | def args = Arrays.asList(argArray)
|
---|
127 | args = args.reverse()
|
---|
128 | def arg = args.pop()
|
---|
129 | while(arg != null) {
|
---|
130 | switch(arg) {
|
---|
131 | case "-i":
|
---|
132 | case "--input-file":
|
---|
133 | inputFile = args.pop()
|
---|
134 | if (inputFile == null) {
|
---|
135 | fail "Error: missing input file"
|
---|
136 | }
|
---|
137 | break
|
---|
138 | case "-o":
|
---|
139 | case "--output-file":
|
---|
140 | outputFile = args.pop()
|
---|
141 | if (outputFile == null) {
|
---|
142 | fail "Error: missing output file"
|
---|
143 | }
|
---|
144 | break
|
---|
145 | case "-h":
|
---|
146 | case "--help":
|
---|
147 | usage()
|
---|
148 | System.exit(0)
|
---|
149 | break
|
---|
150 |
|
---|
151 | default:
|
---|
152 | fail "Illegal argument ${arg}"
|
---|
153 | }
|
---|
154 | arg = args.empty ? null : args.pop()
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (inputFile) {
|
---|
158 | reader = new File(inputFile).newReader("UTF-8")
|
---|
159 | } else {
|
---|
160 | reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"))
|
---|
161 | }
|
---|
162 | if (outputFile) {
|
---|
163 | writer = new File(outputFile).newWriter("UTF-8")
|
---|
164 | } else {
|
---|
165 | writer = new PrintWriter(new OutputStreamWriter(System.out, "UTF-8"))
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | static public void main(args) {
|
---|
170 | def task = new MasterToOsmConverter()
|
---|
171 | task.processCommandLineOptions args
|
---|
172 | task.process()
|
---|
173 | }
|
---|
174 | }
|
---|