Created
July 2, 2019 10:54
-
-
Save ficapy/db90a82a690d417f66128831a9c914ab to your computer and use it in GitHub Desktop.
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 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