| Conditions | 31 |
| Total Lines | 135 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like _import_osm_dom() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # ~*~ coding: utf-8 ~*~ |
||
| 33 | def _import_osm_dom(osma, session, dom): |
||
| 34 | """ Import a DOM tree from OSM XML into an OSMAlchemy model. |
||
| 35 | |||
| 36 | Not called directly; used by _import_osm_xml and _import_osm_file. |
||
| 37 | """ |
||
| 38 | |||
| 39 | def _dom_attrs_to_any(e, element): |
||
| 40 | if "version" in e.attributes.keys(): |
||
| 41 | element.version = int(e.attributes["version"].value) |
||
| 42 | if "changeset" in e.attributes.keys(): |
||
| 43 | element.changeset = int(e.attributes["changeset"].value) |
||
| 44 | if "user" in e.attributes.keys(): |
||
| 45 | element.user = e.attributes["user"].value |
||
| 46 | if "uid" in e.attributes.keys(): |
||
| 47 | element.uid = int(e.attributes["uid"].value) |
||
| 48 | if "visible" in e.attributes.keys(): |
||
| 49 | element.visible = True if e.attributes["visible"].value == "true" else False |
||
| 50 | if "timestamp" in e.attributes.keys(): |
||
| 51 | element.timestamp = dateutil.parser.parse(e.attributes["timestamp"].value) |
||
| 52 | |||
| 53 | def _dom_tags_to_any(e, element): |
||
| 54 | # Target dictionary |
||
| 55 | tags = {} |
||
| 56 | |||
| 57 | # Iterate over all <tag /> nodes in the DOM element |
||
| 58 | for t in e.getElementsByTagName("tag"): |
||
| 59 | # Append data to tags |
||
| 60 | tags[t.attributes["k"].value] = t.attributes["v"].value |
||
| 61 | |||
| 62 | # Store tags dictionary in object |
||
| 63 | element.tags = tags |
||
| 64 | |||
| 65 | def _dom_to_node(e): |
||
| 66 | with session.no_autoflush: |
||
| 67 | # Get mandatory node id |
||
| 68 | id = int(e.attributes["id"].value) |
||
| 69 | |||
| 70 | # Find object in database and create if non-existent |
||
| 71 | node = session.query(osma.Node).filter_by(id=id).scalar() |
||
| 72 | if node is None: |
||
| 73 | node = osma.Node(id=id) |
||
| 74 | |||
| 75 | # Store mandatory latitude and longitude |
||
| 76 | node.latitude = e.attributes["lat"].value |
||
| 77 | node.longitude = e.attributes["lon"].value |
||
| 78 | |||
| 79 | # Store other attributes and tags |
||
| 80 | _dom_attrs_to_any(e, node) |
||
| 81 | _dom_tags_to_any(e, node) |
||
| 82 | |||
| 83 | # Add to session |
||
| 84 | session.add(node) |
||
| 85 | session.commit() |
||
| 86 | |||
| 87 | def _dom_to_way(e): |
||
| 88 | with session.no_autoflush: |
||
| 89 | # Get mandatory way id |
||
| 90 | id = int(e.attributes["id"].value) |
||
| 91 | |||
| 92 | # Find object in database and create if non-existent |
||
| 93 | way = session.query(osma.Way).filter_by(id=id).scalar() |
||
| 94 | if way is None: |
||
| 95 | way = osma.Way(id=id) |
||
| 96 | |||
| 97 | # Find all related nodes |
||
| 98 | for n in e.getElementsByTagName("nd"): |
||
| 99 | # Get node id and find object |
||
| 100 | ref = int(n.attributes["ref"].value) |
||
| 101 | node = session.query(osma.Node).filter_by(id=ref).one() |
||
| 102 | # Append to nodes in way |
||
| 103 | way.nodes.append(node) |
||
| 104 | |||
| 105 | # Store other attributes and tags |
||
| 106 | _dom_attrs_to_any(e, way) |
||
| 107 | _dom_tags_to_any(e, way) |
||
| 108 | |||
| 109 | # Add to session |
||
| 110 | session.add(way) |
||
| 111 | session.commit() |
||
| 112 | |||
| 113 | def _dom_to_relation(e): |
||
| 114 | with session.no_autoflush: |
||
| 115 | # Get mandatory way id |
||
| 116 | id = int(e.attributes["id"].value) |
||
| 117 | |||
| 118 | # Find object in database and create if non-existent |
||
| 119 | relation = session.query(osma.Relation).filter_by(id=id).scalar() |
||
| 120 | if relation is None: |
||
| 121 | relation = osma.Relation(id=id) |
||
| 122 | |||
| 123 | # Find all members |
||
| 124 | for m in e.getElementsByTagName("member"): |
||
| 125 | # Get member attributes |
||
| 126 | ref = int(m.attributes["ref"].value) |
||
| 127 | type = m.attributes["type"].value |
||
| 128 | |||
| 129 | if "role" in m.attributes.keys(): |
||
| 130 | role = m.attributes["role"].value |
||
| 131 | else: |
||
| 132 | role = "" |
||
| 133 | element = session.query(osma.Element).filter_by(id=ref, type=type).scalar() |
||
| 134 | if element is None: |
||
| 135 | # We do not know the member yet, create a stub |
||
| 136 | if type == "node": |
||
| 137 | element = osma.Node(id=ref) |
||
| 138 | elif type == "way": |
||
| 139 | element = osma.Way(id=ref) |
||
| 140 | elif type == "relation": |
||
| 141 | element = osma.Relation(id=ref) |
||
| 142 | # We need to commit here because element could be repeated |
||
| 143 | session.add(element) |
||
| 144 | session.commit() |
||
| 145 | # Append to members |
||
| 146 | relation.members.append((element, role)) |
||
| 147 | |||
| 148 | # Store other attributes and tags |
||
| 149 | _dom_attrs_to_any(e, relation) |
||
| 150 | _dom_tags_to_any(e, relation) |
||
| 151 | |||
| 152 | # Add to session |
||
| 153 | session.add(relation) |
||
| 154 | session.commit() |
||
| 155 | |||
| 156 | # Get root element |
||
| 157 | osm = dom.documentElement |
||
| 158 | |||
| 159 | # Iterate over children to find nodes, ways and relations |
||
| 160 | for e in osm.childNodes: |
||
| 161 | # Determine element type |
||
| 162 | if e.nodeName == "node": |
||
| 163 | _dom_to_node(e) |
||
| 164 | elif e.nodeName == "way": |
||
| 165 | _dom_to_way(e) |
||
| 166 | elif e.nodeName == "relation": |
||
| 167 | _dom_to_relation(e) |
||
| 168 | |||
| 194 |