source: osm/applications/editors/josm/haiti_earthquake/hospitals/HospitalChunkCheck.groovy@ 19879

Last change on this file since 19879 was 19879, checked in by guggis, 16 years ago

updated XPath expressions

File size: 4.2 KB
Line 
1package ch.guggis.haiti.hospital;
2import java.io.BufferedReader;
3import javax.xml.xpath.*
4import groovy.util.XmlParser
5import groovy.xml.DOMBuilder
6
7/**
8 * Takes one of the chunks of health facilities posted here
9 * http://github.com/wonderchook/PAHO_to_OSM/ and checks for each
10 * entry
11 * <ol>
12 * <li>whether there are other already mapped hospitals in the neighborhood</li>
13 * <li>whether there are other already mapped health facilities in the neighborhood</li>
14 * <li>whether an OSM object with the given PAHO id already exists</li>
15 * </ol>
16 *
17 * <strong>Usage</strong>
18 * <pre>
19 * groovy ch.guggis.haiti.hospital.HospitalChunkCheck [options] file
20 *
21 * Options:
22 * -h, --help show help information
23 * </pre>
24 */
25class HospitalChunkCheck {
26
27 def File inputFile
28 def xpath = XPathFactory.newInstance().newXPath()
29
30 public HospitalChunkCheck(File inputFile) {
31 this.inputFile = inputFile
32 }
33
34 def String buildQueryBbox( node) {
35 def minLat = xpath.evaluate("./@lat", node, XPathConstants.NUMBER) - 0.003
36 def minLon = xpath.evaluate("./@lon", node, XPathConstants.NUMBER) - 0.003
37 def maxLat = xpath.evaluate("./@lat", node, XPathConstants.NUMBER) + 0.003
38 def maxLon = xpath.evaluate("./@lon", node, XPathConstants.NUMBER) + 0.003
39 return "bbox=${minLon},${minLat},${maxLon},${maxLat}"
40 }
41
42 def int countHospitalsInProximity(node) {
43 def bbox = buildQueryBbox(node)
44 def queryUrl = "http://xapi.openstreetmap.org/api/0.6/*[amenity=hospital][${bbox}]"
45 def xapiResponse = DOMBuilder.parse(new InputStreamReader(new URL(queryUrl).openStream(), "UTF-8"))
46 def int count = xpath.evaluate("count(//tag[@k = 'amenity'][@v = 'hospital']/..)", xapiResponse, XPathConstants.NUMBER)
47 return count
48 }
49
50 def int countHealthFacilitiesInProximity(node) {
51 def bbox = buildQueryBbox(node)
52 def queryUrl = "http://xapi.openstreetmap.org/api/0.6/*[health_facility:paho_id=*][${bbox}]"
53 def xapiResponse = DOMBuilder.parse(new InputStreamReader(new URL(queryUrl).openStream(), "UTF-8"))
54 def int count = xpath.evaluate("count(//tag[@k = 'health_facility:paho_id']/..)", xapiResponse, XPathConstants.NUMBER)
55 return count
56 }
57
58 def healthFacilityAlreadyMapped(node) {
59 def id = getHealthFacilityId(node)
60 def queryUrl = "http://xapi.openstreetmap.org/api/0.6/*[health_facility:paho_id=${id}]"
61 def xapiResponse = DOMBuilder.parse(new InputStreamReader(new URL(queryUrl).openStream(), "UTF-8"))
62 def int count = xpath.evaluate("count(//tag[@k = 'health_facility:paho_id'][@v ='${id}'])", xapiResponse, XPathConstants.NUMBER)
63 return count > 0
64 }
65
66 def getHealthFacilityId(node) {
67 return xpath.evaluate("./tag[@k = 'health_facility:paho_id']/@v", node)
68 }
69
70 def getName(node) {
71 return xpath.evaluate("./tag[@k = 'name']/@v", node)
72 }
73
74 def getNodeId(node) {
75 return xpath.evaluate("./@id", node)
76 }
77
78 def process() {
79 inputFile.withReader("UTF-8") {
80 reader ->
81 def doc = DOMBuilder.parse(reader)
82 def nodes = xpath.evaluate("//node", doc, XPathConstants.NODESET)
83 nodes.each {
84 def int numHospitals = countHospitalsInProximity(it)
85 def int numHealthFacilities = countHealthFacilitiesInProximity(it)
86 def name = getName(it)
87 def id = getNodeId(it)
88 def alreadyMapped = healthFacilityAlreadyMapped(it)
89 def action = "AUTOMATIC"
90 if (alreadyMapped || numHospitals > 0 || numHealthFacilities >0) {
91 action = "MANUAL MERGE"
92 }
93 println "${action}: id:${id},name: ${name}, num hospitals: ${numHospitals}, num health facilities: ${numHealthFacilities}, already mapped: ${alreadyMapped}"
94 }
95 }
96 }
97
98 static def usage() {
99 println """
100groovy ch.guggis.haiti.hospital.HospitalChunkCheck [options] file
101
102Options:
103 -h, --help show help information
104"""
105 }
106
107
108
109 static public void main(args) {
110 def files = []
111 args.each {
112 arg ->
113 switch(arg) {
114 case "-h":
115 case "--help":
116 usage();
117 System.exit(0)
118 default:
119 files << arg
120 }
121 }
122 if (files.size() != 1) {
123 System.err.println("Error: Exactly one input file required")
124 usage()
125 System.exit(1)
126 }
127 def checker = new HospitalChunkCheck(new File(files[0]))
128 checker.process()
129 }
130}
Note: See TracBrowser for help on using the repository browser.