Created
November 17, 2016 11:27
-
-
Save timtroendle/1435555934865dc4e4e59ca5d1babb4a to your computer and use it in GitHub Desktop.
Script to transform UKMap and LSOA data into a form readable by CiMo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import geopandas as gpd | |
import click | |
click.disable_unicode_literals_warning = True | |
ACTUAL_LSOA_COLUMN_NAME = 'LSOA_CODE' | |
NEEDED_LSOA_COLUMN_NAME = 'LSOAcode' | |
def cast_gtn_to_int(path_to_file, path_to_modified_file): | |
data = gpd.read_file(path_to_file.as_posix()) | |
data.GTN = [int(gtn) for gtn in data.GTN] | |
data.to_file(path_to_modified_file.as_posix()) | |
print('Casted GTN to integer and wrote results to {}.'.format(path_to_modified_file)) | |
def rename_lsoa_column(path_to_file, path_to_modified_file): | |
data = gpd.read_file(path_to_file.as_posix()) | |
data[NEEDED_LSOA_COLUMN_NAME] = data[ACTUAL_LSOA_COLUMN_NAME] | |
del data[ACTUAL_LSOA_COLUMN_NAME] | |
data.to_file(path_to_modified_file.as_posix()) | |
print('Renamed column "{}" to "{}" and wrote results to {}.'.format(ACTUAL_LSOA_COLUMN_NAME, | |
NEEDED_LSOA_COLUMN_NAME, | |
path_to_modified_file)) | |
class ShapefileParamType(click.ParamType): | |
name = 'Shapefile' | |
def convert(self, value, param, ctx): | |
path = Path(value) | |
if not path.suffix == '.shp': | |
self.fail('File "{}" is not a shape file.'.format(value)) | |
if not path.exists(): | |
self.fail('Path "{}" does not exist.'.format(value)) | |
return path | |
@click.command(name='ukmap-converter') | |
@click.argument('shape_file', type=ShapefileParamType()) | |
def command_line_interface(shape_file): | |
"""Convertes UKMap files to be read by CiMo. | |
The script will convert GTN from float to integer, and rename the LSOAcode column. | |
Result files will be written to the same folder with a 'modified' extension. | |
\b | |
Parameters: | |
shape_file: The UKMap based shape file to be modified. | |
""" | |
path_to_modified_file = shape_file.with_name(shape_file.stem + '_modified') | |
cast_gtn_to_int(shape_file, path_to_modified_file) | |
rename_lsoa_column(path_to_modified_file, path_to_modified_file) | |
if __name__ == '__main__': | |
command_line_interface() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could this script be incorporated into MasterBeforePreProcess.py?