Skip to content

Instantly share code, notes, and snippets.

@timtroendle
Created November 17, 2016 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timtroendle/1435555934865dc4e4e59ca5d1babb4a to your computer and use it in GitHub Desktop.
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.
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()
@alllaw
Copy link

alllaw commented Dec 19, 2016

Could this script be incorporated into MasterBeforePreProcess.py?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment