-
Notifications
You must be signed in to change notification settings - Fork 4
/
vagueplaces.py
executable file
·306 lines (248 loc) · 8.11 KB
/
vagueplaces.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
Vagueplaces Generator
\author Jordi Castells
\date 10 August 2012
\mainpage
This software is implemented as a Final project of a Geoinformatics Master course at ITC Faculty of Geo-Information Science and Earth Observation.
"""
from SPARQLWrapper import SPARQLWrapper, SPARQLExceptions, JSON
import argparse
import signal
import os
import sys
import tempfile
import xml, warnings
import cSpinner
import cPlace
import cReport
import geom_functions as GEOM
# ###########################
#
# ARGUMENT PARSING
#
# ###########################
parser = argparse.ArgumentParser(description='Retrieve dbpedia points and generate their alpha shape')
parser.add_argument('--query', action='store', dest='stringval', default=None,nargs='+',
help='List of keywords to filter from the Abstract results. Interpreted as Logical disjunction')
parser.add_argument('--alpha',type=float,default=0.1,dest='floatval')
parser.add_argument('--reportFile', default=None, dest='reportFile',help='Store report to a file instead that StdOut')
parser.add_argument('CSV_POINT_OUTPUT', type=argparse.FileType('wb', 0),
help='Retrieved points file out as CSV.')
parser.add_argument('--live', action='store_true',default=False,
dest='live_bool',
help='Use Dbpedia live SPARQL endpoint instead of last released version')
parser.add_argument('--verbose', action='store_true', default=False,
dest='debug_bool',
help='Verbose output')
arguments = parser.parse_args()
# ###########################
#
# INITIALIZATIONS
#
# ###########################
query_list = arguments.stringval
OF = arguments.CSV_POINT_OUTPUT
alpha = arguments.floatval
isdebug = arguments.debug_bool
islive = arguments.live_bool
RESULTS_QUERY = 500000
PLACES = []
#sparql endpoint
if islive:
#sparql = SPARQLWrapper("http://live.dbpedia.org/sparql")
sparql = SPARQLWrapper("http://dbpedia-live.openlinksw.com/sparql")
else:
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
#Spinner
S = cSpinner.cSpinner()
S.set_msg("loading")
S.start()
#report
REPORT = cReport.cReport();
REPORT.set_query(str(query_list));
REPORT.set_points_filename(os.path.realpath(str(OF.name)));
# ###########################
#
# FUNCTIONS
#
# ###########################
def european_countries():
"""
Retrieve an europe country list from DBpedia with URIs.
@return List with country URIS
"""
sparql.setReturnFormat(JSON)
sparql.setQuery("""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX yago: <http://dbpedia.org/class/yago/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?place WHERE {
?place rdf:type yago:EuropeanCountries .
?place rdf:type dbo:Country
}
"""
)
results = sparql.query().convert()
if not isinstance(results, dict):
warnings.warn("Parsing Failure. Not a dictionary")
finish_program()
return results["results"]["bindings"]
def get_points(country_uri,query_list,offset,limit):
"""
Retrieve a list of points from DBpedia matching the input.
\param country_uri country to query
\param query_list substring to check in the Abstract
\param offset Offset to start retrieving
\param limit Limit of lines to retrieve
\return List with points with title,geolat,geolong
"""
regex_list = []
for q in query_list:
regex_list.append("""regex(?abstract,\" """+str(q)+"""\","i") """)
query = "||".join(regex_list)
querystr = """
SELECT DISTINCT ?title,?geolat,?geolong
WHERE{
?place rdf:type dbo:Place .
?place dbo:country <""" + country_uri + """> .
?place foaf:name ?title .
?place geo:lat ?geolat .
?place geo:long ?geolong .
?place dbo:abstract ?abstract .
FILTER ("""+ query +""")
}
OFFSET """ + str(offset) + """
LIMIT """ + str(limit)+ """
"""
sparql.setQuery(querystr)
country_results = sparql.query().convert()
return country_results["results"]["bindings"]
def gen_heatmap():
"""
\todo gen_heatmap is not implemented
"""
pass
def gen_convex_hull():
"""
Generate the convex hull for the report
"""
plist = []
for p in PLACES:
plist.append((float(p.lon),float(p.lat)))
REPORT.set_wkt_chull(GEOM.convex_hull(plist))
def gen_alpha_shape(cgalfile,alpha):
"""
External system execution of alpha_shaper to generate a WKT alpha shape file.
Expects a CGAL file with lon lat corrdinates and the first line an integer
of the total number of lines to read
"""
opt_alpha,wkt_polygons = GEOM.alpha_shape(cgalfile,alpha);
REPORT.set_alphas(alpha,opt_alpha)
REPORT.set_wkt_ashape(wkt_polygons)
def finish_program():
OF.close()
S.stop()
sys.exit(0)
def write_file_cgal(fileh):
"""
Writes a file to be read by cgal alpha_shape generator
"""
fileh.write(str(len(PLACES))+"\n")
for p in PLACES:
fileh.write(p.lon+" "+p.lat+"\n")
def write_file_csv(fileh):
"""
Writes a CSV file to be opened by a GIS software. WKT
"""
header = "name;WKT;Country;Abstract\n"
fileh.write(header)
for p in PLACES:
fileh.write(p.name+"; POINT("+p.lon+" "+p.lat+");"+p.country+";"+p.text+"\n")
def write_file(fileh,wf):
"""
Write a file (fileh) with the format (wf). Accepting csv and cgal
"""
if wf.lower() == 'cgal':
write_file_cgal(fileh)
elif wf.lower() == 'csv':
write_file_csv(fileh)
fileh.close()
# ###########################
#
# SIGNAL HANDLING
#
# ###########################
def kill_handler(signal, frame):
print 'Kill Signal Recieved'
finish_program()
signal.signal(signal.SIGINT, kill_handler)
# ###########################
#
# START
#
# ###########################
for country in european_countries():
country_uri = country["place"]["value"]
country_name = country_uri.rpartition('/')[-1]
total_results = 0
offset = 0
query_results = 1
S.set_msg(country_name)
while query_results > 0:
try:
country_results = get_points(country_uri,query_list,offset,RESULTS_QUERY)
for result in country_results:
title = result["title"]["value"].encode('ascii','ignore')
lat = result ["geolat"]["value"]
lon = result["geolong"]["value"]
country = country_name
#abstract = result["abstract"]["value"].encode('ascii','ignore')
abstract = ""
if(lat!='NAN' and lon != 'NAN'):
PLACES.append(cPlace.cPlace(title,lat,lon,abstract,country))
query_results = len(country_results)
offset = offset + query_results
total_results += query_results
except Exception as inst:
print type(inst)
print "EXCEPTION"
if isdebug:
sys.stdout.write("\r\x1b[K"+country_uri+" "+str(total_results)+"\n")
sys.stdout.flush()
REPORT.set_country_count(PLACES);
if (len(PLACES) > 0):
# ###########################
#
# POLYGON GENERATION
#
# ###########################
tmpfile = tempfile.NamedTemporaryFile(prefix='vagueplace',delete=False);
write_file(tmpfile,'cgal')
gen_alpha_shape(tmpfile,alpha);
gen_convex_hull();
# ###########################
#
# REPORT PRINTING
#
# ###########################
if arguments.reportFile:
if not REPORT.write_report(arguments.reportFile):
print "Failure writing report file %s"% arguments.reportFile
else:
REPORT.print_report();
# ###########################
#
# FILE WRITING
#
# ###########################
S.pause();
write_file(OF,'csv')
# ###########################
#
# CLOSURE
#
# ###########################
print
else:
print "No results for this query"
finish_program()