Skip to content

Instantly share code, notes, and snippets.

@ficapy
Created July 2, 2019 10:54
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 ficapy/db90a82a690d417f66128831a9c914ab to your computer and use it in GitHub Desktop.
Save ficapy/db90a82a690d417f66128831a9c914ab to your computer and use it in GitHub Desktop.
from shapely.geometry import mapping, shape
import fiona
import csv
record_index = 'grid filed1 filed2'
record_index = record_index.split()
all_record = {}
with open("resut.csv", 'r') as f:
r = csv.DictReader(f)
for i in r:
all_record[i['\ufeffgrid']] = i
with fiona.collection('demo_area.shp', 'r') as polygon:
schema = polygon.schema.copy()
for i in record_index:
if i != 'grid':
schema['properties'][i] = 'int'
schema['grid'] = 'str'
with fiona.collection('test_area.shp', 'w', 'ESRI Shapefile', schema) as output:
polygons = [elem for elem in polygon]
for poly in polygons:
res = {}
ret_pro = {**poly['properties'], **all_record[poly['properties']['grid']]}
del ret_pro['\ufeffgrid']
res['properties'] = ret_pro
res['geometry'] = mapping(shape(poly['geometry']))
output.write(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment