lines,l_records = shapefile.load_as_dict(line_shapename)


#--tolerance distance
warn_dist = 50.0
tol_dist = 0.0

#--set the writer instance
in_shapename = '..\\_gis\\shapes\\sw_structures'
out_shapename = '..\\_gis\\scratch\\sw_structures_reaches'
shp_points = shapefile.Reader(in_shapename)
points = shp_points.shapes()
p_records = shp_points.records()
p_name_idx,dwnstr_idx = 4,6

wr = shapefile.writer_like(in_shapename)
wr.field('upstream',fieldType='N',size=10)


f_warn = open('check_structures.warn','w')
for p,r in zip(points,p_records):            
    s_name = r[p_name_idx]
    s_dwn_reach = r[dwnstr_idx]

    #--find the index of the downstream reach
    dw_idx = None
    for i,reach in enumerate(l_records['reach']):
        if reach == s_dwn_reach:
            dw_idx = i
            break
    if dw_idx == None:
示例#2
0
import pandas
import shapefile

shape = shapefile.Reader('..\\shapes\\NEXRAD_pixels_florida_west')

df_nex = pandas.read_csv('NEXRAD.csv', nrows=1)
df_keys_nex = list(df_nex.keys())

df_pet = pandas.read_csv('PET.csv', nrows=1)
df_keys_pet = list(df_pet.keys())

wr = shapefile.writer_like('..\\shapes\\NEXRAD_pixels_florida')
for i in range(shape.numRecords):
    pnum = shape.record(i)[0]
    if str(pnum) in df_keys_nex:
        shp = shape.shape(i)
        wr.poly([shp.points], shapeType=shp.shapeType)
        wr.record([pnum])
wr.save('..\\shapes\\df_pixels')

wr = shapefile.writer_like('..\\shapes\\NEXRAD_pixels_florida')
for i in range(shape.numRecords):
    pnum = shape.record(i)[0]
    if str(pnum) in df_keys_pet:
        shp = shape.shape(i)
        wr.poly([shp.points], shapeType=shp.shapeType)
        wr.record([pnum])
wr.save('..\\shapes\\df_pixels_pet')
print 'loading grid shapefile'
grid_shp = shapefile.Reader(grid_shapename)
grid_shapes = grid_shp.shapes()
grid_recs = grid_shp.records()
grid_polys = []
print 'building grid polygons'
for i, (shape, rec) in enumerate(zip(grid_shapes, grid_recs)):
    if i % 500 == 0:
        print 'record', i, '\r',
    poly = Polygon(shape.points)
    if not poly.is_valid:
        raise Exception('invalid nexrad geometry' + str(rec))
    grid_polys.append(poly)

print '\nintersecting grid and nexrad polygons'
wr = shapefile.writer_like(grid_shapename)
wr.field('nex_pix', fieldType='C', size=50)
wr.field('nex_frac', fieldType='C', size=50)
for i, (gshape, grec,
        gpoly) in enumerate(zip(grid_shapes, grid_recs, grid_polys)):

    print 'record', i, '\r',
    #--search for intersections
    #--sum up the total intersected area - for grid cells not completely covered
    pixs, areas = [], []
    tot_area = 0.0
    for npoly, pix in zip(nex_polys, nex_pixelnums):
        if gpoly.intersects(npoly):
            ipoly = gpoly.intersection(npoly)
            area = ipoly.area
            tot_area += area
        if match == None:
            for i, station in enumerate(station_names):
                if fuzz.partial_ratio(name, station) > 80:
                    match = station
                    break
        #--try for a token sort match
        if match == None:
            for i, station in enumerate(station_names):
                if fuzz.token_sort_ratio(name, station) > 80:
                    match = station
                    break
        if match == None:
            #raise LookupError('could not found a match for primary structure'+str(name))
            print 'could not found a match for primary structure' + str(name)
            shape_dbhydro_names.append('none')
        else:
            shape_dbhydro_names.append(match)
    else:
        shape_dbhydro_names.append('none')

wr = shapefile.writer_like(shapename)
wr.field('dbhydro', fieldType='C', size=50)
shp = shapefile.Reader(shapename)
records = shp.records()

for shape, record, dbhydro_name in zip(shapes, records, shape_dbhydro_names):
    wr.poly([shape.points], shapeType=shape.shapeType)
    record.append(dbhydro_name)
    wr.record(record)
wr.save('..\\_gis\\scratch\\sw_structures')
import numpy as np
import shapefile

#--load the master grid file
grid_shapename = 'shapes\\broward_grid_master'
grid_shp = shapefile.Reader(grid_shapename)
grid_polygons = []

wr = shapefile.writer_like(grid_shapename)

wr_extent = shapefile.Writer()
wr_extent.field('somejunk',fieldType='C',size=10)

xmin,xmax = 1.0e+10,-1.0e+10
ymin,ymax = 1.0e+10,-1.0e+10
for i in range(grid_shp.numRecords):
    shape = grid_shp.shape(i)
    rec = grid_shp.record(i)
    points = shape.points
    arr = np.array(points)
    if arr[:,0].max() > xmax:
        xmax = arr[:,0].max()
    if arr[:,0].min() < xmin:
        xmin = arr[:,0].min()

    if arr[:,1].max() > ymax:
        ymax = arr[:,1].max()
    if arr[:,1].min() < ymin:
        ymin = arr[:,1].min()

    points_reorder = [points[0],points[3],points[2],points[1],points[0]]
import pandas
import shapefile

shape = shapefile.Reader('..\\shapes\\NEXRAD_pixels_florida_west')

df_nex = pandas.read_csv('NEXRAD.csv',nrows=1)
df_keys_nex = list(df_nex.keys())

df_pet = pandas.read_csv('PET.csv',nrows=1)
df_keys_pet = list(df_pet.keys())


wr = shapefile.writer_like('..\\shapes\\NEXRAD_pixels_florida')
for i in range(shape.numRecords):
    pnum = shape.record(i)[0]
    if str(pnum) in df_keys_nex:
        shp = shape.shape(i)
        wr.poly([shp.points],shapeType=shp.shapeType)
        wr.record([pnum])
wr.save('..\\shapes\\df_pixels')

wr = shapefile.writer_like('..\\shapes\\NEXRAD_pixels_florida')
for i in range(shape.numRecords):
    pnum = shape.record(i)[0]
    if str(pnum) in df_keys_pet:
        shp = shape.shape(i)
        wr.poly([shp.points],shapeType=shp.shapeType)
        wr.record([pnum])
wr.save('..\\shapes\\df_pixels_pet')

示例#7
0
print 'calc rbf...'
interp_topo = np.zeros((simple.grid.nrow, simple.grid.ncol))
for i in range(simple.grid.nrow):
    for j in range(simple.grid.ncol):
        x, y = simple.grid.cols[j] + (simple.grid.delta /
                                      2.0), simple.grid.rows[i] + (
                                          simple.grid.delta / 2.0)
        itopo = rbfi(x, y, 1)
        interp_topo[i, j] = itopo
interp_topo[np.where(interp_topo < 100.0)] = 100.0
print interp_topo.min()
interp_topo = np.flipud(interp_topo)
np.savetxt('ref\\interp_topo.ref', interp_topo, fmt=' %15.6E')

print 'loading grid shape'
shapes, records = shapefile.load_as_dict('shapes\\simple_grid')
wr = shapefile.writer_like('shapes\\simple_grid')

print 'writing new grid shape'
ibound = np.zeros((simple.grid.nrow, simple.grid.ncol))
for shape, row, col, ibnd in zip(shapes, records['row'], records['column'],
                                 records['ibound']):
    wr.poly([shape.points])
    ibound[row - 1, col - 1] = ibnd
    topo = interp_topo[row - 1, col - 1]
    rec = [row, col, ibnd, topo]
    wr.record(rec)

wr.save('shapes\\simple_topo')
np.savetxt('ref\\ibound.ref', ibound, fmt=' %4d')
示例#8
0
    if os.path.exists(conc_plt_dir+pname):
        conc_pltnames.append(conc_plt_dir+pname)
    else:
        conc_pltnames.append('')

#--load the shapefile
shapename = '..\\_gis\\shapes\\broward_nwis_sites'
#shapes,records = shapefile.load_as_dict(shapename)
site_no_idx = 2
site_type_idx = 4
shp = shapefile.Reader(shapename)
shapes = shp.shapes()
records= shp.records()

#--new shape instance for gw sites
wr_gw = shapefile.writer_like(shapename)
wr_gw.field('navd_len',fieldType='N',size=10,decimal=0)
wr_gw.field('navd_pth',fieldType='C',size=100)
wr_gw.field('conc_len',fieldType='N',size=10,decimal=0)
wr_gw.field('conc_pth',fieldType='C',size=100)


#--new shape instance for gw sites
wr_sw = shapefile.writer_like(shapename)
wr_sw.field('navd_len',fieldType='N',size=10,decimal=0)
wr_sw.field('navd_pth',fieldType='C',size=100)
wr_sw.field('conc_len',fieldType='N',size=10,decimal=0)
wr_sw.field('conc_pth',fieldType='C',size=100)


select_radius = 5280.0 * 4.0

#--first get the nwis conc data
nwis_shapename = '..\\_gis\\scratch\\broward_nwis_sites_reclen_gw'
fields = shapefile.get_fieldnames(nwis_shapename)
conc_idx = fields.index('conc_len')
siteno_idx = fields.index('site_no')
sitename_idx = fields.index('station_nm')
hole_idx = fields.index('hole_depth')
wdepth_idx = fields.index('well_depth')
shp = shapefile.Reader(nwis_shapename)
shapes = shp.shapes()
records = shp.records()

wr = shapefile.writer_like(nwis_shapename)

#--only use those conc records that have > 0 length
sel_records, sel_points = [], []
for shape, rec in zip(shapes, records):
    shp_pt = Point(shape.points[0])
    if (airport_pt.distance(shp_pt) < select_radius) and (int(rec[conc_idx]) >
                                                          0):
        sel_records.append(rec)
        sel_points.append(shape.points[0])
        wr.poly([shape.points], shapeType=shape.shapeType)
        wr.record(rec)
wr.save('..\\_gis\\scratch\\airport_nwis_sites')

#--build siteno,sitename tuples
sel_tups = []



#--write a valid points shapefile
wr = shapefile.Writer()
wr.field('x',fieldType='N',size=20,decimal=3)
wr.field('y',fieldType='N',size=20,decimal=3)
for [x,y] in valid_points:
    wr.poly([[[x,y]]],shapeType=shapefile.POINT)
    wr.record([x,y])
wr.save('shapes\\nexrad_points')    


#--write a nearest-neighbor nexrad shapefile
wr = shapefile.writer_like(shapename_nex)
wr.field('group',fieldType='N',size=2,decimal=0)
pmap = {}
for shape,poly,record in zip(nexrad_shapes,nexrad_polygons,nexrad_records):
    imin,dist = None,1.0e+30
    for i,v in enumerate(valid_Points):
        d = poly.distance(v)
        if d < dist:
            dist = d
            imin = i
    if imin in pmap.keys():
        pmap[imin].append(record[0])
    else:
        pmap[imin] = [record[0]]            
    wr.poly([shape.points],shapeType=shape.shapeType)
    record.append(imin)
示例#11
0
            if ibnd != 0 and ibnd != 2 and point.intersects(poly):
                valid_points.append([x, y])
                valid_Points.append(point)
        pass

#--write a valid points shapefile
wr = shapefile.Writer()
wr.field('x', fieldType='N', size=20, decimal=3)
wr.field('y', fieldType='N', size=20, decimal=3)
for [x, y] in valid_points:
    wr.poly([[[x, y]]], shapeType=shapefile.POINT)
    wr.record([x, y])
wr.save('shapes\\nexrad_points')

#--write a nearest-neighbor nexrad shapefile
wr = shapefile.writer_like(shapename_nex)
wr.field('group', fieldType='N', size=2, decimal=0)
pmap = {}
for shape, poly, record in zip(nexrad_shapes, nexrad_polygons, nexrad_records):
    imin, dist = None, 1.0e+30
    for i, v in enumerate(valid_Points):
        d = poly.distance(v)
        if d < dist:
            dist = d
            imin = i
    if imin in pmap.keys():
        pmap[imin].append(record[0])
    else:
        pmap[imin] = [record[0]]
    wr.poly([shape.points], shapeType=shape.shapeType)
    record.append(imin)
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")
#X,Y,Z = np.meshgrid(xs,ys)
print 'calc rbf...'
interp_topo = np.zeros((simple.grid.nrow,simple.grid.ncol))
for i in range(simple.grid.nrow):
    for j in range(simple.grid.ncol):
        x,y = simple.grid.cols[j] + (simple.grid.delta/2.0),simple.grid.rows[i] + (simple.grid.delta/2.0)
        itopo = rbfi(x,y,1)
        interp_topo[i,j] = itopo
interp_topo[np.where(interp_topo<100.0)] = 100.0
print interp_topo.min()
interp_topo = np.flipud(interp_topo)
np.savetxt('ref\\interp_topo.ref',interp_topo,fmt=' %15.6E')

print 'loading grid shape'        
shapes,records = shapefile.load_as_dict('shapes\\simple_grid')
wr = shapefile.writer_like('shapes\\simple_grid')

print 'writing new grid shape'
ibound = np.zeros((simple.grid.nrow,simple.grid.ncol))
for shape,row,col,ibnd in zip(shapes,records['row'],records['column'],records['ibound']):
    wr.poly([shape.points])
    ibound[row-1,col-1] = ibnd
    topo = interp_topo[row-1,col-1]
    rec = [row,col,ibnd,topo]
    wr.record(rec)

wr.save('shapes\\simple_topo')
np.savetxt('ref\\ibound.ref',ibound,fmt=' %4d')