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
|
#!/usr/bin/python
## Copyright (c) 2011 Astun Technology
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
## THE SOFTWARE.
"""
A collection of classes used to manipulate Ordnance Survey GB GML data,
used with prepgml4ogr.py. (But here used/called from Stetl Filter)
"""
import os
from lxml import etree
class prep_osgml():
"""
Base class that provides the main interface methods `prepare_feature` and
`get_feat_types` and performs basic manipulation such as exposing the fid,
adding and element containing the filename of the source and adding an
element with the orientation in degrees.
"""
def __init__(self, inputfile):
self.inputfile = inputfile
self.feat_types = []
def get_feat_types(self):
return self.feat_types
def prepare_feature(self, feat_str):
# Parse the xml string into something useful
feat_elm = etree.fromstring(feat_str)
feat_elm = self._prepare_feat_elm(feat_elm)
return etree.tostring(feat_elm,
encoding='UTF-8',
pretty_print=True).decode('utf_8')
def _prepare_feat_elm(self, feat_elm):
feat_elm = self._add_fid_elm(feat_elm)
feat_elm = self._add_filename_elm(feat_elm)
feat_elm = self._add_orientation_degree_elms(feat_elm)
return feat_elm
def _add_fid_elm(self, feat_elm):
# Create an element with the fid
elm = etree.SubElement(feat_elm, "fid")
elm.text = feat_elm.get('fid')
return feat_elm
def _add_filename_elm(self, feat_elm):
# Create an element with the filename
elm = etree.SubElement(feat_elm, "filename")
elm.text = os.path.basename(self.inputfile)
return feat_elm
def _add_orientation_degree_elms(self, feat_elm):
# Correct any orientation values to be a
# tenth of their original value
orientation_elms = feat_elm.xpath('//orientation')
for elm in orientation_elms:
# Add a new orientDeg element as a child to the
# the orientation elm to be orientation/10
# (this applies integer division which is fine in
# this instance as we are not concerned with the decimals)
degree_elm = etree.SubElement(elm.getparent(), "orientDeg")
degree_elm.text = str(int(elm.text) / 10)
return feat_elm
class prep_vml(prep_osgml):
"""
Preperation class for OS VectorMap Local features.
"""
def __init__(self, inputfile):
prep_osgml.__init__(self, inputfile)
self.feat_types = [
'Text',
'VectorMapPoint',
'Line',
'RoadCLine',
'Area'
]
class prep_osmm_topo(prep_osgml):
"""
Preperation class for OS MasterMap features which in addition to the work
performed by `prep_osgml` adds `themes`, `descriptiveGroups` and
`descriptiveTerms` elements containing a delimited string of the attributes
that can appear multiple times.
"""
def __init__(self, inputfile):
prep_osgml.__init__(self, inputfile)
self.feat_types = [
'BoundaryLine',
'CartographicSymbol',
'CartographicText',
'TopographicArea',
'TopographicLine',
'TopographicPoint'
]
self.list_seperator = ', '
def _prepare_feat_elm(self, feat_elm):
feat_elm = prep_osgml._prepare_feat_elm(self, feat_elm)
feat_elm = self._add_lists_elms(feat_elm)
return feat_elm
def _add_lists_elms(self, feat_elm):
feat_elm = self._create_list_of_terms(feat_elm, 'theme')
feat_elm = self._create_list_of_terms(feat_elm, 'descriptiveGroup')
feat_elm = self._create_list_of_terms(feat_elm, 'descriptiveTerm')
return feat_elm
def _create_list_of_terms(self, feat_elm, name):
text_list = feat_elm.xpath('//%s/text()' % name)
if len(text_list):
elm = etree.SubElement(feat_elm, "%ss" % name)
elm.text = self.list_seperator.join(text_list)
return feat_elm
class prep_osmm_topo_qgis(prep_osmm_topo):
"""
Preperation class for OS MasterMap features which in addition to the work performed by
`prep_osmm_topo` adds QGIS specific label attributes such as `qFont` and `aAnchorPos`.
"""
def __init__(self, filename):
prep_osmm_topo.__init__(self, filename)
# AC - define the font
if os.name is 'posix':
# Will probably need different font names
self.fonts = ('Garamond', 'Arial', 'Roman', 'ScriptC')
elif os.name is 'nt':
# Ordnance Survey use
# 'Lutheran', 'Normal', 'Light Roman', 'Suppressed text'
self.fonts = ('GothicE', 'Monospac821 BT', 'Consolas', 'ScriptC', 'Arial Narrow')
elif os.name is 'mac':
# Will probably need different font name
self.fonts = ('Garamond', 'Arial', 'Roman', 'ScriptC')
# AC - the possible text placement positions used by QGIS
self.anchorPosition = ('Bottom Left', 'Left', 'Top Left', 'Bottom',
'Over', 'Top', 'Bottom Right', 'Right', 'Top Right')
def _prepare_feat_elm(self, feat_elm):
feat_elm = prep_osmm_topo._prepare_feat_elm(self, feat_elm)
feat_elm = self._add_qgis_elms(feat_elm)
return feat_elm
def _add_qgis_elms(self, feat_elm):
if feat_elm.tag == 'CartographicText':
text_render_elm = feat_elm.xpath('//textRendering')[0]
anchor_pos = int(text_render_elm.xpath('./anchorPosition/text()')[0])
try:
anchor_pos = self.anchorPosition[anchor_pos]
except:
anchor_pos = 4
elm = etree.SubElement(text_render_elm, 'qAnchorPos')
elm.text = anchor_pos
font = int(text_render_elm.xpath('./font/text()')[0])
try:
font = self.fonts[font]
except:
font = 'unknown font (%s)' % str(font)
elm = etree.SubElement(text_render_elm, 'qFont')
elm.text = font
return feat_elm
class prep_osmm_itn(prep_osgml):
"""
Preperation class for OS MasterMap ITN features.
"""
def __init__(self, filename):
prep_osgml.__init__(self, filename)
self.feat_types = [
'FerryLink',
'FerryNode',
'InformationPoint',
'Road',
'RoadNode',
'RoadNodeInformation',
'RoadLink',
'RoadLinkInformation',
'RoadRouteInformation'
]
def _prepare_feat_elm(self, feat_elm):
feat_elm = prep_osgml._prepare_feat_elm(self, feat_elm)
feat_elm = self._expose_links(feat_elm)
return feat_elm
def _expose_links(self, feat_elm):
link_list = feat_elm.xpath('//networkMember | //directedLink | //directedNode | //referenceToRoadLink | //referenceToRoadNode | //referenceToTopographicArea')
for elm in link_list:
for name in elm.attrib:
value = elm.get(name)
if name == 'href':
name = '%s_%s' % (elm.tag, 'ref')
value = value[1:]
elif name == 'orientation':
name = '%s_%s' % (elm.tag, name)
value = '1' if value == '+' else '0'
sub_elm = etree.SubElement(elm, name)
sub_elm.text = value
return feat_elm
class prep_addressbase():
"""
Simple preperation of AddressBase data
"""
def __init__(self, inputfile):
self.inputfile = inputfile
self.feat_types = ['Address']
def get_feat_types(self):
return self.feat_types
def prepare_feature(self, feat_str):
# Parse the xml string into something useful
feat_elm = etree.fromstring(feat_str)
feat_elm = self._prepare_feat_elm(feat_elm)
return etree.tostring(feat_elm,
encoding='UTF-8',
pretty_print=True).decode('utf_8')
def _prepare_feat_elm(self, feat_elm):
feat_elm = self._drop_gmlid(feat_elm)
return feat_elm
def _drop_gmlid(self, feat_elm):
feat_elm.attrib.pop('id')
return feat_elm
|