forked from sgillies/capgrids-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.py
124 lines (98 loc) · 3.46 KB
/
grid.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
from itertools import product
from string import Template
from capgrids import data, box, grid
import geojson
from rdflib import Literal, Namespace, RDF, URIRef
from rdflib.graph import Graph
from shapely import geometry
from shapely import wkt
FOAF_URI = "http://xmlns.com/foaf/0.1/"
FOAF = Namespace(FOAF_URI)
GEO_URI = "http://www.w3.org/2003/01/geo/wgs84_pos#"
GEO = Namespace(GEO_URI)
OSGEO_URI = "http://data.ordnancesurvey.co.uk/ontology/geometry/"
OSGEO = Namespace(OSGEO_URI)
SKOS_URI = "http://www.w3.org/2004/02/skos/core#"
SKOS = Namespace(SKOS_URI)
RDFS_URI = "http://www.w3.org/2000/01/rdf-schema#"
RDFS = Namespace(RDFS_URI)
SPATIAL_URI = "http://geovocab.org/spatial#"
SPATIAL = Namespace(SPATIAL_URI)
OSSPATIAL_URI = "http://data.ordnancesurvey.co.uk/ontology/spatialrelations/"
OSSPATIAL = Namespace(OSSPATIAL_URI)
PLACES = "http://pleiades.stoa.org/places/"
def group(number):
s = '%d' % number
groups = []
while s and s[-1].isdigit():
groups.append(s[-3:])
s = s[:-3]
groups.reverse()
return s + ','.join(groups)
template = Template(open("template.html", "r").read())
for mapnum, records in data.items():
record = records[0]
title = record[1]
scale = "1:%s" % group(int(record[2])*1000)
cols = "%s-%s" % (record[7].upper(), record[8].upper())
rows = "%s-%s" % (record[9], record[10])
bounds = box(mapnum)
# HTML
html = template.substitute(
mapnum=mapnum,
title=title,
scale=scale,
cols=cols,
rows=rows,
bounds=str(bounds)
)
with open("htdocs/%s.html" % mapnum, "w") as f:
f.write(html)
# RDF
g = Graph()
g.bind('rdfs', RDFS)
g.bind('skos', SKOS)
g.bind('spatial', SPATIAL)
g.bind('geo', GEO)
g.bind('foaf', FOAF)
g.bind('osgeo', OSGEO)
g.bind('osspatial', OSSPATIAL)
map_uri = "http://atlantides.org/capgrids/%s" % mapnum
map_ref = URIRef(map_uri + "#this")
g.add((map_ref, FOAF['primaryTopicOf'], URIRef(map_uri + ".html")))
g.add((map_ref, RDFS['label'], Literal(title)))
map_extent = URIRef(map_uri + "#this-extent")
g.add((map_extent, RDF.type, OSGEO['AbstractGeometry']))
g.add((map_ref, OSGEO['extent'], map_extent))
g.add((
map_extent,
OSGEO['asGeoJSON'],
Literal(geojson.dumps(geometry.box(*bounds)))))
g.add((
map_extent,
OSGEO['asWKT'],
Literal(wkt.dumps(geometry.box(*bounds)))))
# Individual Grid cells
cols, rows = grid(mapnum)
for col, row in product(cols, rows):
key = col.upper() + str(row)
bounds = box(mapnum, key)
grid_uri = map_uri + "#" + key
grid_ref = URIRef(grid_uri)
grid_extent_ref = URIRef(grid_uri + "-extent")
g.add((grid_ref, RDFS['label'], Literal("Grid Cell " + key)))
g.add((grid_extent_ref, RDF.type, OSGEO['AbstractGeometry']))
g.add((grid_ref, OSGEO['extent'], grid_extent_ref))
g.add((
grid_extent_ref,
OSGEO['asGeoJSON'],
Literal(geojson.dumps(geometry.box(*bounds)))))
g.add((
grid_extent_ref,
OSGEO['asWKT'],
Literal(wkt.dumps(geometry.box(*bounds)))))
g.add((map_ref, OSSPATIAL['contains'], grid_ref))
with open("htdocs/%s.ttl" % mapnum, "w") as f:
f.write(g.serialize(format="turtle"))
with open("htdocs/%s.rdf" % mapnum, "w") as f:
f.write(g.serialize(format="pretty-xml"))