| Conditions | 2 |
| Total Lines | 97 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
| 1 | """The central module containing all code dealing with importing OSM data. |
||
| 81 | def add_metadata(): |
||
| 82 | """Writes metadata JSON string into table comment.""" |
||
| 83 | # Prepare variables |
||
| 84 | osm_config = egon.data.config.datasets()["openstreetmap"] |
||
| 85 | spatial_and_date = os.path.basename( |
||
| 86 | osm_config["original_data"]["target"]["path"] |
||
| 87 | ).split("-") |
||
| 88 | spatial_extend = spatial_and_date[0] |
||
| 89 | osm_data_date = ( |
||
| 90 | "20" |
||
| 91 | + spatial_and_date[1][0:2] |
||
| 92 | + "-" |
||
| 93 | + spatial_and_date[1][2:4] |
||
| 94 | + "-" |
||
| 95 | + spatial_and_date[1][4:6] |
||
| 96 | ) |
||
| 97 | osm_url = osm_config["original_data"]["source"]["url"] |
||
| 98 | |||
| 99 | # Insert metadata for each table |
||
| 100 | licenses = [ |
||
| 101 | { |
||
| 102 | "name": "Open Data Commons Open Database License 1.0", |
||
| 103 | "title": "", |
||
| 104 | "path": "https://opendatacommons.org/licenses/odbl/1.0/", |
||
| 105 | "instruction": ( |
||
| 106 | "You are free: To Share, To Create, To Adapt;" |
||
| 107 | " As long as you: Attribute, Share-Alike, Keep open!" |
||
| 108 | ), |
||
| 109 | "attribution": "© Reiner Lemoine Institut", |
||
| 110 | } |
||
| 111 | ] |
||
| 112 | for table in osm_config["processed"]["tables"]: |
||
| 113 | table_suffix = table.split("_")[1] |
||
| 114 | meta = { |
||
| 115 | "title": f"OpenStreetMap (OSM) - Germany - {table_suffix}", |
||
| 116 | "description": ( |
||
| 117 | "OpenStreetMap is a free, editable map of the" |
||
| 118 | " whole world that is being built by volunteers" |
||
| 119 | " largely from scratch and released with" |
||
| 120 | " an open-content license." |
||
| 121 | ), |
||
| 122 | "language": ["EN", "DE"], |
||
| 123 | "spatial": { |
||
| 124 | "location": "", |
||
| 125 | "extent": f"{spatial_extend}", |
||
| 126 | "resolution": "", |
||
| 127 | }, |
||
| 128 | "temporal": { |
||
| 129 | "referenceDate": f"{osm_data_date}", |
||
| 130 | "timeseries": { |
||
| 131 | "start": "", |
||
| 132 | "end": "", |
||
| 133 | "resolution": "", |
||
| 134 | "alignment": "", |
||
| 135 | "aggregationType": "", |
||
| 136 | }, |
||
| 137 | }, |
||
| 138 | "sources": [ |
||
| 139 | { |
||
| 140 | "title": ( |
||
| 141 | "Geofabrik - Download - OpenStreetMap Data Extracts" |
||
| 142 | ), |
||
| 143 | "description": ( |
||
| 144 | 'Data dump taken on "referenceDate",' |
||
| 145 | f" i.e. {osm_data_date}." |
||
| 146 | " A subset of this is selected using osm2pgsql" |
||
| 147 | ' using the style file "oedb.style".' |
||
| 148 | ), |
||
| 149 | "path": f"{osm_url}", |
||
| 150 | "licenses": licenses, |
||
| 151 | } |
||
| 152 | ], |
||
| 153 | "licenses": licenses, |
||
| 154 | "contributors": [ |
||
| 155 | { |
||
| 156 | "title": "Guido Pleßmann", |
||
| 157 | "email": "http://github.com/gplssm", |
||
| 158 | "date": time.strftime("%Y-%m-%d"), |
||
| 159 | "object": "", |
||
| 160 | "comment": "Imported data", |
||
| 161 | } |
||
| 162 | ], |
||
| 163 | "metaMetadata": { |
||
| 164 | "metadataVersion": "OEP-1.4.0", |
||
| 165 | "metadataLicense": { |
||
| 166 | "name": "CC0-1.0", |
||
| 167 | "title": "Creative Commons Zero v1.0 Universal", |
||
| 168 | "path": ( |
||
| 169 | "https://creativecommons.org/publicdomain/zero/1.0/" |
||
| 170 | ), |
||
| 171 | }, |
||
| 172 | }, |
||
| 173 | } |
||
| 174 | |||
| 175 | meta_json = "'" + json.dumps(meta) + "'" |
||
| 176 | |||
| 177 | db.submit_comment(meta_json, "openstreetmap", table) |
||
| 178 |