| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
## Copyright (c) 2011 Astun Technology |
| 4 |
|
| 5 |
## Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
## of this software and associated documentation files (the "Software"), to deal |
| 7 |
## in the Software without restriction, including without limitation the rights |
| 8 |
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
## copies of the Software, and to permit persons to whom the Software is |
| 10 |
## furnished to do so, subject to the following conditions: |
| 11 |
|
| 12 |
## The above copyright notice and this permission notice shall be included in |
| 13 |
## all copies or substantial portions of the Software. |
| 14 |
|
| 15 |
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 |
## THE SOFTWARE. |
| 22 |
|
| 23 |
""" |
| 24 |
A collection of classes used to manipulate Ordnance Survey GB GML data, |
| 25 |
used with prepgml4ogr.py. (But here used/called from Stetl Filter) |
| 26 |
""" |
| 27 |
|
| 28 |
import os |
| 29 |
from lxml import etree |
| 30 |
|
| 31 |
|
| 32 |
class prep_osgml(): |
| 33 |
""" |
| 34 |
Base class that provides the main interface methods `prepare_feature` and |
| 35 |
`get_feat_types` and performs basic manipulation such as exposing the fid, |
| 36 |
adding and element containing the filename of the source and adding an |
| 37 |
element with the orientation in degrees. |
| 38 |
|
| 39 |
""" |
| 40 |
def __init__(self, inputfile): |
| 41 |
self.inputfile = inputfile |
| 42 |
self.feat_types = [] |
| 43 |
|
| 44 |
def get_feat_types(self): |
| 45 |
return self.feat_types |
| 46 |
|
| 47 |
def prepare_feature(self, feat_str): |
| 48 |
|
| 49 |
# Parse the xml string into something useful |
| 50 |
feat_elm = etree.fromstring(feat_str) |
| 51 |
feat_elm = self._prepare_feat_elm(feat_elm) |
| 52 |
|
| 53 |
return etree.tostring(feat_elm, |
| 54 |
encoding='UTF-8', |
| 55 |
pretty_print=True).decode('utf_8') |
| 56 |
|
| 57 |
def _prepare_feat_elm(self, feat_elm): |
| 58 |
|
| 59 |
feat_elm = self._add_fid_elm(feat_elm) |
| 60 |
feat_elm = self._add_filename_elm(feat_elm) |
| 61 |
feat_elm = self._add_orientation_degree_elms(feat_elm) |
| 62 |
|
| 63 |
return feat_elm |
| 64 |
|
| 65 |
def _add_fid_elm(self, feat_elm): |
| 66 |
|
| 67 |
# Create an element with the fid |
| 68 |
elm = etree.SubElement(feat_elm, "fid") |
| 69 |
elm.text = feat_elm.get('fid') |
| 70 |
|
| 71 |
return feat_elm |
| 72 |
|
| 73 |
def _add_filename_elm(self, feat_elm): |
| 74 |
|
| 75 |
# Create an element with the filename |
| 76 |
elm = etree.SubElement(feat_elm, "filename") |
| 77 |
elm.text = os.path.basename(self.inputfile) |
| 78 |
|
| 79 |
return feat_elm |
| 80 |
|
| 81 |
def _add_orientation_degree_elms(self, feat_elm): |
| 82 |
|
| 83 |
# Correct any orientation values to be a |
| 84 |
# tenth of their original value |
| 85 |
orientation_elms = feat_elm.xpath('//orientation') |
| 86 |
for elm in orientation_elms: |
| 87 |
# Add a new orientDeg element as a child to the |
| 88 |
# the orientation elm to be orientation/10 |
| 89 |
# (this applies integer division which is fine in |
| 90 |
# this instance as we are not concerned with the decimals) |
| 91 |
degree_elm = etree.SubElement(elm.getparent(), "orientDeg") |
| 92 |
degree_elm.text = str(int(elm.text) / 10) |
| 93 |
|
| 94 |
return feat_elm |
| 95 |
|
| 96 |
|
| 97 |
class prep_vml(prep_osgml): |
| 98 |
""" |
| 99 |
Preperation class for OS VectorMap Local features. |
| 100 |
|
| 101 |
""" |
| 102 |
def __init__(self, inputfile): |
| 103 |
prep_osgml.__init__(self, inputfile) |
| 104 |
self.feat_types = [ |
| 105 |
'Text', |
| 106 |
'VectorMapPoint', |
| 107 |
'Line', |
| 108 |
'RoadCLine', |
| 109 |
'Area' |
| 110 |
] |
| 111 |
|
| 112 |
|
| 113 |
class prep_osmm_topo(prep_osgml): |
| 114 |
""" |
| 115 |
Preperation class for OS MasterMap features which in addition to the work |
| 116 |
performed by `prep_osgml` adds `themes`, `descriptiveGroups` and |
| 117 |
`descriptiveTerms` elements containing a delimited string of the attributes |
| 118 |
that can appear multiple times. |
| 119 |
|
| 120 |
""" |
| 121 |
def __init__(self, inputfile): |
| 122 |
prep_osgml.__init__(self, inputfile) |
| 123 |
self.feat_types = [ |
| 124 |
'BoundaryLine', |
| 125 |
'CartographicSymbol', |
| 126 |
'CartographicText', |
| 127 |
'TopographicArea', |
| 128 |
'TopographicLine', |
| 129 |
'TopographicPoint' |
| 130 |
] |
| 131 |
self.list_seperator = ', ' |
| 132 |
|
| 133 |
def _prepare_feat_elm(self, feat_elm): |
| 134 |
|
| 135 |
feat_elm = prep_osgml._prepare_feat_elm(self, feat_elm) |
| 136 |
feat_elm = self._add_lists_elms(feat_elm) |
| 137 |
|
| 138 |
return feat_elm |
| 139 |
|
| 140 |
def _add_lists_elms(self, feat_elm): |
| 141 |
|
| 142 |
feat_elm = self._create_list_of_terms(feat_elm, 'theme') |
| 143 |
feat_elm = self._create_list_of_terms(feat_elm, 'descriptiveGroup') |
| 144 |
feat_elm = self._create_list_of_terms(feat_elm, 'descriptiveTerm') |
| 145 |
|
| 146 |
return feat_elm |
| 147 |
|
| 148 |
def _create_list_of_terms(self, feat_elm, name): |
| 149 |
text_list = feat_elm.xpath('//%s/text()' % name) |
| 150 |
if len(text_list): |
| 151 |
elm = etree.SubElement(feat_elm, "%ss" % name) |
| 152 |
elm.text = self.list_seperator.join(text_list) |
| 153 |
return feat_elm |
| 154 |
|
| 155 |
|
| 156 |
class prep_osmm_topo_qgis(prep_osmm_topo): |
| 157 |
""" |
| 158 |
Preperation class for OS MasterMap features which in addition to the work performed by |
| 159 |
`prep_osmm_topo` adds QGIS specific label attributes such as `qFont` and `aAnchorPos`. |
| 160 |
|
| 161 |
""" |
| 162 |
|
| 163 |
def __init__(self, filename): |
| 164 |
prep_osmm_topo.__init__(self, filename) |
| 165 |
|
| 166 |
# AC - define the font |
| 167 |
if os.name is 'posix': |
| 168 |
# Will probably need different font names |
| 169 |
self.fonts = ('Garamond', 'Arial', 'Roman', 'ScriptC') |
| 170 |
elif os.name is 'nt': |
| 171 |
# Ordnance Survey use |
| 172 |
# 'Lutheran', 'Normal', 'Light Roman', 'Suppressed text' |
| 173 |
self.fonts = ('GothicE', 'Monospac821 BT', 'Consolas', 'ScriptC', 'Arial Narrow') |
| 174 |
elif os.name is 'mac': |
| 175 |
# Will probably need different font name |
| 176 |
self.fonts = ('Garamond', 'Arial', 'Roman', 'ScriptC') |
| 177 |
|
| 178 |
# AC - the possible text placement positions used by QGIS |
| 179 |
self.anchorPosition = ('Bottom Left', 'Left', 'Top Left', 'Bottom', |
| 180 |
'Over', 'Top', 'Bottom Right', 'Right', 'Top Right') |
| 181 |
|
| 182 |
def _prepare_feat_elm(self, feat_elm): |
| 183 |
|
| 184 |
feat_elm = prep_osmm_topo._prepare_feat_elm(self, feat_elm) |
| 185 |
feat_elm = self._add_qgis_elms(feat_elm) |
| 186 |
|
| 187 |
return feat_elm |
| 188 |
|
| 189 |
def _add_qgis_elms(self, feat_elm): |
| 190 |
|
| 191 |
if feat_elm.tag == 'CartographicText': |
| 192 |
text_render_elm = feat_elm.xpath('//textRendering')[0] |
| 193 |
|
| 194 |
anchor_pos = int(text_render_elm.xpath('./anchorPosition/text()')[0]) |
| 195 |
try: |
| 196 |
anchor_pos = self.anchorPosition[anchor_pos] |
| 197 |
except: |
| 198 |
anchor_pos = 4 |
| 199 |
elm = etree.SubElement(text_render_elm, 'qAnchorPos') |
| 200 |
elm.text = anchor_pos |
| 201 |
|
| 202 |
font = int(text_render_elm.xpath('./font/text()')[0]) |
| 203 |
try: |
| 204 |
font = self.fonts[font] |
| 205 |
except: |
| 206 |
font = 'unknown font (%s)' % str(font) |
| 207 |
elm = etree.SubElement(text_render_elm, 'qFont') |
| 208 |
elm.text = font |
| 209 |
|
| 210 |
return feat_elm |
| 211 |
|
| 212 |
|
| 213 |
class prep_osmm_itn(prep_osgml): |
| 214 |
""" |
| 215 |
Preperation class for OS MasterMap ITN features. |
| 216 |
|
| 217 |
""" |
| 218 |
|
| 219 |
def __init__(self, filename): |
| 220 |
|
| 221 |
prep_osgml.__init__(self, filename) |
| 222 |
|
| 223 |
self.feat_types = [ |
| 224 |
'FerryLink', |
| 225 |
'FerryNode', |
| 226 |
'InformationPoint', |
| 227 |
'Road', |
| 228 |
'RoadNode', |
| 229 |
'RoadNodeInformation', |
| 230 |
'RoadLink', |
| 231 |
'RoadLinkInformation', |
| 232 |
'RoadRouteInformation' |
| 233 |
] |
| 234 |
|
| 235 |
def _prepare_feat_elm(self, feat_elm): |
| 236 |
|
| 237 |
feat_elm = prep_osgml._prepare_feat_elm(self, feat_elm) |
| 238 |
feat_elm = self._expose_links(feat_elm) |
| 239 |
|
| 240 |
return feat_elm |
| 241 |
|
| 242 |
def _expose_links(self, feat_elm): |
| 243 |
|
| 244 |
link_list = feat_elm.xpath('//networkMember | //directedLink | //directedNode | //referenceToRoadLink | //referenceToRoadNode | //referenceToTopographicArea') |
| 245 |
for elm in link_list: |
| 246 |
for name in elm.attrib: |
| 247 |
value = elm.get(name) |
| 248 |
if name == 'href': |
| 249 |
name = '%s_%s' % (elm.tag, 'ref') |
| 250 |
value = value[1:] |
| 251 |
elif name == 'orientation': |
| 252 |
name = '%s_%s' % (elm.tag, name) |
| 253 |
value = '1' if value == '+' else '0' |
| 254 |
sub_elm = etree.SubElement(elm, name) |
| 255 |
sub_elm.text = value |
| 256 |
|
| 257 |
return feat_elm |
| 258 |
|
| 259 |
|
| 260 |
class prep_addressbase(): |
| 261 |
""" |
| 262 |
Simple preperation of AddressBase data |
| 263 |
|
| 264 |
""" |
| 265 |
def __init__(self, inputfile): |
| 266 |
self.inputfile = inputfile |
| 267 |
self.feat_types = ['Address'] |
| 268 |
|
| 269 |
def get_feat_types(self): |
| 270 |
return self.feat_types |
| 271 |
|
| 272 |
def prepare_feature(self, feat_str): |
| 273 |
|
| 274 |
# Parse the xml string into something useful |
| 275 |
feat_elm = etree.fromstring(feat_str) |
| 276 |
feat_elm = self._prepare_feat_elm(feat_elm) |
| 277 |
|
| 278 |
return etree.tostring(feat_elm, |
| 279 |
encoding='UTF-8', |
| 280 |
pretty_print=True).decode('utf_8') |
| 281 |
|
| 282 |
def _prepare_feat_elm(self, feat_elm): |
| 283 |
|
| 284 |
feat_elm = self._drop_gmlid(feat_elm) |
| 285 |
|
| 286 |
return feat_elm |
| 287 |
|
| 288 |
def _drop_gmlid(self, feat_elm): |
| 289 |
|
| 290 |
feat_elm.attrib.pop('id') |
| 291 |
|
| 292 |
return feat_elm |