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