def load_regions(fpath_region_shapes, region_shape_id_fieldname): """ Load the geographic regions (REGION SHAPES) from a suite of shapefiles with file path prefix `fpath_region_shapes`. This also checks to ensure a field with name `region_shape_id_fieldname` exists in each shape dictionary. """ regions = {} # maps a pysal polygon to the input shapedict, # inc. pyshp geometry and shapely geometry for indx, shape_dict in enumerate( polygon_shaperecords(fpath_region_shapes)): # 'geom_pyshp', 'geom_shapely', ... geom_shapely = shape_dict['geom_shapely'] geom_pysal = pysal.cg.asShape(geom_shapely) if region_shape_id_fieldname not in shape_dict: raise ValueError("'%s' not in shape file %s" % (region_shape_id_fieldname, shape_dict.keys())) if not shape_dict[region_shape_id_fieldname].startswith(FUA_ID_PREFIX): # only retain FUA units that start with particular prefix (e.g., UK) continue #else: # pprint(shape_dict) # print shape_dict['geom_shapely'] # exit() regions[geom_pysal] = shape_dict return regions
def get_regions(): regions = {} # maps a pysal polygon to the input shapedict, # inc. pyshp geometry and shapely geometry for indx, shape_dict in enumerate(polygon_shaperecords(fpath_region_shapes)): # 'geom_pyshp', 'geom_shapely', ... geom_shapely = shape_dict['geom_shapely'] geom_pysal = pysal.cg.asShape(geom_shapely) if region_shape_fieldname not in shape_dict: raise ValueError("'%s' not in shape file %s" % (region_shape_fieldname, shape_dict.keys())) regions[geom_pysal] = shape_dict return regions
# Matt J Williams, 2015 # http://mattjw.net # [email protected] from shapefile_tools import polygon_shaperecords if __name__ == "__main__": fpath_cbsa_shapefile_base = "./dat/cb_2013_us_cbsa_500k" min_polys = 8 print print "List of CBSAs that are composed of %s or more polygons" % (min_polys) print fmt = "%-50s %-20s" print fmt % ('CBSA name', 'Num. Polygons') print for shape_dict in polygon_shaperecords(fpath_cbsa_shapefile_base): cbsa_name = shape_dict['NAME'] geom = shape_dict['geom_shapely'] num_polys = len(geom.geoms) if num_polys >= min_polys: print fmt % (cbsa_name, num_polys)