/
match-regions.py
149 lines (105 loc) · 3.98 KB
/
match-regions.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
#! /usr/bin/python
"""
Matches geographic points in a Kasabi dataset to the Ordnance Survey administrative region
that it is in. It uses the OS boundary data to determine whether a given point is in the
region or not.
Usage:
If you don't have os.dat then you need to run parse-boundaries.py first.
This script takes the following parameters:
-d/--dataset the dataset containing the geopoints
-f/--file the name of the output file
-q/--query an alternate sparql query to use
The sparql query must be a select and must select at least three variables
called ?s, ?lat and ?long being the subject, latitude and longitude
The default query is:
select ?s ?lat ?long where {
?s <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat ;
<http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
}
Prerequisites:
Set an environment variable KASABI_API_KEY equal to your Kasabi api key. On
Linux you can do this by adding this line to your ~/.profile file:
export KASABI_API_KEY=blah
You also need to install ijson which is a python wrapper for the yajl streaming json parser.
ijson: http://pypi.python.org/pypi/ijson/
yajl: http://lloyd.github.com/yajl/
Install ruby and cmake
sudo apt-get install ruby
sudo apt-get install cmake
Download the version 1 source of yajl (not version 2), unpack and in
the source directory type:
sudo ./configure
sudo make install
sudo ldconfig
Install ijson with:
sudo easy_install ijson
"""
import sys
import os
import re
import optparse
from shapely.geometry import Polygon, Point
from shapely.wkt import loads
from shapely import iterops
from ijson import items
import urllib2
import urllib
import pytassium
p = optparse.OptionParser()
p.add_option("-d", "--dataset", action="store", dest="dataset", metavar="DATASET", help="use dataset DATASET")
p.add_option("-q", "--query", action="store", dest="query", metavar="QUERY", help="use sparql QUERY")
p.add_option("-f", "--file", action="store", dest="filename", metavar="FILENAME", help="output results to FILENAME")
opts, args = p.parse_args()
if not opts.dataset:
print "Please supply the name of a kasabi dataset and make sure you're subscribed to its sparql endpoint"
sys.exit(1)
if not opts.filename:
print "Please supply the name of the output file"
sys.exit(1)
if not opts.query:
query = "select ?s ?lat ?long where {?s <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat ; <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long . }"
else:
query = opts.sparql
fout = open(opts.filename, "w")
points = []
ds = pytassium.Dataset(opts.dataset, os.environ['KASABI_API_KEY'])
sparql_uri = ds.get_api('sparql').uri
print "Querying dataset for geographic points"
url = "%s?query=%s&apikey=%s" % (sparql_uri, urllib.quote_plus(query), os.environ['KASABI_API_KEY'])
req = urllib2.Request(url, None, {'Accept':'application/json'})
f = urllib2.urlopen(req)
objects = items(f, 'results.bindings.item')
count = 0
for t in objects:
p = Point(float(t['long']['value']), float(t['lat']['value']))
p.uri = t['s']['value']
points.append(p)
count += 1
if count > 0:
if count % 1000 == 0:
print str(count) + " coordinates found"
sys.stdout.flush()
elif count % 25 == 0:
sys.stdout.write('.')
sys.stdout.flush()
print str(count) + " coordinates found"
print "Matching points to regions"
f = open('os.dat', 'r')
count = 0
for line in f:
(region, wkt) = line.split("\t")
polygon = loads(wkt)
for point in iterops.contains(polygon, points, True):
fout.write("<%s> <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/within> <%s> .\n" % (point.uri, region))
fout.write("<%s> <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/contains> <%s> .\n" % (region, point.uri))
count += 1
if count > 0:
if count % 1000 == 0:
print str(count) + " regions checked"
sys.stdout.flush()
elif count % 25 == 0:
sys.stdout.write('.')
sys.stdout.flush()
print str(count) + " regions checked"
f.close()
fout.close()