def calculateShoreLength(gridFilePath, shoreFilePath, outFilePath): gridShp = shapelib.ShapeFile(gridFilePath) gridDbf = dbflib.DBFFile(gridFilePath.split('.')[0] + '.dbf') shoreShp = shapelib.ShapeFile(shoreFilePath) outShp = shapelib.create(outFilePath, shapelib.SHPT_POLYGON) outDbf = dbflib.create(outFilePath.split('.')[0] + '.dbf') outDbf.add_field("ID", dbflib.FTInteger, 10, 0) outDbf.add_field("Impact", dbflib.FTDouble, 30, 6) outDbf.add_field("Length", dbflib.FTDouble, 30, 6) inter = shoreShp.read_object(0).vertices()[1:] if inter: shoreGeo = Polygon(shoreShp.read_object(0).vertices()[0], inter).boundary else: shoreGeo = Polygon(tuple( shoreShp.read_object(0).vertices()[0])).boundary for j in range(gridDbf.record_count()): gridObj = gridShp.read_object(j) gridGeo = Polygon(tuple(gridObj.vertices()[0])) shoreLength = gridGeo.intersection(shoreGeo).length recordDict = gridDbf.read_record(j) newDict = [j, recordDict["Impact"], shoreLength] outShp.write_object(-1, gridObj) outDbf.write_record(j, newDict)
def generateOSCOMInputs(rampFilePath, spillFilePath, containRadius, distThreshold): rampShp = shapelib.ShapeFile(rampFilePath) rampDbf = dbflib.DBFFile(rampFilePath[:-4] + '.dbf') # rampFilePath.split('.')[0] spillShp = shapelib.ShapeFile(spillFilePath) spillDbf = dbflib.DBFFile(spillFilePath[:-4] + '.dbf') # spillFilePath.split('.')[0] vesCapList = [] rampGeoList = [] tn = rampDbf.record_count() for j in range(tn): rampObj = rampShp.read_object(j) rampGeo = Point(tuple(rampObj.vertices()[0])) rampGeoList.append(rampGeo) recordDict = rampDbf.read_record(j) vesCapList.append(recordDict['VesCap']) distDict = {} serveDict = {} coverDict = {} volumeList = [] spillGeoList = [] sn = spillDbf.record_count() for j in range(sn): serveDict[j] = [] spillObj = spillShp.read_object(j) spillGeo = Point(tuple(spillObj.vertices()[0])) spillGeoList.append(spillGeo) recordDict = spillDbf.read_record(j) volumeList.append(recordDict['VOLUME_BBL'] * 42) for i in range(tn): dist = rampGeoList[i].distance(spillGeo) if dist <= distThreshold: distDict[(i, j)] = dist serveDict[j].append(i) servedDict = {} for i in range(tn): servedDict[i] = [] for k, v in serveDict.items(): if i in v: servedDict[i].append(k) for j in range(sn): coverDict[j] = [] for l in range(sn): sdist = spillGeoList[j].distance(spillGeoList[l]) if sdist <= containRadius: coverDict[j].append(l) print 'done' return [vesCapList, volumeList, distDict, serveDict, servedDict, coverDict]
def read_shape_file(self, path): self.name = str(os.path.splitext(os.path.basename(path))[0]) shp = shapelib.ShapeFile(path) shpinfo = shp.info() self.n = shpinfo[0] self.shp = shp if shpinfo[1] == shapelib.SHPT_POINT: self.shape_type = stars.SHP_POINT elif shpinfo[1] == shapelib.SHPT_POLYGON: self.shape_type = stars.SHP_POLYGON else: raise Exception("Shape type not support in CAST") self.extent = shpinfo[2][:2] + shpinfo[3][:2] self.centroids = coordinator.centroids(shp.cobject()) self._shape_objects = coordinator.read_objects(shp.cobject(), self.name) # load index self.locator = shptree.SHPTree(self.shp.cobject(), 2, 0) stars.SHAPE_LOCATOR_INDEX = stars.SHAPE_LOCATOR_QUADTREE #stars.SHAPE_LOCATOR_INDEX = None self.loadDBF(path) self.get_kdtree_locator()
def AddBoomField(gridFilePath, sol, outFilePath): gridSHP = shapelib.ShapeFile(gridFilePath) gridDBF = dbflib.DBFFile(gridFilePath.split('.')[0] + '.dbf') outSHP = shapelib.create(outFilePath, shapelib.SHPT_POLYGON) outDBF = dbflib.create(outFilePath.split('.')[0] + '.dbf') for j in range(gridDBF.field_count()): #if j == 17: #outDBF.add_field(gridDBF.field_info(j)[1], dbflib.FTDouble, 20, 10) #else: outDBF.add_field( gridDBF.field_info(j)[1], gridDBF.field_info(j)[0], gridDBF.field_info(j)[2], gridDBF.field_info(j)[3]) outDBF.add_field('Boomed', dbflib.FTInteger, 5, 0) jj = 0 for j in range(gridDBF.record_count()): vert = gridSHP.read_object(j) recordDict = gridDBF.read_record(j) if j in sol: recordDict['Boomed'] = 1 else: recordDict['Boomed'] = 0 outSHP.write_object(-1, vert) outDBF.write_record(jj, recordDict) jj += 1
def LoadShapefile(filename, shpNature, hostObject): # The shapelib object shp = [] # open the shapefile if os.path.isfile(filename): (dirname, filerelname) = os.path.split(filename) (bodyname, fileext) = os.path.splitext(filerelname) logging.info('(DD) Is FILE OK') # In order to open a shapefile the shapelib needs to have the shx corresponding files if os.path.isfile(os.path.join( dirname, bodyname + '.SHX')) or os.path.isfile( os.path.join(dirname, bodyname + '.shx')): logging.info('(DD) IS SHALELIB OK') shp = shapelib.ShapeFile(filename) else: return 0 if shp: Tshp = (shp.info())[1] logging.info('(II) Reading shapefile with type ', Tshp, ' contents (', filerelname, ') as ', shpNature) #-------- Loading Roads if (shpNature >= 20) and (shpNature <= 29): LoadRoads(shp, hostObject) #-------- Loading Buildings if (shpNature >= 30) and (shpNature <= 39): LoadBuildings(shp, hostObject)
def generateMCLPInputsGrid(rampFilePath, gridFilePath, distThreshold): rampShp = shapelib.ShapeFile(rampFilePath) rampDbf = dbflib.DBFFile(rampFilePath.split('.')[0] + '.dbf') gridShp = shapelib.ShapeFile(gridFilePath) gridDbf = dbflib.DBFFile(gridFilePath.split('.')[0] + '.dbf') rampGeoList = [] tn = rampDbf.record_count() for j in range(tn): rampObj = rampShp.read_object(j) rampGeo = Point(tuple(rampObj.vertices()[0])) rampGeoList.append(rampGeo) recordDict = rampDbf.read_record(j) distDict = {} serveDict = {} gridLenList = [] gridSenList = [] gridGeoList = [] sn = gridDbf.record_count() for j in range(sn): serveDict[j] = [] gridObj = gridShp.read_object(j) gridGeo = Polygon(tuple(gridObj.vertices()[0])).centroid gridGeoList.append(gridGeo) recordDict = gridDbf.read_record(j) gridSenList.append(recordDict['Total_S']) for i in range(tn): dist = rampGeoList[i].distance(gridGeo) if dist <= distThreshold: distDict[(i, j)] = dist serveDict[j].append(i) servedDict = {} for i in range(tn): servedDict[i] = [] for k, v in serveDict.items(): if i in v: servedDict[i].append(k) print 'done' return [gridSenList, serveDict, servedDict]
def generateMCLPInputs(rampFilePath, shoreFilePath, distThreshold): rampShp = shapelib.ShapeFile(rampFilePath) rampDbf = dbflib.DBFFile(rampFilePath.split('.')[0] + '.dbf') shoreShp = shapelib.ShapeFile(shoreFilePath) shoreDbf = dbflib.DBFFile(shoreFilePath.split('.')[0] + '.dbf') rampGeoList = [] tn = rampDbf.record_count() for j in range(tn): rampObj = rampShp.read_object(j) rampGeo = Point(tuple(rampObj.vertices()[0])) rampGeoList.append(rampGeo) recordDict = rampDbf.read_record(j) distDict = {} serveDict = {} shoreSenList = [] shoreGeoList = [] sn = shoreDbf.record_count() for j in range(sn): serveDict[j] = [] shoreObj = shoreShp.read_object(j) shoreGeo = LineString(tuple(shoreObj.vertices()[0])).centroid shoreGeoList.append(shoreGeo) recordDict = shoreDbf.read_record(j) shoreSenList.append(recordDict['ESI_1']) for i in range(tn): dist = rampGeoList[i].distance(shoreGeo) if dist <= distThreshold: distDict[(i, j)] = dist serveDict[j].append(i) servedDict = {} for i in range(tn): servedDict[i] = [] for k, v in serveDict.items(): if i in v: servedDict[i].append(k) print 'done' return [shoreSenList, serveDict, servedDict]
def createPointFast(filePath): shpList = [] shp = shapelib.ShapeFile(filePath) dbf = dbflib.DBFFile(filePath.split('.')[0] + '.dbf') for j in range(dbf.record_count()): geo = Point(tuple(shp.read_object(j).vertices()[0])) shpList.append(geo) return shpList
def generatEBAMInputs(rampFilePath, gridFilePath, speed, hitDay): rampShp = shapelib.ShapeFile(rampFilePath) rampDbf = dbflib.DBFFile(rampFilePath.split('.')[0] + '.dbf') gridShp = shapelib.ShapeFile(gridFilePath) gridDbf = dbflib.DBFFile(gridFilePath.split('.')[0] + '.dbf') ebCap = [] rampGeoList = [] cdDict = {} tn = rampDbf.record_count() for j in range(tn): rampObj = rampShp.read_object(j) rampGeo = Point(tuple(rampObj.vertices()[0])) rampGeoList.append(rampGeo) recordDict = rampDbf.read_record(j) ebCap.append(recordDict['EBCap_Mete']) cdDict[j] = [] distDict = {} coverDict = {} impactList = [] needBoomList = [] for j in range(gridDbf.record_count()): gridObj = gridShp.read_object(j) gridGeo = Polygon(tuple(gridObj.vertices()[0])) recordDict = gridDbf.read_record(j) impactList.append(recordDict['Impact']) needBoomList.append(recordDict['Length']) coverDict[j] = [] for i in range(tn): dist = rampGeoList[i].distance(gridGeo) hours = dist / speed if hours <= hitDay * 24: coverDict[j].append(i) cdDict[i].append(j) distDict[(i, j)] = dist return [ebCap, impactList, needBoomList, distDict, coverDict, cdDict]
def createPolygonFastNoInterior(filePath): shpList = [] shp = shapelib.ShapeFile(filePath) dbf = dbflib.DBFFile(filePath.split('.')[0] + '.dbf') for j in range(dbf.record_count()): inter = shp.read_object(j).vertices()[1:] geo = Polygon(tuple(shp.read_object(j).vertices()[0])) shpList.append(geo.buffer(0)) return shpList
def read_shapefile(filename): # open the shapefile shp = shapelib.ShapeFile(filename) # the info method returns a tuple (num_shapes, type, min, max) where # num_shapes is the number of shapes, type is the type code (one of # the SHPT* constants defined in the shapelib module) and min and # max are 4-element lists with the min. and max. values of the # vertices. print shp.info() # read_object reads a shape obj = shp.read_object(0) # The vertices method returns the shape as a list of lists of tuples. print obj.vertices()[0][:10] # The extents returns a tuple with two 4-element lists with the min. # and max. values of the vertices. print obj.extents() # The type attribute is the type code (one of the SHPT* constants # defined in the shapelib module) print obj.type # The id attribute is the shape id print obj.id # the cobject method returns a PyCObject containing the shapelib # SHPHandle. This is useful for passing shapefile objects to # C-Python extensions. print shp.cobject() # build a quad tree from the shapefile. The first argument must be # the return value of the shape file object's cobject method (this # is currently needed to access the shape file at the C-level). The # second argument is the dimension and the third the maximum depth. # 0 means to guess an appropriate depth tree = shptree.SHPTree(shp.cobject(), 2, 0) # Retrieve the ids for a region. Here we just use the extents of the # object previously read from the shapefile minima, maxima = obj.extents() print tree.find_shapes(minima[:2], maxima[:2])
def simulateRampCapacity(rampFilePath, outFilePath): rampShp = shapelib.ShapeFile(rampFilePath) rampDbf = dbflib.DBFFile(rampFilePath.split('.')[0] + '.dbf') outShp = shapelib.create(outFilePath, shapelib.SHPT_POINT) outDbf = dbflib.create(outFilePath.split('.')[0] + '.dbf') outDbf.add_field("ID", dbflib.FTInteger, 10, 0) outDbf.add_field("EBCap", dbflib.FTInteger, 30, 0) outDbf.add_field("VesCap", dbflib.FTInteger, 10, 0) tn = rampDbf.record_count() ebCap = randint(500, high=5001, size=tn) vesCap = choice(6, tn, p=[0.5, 0.2, 0.1, 0.1, 0.05, 0.05]) for j in range(tn): rampObj = rampShp.read_object(j) recordDict = rampDbf.read_record(j) newDict = [j, ebCap[j], vesCap[j]] outShp.write_object(-1, rampObj) outDbf.write_record(j, newDict)
def clear_small_polygons(self, shapeFile, newShapeFile, lenLimit): shp = shapelib.ShapeFile(shapeFile) dbf = dbflib.open(shapeFile) newSHP = shapelib.create(newShapeFile, self.shpType) newDBF = dbflib.create(newShapeFile) for f in range(dbf.field_count()): fi = dbf.field_info(f) newDBF.add_field(fi[1], fi[0], fi[2], fi[3]) bb = 0 for b in range(shp.info()[0]): sobj = shp.read_object(b) rec = dbf.read_record(b) if self.length_of_polygon(sobj.vertices()) > lenLimit: shpobj = shapelib.SHPObject(self.shpType, bb, [sobj.vertices()[0]]) newSHP.write_object(bb, shpobj) newDBF.write_record(bb, rec) bb += 1 shp.close() dbf.close() newSHP.close() newDBF.close()
def __init__(self, buildingSHPFile): self.buildingSHPFile = buildingSHPFile self.shp = shapelib.ShapeFile(buildingSHPFile) self.shpNum, self.shpType = self.shp.info()[0], self.shp.info()[1]