Skip to content

Instantly share code, notes, and snippets.

@alfredo
Last active January 2, 2020 20:22
Show Gist options
  • Save alfredo/909f1dc74a41c8eb1b89399f33b1b670 to your computer and use it in GitHub Desktop.
Save alfredo/909f1dc74a41c8eb1b89399f33b1b670 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import pandas as pd
import shapefile as shp
def read_shape_file(*, file_path, encoding="ISO-8859-1"):
"""Reads the shape file and specifies shp file encoding."""
return shp.Reader(file_path, encoding=encoding)
def prepare_data_frame(*, shape_file):
"""Transforms the shapefile into a panda's dataframe object.
This object will contain the column values and data points of the shape."""
column_names = [r[0] for r in shape_file.fields][1:]
records = shape_file.records()
shape_points = [s.points for s in shape_file.shapes()]
data_frame = pd.DataFrame(columns=column_names, data=records)
data_frame = data_frame.assign(coords=shape_points)
return data_frame
def inspect_data_frame(*, data_frame):
"""Prints all States and Values available in the DataFrame object"""
print("Available states: {}".format(set(data_frame.EDO_LEY)))
print("Available values: {}".format(set(data_frame.DPHLIL_LEY)))
def main():
shape_file = read_shape_file(file_path="./data/PHLITL_2000/PHLITL_2000.shp")
data_frame = prepare_data_frame(shape_file=shape_file)
inspect_data_frame(data_frame=data_frame)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment