Skip to content

Instantly share code, notes, and snippets.

@JonnoFTW
Created August 16, 2017 04:33
Show Gist options
  • Save JonnoFTW/2831d2743c8610a590405825b880cc15 to your computer and use it in GitHub Desktop.
Save JonnoFTW/2831d2743c8610a590405825b880cc15 to your computer and use it in GitHub Desktop.
shapefile renderer for pyramid
writer = shapefile.Writer(shapeType=shapefile.POINT)
writer.autoBalance = 1
# iterate through the entire dataset and extract the field names
headers = {}
for row in data:
headers.update({k.replace('PID_', '').split(' ')[0]: type(v) for k, v in row.items() if v is not None})
del headers['pos']
writer.field('latitude', 'N', '32', 15)
writer.field('longitude', 'N', '32', 15)
header_map = {
int: ('N',),
float: ('N', 7, 3),
str: ('C', 30),
datetime: ('T',)
}
for h, t in headers.items():
writer.field(h, *header_map[t])
def check_field(f, r):
if f[0] in r:
return r[f[0]]
else:
return ""
for row in data:
row = {k.replace('PID_', '').split(' ')[0]: v for k, v in row.items()}
if 'pos' is None:
continue
if 'pos' in row and row['pos'] is not None:
row['latitude'] = row['pos']['coordinates'][1]
row['longitude'] = row['pos']['coordinates'][0]
del row['pos']
writer.point(row['longitude'], row['latitude'])
writer.record(*[check_field(f, row) for f in writer.fields])
dbfout = BytesIO()
shpout = BytesIO()
shxout = BytesIO()
writer.save(shp=shpout, dbf=dbfout, shx=shxout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment