forked from markiliffe/hxl-extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hxl2geoserver.py
81 lines (61 loc) · 2.17 KB
/
hxl2geoserver.py
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
import optparse
import sys
from hxl import HXLException
import hxl.geoserver
import hxl.sparql
def import_pcode(server, pcode):
print 'Importing %s...' % (pcode,)
try:
country = hxl.sparql.query_country_information(pcode)
if not country:
print 'Unknown country'
return
(featureName,) = country
server.create_multipolygon_layer(pcode, featureName)
country = hxl.sparql.query_country_geometry(pcode)
if country:
(country_name, country_polygons) = country
server.insert_multipolygon(pcode, country_name, country_polygons)
apls = hxl.sparql.query_country_apls(pcode)
apl_layer_name = pcode + '_APLs'
server.create_multipoint_layer(apl_layer_name, featureName + ' APLs')
if apls:
server.insert_multipoint(apl_layer_name, apls)
except HXLException as e:
print e
def main():
p = optparse.OptionParser(usage='usage: %prog [options] [pcode, pcode, ...]')
p.add_option('--server', '-s', dest='server', default='127.0.0.1', help='GeoServer instance')
p.add_option('--port', '-p', dest='port', type='int', default=8080)
p.add_option('--user', '-u', dest='username', default='admin')
p.add_option('--dbname', '-d', dest='dbname', default=None)
p.add_option('--apls', dest='global_apls', action='store_true', default=False, help='Create layer with all APLs')
p.add_option('--all', dest='all_pcodes', action='store_true', default=False, help='Import data from all countries')
o, pcodes = p.parse_args()
if not o.dbname:
p.print_help()
sys.exit(1)
print 'GeoServer password for %s: ' % (o.username,),
password = sys.stdin.readline().strip()
if not password:
print '\nNo password given'
sys.exit(1)
if o.all_pcodes:
pcodes = hxl.sparql.query_country_pcodes()
elif o.global_apls:
pass
elif not pcodes:
print 'No pcodes given'
sys.exit(1)
server = hxl.geoserver.GeoServer(o.server, o.port, '/geoserver', o.username, password, o.dbname)
if o.global_apls:
apls = hxl.sparql.query_all_apls()
try:
server.create_multipoint_layer('APLs', 'Affected Population Locations')
server.insert_multipoint('APLs', apls)
except HXLException as e:
print e
for pcode in pcodes:
import_pcode(server, pcode)
if __name__ == '__main__':
main()