import copy import shapefile shape_name = 'scratch\\broward_grid_pixelmap' shp = shapefile.Reader(shape_name) attrib_idx = shapefile.load_attrib_idx(shape_name) wr = shapefile.writer_like(shape_name) for i in range(shp.numRecords): shape = shp.shape(i) rec = shp.record(i) wr.poly([shape.points], shapeType=shape.shapeType) pixels = rec[attrib_idx['pixels']] raw = pixels.split() for i, r in enumerate(raw): if r.startswith('['): raw[i] = copy.deepcopy(r[1:-1]) #print i,raw[i],raw rec[attrib_idx['pixels']] = ' '.join(raw) wr.record(rec) wr.save('scratch\\borward_grid_pixelmap_fix')
pg_cellnum = np.array(pg_records['cellnum']) #pg_delx,pg_dely = np.array(pg_records['delx']),np.array(pg_records['dely']) print 'done' #--writer instance wr = shapefile.writer_like(grid_shapename) wr.field('pixels',fieldType='C',size=50) wr.field('fractions',fieldType='C',size=50) #--load the grid... print 'loading grid shapefile' #grid_shapes,grid_records = shapefile.load_as_dict(grid_shapename) grid_shp = shapefile.Reader(grid_shapename) attrib_idx = shapefile.load_attrib_idx(grid_shapename) for i in range(grid_shp.numRecords): if i % 50 == 0: print 'cell ',i+1,' of ',grid_shp.numRecords #--get this shape and record shp,rec = grid_shp.shape(i),grid_shp.record(i) cn = rec[attrib_idx['cellnum']] #--get the indices of cells that have this cell number idxs = np.argwhere(pg_cellnum==cn) #--if atleast one pixel intersected this cell if idxs.shape[0] > 0: tot_area = 0.0 pixs = [] areas = [] for idx in idxs:
import copy import shapefile shape_name = "scratch\\broward_grid_pixelmap" shp = shapefile.Reader(shape_name) attrib_idx = shapefile.load_attrib_idx(shape_name) wr = shapefile.writer_like(shape_name) for i in range(shp.numRecords): shape = shp.shape(i) rec = shp.record(i) wr.poly([shape.points], shapeType=shape.shapeType) pixels = rec[attrib_idx["pixels"]] raw = pixels.split() for i, r in enumerate(raw): if r.startswith("["): raw[i] = copy.deepcopy(r[1:-1]) # print i,raw[i],raw rec[attrib_idx["pixels"]] = " ".join(raw) wr.record(rec) wr.save("scratch\\borward_grid_pixelmap_fix")